1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Contents;
13
14 use ICanBoogie\ActiveRecord\DateTimePropertySupport;
15 use ICanBoogie\DateTime;
16 use ICanBoogie\PropertyNotWritable;
17
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
34 class Content extends \Icybee\Modules\Nodes\Node
35 {
36 const SUBTITLE = 'subtitle';
37 const BODY = 'body';
38 const EXCERPT = 'excerpt';
39 const DATE = 'date';
40 const EDITOR = 'editor';
41 const IS_HOME_EXCLUDED = 'is_home_excluded';
42
43 44 45 46 47 48 49
50 static public function on_views_activerecordprovider_alter_result(\Icybee\Modules\Views\ActiveRecordProvider\AlterResultEvent $event, \Icybee\Modules\Views\ActiveRecordProvider $target)
51 {
52 if (!is_array($event->result) || !($event->module instanceof Module))
53 {
54 return;
55 }
56
57 $cache = self::obtain_cache();
58
59 if (!$cache)
60 {
61 return;
62 }
63
64 $records = $event->result;
65 $record = current($records);
66
67 if (!($record instanceof Content))
68 {
69 return;
70 }
71
72 $keys_and_dates = [];
73
74
75
76 foreach ($records as $record)
77 {
78 $keys_and_dates[$record->nid] = $record->updated_at;
79 }
80
81 if (!$keys_and_dates)
82 {
83 return;
84 }
85
86
87
88
89 $rendered_list_by_nid = [];
90
91 foreach ($cache->filter_by_nid(array_keys($keys_and_dates)) as $rendered)
92 {
93 $rendered_list_by_nid[$rendered->nid] = $rendered;
94 }
95
96 foreach ($records as $record)
97 {
98 $nid = $record->nid;
99
100 if (empty($rendered_list_by_nid[$nid]))
101 {
102 continue;
103 }
104
105 $rendered = $rendered_list_by_nid[$nid];
106
107 if ($record->updated_at > $rendered->updated_at)
108 {
109 continue;
110 }
111
112 $record->rendered_body = $rendered->body;
113 }
114 }
115
116 117 118 119 120
121 public $subtitle;
122
123 124 125 126 127 128 129
130 public $body;
131
132 133 134 135 136
137 public $excerpt;
138
139 140 141 142 143 144 145
146 protected function get_excerpt()
147 {
148 return \ICanBoogie\excerpt((string) $this);
149 }
150
151 use DateProperty;
152
153 154 155 156 157
158 public $editor;
159
160 161 162 163 164
165 public $is_home_excluded;
166
167 168 169 170 171 172 173
174 public function __construct($model='contents')
175 {
176 if (empty($this->excerpt))
177 {
178 unset($this->excerpt);
179 }
180
181 parent::__construct($model);
182 }
183
184 185 186 187
188 static private $use_cache;
189 static private $cache_model;
190
191 static private function obtain_cache()
192 {
193 global $core;
194
195 if (self::$cache_model)
196 {
197 return self::$cache_model;
198 }
199
200 if (self::$use_cache === null)
201 {
202 self::$use_cache = !empty($core->registry['contents.cache_rendered_body']);
203 }
204
205 if (!self::$use_cache)
206 {
207 return;
208 }
209
210 return self::$cache_model = $core->models['contents/rendered'];
211 }
212
213 private $rendered_body;
214
215 216 217 218 219 220 221 222 223
224 public function __toString()
225 {
226 global $core;
227
228 $rendered_body = $this->rendered_body;
229
230 if ($rendered_body)
231 {
232 return $rendered_body;
233 }
234
235 $rendered_body = $body = $this->body;
236
237 try
238 {
239 $cache = self::obtain_cache();
240
241 if ($cache)
242 {
243 $nid = $this->nid;
244 $updated_at = $this->updated_at;
245 $cached = $cache
246 ->select('body')
247 ->filter_by_nid_and_updated_at($nid, $updated_at)
248 ->rc;
249
250 if ($cached)
251 {
252 return $cached;
253 }
254
255 if ($this->editor)
256 {
257 $rendered_body = $this->render_body();
258 }
259
260 if ($rendered_body && $rendered_body != $body)
261 {
262 $cache->save([
263
264 'nid' => $nid,
265 'updated_at' => $updated_at,
266 'body' => $rendered_body
267
268 ], null, [ 'on duplicate' => true ] );
269 }
270 }
271 else if ($this->editor)
272 {
273 $rendered_body = $this->render_body();
274 }
275 }
276 catch (\Exception $e)
277 {
278 $rendered_body = \ICanBoogie\Debug::format_alert($e);
279 }
280
281 $this->rendered_body = $rendered_body;
282
283 return $rendered_body;
284 }
285
286 287 288 289 290
291 protected function render_body()
292 {
293 $body = $this->body;
294
295 if (!$this->editor)
296 {
297 return $body;
298 }
299
300 $editor = \ICanBoogie\Core::get()->editors[$this->editor];
301
302 return (string) $editor->render($editor->unserialize($body));
303 }
304
305 306 307 308 309
310 protected function get_year()
311 {
312 return $this->get_date()->year;
313 }
314
315 316 317 318 319
320 protected function get_month()
321 {
322 return $this->get_date()->format('m');
323 }
324
325 326 327 328 329
330 protected function get_day()
331 {
332 return $this->get_date()->format('d');
333 }
334
335 336 337
338 protected function lazy_get_previous()
339 {
340 $ids = $this->model->select('nid')->order('date, created_at, nid')->own->visible->all(\PDO::FETCH_COLUMN);
341 $key = array_search($this->nid, $ids);
342
343 return $key ? $this->model[$ids[$key - 1]] : null;
344 }
345
346 347 348
349 protected function lazy_get_next()
350 {
351 $ids = $this->model->select('nid')->order('date, created_at, nid')->own->visible->all(\PDO::FETCH_COLUMN);
352 $key = array_search($this->nid, $ids);
353
354 return $key < count($ids) - 1 ? $this->model[$ids[$key + 1]] : null;
355 }
356
357 358 359 360 361 362 363 364 365 366
367 public function excerpt($limit=55)
368 {
369 return isset($this->excerpt) ? \ICanBoogie\excerpt($this->excerpt, $limit) : \ICanBoogie\excerpt((string) $this, $limit);
370 }
371 }
372
373 374 375 376 377
378 trait DateProperty
379 {
380 381 382 383 384
385 private $date;
386
387 388 389 390 391
392 protected function get_date()
393 {
394 return DateTimePropertySupport::datetime_get($this->date);
395 }
396
397 398 399 400 401
402 protected function set_date($datetime)
403 {
404 DateTimePropertySupport::datetime_set($this->date, $datetime);
405 }
406 }