/backend/modules/content_blocks/engine/model.php

https://github.com/Doap/forkcms · PHP · 267 lines · 147 code · 36 blank · 84 comment · 5 complexity · a815e5646791b297aba4ab525cedaf88 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 content_blocks module
  10. *
  11. * @author Davy Hellemans <davy@netlash.com>
  12. * @author Tijs Verkoyen <tijs@sumocoders.be>
  13. * @author Matthias Mullie <matthias@mullie.eu>
  14. * @author Dieter Vanden Eynde <dieter@netlash.com>
  15. */
  16. class BackendContentBlocksModel
  17. {
  18. const QRY_BROWSE =
  19. 'SELECT i.id, i.title
  20. FROM content_blocks AS i
  21. WHERE i.status = ? AND i.language = ?';
  22. const QRY_BROWSE_REVISIONS =
  23. 'SELECT i.id, i.revision_id, i.title, UNIX_TIMESTAMP(i.edited_on) AS edited_on, i.user_id
  24. FROM content_blocks AS i
  25. WHERE i.status = ? AND i.id = ? AND i.language = ?
  26. ORDER BY i.edited_on DESC';
  27. /**
  28. * Delete an item.
  29. *
  30. * @param int $id The id of the record to delete.
  31. */
  32. public static function delete($id)
  33. {
  34. $id = (int) $id;
  35. $db = BackendModel::getDB(true);
  36. // get item
  37. $item = self::get($id);
  38. // build extra
  39. $extra = array('id' => $item['extra_id'],
  40. 'module' => 'content_blocks',
  41. 'type' => 'widget',
  42. 'action' => 'detail');
  43. // delete extra
  44. $db->delete('modules_extras', 'id = ? AND module = ? AND type = ? AND action = ?', array($extra['id'], $extra['module'], $extra['type'], $extra['action']));
  45. // update blocks with this item linked
  46. $db->update('pages_blocks', array('extra_id' => null, 'html' => ''), 'extra_id = ?', array($item['extra_id']));
  47. // delete all records
  48. $db->delete('content_blocks', 'id = ? AND i.language = ?', array($id, BL::getWorkingLanguage()));
  49. }
  50. /**
  51. * Does the item exist.
  52. *
  53. * @param int $id The id of the record to check for existence.
  54. * @param bool[optional] $activeOnly Only check in active items?
  55. * @return bool
  56. */
  57. public static function exists($id, $activeOnly = true)
  58. {
  59. $db = BackendModel::getDB();
  60. // if the item should also be active, there should be at least one row to return true
  61. if((bool) $activeOnly) return (bool) $db->getVar(
  62. 'SELECT COUNT(i.id)
  63. FROM content_blocks AS i
  64. WHERE i.id = ? AND i.status = ? AND i.language = ?',
  65. array((int) $id, 'active', BL::getWorkingLanguage())
  66. );
  67. // fallback, this doesn't take the active status in account
  68. return (bool) $db->getVar(
  69. 'SELECT COUNT(i.id)
  70. FROM content_blocks AS i
  71. WHERE i.revision_id = ? AND i.language = ?',
  72. array((int) $id, BL::getWorkingLanguage())
  73. );
  74. }
  75. /**
  76. * Get all data for a given id.
  77. *
  78. * @param int $id The id for the record to get.
  79. * @return array
  80. */
  81. public static function get($id)
  82. {
  83. return (array) BackendModel::getDB()->getRecord(
  84. 'SELECT i.*, UNIX_TIMESTAMP(i.created_on) AS created_on, UNIX_TIMESTAMP(i.edited_on) AS edited_on
  85. FROM content_blocks AS i
  86. WHERE i.id = ? AND i.status = ? AND i.language = ?
  87. LIMIT 1',
  88. array((int) $id, 'active', BL::getWorkingLanguage())
  89. );
  90. }
  91. /**
  92. * Get the maximum id.
  93. *
  94. * @return int
  95. */
  96. public static function getMaximumId()
  97. {
  98. return (int) BackendModel::getDB()->getVar(
  99. 'SELECT MAX(i.id) FROM content_blocks AS i WHERE i.language = ? LIMIT 1',
  100. array(BL::getWorkingLanguage())
  101. );
  102. }
  103. /**
  104. * Get all data for a given revision.
  105. *
  106. * @param int $id The Id for the item wherefor you want a revision.
  107. * @param int $revisionId The Id of the revision.
  108. * @return array
  109. */
  110. public static function getRevision($id, $revisionId)
  111. {
  112. return (array) BackendModel::getDB()->getRecord(
  113. 'SELECT i.*, UNIX_TIMESTAMP(i.created_on) AS created_on, UNIX_TIMESTAMP(i.edited_on) AS edited_on
  114. FROM content_blocks AS i
  115. WHERE i.id = ? AND i.revision_id = ? AND i.language = ?
  116. LIMIT 1',
  117. array((int) $id, (int) $revisionId, BL::getWorkingLanguage())
  118. );
  119. }
  120. /**
  121. * Get templates.
  122. *
  123. * @return array
  124. */
  125. public static function getTemplates()
  126. {
  127. // fetch templates available in core
  128. $templates = SpoonFile::getList(FRONTEND_MODULES_PATH . '/content_blocks/layout/widgets', '/.*?\.tpl/');
  129. // fetch current active theme
  130. $theme = BackendModel::getModuleSetting('core', 'theme', 'core');
  131. // fetch theme templates if a theme is selected
  132. if($theme != 'core') $templates = array_merge($templates, SpoonFile::getList(FRONTEND_PATH . '/themes/' . $theme . '/modules/content_blocks/layout/widgets', '/.*?\.tpl/'));
  133. // no duplicates (core templates will be overridden by theme templates) and sort alphabetically
  134. $templates = array_unique($templates);
  135. sort($templates);
  136. return $templates;
  137. }
  138. /**
  139. * Add a new item.
  140. *
  141. * @param array $item The data to insert.
  142. * @return int
  143. */
  144. public static function insert(array $item)
  145. {
  146. $db = BackendModel::getDB(true);
  147. // build extra
  148. $extra = array(
  149. 'module' => 'content_blocks',
  150. 'type' => 'widget',
  151. 'label' => 'ContentBlocks',
  152. 'action' => 'detail',
  153. 'data' => null,
  154. 'hidden' => 'N',
  155. 'sequence' => $db->getVar(
  156. 'SELECT MAX(i.sequence) + 1
  157. FROM modules_extras AS i
  158. WHERE i.module = ?',
  159. array('content_blocks')
  160. )
  161. );
  162. if(is_null($extra['sequence'])) $extra['sequence'] = $db->getVar(
  163. 'SELECT CEILING(MAX(i.sequence) / 1000) * 1000
  164. FROM modules_extras AS i'
  165. );
  166. // insert extra
  167. $item['extra_id'] = $db->insert('modules_extras', $extra);
  168. $extra['id'] = $item['extra_id'];
  169. // insert and return the new revision id
  170. $item['revision_id'] = $db->insert('content_blocks', $item);
  171. // update extra (item id is now known)
  172. $extra['data'] = serialize(array(
  173. 'id' => $item['id'],
  174. 'extra_label' => $item['title'],
  175. 'language' => $item['language'],
  176. 'edit_url' => BackendModel::createURLForAction('edit') . '&id=' . $item['id'])
  177. );
  178. $db->update(
  179. 'modules_extras',
  180. $extra,
  181. 'id = ? AND module = ? AND type = ? AND action = ?',
  182. array($extra['id'], $extra['module'], $extra['type'], $extra['action'])
  183. );
  184. return $item['revision_id'];
  185. }
  186. /**
  187. * Update an existing item.
  188. *
  189. * @param array $item The new data.
  190. * @return int
  191. */
  192. public static function update(array $item)
  193. {
  194. $db = BackendModel::getDB(true);
  195. // build extra
  196. $extra = array(
  197. 'id' => $item['extra_id'],
  198. 'module' => 'content_blocks',
  199. 'type' => 'widget',
  200. 'label' => 'ContentBlocks',
  201. 'action' => 'detail',
  202. 'data' => serialize(array(
  203. 'id' => $item['id'],
  204. 'extra_label' => $item['title'],
  205. 'language' => $item['language'],
  206. 'edit_url' => BackendModel::createURLForAction('edit') . '&id=' . $item['id'])
  207. ),
  208. 'hidden' => 'N');
  209. // update extra
  210. $db->update('modules_extras', $extra, 'id = ? AND module = ? AND type = ? AND action = ?', array($extra['id'], $extra['module'], $extra['type'], $extra['action']));
  211. // archive all older versions
  212. $db->update('content_blocks', array('status' => 'archived'), 'id = ? AND language = ?', array($item['id'], BL::getWorkingLanguage()));
  213. // insert new version
  214. $item['revision_id'] = $db->insert('content_blocks', $item);
  215. // how many revisions should we keep
  216. $rowsToKeep = (int) BackendModel::getModuleSetting('content_blocks', 'max_num_revisions', 20);
  217. // get revision-ids for items to keep
  218. $revisionIdsToKeep = (array) $db->getColumn(
  219. 'SELECT i.revision_id
  220. FROM content_blocks AS i
  221. WHERE i.id = ? AND i.language = ? AND i.status = ?
  222. ORDER BY i.edited_on DESC
  223. LIMIT ?',
  224. array($item['id'], BL::getWorkingLanguage(), 'archived', $rowsToKeep)
  225. );
  226. // delete other revisions
  227. if(!empty($revisionIdsToKeep)) $db->delete('content_blocks', 'id = ? AND language = ? AND status = ? AND revision_id NOT IN (' . implode(', ', $revisionIdsToKeep) . ')', array($item['id'], BL::getWorkingLanguage(), 'archived'));
  228. // return the new revision_id
  229. return $item['revision_id'];
  230. }
  231. }