/backend/modules/faq/engine/model.php

https://github.com/janmoesen/forkcms · PHP · 432 lines · 249 code · 39 blank · 144 comment · 10 complexity · 4f0b073434a011241aee794cbff2e3af MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * In this file we store all generic functions that we will be using in the faq module
  10. *
  11. * @author Lester Lievens <lester.lievens@netlash.com>
  12. * @author Matthias Mullie <matthias@mullie.eu>
  13. * @author Annelies Van Extergem <annelies.vanextergem@netlash.com>
  14. * @author Jelmer Snoeck <jelmer.snoeck@netlash.com>
  15. */
  16. class BackendFaqModel
  17. {
  18. const QRY_DATAGRID_BROWSE =
  19. 'SELECT i.id, i.category_id, i.question, i.hidden, i.sequence
  20. FROM faq_questions AS i
  21. WHERE i.language = ? AND i.category_id = ?
  22. ORDER BY i.sequence ASC';
  23. const QRY_DATAGRID_BROWSE_CATEGORIES =
  24. 'SELECT i.id, i.title, COUNT(p.id) AS num_items, i.sequence
  25. FROM faq_categories AS i
  26. LEFT OUTER JOIN faq_questions AS p ON i.id = p.category_id AND p.language = i.language
  27. WHERE i.language = ?
  28. GROUP BY i.id
  29. ORDER BY i.sequence ASC';
  30. /**
  31. * Delete a question
  32. *
  33. * @param int $id
  34. */
  35. public static function delete($id)
  36. {
  37. BackendModel::getDB(true)->delete('faq_questions', 'id = ?', array((int) $id));
  38. BackendTagsModel::saveTags($id, '', 'faq');
  39. }
  40. /**
  41. * Delete a specific category
  42. *
  43. * @param int $id
  44. */
  45. public static function deleteCategory($id)
  46. {
  47. $db = BackendModel::getDB(true);
  48. $item = self::getCategory($id);
  49. if(!empty($item))
  50. {
  51. $db->delete('meta', 'id = ?', array($item['meta_id']));
  52. $db->delete('faq_categories', 'id = ?', array((int) $id));
  53. $db->update('faq_questions', array('category_id' => null), 'category_id = ?', array((int) $id));
  54. // invalidate the cache for blog
  55. BackendModel::invalidateFrontendCache('faq', BL::getWorkingLanguage());
  56. }
  57. }
  58. /**
  59. * Is the deletion of a category allowed?
  60. *
  61. * @param int $id
  62. * @return bool
  63. */
  64. public static function deleteCategoryAllowed($id)
  65. {
  66. return (BackendModel::getDB()->getVar(
  67. 'SELECT 1
  68. FROM faq_questions AS i
  69. WHERE i.category_id = ? AND i.language = ?
  70. LIMIT 1',
  71. array((int) $id, BL::getWorkingLanguage())) == 0);
  72. }
  73. /**
  74. * Delete the feedback
  75. *
  76. * @param int $itemId
  77. */
  78. public static function deleteFeedback($itemId)
  79. {
  80. BackendModel::getDB(true)->update('faq_feedback', array('processed' => 'Y'), 'id = ?', (int) $itemId);
  81. }
  82. /**
  83. * Does the question exsist?
  84. *
  85. * @param int $id
  86. * @return bool
  87. */
  88. public static function exists($id)
  89. {
  90. return (bool) BackendModel::getDB()->getVar(
  91. 'SELECT 1
  92. FROM faq_questions AS i
  93. WHERE i.id = ? AND i.language = ?
  94. LIMIT 1',
  95. array((int) $id, BL::getWorkingLanguage()));
  96. }
  97. /**
  98. * Does the category exist?
  99. *
  100. * @param int $id
  101. * @return bool
  102. */
  103. public static function existsCategory($id)
  104. {
  105. return (bool) BackendModel::getDB()->getVar(
  106. 'SELECT 1
  107. FROM faq_categories AS i
  108. WHERE i.id = ? AND i.language = ?
  109. LIMIT 1',
  110. array((int) $id, BL::getWorkingLanguage()));
  111. }
  112. /**
  113. * Fetch a question
  114. *
  115. * @param int $id
  116. * @return array
  117. */
  118. public static function get($id)
  119. {
  120. return (array) BackendModel::getDB()->getRecord(
  121. 'SELECT i.*, m.url
  122. FROM faq_questions AS i
  123. INNER JOIN meta AS m ON m.id = i.meta_id
  124. WHERE i.id = ? AND i.language = ?',
  125. array((int) $id, BL::getWorkingLanguage()));
  126. }
  127. /**
  128. * Fetches all the feedback that is available
  129. *
  130. * @param int $limit
  131. * @return array
  132. */
  133. public static function getAllFeedback($limit = 5)
  134. {
  135. return (array) BackendModel::getDB()->getRecords(
  136. 'SELECT f.*
  137. FROM faq_feedback AS f
  138. WHERE f.processed = ?
  139. LIMIT ?',
  140. array('N', (int) $limit)
  141. );
  142. }
  143. /**
  144. * Fetches all the feedback for a question
  145. *
  146. * @param int $id The question id.
  147. * @return array
  148. */
  149. public static function getAllFeedbackForQuestion($id)
  150. {
  151. return (array) BackendModel::getDB()->getRecords(
  152. 'SELECT f.*
  153. FROM faq_feedback AS f
  154. WHERE f.question_id = ? AND f.processed = ?',
  155. array((int) $id, 'N'));
  156. }
  157. /**
  158. * Get all items by a given tag id
  159. *
  160. * @param int $tagId
  161. * @return array
  162. */
  163. public static function getByTag($tagId)
  164. {
  165. $items = (array) BackendModel::getDB()->getRecords(
  166. 'SELECT i.id AS url, i.question, mt.module
  167. FROM modules_tags AS mt
  168. INNER JOIN tags AS t ON mt.tag_id = t.id
  169. INNER JOIN faq_questions AS i ON mt.other_id = i.id
  170. WHERE mt.module = ? AND mt.tag_id = ? AND i.language = ?',
  171. array('faq', (int) $tagId, BL::getWorkingLanguage()));
  172. foreach($items as &$row)
  173. {
  174. $row['url'] = BackendModel::createURLForAction('edit', 'faq', null, array('id' => $row['url']));
  175. }
  176. return $items;
  177. }
  178. /**
  179. * Get all the categories
  180. *
  181. * @param bool[optional] $includeCount
  182. * @return array
  183. */
  184. public static function getCategories($includeCount = false)
  185. {
  186. $db = BackendModel::getDB();
  187. if($includeCount)
  188. {
  189. return (array) $db->getPairs(
  190. 'SELECT i.id, CONCAT(i.title, " (", COUNT(p.category_id) ,")") AS title
  191. FROM faq_categories AS i
  192. LEFT OUTER JOIN faq_questions AS p ON i.id = p.category_id AND i.language = p.language
  193. WHERE i.language = ?
  194. GROUP BY i.id',
  195. array(BL::getWorkingLanguage()));
  196. }
  197. return (array) $db->getPairs(
  198. 'SELECT i.id, i.title
  199. FROM faq_categories AS i
  200. WHERE i.language = ?',
  201. array(BL::getWorkingLanguage()));
  202. }
  203. /**
  204. * Fetch a category
  205. *
  206. * @param int $id
  207. * @return array
  208. */
  209. public static function getCategory($id)
  210. {
  211. return (array) BackendModel::getDB()->getRecord(
  212. 'SELECT i.*
  213. FROM faq_categories AS i
  214. WHERE i.id = ? AND i.language = ?',
  215. array((int) $id, BL::getWorkingLanguage()));
  216. }
  217. /**
  218. * Fetch the feedback item
  219. *
  220. * @param int $id
  221. * @return array
  222. */
  223. public static function getFeedback($id)
  224. {
  225. return (array) BackendModel::getDB()->getRecord(
  226. 'SELECT f.*
  227. FROM faq_feedback AS f
  228. WHERE f.id = ?',
  229. array((int) $id));
  230. }
  231. /**
  232. * Get the maximum sequence for a category
  233. *
  234. * @return int
  235. */
  236. public static function getMaximumCategorySequence()
  237. {
  238. return (int) BackendModel::getDB()->getVar(
  239. 'SELECT MAX(i.sequence)
  240. FROM faq_categories AS i
  241. WHERE i.language = ?',
  242. array(BL::getWorkingLanguage()));
  243. }
  244. /**
  245. * Get the max sequence id for a category
  246. *
  247. * @param int $id The category id.
  248. * @return int
  249. */
  250. public static function getMaximumSequence($id)
  251. {
  252. return (int) BackendModel::getDB()->getVar(
  253. 'SELECT MAX(i.sequence)
  254. FROM faq_questions AS i
  255. WHERE i.category_id = ?',
  256. array((int) $id));
  257. }
  258. /**
  259. * Retrieve the unique URL for an item
  260. *
  261. * @param string $url
  262. * @param int[optional] $id The id of the item to ignore.
  263. * @return string
  264. */
  265. public static function getURL($url, $id = null)
  266. {
  267. $url = SpoonFilter::urlise((string) $url);
  268. $db = BackendModel::getDB();
  269. // new item
  270. if($id === null)
  271. {
  272. // already exists
  273. if((bool) $db->getVar(
  274. 'SELECT 1
  275. FROM faq_questions AS i
  276. INNER JOIN meta AS m ON i.meta_id = m.id
  277. WHERE i.language = ? AND m.url = ?
  278. LIMIT 1',
  279. array(BL::getWorkingLanguage(), $url)))
  280. {
  281. $url = BackendModel::addNumber($url);
  282. return self::getURL($url);
  283. }
  284. }
  285. // current category should be excluded
  286. else
  287. {
  288. // already exists
  289. if((bool) $db->getVar(
  290. 'SELECT 1
  291. FROM faq_questions AS i
  292. INNER JOIN meta AS m ON i.meta_id = m.id
  293. WHERE i.language = ? AND m.url = ? AND i.id != ?
  294. LIMIT 1',
  295. array(BL::getWorkingLanguage(), $url, $id)))
  296. {
  297. $url = BackendModel::addNumber($url);
  298. return self::getURL($url, $id);
  299. }
  300. }
  301. return $url;
  302. }
  303. /**
  304. * Retrieve the unique URL for a category
  305. *
  306. * @param string $url
  307. * @param int[optional] $id The id of the category to ignore.
  308. * @return string
  309. */
  310. public static function getURLForCategory($url, $id = null)
  311. {
  312. $url = SpoonFilter::urlise((string) $url);
  313. $db = BackendModel::getDB();
  314. // new category
  315. if($id === null)
  316. {
  317. if((bool) $db->getVar(
  318. 'SELECT 1
  319. FROM faq_categories AS i
  320. INNER JOIN meta AS m ON i.meta_id = m.id
  321. WHERE i.language = ? AND m.url = ?
  322. LIMIT 1',
  323. array(BL::getWorkingLanguage(), $url)))
  324. {
  325. $url = BackendModel::addNumber($url);
  326. return self::getURLForCategory($url);
  327. }
  328. }
  329. // current category should be excluded
  330. else
  331. {
  332. if((bool) $db->getVar(
  333. 'SELECT 1
  334. FROM faq_categories AS i
  335. INNER JOIN meta AS m ON i.meta_id = m.id
  336. WHERE i.language = ? AND m.url = ? AND i.id != ?
  337. LIMIT 1',
  338. array(BL::getWorkingLanguage(), $url, $id)))
  339. {
  340. $url = BackendModel::addNumber($url);
  341. return self::getURLForCategory($url, $id);
  342. }
  343. }
  344. return $url;
  345. }
  346. /**
  347. * Insert a question in the database
  348. *
  349. * @param array $item
  350. * @return int
  351. */
  352. public static function insert(array $item)
  353. {
  354. $insertId = BackendModel::getDB(true)->insert('faq_questions', $item);
  355. BackendModel::invalidateFrontendCache('faq', BL::getWorkingLanguage());
  356. return $insertId;
  357. }
  358. /**
  359. * Insert a category in the database
  360. *
  361. * @param array $item
  362. * @param array[optional] $meta The metadata for the category to insert.
  363. * @return int
  364. */
  365. public static function insertCategory(array $item, $meta = null)
  366. {
  367. $db = BackendModel::getDB(true);
  368. if($meta !== null) $item['meta_id'] = $db->insert('meta', $meta);
  369. $item['id'] = $db->insert('faq_categories', $item);
  370. BackendModel::invalidateFrontendCache('faq', BL::getWorkingLanguage());
  371. return $item['id'];
  372. }
  373. /**
  374. * Update a certain question
  375. *
  376. * @param array $item
  377. */
  378. public static function update(array $item)
  379. {
  380. BackendModel::getDB(true)->update('faq_questions', $item, 'id = ?', array((int) $item['id']));
  381. BackendModel::invalidateFrontendCache('faq', BL::getWorkingLanguage());
  382. }
  383. /**
  384. * Update a certain category
  385. *
  386. * @param array $item
  387. */
  388. public static function updateCategory(array $item)
  389. {
  390. BackendModel::getDB(true)->update('faq_categories', $item, 'id = ?', array($item['id']));
  391. BackendModel::invalidateFrontendCache('faq', BL::getWorkingLanguage());
  392. }
  393. }