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