public
Description: The ultra-lightweight ultra-flexible blogging engine with a fetish for birds and misspellings.
Homepage: http://chyrp.net/
Clone URL: git://github.com/vito/chyrp.git
Click here to lend your support to: chyrp and make a donation at www.pledgie.com !
chyrp / includes / model / Page.php
100755 281 lines (241 sloc) 9.996 kb
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
<?php
    /**
* Class: Page
* The Page model.
*
* See Also:
* <Model>
*/
    class Page extends Model {
        public $belongs_to = array("user", "parent" => "page");
 
        public $has_many = array("children" => array("page", "parent"));
 
        /**
* Function: __construct
* See Also:
* <Model::grab>
*/
        public function __construct($page_id, $options = array()) {
            if (!isset($page_id) and empty($options)) return;
            parent::grab($this, $page_id, $options);
 
            if ($this->no_results)
                return false;
 
            $this->slug = $this->url;
 
            $this->filtered = !isset($options["filter"]) or $options["filter"];
 
            $trigger = Trigger::current();
 
            $trigger->filter($this, "page");
 
            if ($this->filtered) {
                $this->title_unfiltered = $this->title;
                $this->body_unfiltered = $this->body;
 
                $trigger->filter($this->title, array("markup_title", "markup_page_title"), $this);
                $trigger->filter($this->body, array("markup_text", "markup_page_text"), $this);
 
                $trigger->filter($this, "filter_page");
            }
        }
 
        /**
* Function: find
* See Also:
* <Model::search>
*/
        static function find($options = array(), $options_for_object = array()) {
            return parent::search(get_class(), $options, $options_for_object);
        }
 
        /**
* Function: add
* Adds a page to the database.
*
* Calls the @add_page@ trigger with the new <Page>.
*
* Parameters:
* $title - The Title for the new page.
* $body - The Body for the new page.
* $body - The <User> or <User.id> of the page's author.
* $parent_id - The ID of the new page's parent page (0 for none).
* $show_in_list - Whether or not to show it in the pages list.
* $list_order - The order of the page in the list.
* $clean - The clean URL.
* $url - The unique URL.
* $created_at - The new page's "created" timestamp.
* $updated_at - The new page's "last updated" timestamp.
*
* Returns:
* The newly created <Page>.
*
* See Also:
* <update>
*/
        static function add($title,
                            $body,
                            $user = null,
                            $parent_id = 0,
                            $show_in_list = true,
                            $list_order = 0,
                            $clean = "",
                            $url = "",
                            $created_at = null,
                            $updated_at = "0000-00-00 00:00:00") {
            $user_id = ($user instanceof User) ? $user->id : $user ;
 
            $sql = SQL::current();
            $visitor = Visitor::current();
            $trigger = Trigger::current();
 
            $sql->insert("pages",
                         array("title" => $title,
                               "body" => $body,
                               "user_id" => oneof($user_id, $visitor->id),
                               "parent_id" => oneof($parent_id, 0),
                               "show_in_list" => oneof($show_in_list, true),
                               "list_order" => oneof($list_order, 0),
                               "clean" => oneof($clean, sanitize($title)),
                               "url" => oneof($url, self::check_url($clean)),
                               "created_at" => oneof($created_at, datetime()),
                               "updated_at" => oneof($updated_at, "0000-00-00 00:00:00")));
 
            $page = new self($sql->latest());
 
            $trigger->call("add_page", $page);
 
            return $page;
        }
 
        /**
* Function: update
* Updates the page.
*
* Calls the @update_page@ trigger with the updated <Page> and the original <Page>.
*
* Parameters:
* $title - The new Title.
* $body - The new Body.
* $body - The <User> or <User.id> of the page's author.
* $parent_id - The new parent ID.
* $show_in_list - Whether or not to show it in the pages list.
* $clean - The page's clean URL.
* $url - The page's unique URL.
* $created_at - The page's "created" timestamp.
* $updated_at - The page's "last updated" timestamp.
*/
        public function update($title = null,
                               $body = null,
                               $user = null,
                               $parent_id = null,
                               $show_in_list = null,
                               $list_order = null,
                               $clean = null,
                               $url = null,
                               $created_at = null,
                               $updated_at = null) {
            if ($this->no_results)
                return false;
 
            $user_id = ($user instanceof User) ? $user->id : $user ;
 
            $sql = SQL::current();
            $trigger = Trigger::current();
 
            $old = clone $this;
 
            foreach (array("title", "body", "user_id", "parent_id", "show_in_list",
                           "list_order", "clean", "url", "created_at", "updated_at") as $attr)
                if ($attr == "updated_at" and $updated_at !== false)
                    $this->$attr = fallback($$attr, datetime());
                else
                    $this->$attr = fallback($$attr, $this->$attr);
 
            $sql->update("pages",
                         array("id" => $this->id),
                         array("title" => $title,
                               "body" => $body,
                               "user_id" => $user,
                               "parent_id" => $parent_id,
                               "show_in_list" => $show_in_list,
                               "list_order" => $list_order,
                               "clean" => $clean,
                               "url" => $url,
                               "created_at" => $created_at,
                               "updated_at" => $updated_at));
 
            $trigger->call("update_page", $this, $old);
        }
 
        /**
* Function: delete
* Deletes the given page.
*
* Calls the @delete_page@ trigger with the <Page> to delete.
*
* Parameters:
* $page_id - The page to delete.
* $recursive - Should the page's children be deleted? (default: false)
*/
        static function delete($page_id, $recursive = false) {
            if ($recursive) {
                $page = new self($page_id);
                foreach ($page->children as $child)
                    self::delete($child->id);
            }
 
            parent::destroy(get_class(), $page_id);
        }
 
        /**
* Function: exists
* Checks if a page exists.
*
* Parameters:
* $page_id - The page ID to check
*/
        static function exists($page_id) {
            return SQL::current()->count("pages", array("id" => $page_id)) == 1;
        }
 
        /**
* Function: check_url
* Checks if a given clean URL is already being used as another page's URL.
*
* Parameters:
* $clean - The clean URL to check.
*
* Returns:
* The unique version of the passed clean URL. If it's not used, it's the same as $clean. If it is, a number is appended.
*/
        static function check_url($clean) {
            $count = SQL::current()->count("pages", array("clean" => $clean));
            return (!$count or empty($clean)) ? $clean : $clean."-".($count + 1) ;
        }
 
        /**
* Function: url
* Returns a page's URL.
*/
        public function url() {
            if ($this->no_results)
                return false;
 
            $config = Config::current();
            if (!$config->clean_urls)
                return $config->url."/?action=page&amp;url=".urlencode($this->url);
 
            $url = array("", urlencode($this->url));
 
            $page = $this;
            while (isset($page->parent_id) and $page->parent_id) {
                $url[] = urlencode($page->parent->url);
                $page = $page->parent;
            }
 
            return url("page/".implode("/", array_reverse($url)));
        }
 
        /**
* Function: parent
* Returns a page's parent. Example: $page->parent()->parent()->title
*
* !! DEPRECATED AFTER 2.0 !!
*/
        public function parent() {
            if ($this->no_results or !$this->parent_id)
                return false;
 
            return new self($this->parent_id);
        }
 
        /**
* Function: children
* Returns a page's children.
*
* !! DEPRECATED AFTER 2.0 !!
*/
        public function children() {
            if ($this->no_results)
                return false;
 
            return self::find(array("where" => array("parent_id" => $this->id)));
        }
 
        /**
* Function: user
* Returns a page's creator. Example: $page->user->full_name
*
* !! DEPRECATED AFTER 2.0 !!
*/
        public function user() {
            if ($this->no_results)
                return false;
 
            return new User($this->user_id);
        }
    }