|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The page model |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace Phile\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Phile\Core\Router; |
|
8
|
|
|
use Phile\Core\Event; |
|
9
|
|
|
use Phile\Core\Registry; |
|
10
|
|
|
use Phile\Core\ServiceLocator; |
|
11
|
|
|
use Phile\Repository\Page as Repository; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* the Model class for a page |
|
15
|
|
|
* |
|
16
|
|
|
* @author Frank Nägler |
|
17
|
|
|
* @link https://philecms.com |
|
18
|
|
|
* @license http://opensource.org/licenses/MIT |
|
19
|
|
|
* @package Phile\Model |
|
20
|
|
|
*/ |
|
21
|
|
|
class Page |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Phile\Model\Meta the meta model |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $meta; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string the content |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $content; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string the path to the original file |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $filePath; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string the raw file |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $rawData; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \Phile\ServiceLocator\ParserInterface the parser |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $parser; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string the pageId of the page |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $pageId; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var string The content folder, as passed to the class constructor when initiating the object. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $contentFolder; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var string content extension |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $contentExtension; |
|
62
|
|
|
|
|
63
|
|
|
/** @var Repository */ |
|
64
|
|
|
protected $repository; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* the constructor |
|
68
|
|
|
* |
|
69
|
|
|
* @param $filePath |
|
70
|
|
|
* @param string $folder |
|
71
|
|
|
*/ |
|
72
|
17 |
|
public function __construct($filePath, $folder = null) |
|
73
|
|
|
{ |
|
74
|
17 |
|
$settings = Registry::get('Phile_Settings'); |
|
75
|
17 |
|
$this->contentFolder = $folder ?: $settings['content_dir']; |
|
76
|
17 |
|
$this->contentExtension = $settings['content_ext']; |
|
77
|
17 |
|
$this->setFilePath($filePath); |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @triggerEvent before_load_content this event is triggered before the content is loaded |
|
81
|
|
|
* |
|
82
|
|
|
* @param string filePath the path to the file |
|
83
|
|
|
* @param \Phile\Model\Page page the page model |
|
84
|
|
|
*/ |
|
85
|
17 |
|
Event::triggerEvent('before_load_content', array('filePath' => &$this->filePath, 'page' => &$this)); |
|
86
|
17 |
|
if (file_exists($this->filePath)) { |
|
87
|
17 |
|
$this->rawData = file_get_contents($this->filePath); |
|
88
|
17 |
|
$this->parseRawData(); |
|
89
|
|
|
} |
|
90
|
|
|
/** |
|
91
|
|
|
* @triggerEvent after_load_content this event is triggered after the content is loaded |
|
92
|
|
|
* |
|
93
|
|
|
* @param string filePath the path to the file |
|
94
|
|
|
* @param string rawData the raw data |
|
95
|
|
|
* @param \Phile\Model\Page page the page model |
|
96
|
|
|
*/ |
|
97
|
17 |
|
Event::triggerEvent( |
|
98
|
17 |
|
'after_load_content', |
|
99
|
|
|
[ |
|
100
|
17 |
|
'filePath' => &$this->filePath, |
|
101
|
17 |
|
'rawData' => $this->rawData, |
|
102
|
17 |
|
'page' => &$this |
|
103
|
|
|
] |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
17 |
|
$this->parser = ServiceLocator::getService('Phile_Parser'); |
|
107
|
17 |
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* method to get content of page, this method returned the parsed content |
|
111
|
|
|
* |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
3 |
|
public function getContent() |
|
115
|
|
|
{ |
|
116
|
|
|
/** |
|
117
|
|
|
* @triggerEvent before_parse_content this event is triggered before the content is parsed |
|
118
|
|
|
* |
|
119
|
|
|
* @param string content the raw data |
|
120
|
|
|
* @param \Phile\Model\Page page the page model |
|
121
|
|
|
*/ |
|
122
|
3 |
|
Event::triggerEvent('before_parse_content', array('content' => $this->content, 'page' => &$this)); |
|
123
|
3 |
|
$content = $this->parser->parse($this->content); |
|
124
|
|
|
/** |
|
125
|
|
|
* @triggerEvent after_parse_content this event is triggered after the content is parsed |
|
126
|
|
|
* |
|
127
|
|
|
* @param string content the parsed content |
|
128
|
|
|
* @param \Phile\Model\Page page the page model |
|
129
|
|
|
*/ |
|
130
|
3 |
|
Event::triggerEvent('after_parse_content', array('content' => &$content, 'page' => &$this)); |
|
131
|
|
|
|
|
132
|
3 |
|
return $content; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* set content of page |
|
137
|
|
|
* |
|
138
|
|
|
* @param $content |
|
139
|
|
|
*/ |
|
140
|
3 |
|
public function setContent($content) |
|
141
|
|
|
{ |
|
142
|
3 |
|
$this->content = $content; |
|
143
|
3 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* get raw (un-parsed) page content |
|
147
|
|
|
* |
|
148
|
|
|
* @return string |
|
149
|
|
|
*/ |
|
150
|
2 |
|
public function getRawContent() |
|
151
|
|
|
{ |
|
152
|
2 |
|
return $this->content; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* get the meta model |
|
157
|
|
|
* |
|
158
|
|
|
* @return Meta |
|
159
|
|
|
*/ |
|
160
|
8 |
|
public function getMeta() |
|
161
|
|
|
{ |
|
162
|
8 |
|
return $this->meta; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* parse the raw content |
|
167
|
|
|
*/ |
|
168
|
17 |
|
protected function parseRawData() |
|
169
|
|
|
{ |
|
170
|
17 |
|
$this->meta = new Meta($this->rawData); |
|
171
|
|
|
// Remove only the optional, leading meta-block comment |
|
172
|
17 |
|
$rawData = trim($this->rawData); |
|
173
|
17 |
|
if (strncmp('<!--', $rawData, 4) === 0) { |
|
174
|
|
|
// leading meta-block comment uses the <!-- --> style |
|
175
|
16 |
|
$this->content = substr($rawData, max(4, strpos($rawData, '-->') + 3)); |
|
176
|
1 |
|
} elseif (strncmp('/*', $rawData, 2) === 0) { |
|
177
|
|
|
// leading meta-block comment uses the /* */ style |
|
178
|
|
|
$this->content = substr($rawData, strpos($rawData, '*/') + 2); |
|
179
|
|
|
} else { |
|
180
|
|
|
// no leading meta-block comment |
|
181
|
1 |
|
$this->content = $rawData; |
|
182
|
|
|
} |
|
183
|
17 |
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets repository this page was retrieved by/belongs to |
|
187
|
|
|
* |
|
188
|
|
|
* @param Repository $repository |
|
189
|
|
|
* @return $this |
|
190
|
|
|
*/ |
|
191
|
17 |
|
public function setRepository(Repository $repository) |
|
192
|
|
|
{ |
|
193
|
17 |
|
$this->repository = $repository; |
|
194
|
17 |
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Gets repository this page belongs to |
|
199
|
|
|
* |
|
200
|
|
|
* @return Repository |
|
201
|
|
|
*/ |
|
202
|
5 |
|
public function getRepository() |
|
203
|
|
|
{ |
|
204
|
5 |
|
if (!$this->repository) { |
|
205
|
1 |
|
$this->repository = new Repository(); |
|
206
|
|
|
} |
|
207
|
5 |
|
return $this->repository; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* get the title of page from meta information |
|
212
|
|
|
* |
|
213
|
|
|
* @return string|null |
|
214
|
|
|
*/ |
|
215
|
5 |
|
public function getTitle() |
|
216
|
|
|
{ |
|
217
|
5 |
|
return $this->getMeta()->get('title'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* get Phile $pageId |
|
222
|
|
|
* |
|
223
|
|
|
* @param string $filePath |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
17 |
|
protected function buildPageId($filePath) |
|
227
|
|
|
{ |
|
228
|
17 |
|
$pageId = str_replace($this->contentFolder, '', $filePath); |
|
229
|
17 |
|
$pageId = str_replace($this->contentExtension, '', $pageId); |
|
230
|
17 |
|
$pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId); |
|
231
|
17 |
|
$pageId = ltrim($pageId, '/'); |
|
232
|
17 |
|
$pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId); |
|
233
|
17 |
|
return $pageId; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* get the url of page |
|
238
|
|
|
* |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
2 |
|
public function getUrl() |
|
242
|
|
|
{ |
|
243
|
2 |
|
return (new Router)->urlForPage($this->pageId, false); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* set the filepath of the page |
|
248
|
|
|
* |
|
249
|
|
|
* @param string $filePath |
|
250
|
|
|
*/ |
|
251
|
17 |
|
public function setFilePath($filePath) |
|
252
|
|
|
{ |
|
253
|
17 |
|
$this->filePath = $filePath; |
|
254
|
17 |
|
$this->pageId = $this->buildPageId($this->filePath); |
|
255
|
17 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* get the filepath of the page |
|
259
|
|
|
* |
|
260
|
|
|
* @return string |
|
261
|
|
|
*/ |
|
262
|
3 |
|
public function getFilePath() |
|
263
|
|
|
{ |
|
264
|
3 |
|
return $this->filePath; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* get the folder name |
|
269
|
|
|
* |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getFolder() |
|
273
|
|
|
{ |
|
274
|
|
|
return basename(dirname($this->getFilePath())); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
2 |
|
public function getPageId() |
|
278
|
|
|
{ |
|
279
|
2 |
|
return $this->pageId; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* get the previous page if one exist |
|
284
|
|
|
* |
|
285
|
|
|
* @return null|\Phile\Model\Page |
|
286
|
|
|
*/ |
|
287
|
1 |
|
public function getPreviousPage() |
|
288
|
|
|
{ |
|
289
|
1 |
|
return $this->getRepository()->getPageOffset($this, -1); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* get the next page if one exist |
|
294
|
|
|
* |
|
295
|
|
|
* @return null|\Phile\Model\Page |
|
296
|
|
|
*/ |
|
297
|
1 |
|
public function getNextPage() |
|
298
|
|
|
{ |
|
299
|
1 |
|
return $this->getRepository()->getPageOffset($this, 1); |
|
300
|
|
|
} |
|
301
|
|
|
} |
|
302
|
|
|
|