1 <?php
2
3 /*
4 * This file is part of the Icybee package.
5 *
6 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Icybee\Modules\Pages;
13
14 /**
15 * A content of a page.
16 *
17 * @property-read mixed $rendered The rendered version of the content.
18 */
19 class Content extends \ICanBoogie\ActiveRecord
20 {
21 /**
22 * The identifier of the page the content belongs to.
23 *
24 * @var int
25 */
26 public $pageid;
27
28 /**
29 * The identifier of the content.
30 *
31 * @var string
32 */
33 public $contentid;
34
35 /**
36 * The content.
37 *
38 * @var string
39 */
40 public $content;
41
42 /**
43 * The editor name used to edit and render the content.
44 *
45 * @var string
46 */
47 public $editor;
48
49 /**
50 * The rendered version of the content.
51 *
52 * @var string|object
53 */
54 private $rendered;
55
56 /**
57 * Returns the rendered contents.
58 *
59 * @return mixed
60 */
61 protected function get_rendered()
62 {
63 return $this->render();
64 }
65
66 public function __construct($model='pages/contents')
67 {
68 parent::__construct($model);
69 }
70
71 /**
72 * Renders the content as a string or an object.
73 *
74 * Exceptions thrown during the rendering are caught. The message of the exception is used
75 * as rendered content and the exception is rethrown.
76 *
77 * @throws Exception
78 *
79 * @return string|object The rendered content.
80 */
81 public function render()
82 {
83 if ($this->rendered !== null)
84 {
85 return $this->rendered;
86 }
87
88 $editor = \ICanBoogie\Core::get()->editors[$this->editor];
89 return $this->rendered = $editor->render($editor->unserialize($this->content));
90 }
91
92 public function __toString()
93 {
94 try
95 {
96 return (string) $this->render();
97 }
98 catch (\Exception $e)
99 {
100 return \ICanBoogie\Debug::format_alert($e);
101 }
102 }
103 }