-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageManager.php
More file actions
125 lines (107 loc) · 3.17 KB
/
PageManager.php
File metadata and controls
125 lines (107 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Perform\PageEditorBundle;
use Doctrine\ORM\EntityManagerInterface;
use Perform\PageEditorBundle\Entity\Section;
use Perform\PageEditorBundle\Entity\Version;
use Perform\RichContentBundle\Entity\Content;
use Perform\RichContentBundle\Renderer\RendererInterface;
/**
* Renders page content fragments.
*
* The manager can be put into 'edit mode', where the content will be
* rendered using the rich content editor, allowing it to be updated
* directly on the page.
*
* The current version must be set before rendering, either by setting
* the page name with setCurrentPage(), or the version object itself
* with setCurrentVersion().
*
* @author Glynn Forrest <me@glynnforrest.com>
**/
class PageManager
{
protected $entityManager;
protected $twig;
protected $renderer;
protected $editMode;
protected $currentPage;
protected $currentVersion;
public function __construct(EntityManagerInterface $entityManager, \Twig_Environment $twig, RendererInterface $renderer)
{
$this->entityManager = $entityManager;
$this->twig = $twig;
$this->renderer = $renderer;
}
/**
* Enable 'edit mode' for the current request.
*/
public function enableEditMode($enabled = true)
{
$this->editMode = (bool) $enabled;
}
public function inEditMode()
{
return (bool) $this->editMode;
}
/**
* @param string $currentPage
*/
public function setCurrentPage($currentPage)
{
$this->currentPage = $currentPage;
$this->currentVersion = null;
}
/**
* @return string
*/
public function getCurrentPage()
{
return $this->currentPage;
}
/**
* @return bool
*/
public function hasCurrentPage()
{
return (bool) $this->currentPage;
}
public function setCurrentVersion(Version $version)
{
$this->currentVersion = $version;
$this->currentPage = $version->getPage();
}
public function getCurrentVersion()
{
if ($this->currentVersion) {
return $this->currentVersion;
}
if (!$this->currentPage) {
throw new \Exception(sprintf('The current page name must declared to render content.'));
}
return $this->currentVersion = $this->entityManager
->getRepository('PerformPageEditorBundle:Version')
->findDefaultVersion($this->currentPage);
}
public function render($sectionName)
{
return $this->inEditMode() ?
$this->renderEditorSection($sectionName) :
$this->renderPublishedSection($sectionName);
}
public function renderPublishedSection($sectionName)
{
$version = $this->getCurrentVersion();
$section = $version->getSection($sectionName);
if (!$section) {
// throw exception for debug, recover gracefully for prod
return '';
}
return $this->renderer->render($section->getContent());
}
protected function renderEditorSection($sectionName)
{
return $this->twig->render('@PerformPageEditor/section.html.twig', [
'sectionName' => $sectionName,
]);
}
}