/
PageView.php
302 lines (274 loc) · 12.1 KB
/
PageView.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
namespace Concrete\Core\Page\View;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Page\Theme\Theme;
use Environment;
use Events;
use Loader;
use PageCache;
use Concrete\Core\Entity\Page\Template;
use Concrete\Core\Page\Template as PageTemplate;
use PageTheme;
use Permissions;
use URL;
use Concrete\Core\User\User;
use View;
use Config;
class PageView extends View
{
protected $c; // page
protected $cp;
protected $pTemplateID;
protected $customStyleMap;
public function getScopeItems()
{
$items = parent::getScopeItems();
$items['c'] = $this->c;
$items['theme'] = $this->themeObject;
return $items;
}
/**
* Called from previewing functions, this lets us override the page's template with one of our own choosing.
*/
public function setCustomPageTemplate(Template $pt)
{
$this->pTemplateID = $pt->getPageTemplateID();
}
public function getPageTemplate()
{
return PageTemplate::getByID($this->pTemplateID);
}
/**
* Called from previewing functions, this lets us override the page's theme with one of our own choosing.
*/
public function setCustomPageTheme(PageTheme $pt)
{
$this->themeObject = $pt;
$this->themePkgHandle = $pt->getPackageHandle();
}
public function renderSinglePageByFilename($cFilename, $pkgHandle = null)
{
$this->loadViewThemeObject();
$env = Environment::get();
$cFilename = trim($cFilename, '/');
// if we have this exact template in the theme, we use that as the outer wrapper and we don't do an inner content file
$exactThemeTemplate = $env->getRecord(DIRNAME_THEMES . '/' . $this->themeHandle . '/' . $cFilename, $this->themePkgHandle);
if ($exactThemeTemplate->exists()) {
$this->setViewTemplate($exactThemeTemplate->file);
} else {
// use a content wrapper from themes/core if specified
// e.g. $this->render('your/page', 'none') would use themes/core/none.php to print the $innerContent without a wrapper
$coreThemeTemplate = $env->getRecord(DIRNAME_THEMES . '/' . DIRNAME_THEMES_CORE . '/' . $this->themePkgHandle . '.php');
if ($coreThemeTemplate->exists()) {
$this->setViewTemplate($coreThemeTemplate->file);
} else {
// check for other themes or in a package if one was specified
$themeTemplate = $env->getRecord(DIRNAME_THEMES . '/' . $this->themeHandle . '/' . $this->controller->getThemeViewTemplate(), $this->themePkgHandle);
if ($themeTemplate->exists()) {
$this->setViewTemplate($themeTemplate->file);
} else {
// fall back to the active theme wrapper if nothing else was found
$fallbackTheme = PageTheme::getByHandle($this->themeHandle);
$fallbackPkgHandle = ($fallbackTheme instanceof PageTheme) ? $fallbackTheme->getPackageHandle() : $this->themePkgHandle;
$fallbackTemplate = $env->getRecord(DIRNAME_THEMES . '/' . $this->themeHandle . '/' . $this->controller->getThemeViewTemplate(), $fallbackPkgHandle);
$path = $fallbackTemplate->file;
if (basename($path) != FILENAME_THEMES_VIEW) {
// We're going to check to see if this file actually exists in the theme. Otherwise we're going to use the default wrapper.
// Ideally this would happen in getThemeViewTemplate but it's hard to add the logic there.
if (!$fallbackTemplate->exists()) {
$path = $env->getPath(DIRNAME_THEMES . '/' . $this->themeHandle . '/' . FILENAME_THEMES_VIEW, $fallbackPkgHandle);
}
}
$this->setViewTemplate($path);
}
}
// set the inner content for the theme wrapper we found
if (!isset($pkgHandle)) { // This way we can pass in a false and skip this.
$pkgHandle = $this->c->getPackageHandle();
}
$this->setInnerContentFile(
$env->getPath(
DIRNAME_PAGES . '/' . $cFilename,
$pkgHandle
)
);
}
}
public function setupRender()
{
$env = Environment::get();
if (isset($this->innerContentFile)) {
// this has already been rendered (e.g. by calling $this->render()
// from within a controller. So we don't reset it.
return false;
}
if ($this->c->getPageTypeID() == 0 && $this->c->getCollectionFilename()) {
$this->renderSinglePageByFilename($this->c->getCollectionFilename());
} else {
$this->loadViewThemeObject();
$pt = $this->getPageTemplate();
$rec = null;
if ($pt) {
$rec = $env->getRecord(
DIRNAME_THEMES . '/' . $this->themeHandle . '/' . $pt->getPageTemplateHandle() . '.php',
$this->themePkgHandle);
}
if ($rec && $rec->exists()) {
$this->setViewTemplate(
$env->getPath(
DIRNAME_THEMES . '/' . $this->themeHandle . '/' . $pt->getPageTemplateHandle() . '.php',
$this->themePkgHandle));
} else {
$pTemplatePkgHandle = isset($this->pTemplatePkgHandle) ? $this->pTemplatePkgHandle : null;
$rec = $env->getRecord(
DIRNAME_PAGE_TEMPLATES . '/' . $this->c->getPageTemplateHandle() . '.php',
$pTemplatePkgHandle);
if ($rec->exists()) {
$this->setInnerContentFile(
$env->getPath(
DIRNAME_PAGE_TEMPLATES . '/' . $this->c->getPageTemplateHandle() . '.php',
$pTemplatePkgHandle));
$this->setViewTemplate(
$env->getPath(
DIRNAME_THEMES . '/' . $this->themeHandle . '/' . $this->controller->getThemeViewTemplate(),
$this->themePkgHandle));
} else {
$this->setViewTemplate(
$env->getPath(
DIRNAME_THEMES . '/' . $this->themeHandle . '/' . FILENAME_THEMES_DEFAULT,
$this->themePkgHandle));
}
}
}
}
public function getStyleSheet($stylesheet)
{
if ($this->themeObject->isThemePreviewRequest()) {
return $this->themeObject->getStylesheet($stylesheet);
}
if ($this->c->hasPageThemeCustomizations()) {
// page has theme customizations, check if we need to serve an uncached version of the style sheet,
// either because caching is deactivated or because the version is not approved yet
if ($this->c->getVersionObject()->isApprovedNow()) {
// approved page, return handler script if caching is deactivated
if (!Config::get('concrete.cache.theme_css')) {
return URL::to('/ccm/system/css/page', $this->c->getCollectionID(), $stylesheet);
}
} else {
// this means that we're potentially viewing customizations that haven't been approved yet. So we're going to
// pipe them all through a handler script, basically uncaching them.
return URL::to('/ccm/system/css/page', $this->c->getCollectionID(), $stylesheet, $this->c->getVersionID());
}
}
$env = Environment::get();
$output = Config::get('concrete.cache.directory') . '/pages/' . $this->c->getCollectionID() . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle();
$relative = REL_DIR_FILES_CACHE . '/pages/' . $this->c->getCollectionID() . '/' . DIRNAME_CSS . '/' . $this->getThemeHandle();
$r = $env->getRecord(
DIRNAME_THEMES . '/' . $this->themeObject->getThemeHandle() . '/' . DIRNAME_CSS . '/' . $stylesheet,
$this->themeObject->getPackageHandle());
if ($r->exists()) {
$sheetObject = new \Concrete\Core\StyleCustomizer\Stylesheet(
$stylesheet,
$r->file,
$r->url,
$output,
$relative);
if ($sheetObject->outputFileExists()) {
return $sheetObject->getOutputRelativePath();
} else {
// cache output file doesn't exist, check if page has theme customizations
if ($this->c->hasPageThemeCustomizations()) {
// build style sheet with page theme customizations
$style = $this->c->getCustomStyleObject();
if (is_object($style)) {
$scl = $style->getValueList();
$sheetObject->setValueList($scl);
// write cache output file
$sheetObject->output();
// return cache output file
return $sheetObject->getOutputRelativePath();
}
}
}
return $this->themeObject->getStylesheet($stylesheet);
}
/*
* deprecated - but this is for backward compatibility. If we don't have a stylesheet in the css/
* directory we just pass through and return the passed file in the current directory.
*/
return $env->getURL(
DIRNAME_THEMES . '/' . $this->themeObject->getThemeHandle() . '/' . $stylesheet,
$this->themeObject->getPackageHandle()
);
}
public function startRender()
{
parent::startRender();
$this->c->outputCustomStyleHeaderItems();
// do we have any custom menu plugins?
$cp = new Permissions($this->c);
$this->cp = $cp;
if ($cp->canViewToolbar()) {
$dh = Loader::helper('concrete/dashboard');
if (!$dh->inDashboard()
&& $this->c->getCollectionPath() != '/page_not_found'
&& $this->c->getCollectionPath() != '/download_file'
&& !$this->c->isPageDraft()
&& !$this->c->isMasterCollection()
) {
$app = Application::getFacadeApplication();
$u = $app->make(User::class);
$u->markPreviousFrontendPage($this->c);
}
}
}
public function finishRender($contents)
{
$contents = parent::finishRender($contents);
$event = new \Symfony\Component\EventDispatcher\GenericEvent();
$event->setArgument('contents', $contents);
Events::dispatch('on_page_output', $event);
$contents = $event->getArgument('contents');
$cache = PageCache::getLibrary();
$shouldAddToCache = $cache->shouldAddToCache($this);
if ($shouldAddToCache) {
$cache->set($this->c, $contents);
}
return $contents;
}
/**
* @deprecated
*/
public function getCollectionObject()
{
return $this->getPageObject();
}
public function getPageObject()
{
return $this->c;
}
public function section($url)
{
if (!empty($this->viewPath)) {
$url = '/' . trim($url, '/');
if (strpos($this->viewPath, $url) !== false && strpos($this->viewPath, $url) == 0) {
return true;
}
}
}
protected function constructView($page = false)
{
$this->c = $page;
parent::constructView($page->getCollectionPath());
if (!isset($this->pTemplateID)) {
$this->pTemplateID = $this->c->getPageTemplateID();
$pto = $this->c->getPageTemplateObject();
if ($pto && $pto->getPackageID()) {
$this->pTemplatePkgHandle = $this->c->getPageTemplateObject()->getPackageHandle();
}
}
if (!isset($this->pThemeID)) {
$this->pThemeID = $this->c->getPageTemplateID(); // @TODO kill this code? It looks completely wrong.
}
}
}