PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/titania/includes/objects/faq.php

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