Subs-Categories.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * This file contains the functions to add, modify, remove, collapse and expand categories.
  4. *
  5. * Simple Machines Forum (SMF)
  6. *
  7. * @package SMF
  8. * @author Simple Machines http://www.simplemachines.org
  9. * @copyright 2013 Simple Machines and individual contributors
  10. * @license http://www.simplemachines.org/about/smf/license.php BSD
  11. *
  12. * @version 2.1 Alpha 1
  13. */
  14. if (!defined('SMF'))
  15. die('No direct access...');
  16. /**
  17. * Edit the position and properties of a category.
  18. * general function to modify the settings and position of a category.
  19. * used by ManageBoards.php to change the settings of a category.
  20. *
  21. * @param int $category_id
  22. * @param array $catOptions
  23. */
  24. function modifyCategory($category_id, $catOptions)
  25. {
  26. global $sourcedir, $smcFunc;
  27. $catUpdates = array();
  28. $catParameters = array();
  29. $cat_id = $category_id;
  30. call_integration_hook('integrate_pre_modify_category', array($cat_id, &$catOptions));
  31. // Wanna change the categories position?
  32. if (isset($catOptions['move_after']))
  33. {
  34. // Store all categories in the proper order.
  35. $cats = array();
  36. $cat_order = array();
  37. // Setting 'move_after' to '0' moves the category to the top.
  38. if ($catOptions['move_after'] == 0)
  39. $cats[] = $category_id;
  40. // Grab the categories sorted by cat_order.
  41. $request = $smcFunc['db_query']('', '
  42. SELECT id_cat, cat_order
  43. FROM {db_prefix}categories
  44. ORDER BY cat_order',
  45. array(
  46. )
  47. );
  48. while ($row = $smcFunc['db_fetch_assoc']($request))
  49. {
  50. if ($row['id_cat'] != $category_id)
  51. $cats[] = $row['id_cat'];
  52. if ($row['id_cat'] == $catOptions['move_after'])
  53. $cats[] = $category_id;
  54. $cat_order[$row['id_cat']] = $row['cat_order'];
  55. }
  56. $smcFunc['db_free_result']($request);
  57. // Set the new order for the categories.
  58. foreach ($cats as $index => $cat)
  59. if ($index != $cat_order[$cat])
  60. $smcFunc['db_query']('', '
  61. UPDATE {db_prefix}categories
  62. SET cat_order = {int:new_order}
  63. WHERE id_cat = {int:current_category}',
  64. array(
  65. 'new_order' => $index,
  66. 'current_category' => $cat,
  67. )
  68. );
  69. // If the category order changed, so did the board order.
  70. require_once($sourcedir . '/Subs-Boards.php');
  71. reorderBoards();
  72. }
  73. if (isset($catOptions['cat_name']))
  74. {
  75. $catUpdates[] = 'name = {string:cat_name}';
  76. $catParameters['cat_name'] = $catOptions['cat_name'];
  77. }
  78. // Can a user collapse this category or is it too important?
  79. if (isset($catOptions['is_collapsible']))
  80. {
  81. $catUpdates[] = 'can_collapse = {int:is_collapsible}';
  82. $catParameters['is_collapsible'] = $catOptions['is_collapsible'] ? 1 : 0;
  83. }
  84. $cat_id = $category_id;
  85. call_integration_hook('integrate_modify_category', array($cat_id, &$catUpdates, &$catParameters));
  86. // Do the updates (if any).
  87. if (!empty($catUpdates))
  88. {
  89. $smcFunc['db_query']('', '
  90. UPDATE {db_prefix}categories
  91. SET
  92. ' . implode(',
  93. ', $catUpdates) . '
  94. WHERE id_cat = {int:current_category}',
  95. array_merge($catParameters, array(
  96. 'current_category' => $category_id,
  97. ))
  98. );
  99. if (empty($catOptions['dont_log']))
  100. logAction('edit_cat', array('catname' => isset($catOptions['cat_name']) ? $catOptions['cat_name'] : $category_id), 'admin');
  101. }
  102. }
  103. /**
  104. * Create a new category.
  105. * general function to create a new category and set its position.
  106. * allows (almost) the same options as the modifyCat() function.
  107. * returns the ID of the newly created category.
  108. *
  109. * @param array $catOptions
  110. */
  111. function createCategory($catOptions)
  112. {
  113. global $smcFunc;
  114. // Check required values.
  115. if (!isset($catOptions['cat_name']) || trim($catOptions['cat_name']) == '')
  116. trigger_error('createCategory(): A category name is required', E_USER_ERROR);
  117. // Set default values.
  118. if (!isset($catOptions['move_after']))
  119. $catOptions['move_after'] = 0;
  120. if (!isset($catOptions['is_collapsible']))
  121. $catOptions['is_collapsible'] = true;
  122. // Don't log an edit right after.
  123. $catOptions['dont_log'] = true;
  124. $cat_columns = array(
  125. 'name' => 'string-48',
  126. );
  127. $cat_parameters = array(
  128. $catOptions['cat_name'],
  129. );
  130. call_integration_hook('integrate_create_category', array(&$catOptions, &$cat_columns, &$cat_parameters));
  131. // Add the category to the database.
  132. $smcFunc['db_insert']('',
  133. '{db_prefix}categories',
  134. $cat_columns,
  135. $cat_parameters,
  136. array('id_cat')
  137. );
  138. // Grab the new category ID.
  139. $category_id = $smcFunc['db_insert_id']('{db_prefix}categories', 'id_cat');
  140. // Set the given properties to the newly created category.
  141. modifyCategory($category_id, $catOptions);
  142. logAction('add_cat', array('catname' => $catOptions['cat_name']), 'admin');
  143. // Return the database ID of the category.
  144. return $category_id;
  145. }
  146. /**
  147. * Remove one or more categories.
  148. * general function to delete one or more categories.
  149. * allows to move all boards in the categories to a different category before deleting them.
  150. * if moveChildrenTo is set to null, all boards inside the given categorieswill be deleted.
  151. * deletes all information that's associated with the given categories.
  152. * updates the statistics to reflect the new situation.
  153. *
  154. * @param string $categories
  155. * @param int $moveBoardsTo = null
  156. */
  157. function deleteCategories($categories, $moveBoardsTo = null)
  158. {
  159. global $sourcedir, $smcFunc, $cat_tree;
  160. require_once($sourcedir . '/Subs-Boards.php');
  161. getBoardTree();
  162. call_integration_hook('integrate_delete_category', array($categories, &$moveBoardsTo));
  163. // With no category set to move the boards to, delete them all.
  164. if ($moveBoardsTo === null)
  165. {
  166. $request = $smcFunc['db_query']('', '
  167. SELECT id_board
  168. FROM {db_prefix}boards
  169. WHERE id_cat IN ({array_int:category_list})',
  170. array(
  171. 'category_list' => $categories,
  172. )
  173. );
  174. $boards_inside = array();
  175. while ($row = $smcFunc['db_fetch_assoc']($request))
  176. $boards_inside[] = $row['id_board'];
  177. $smcFunc['db_free_result']($request);
  178. if (!empty($boards_inside))
  179. deleteBoards($boards_inside, null);
  180. }
  181. // Make sure the safe category is really safe.
  182. elseif (in_array($moveBoardsTo, $categories))
  183. trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR);
  184. // Move the boards inside the categories to a safe category.
  185. else
  186. $smcFunc['db_query']('', '
  187. UPDATE {db_prefix}boards
  188. SET id_cat = {int:new_parent_cat}
  189. WHERE id_cat IN ({array_int:category_list})',
  190. array(
  191. 'category_list' => $categories,
  192. 'new_parent_cat' => $moveBoardsTo,
  193. )
  194. );
  195. // Noone will ever be able to collapse these categories anymore.
  196. $smcFunc['db_query']('', '
  197. DELETE FROM {db_prefix}collapsed_categories
  198. WHERE id_cat IN ({array_int:category_list})',
  199. array(
  200. 'category_list' => $categories,
  201. )
  202. );
  203. // Do the deletion of the category itself
  204. $smcFunc['db_query']('', '
  205. DELETE FROM {db_prefix}categories
  206. WHERE id_cat IN ({array_int:category_list})',
  207. array(
  208. 'category_list' => $categories,
  209. )
  210. );
  211. // Log what we've done.
  212. foreach ($categories as $category)
  213. logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin');
  214. // Get all boards back into the right order.
  215. reorderBoards();
  216. }
  217. /**
  218. * Collapse, expand or toggle one or more categories for one or more members.
  219. * if members is null, the category is collapsed/expanded for all members.
  220. * allows three changes to the status: 'expand', 'collapse' and 'toggle'.
  221. * if check_collapsable is set, only category allowed to be collapsed, will be collapsed.
  222. *
  223. * @param array $categories
  224. * @param string $new_status
  225. * @param array $members = null
  226. * @param bool $check_collapsable = true
  227. */
  228. function collapseCategories($categories, $new_status, $members = null, $check_collapsable = true)
  229. {
  230. global $smcFunc;
  231. // Collapse or expand the categories.
  232. if ($new_status === 'collapse' || $new_status === 'expand')
  233. {
  234. $smcFunc['db_query']('', '
  235. DELETE FROM {db_prefix}collapsed_categories
  236. WHERE id_cat IN ({array_int:category_list})' . ($members === null ? '' : '
  237. AND id_member IN ({array_int:member_list})'),
  238. array(
  239. 'category_list' => $categories,
  240. 'member_list' => $members,
  241. )
  242. );
  243. if ($new_status === 'collapse')
  244. $smcFunc['db_query']('', '
  245. INSERT INTO {db_prefix}collapsed_categories
  246. (id_cat, id_member)
  247. SELECT c.id_cat, mem.id_member
  248. FROM {db_prefix}categories AS c
  249. INNER JOIN {db_prefix}members AS mem ON (' . ($members === null ? '1=1' : '
  250. mem.id_member IN ({array_int:member_list})') . ')
  251. WHERE c.id_cat IN ({array_int:category_list})' . ($check_collapsable ? '
  252. AND c.can_collapse = {int:is_collapsible}' : ''),
  253. array(
  254. 'member_list' => $members,
  255. 'category_list' => $categories,
  256. 'is_collapsible' => 1,
  257. )
  258. );
  259. }
  260. // Toggle the categories: collapsed get expanded and expanded get collapsed.
  261. elseif ($new_status === 'toggle')
  262. {
  263. // Get the current state of the categories.
  264. $updates = array(
  265. 'insert' => array(),
  266. 'remove' => array(),
  267. );
  268. $request = $smcFunc['db_query']('', '
  269. SELECT mem.id_member, c.id_cat, IFNULL(cc.id_cat, 0) AS is_collapsed, c.can_collapse
  270. FROM {db_prefix}members AS mem
  271. INNER JOIN {db_prefix}categories AS c ON (c.id_cat IN ({array_int:category_list}))
  272. LEFT JOIN {db_prefix}collapsed_categories AS cc ON (cc.id_cat = c.id_cat AND cc.id_member = mem.id_member)
  273. ' . ($members === null ? '' : '
  274. WHERE mem.id_member IN ({array_int:member_list})'),
  275. array(
  276. 'category_list' => $categories,
  277. 'member_list' => $members,
  278. )
  279. );
  280. while ($row = $smcFunc['db_fetch_assoc']($request))
  281. {
  282. if (empty($row['is_collapsed']) && (!empty($row['can_collapse']) || !$check_collapsable))
  283. $updates['insert'][] = array($row['id_member'], $row['id_cat']);
  284. elseif (!empty($row['is_collapsed']))
  285. $updates['remove'][] = '(id_member = ' . $row['id_member'] . ' AND id_cat = ' . $row['id_cat'] . ')';
  286. }
  287. $smcFunc['db_free_result']($request);
  288. // Collapse the ones that were originally expanded...
  289. if (!empty($updates['insert']))
  290. $smcFunc['db_insert']('replace',
  291. '{db_prefix}collapsed_categories',
  292. array(
  293. 'id_cat' => 'int', 'id_member' => 'int',
  294. ),
  295. $updates['insert'],
  296. array('id_cat', 'id_member')
  297. );
  298. // And expand the ones that were originally collapsed.
  299. if (!empty($updates['remove']))
  300. $smcFunc['db_query']('', '
  301. DELETE FROM {db_prefix}collapsed_categories
  302. WHERE ' . implode(' OR ', $updates['remove']),
  303. array(
  304. )
  305. );
  306. }
  307. }
  308. ?>