KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Page.php 10KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * Class: Page
  4. * The Page model.
  5. *
  6. * See Also:
  7. * <Model>
  8. */
  9. class Page extends Model {
  10. public $belongs_to = array("user", "parent" => array("model" => "page"));
  11. public $has_many = array("children" => array("model" => "page", "by" => "parent"));
  12. /**
  13. * Function: __construct
  14. * See Also:
  15. * <Model::grab>
  16. */
  17. public function __construct($page_id, $options = array()) {
  18. if (!isset($page_id) and empty($options)) return;
  19. parent::grab($this, $page_id, $options);
  20. if ($this->no_results)
  21. return false;
  22. $this->slug = $this->url;
  23. $this->filtered = !isset($options["filter"]) or $options["filter"];
  24. $trigger = Trigger::current();
  25. $trigger->filter($this, "page");
  26. if ($this->filtered) {
  27. $this->title_unfiltered = $this->title;
  28. $this->body_unfiltered = $this->body;
  29. $trigger->filter($this->title, array("markup_title", "markup_page_title"), $this);
  30. $trigger->filter($this->body, array("markup_text", "markup_page_text"), $this);
  31. $trigger->filter($this, "filter_page");
  32. }
  33. }
  34. /**
  35. * Function: find
  36. * See Also:
  37. * <Model::search>
  38. */
  39. static function find($options = array(), $options_for_object = array()) {
  40. return parent::search(get_class(), $options, $options_for_object);
  41. }
  42. /**
  43. * Function: add
  44. * Adds a page to the database.
  45. *
  46. * Calls the @add_page@ trigger with the new <Page>.
  47. *
  48. * Parameters:
  49. * $title - The Title for the new page.
  50. * $body - The Body for the new page.
  51. * $user - The <User> or <User.id> of the page's author.
  52. * $parent_id - The ID of the new page's parent page (0 for none).
  53. * $show_in_list - Whether or not to show it in the pages list.
  54. * $list_order - The order of the page in the list.
  55. * $clean - The clean URL.
  56. * $url - The unique URL.
  57. * $created_at - The new page's "created" timestamp.
  58. * $updated_at - The new page's "last updated" timestamp.
  59. *
  60. * Returns:
  61. * The newly created <Page>.
  62. *
  63. * See Also:
  64. * <update>
  65. */
  66. static function add($title,
  67. $body,
  68. $user = null,
  69. $parent_id = 0,
  70. $show_in_list = true,
  71. $list_order = 0,
  72. $clean = "",
  73. $url = "",
  74. $created_at = null,
  75. $updated_at = null) {
  76. $user_id = ($user instanceof User) ? $user->id : $user ;
  77. $sql = SQL::current();
  78. $visitor = Visitor::current();
  79. $trigger = Trigger::current();
  80. $new_values = array("title" => $title,
  81. "body" => $body,
  82. "user_id" => oneof($user_id, $visitor->id),
  83. "parent_id" => oneof($parent_id, 0),
  84. "show_in_list" => oneof($show_in_list, true),
  85. "list_order" => oneof($list_order, 0),
  86. "clean" => oneof($clean, sanitize($title)),
  87. "url" => oneof($url, self::check_url($clean)),
  88. "created_at" => oneof($created_at, datetime()),
  89. "updated_at" => oneof($updated_at, "0000-00-00 00:00:00"));
  90. $trigger->filter($new_values, "before_add_page");
  91. $sql->insert("pages", $new_values);
  92. $page = new self($sql->latest("pages"));
  93. $trigger->call("add_page", $page);
  94. return $page;
  95. }
  96. /**
  97. * Function: update
  98. * Updates the page.
  99. *
  100. * Calls the @update_page@ trigger with the updated <Page> and the original <Page>.
  101. *
  102. * Parameters:
  103. * $title - The new Title.
  104. * $body - The new Body.
  105. * $user - The <User> or <User.id> of the page's author.
  106. * $parent_id - The new parent ID.
  107. * $show_in_list - Whether or not to show it in the pages list.
  108. * $clean - The page's clean URL.
  109. * $url - The page's unique URL.
  110. * $created_at - The page's "created" timestamp.
  111. * $updated_at - The page's "last updated" timestamp.
  112. */
  113. public function update($title = null,
  114. $body = null,
  115. $user = null,
  116. $parent_id = null,
  117. $show_in_list = null,
  118. $list_order = null,
  119. $clean = null,
  120. $url = null,
  121. $created_at = null,
  122. $updated_at = null) {
  123. if ($this->no_results)
  124. return false;
  125. $user_id = ($user instanceof User) ? $user->id : $user ;
  126. $sql = SQL::current();
  127. $trigger = Trigger::current();
  128. $old = clone $this;
  129. foreach (array("title", "body", "user_id", "parent_id", "show_in_list",
  130. "list_order", "clean", "url", "created_at", "updated_at") as $attr)
  131. if ($attr == "updated_at" and $$attr === null)
  132. $this->$attr = $$attr = datetime();
  133. else
  134. $this->$attr = $$attr = ($$attr !== null ? $$attr : $this->$attr);
  135. $new_values = array("title" => $title,
  136. "body" => $body,
  137. "user_id" => $user_id,
  138. "parent_id" => $parent_id,
  139. "show_in_list" => $show_in_list,
  140. "list_order" => $list_order,
  141. "clean" => $clean,
  142. "url" => $url,
  143. "created_at" => $created_at,
  144. "updated_at" => $updated_at);
  145. $trigger->filter($new_values, "before_update_page");
  146. $sql->update("pages",
  147. array("id" => $this->id),
  148. $new_values);
  149. $trigger->call("update_page", $this, $old);
  150. }
  151. /**
  152. * Function: delete
  153. * Deletes the given page.
  154. *
  155. * Calls the @delete_page@ trigger with the <Page> to delete.
  156. *
  157. * Parameters:
  158. * $page_id - The page to delete.
  159. * $recursive - Should the page's children be deleted? (default: false)
  160. */
  161. static function delete($page_id, $recursive = false) {
  162. if ($recursive) {
  163. $page = new self($page_id);
  164. foreach ($page->children as $child)
  165. self::delete($child->id);
  166. }
  167. parent::destroy(get_class(), $page_id);
  168. }
  169. /**
  170. * Function: exists
  171. * Checks if a page exists.
  172. *
  173. * Parameters:
  174. * $page_id - The page ID to check
  175. */
  176. static function exists($page_id) {
  177. return SQL::current()->count("pages", array("id" => $page_id)) == 1;
  178. }
  179. /**
  180. * Function: check_url
  181. * Checks if a given clean URL is already being used as another page's URL.
  182. *
  183. * Parameters:
  184. * $clean - The clean URL to check.
  185. *
  186. * Returns:
  187. * 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.
  188. */
  189. static function check_url($clean) {
  190. $count = SQL::current()->count("pages", array("clean" => $clean));
  191. return (!$count or empty($clean)) ? $clean : $clean."-".($count + 1) ;
  192. }
  193. /**
  194. * Function: url
  195. * Returns a page's URL.
  196. */
  197. public function url() {
  198. if ($this->no_results)
  199. return false;
  200. $config = Config::current();
  201. if (!$config->clean_urls)
  202. return $config->url."/?action=page&amp;url=".urlencode($this->url);
  203. $url = array("", urlencode($this->url));
  204. $page = $this;
  205. while (isset($page->parent_id) and $page->parent_id) {
  206. $url[] = urlencode($page->parent->url);
  207. $page = $page->parent;
  208. }
  209. return url("page/".implode("/", array_reverse($url)), MainController::current());
  210. }
  211. /**
  212. * Function: parent
  213. * Returns a page's parent. Example: $page->parent()->parent()->title
  214. *
  215. * !! DEPRECATED AFTER 2.0 !!
  216. */
  217. public function parent() {
  218. if ($this->no_results or !$this->parent_id)
  219. return false;
  220. return new self($this->parent_id);
  221. }
  222. /**
  223. * Function: children
  224. * Returns a page's children.
  225. *
  226. * !! DEPRECATED AFTER 2.0 !!
  227. */
  228. public function children() {
  229. if ($this->no_results)
  230. return false;
  231. return self::find(array("where" => array("parent_id" => $this->id)));
  232. }
  233. /**
  234. * Function: user
  235. * Returns a page's creator. Example: $page->user->full_name
  236. *
  237. * !! DEPRECATED AFTER 2.0 !!
  238. */
  239. public function user() {
  240. if ($this->no_results)
  241. return false;
  242. return new User($this->user_id);
  243. }
  244. }