PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/titania/includes/objects/faq.php

https://github.com/bantu/customisation-db
PHP | 318 lines | 185 code | 43 blank | 90 comment | 20 complexity | 2e7ee18bb140f6b1c139b7ebecbddf07 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package Titania
  5. * @version $Id$
  6. * @copyright (c) 2008 phpBB Customisation Database Team
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_TITANIA'))
  14. {
  15. exit;
  16. }
  17. if (!class_exists('titania_message_object'))
  18. {
  19. require TITANIA_ROOT . 'includes/core/object_message.' . PHP_EXT;
  20. }
  21. /**
  22. * Class to titania faq.
  23. * @package Titania
  24. */
  25. class titania_faq extends titania_message_object
  26. {
  27. /**
  28. * SQL Table
  29. *
  30. * @var string
  31. */
  32. protected $sql_table = TITANIA_CONTRIB_FAQ_TABLE;
  33. /**
  34. * SQL identifier field
  35. *
  36. * @var string
  37. */
  38. protected $sql_id_field = 'faq_id';
  39. /**
  40. * Object type (for message tool)
  41. *
  42. * @var string
  43. */
  44. protected $object_type = TITANIA_FAQ;
  45. /**
  46. * Constructor class for titania faq
  47. *
  48. * @param int $faq_id
  49. */
  50. public function __construct($faq_id = false)
  51. {
  52. // Configure object properties
  53. $this->object_config = array_merge($this->object_config, array(
  54. 'faq_id' => array('default' => 0),
  55. 'contrib_id' => array('default' => 0),
  56. 'faq_subject' => array('default' => '', 'message_field' => 'subject', 'max' => 255),
  57. 'faq_text' => array('default' => '', 'message_field' => 'message'),
  58. 'faq_text_bitfield' => array('default' => '', 'message_field' => 'message_bitfield'),
  59. 'faq_text_uid' => array('default' => '', 'message_field' => 'message_uid'),
  60. 'faq_text_options' => array('default' => 7, 'message_field' => 'message_options'),
  61. 'faq_views' => array('default' => 0),
  62. 'faq_access' => array('default' => 2, 'message_field' => 'access'),
  63. 'left_id' => array('default' => 0),
  64. 'right_id' => array('default' => 0),
  65. ));
  66. if ($faq_id !== false)
  67. {
  68. $this->faq_id = $faq_id;
  69. $this->load();
  70. }
  71. }
  72. /**
  73. * Validate that all the data is correct
  74. *
  75. * @return array empty array on success, array with (string) errors ready for output on failure
  76. */
  77. public function validate()
  78. {
  79. $error = array();
  80. if (utf8_clean_string($this->faq_subject) === '')
  81. {
  82. $error[] = phpbb::$user->lang['EMPTY_SUBJECT'];
  83. }
  84. $message_length = utf8_strlen($this->faq_text);
  85. if ($message_length < (int) phpbb::$config['min_post_chars'])
  86. {
  87. $error[] = sprintf(phpbb::$user->lang['TOO_FEW_CHARS_LIMIT'], $message_length, (int) phpbb::$config['min_post_chars']);
  88. }
  89. else if (phpbb::$config['max_post_chars'] > 0 && $message_length > (int) phpbb::$config['max_post_chars'])
  90. {
  91. $error[] = sprintf(phpbb::$user->lang['TOO_MANY_CHARS_POST'], $message_length, (int) phpbb::$config['max_post_chars']);
  92. }
  93. return $error;
  94. }
  95. /**
  96. * Submit data in the post_data format (from includes/tools/message.php)
  97. *
  98. * @param object $message The message object
  99. */
  100. public function post_data($message)
  101. {
  102. $this->__set_array(array(
  103. 'contrib_id' => titania::$contrib->contrib_id,
  104. ));
  105. parent::post_data($message);
  106. }
  107. /**
  108. * Build view URL for a faq
  109. *
  110. * @param string $action
  111. * @param int $faq_id
  112. *
  113. * @return string
  114. */
  115. public function get_url($action = '', $faq_id = false)
  116. {
  117. $url = titania::$contrib->get_url('faq');
  118. $faq_id = (($faq_id) ? $faq_id : $this->faq_id);
  119. if ($action == 'create')
  120. {
  121. return titania_url::append_url($url, array('action' => $action));
  122. }
  123. else if (!$action)
  124. {
  125. return titania_url::append_url($url, array('f' => $faq_id));
  126. }
  127. return titania_url::append_url($url, array('action' => $action, 'f' => $faq_id));
  128. }
  129. /**
  130. * Update data or submit new faq
  131. *
  132. * @return void
  133. */
  134. public function submit()
  135. {
  136. // Get the FAQ count to update it
  137. $sql = 'SELECT contrib_faq_count FROM ' . TITANIA_CONTRIBS_TABLE . '
  138. WHERE contrib_id = ' . $this->contrib_id;
  139. phpbb::$db->sql_query($sql);
  140. $contrib_faq_count = phpbb::$db->sql_fetchfield('contrib_faq_count');
  141. phpbb::$db->sql_freeresult();
  142. // If already submitted we need to decrement first
  143. if ($this->faq_id)
  144. {
  145. if (empty($this->sql_data))
  146. {
  147. throw new exception('Modifying a FAQ entry requires you load it through the load() function (we require the original information).');
  148. }
  149. $original_flags = titania_count::update_flags($this->sql_data['faq_access']);
  150. $contrib_faq_count = titania_count::decrement($contrib_faq_count, $original_flags);
  151. }
  152. // Update the FAQ count
  153. $flags = titania_count::update_flags($this->faq_access);
  154. $sql = 'UPDATE ' . TITANIA_CONTRIBS_TABLE . '
  155. SET contrib_faq_count = \'' . phpbb::$db->sql_escape(titania_count::increment($contrib_faq_count, $flags)) . '\'
  156. WHERE contrib_id = ' . $this->contrib_id;
  157. phpbb::$db->sql_query($sql);
  158. // Submit this FAQ item
  159. parent::submit();
  160. // Index
  161. titania_search::index(TITANIA_FAQ, $this->faq_id, array(
  162. 'title' => $this->faq_subject,
  163. 'text' => $this->faq_text,
  164. 'text_uid' => $this->faq_text_uid,
  165. 'text_bitfield' => $this->faq_text_bitfield,
  166. 'text_options' => $this->faq_text_options,
  167. 'author' => 0,
  168. 'date' => 0,
  169. 'url' => titania_url::unbuild_url($this->get_url()),
  170. 'access_level' => $this->faq_access,
  171. ));
  172. }
  173. public function delete()
  174. {
  175. titania_search::delete(TITANIA_FAQ, $this->faq_id);
  176. // Update the FAQ count
  177. $sql = 'SELECT contrib_faq_count FROM ' . TITANIA_CONTRIBS_TABLE . '
  178. WHERE contrib_id = ' . $this->contrib_id;
  179. phpbb::$db->sql_query($sql);
  180. $contrib_faq_count = phpbb::$db->sql_fetchfield('contrib_faq_count');
  181. phpbb::$db->sql_freeresult();
  182. $flags = titania_count::update_flags($this->faq_access);
  183. $sql = 'UPDATE ' . TITANIA_CONTRIBS_TABLE . '
  184. SET contrib_faq_count = \'' . phpbb::$db->sql_escape(titania_count::decrement($contrib_faq_count, $flags)) . '\'
  185. WHERE contrib_id = ' . $this->contrib_id;
  186. phpbb::$db->sql_query($sql);
  187. parent::delete();
  188. }
  189. /**
  190. * Move a FAQ item
  191. *
  192. * @param string $direction (move_up|move_down)
  193. */
  194. public function move($faq_row, $action = 'move_up', $steps = 1)
  195. {
  196. /**
  197. * Fetch all the siblings between the faq's current spot
  198. * and where we want to move it to. If there are less than $steps
  199. * siblings between the current spot and the target then the
  200. * faq will move as far as possible
  201. */
  202. $sql = 'SELECT faq_id, left_id, right_id
  203. FROM ' . $this->sql_table . '
  204. WHERE contrib_id = ' . $this->contrib_id . '
  205. AND ' . (($action == 'move_up') ? "right_id < {$faq_row['right_id']} ORDER BY right_id DESC" : "left_id > {$faq_row['left_id']} ORDER BY left_id ASC");
  206. $result = phpbb::$db->sql_query_limit($sql, $steps);
  207. $target = array();
  208. while ($row = phpbb::$db->sql_fetchrow($result))
  209. {
  210. $target = $row;
  211. }
  212. phpbb::$db->sql_freeresult($result);
  213. if (!sizeof($target))
  214. {
  215. // The faq is already on top or bottom
  216. return false;
  217. }
  218. /**
  219. * $left_id and $right_id define the scope of the nodes that are affected by the move.
  220. * $diff_up and $diff_down are the values to substract or add to each node's left_id
  221. * and right_id in order to move them up or down.
  222. * $move_up_left and $move_up_right define the scope of the nodes that are moving
  223. * up. Other nodes in the scope of ($left_id, $right_id) are considered to move down.
  224. */
  225. if ($action == 'move_up')
  226. {
  227. $left_id = $target['left_id'];
  228. $right_id = $faq_row['right_id'];
  229. $diff_up = $faq_row['left_id'] - $target['left_id'];
  230. $diff_down = $faq_row['right_id'] + 1 - $faq_row['left_id'];
  231. $move_up_left = $faq_row['left_id'];
  232. $move_up_right = $faq_row['right_id'];
  233. }
  234. else
  235. {
  236. $left_id = $faq_row['left_id'];
  237. $right_id = $target['right_id'];
  238. $diff_up = $faq_row['right_id'] + 1 - $faq_row['left_id'];
  239. $diff_down = $target['right_id'] - $faq_row['right_id'];
  240. $move_up_left = $faq_row['right_id'] + 1;
  241. $move_up_right = $target['right_id'];
  242. }
  243. // Now do the dirty job
  244. $sql = 'UPDATE ' . $this->sql_table . "
  245. SET left_id = left_id + CASE
  246. WHEN left_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
  247. ELSE {$diff_down}
  248. END,
  249. right_id = right_id + CASE
  250. WHEN right_id BETWEEN {$move_up_left} AND {$move_up_right} THEN -{$diff_up}
  251. ELSE {$diff_down}
  252. END
  253. WHERE contrib_id = " . $this->contrib_id . "
  254. AND left_id BETWEEN {$left_id} AND {$right_id}
  255. AND right_id BETWEEN {$left_id} AND {$right_id}";
  256. phpbb::$db->sql_query($sql);
  257. return true;
  258. }
  259. /*
  260. * Increase a FAQ views counter
  261. *
  262. * @return void
  263. */
  264. public function increase_views_counter()
  265. {
  266. if (phpbb::$user->data['is_bot'])
  267. {
  268. return;
  269. }
  270. $sql = 'UPDATE ' . $this->sql_table . '
  271. SET faq_views = faq_views + 1
  272. WHERE faq_id = ' . (int) $this->faq_id;
  273. phpbb::$db->sql_query($sql);
  274. }
  275. }