Subs-Categories.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 2012 Simple Machines
  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('Hacking attempt...');
  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 int $createCategory
  110. * @param array $catOptions
  111. */
  112. function createCategory($catOptions)
  113. {
  114. global $smcFunc;
  115. // Check required values.
  116. if (!isset($catOptions['cat_name']) || trim($catOptions['cat_name']) == '')
  117. trigger_error('createCategory(): A category name is required', E_USER_ERROR);
  118. // Set default values.
  119. if (!isset($catOptions['move_after']))
  120. $catOptions['move_after'] = 0;
  121. if (!isset($catOptions['is_collapsible']))
  122. $catOptions['is_collapsible'] = true;
  123. // Don't log an edit right after.
  124. $catOptions['dont_log'] = true;
  125. $cat_columns = array(
  126. 'name' => 'string-48',
  127. );
  128. $cat_parameters = array(
  129. $catOptions['cat_name'],
  130. );
  131. call_integration_hook('integrate_create_category', array(&$catOptions, &$cat_columns, &$cat_parameters));
  132. // Add the category to the database.
  133. $smcFunc['db_insert']('',
  134. '{db_prefix}categories',
  135. $cat_columns,
  136. $cat_parameters,
  137. array('id_cat')
  138. );
  139. // Grab the new category ID.
  140. $category_id = $smcFunc['db_insert_id']('{db_prefix}categories', 'id_cat');
  141. // Set the given properties to the newly created category.
  142. modifyCategory($category_id, $catOptions);
  143. logAction('add_cat', array('catname' => $catOptions['cat_name']), 'admin');
  144. // Return the database ID of the category.
  145. return $category_id;
  146. }
  147. /**
  148. * Remove one or more categories.
  149. * general function to delete one or more categories.
  150. * allows to move all boards in the categories to a different category before deleting them.
  151. * if moveChildrenTo is set to null, all boards inside the given categorieswill be deleted.
  152. * deletes all information that's associated with the given categories.
  153. * updates the statistics to reflect the new situation.
  154. *
  155. * @param array $categories_to_remove
  156. * @param int $moveChildrenTo = null
  157. */
  158. function deleteCategories($categories, $moveBoardsTo = null)
  159. {
  160. global $sourcedir, $smcFunc, $cat_tree;
  161. require_once($sourcedir . '/Subs-Boards.php');
  162. getBoardTree();
  163. call_integration_hook('integrate_delete_category', array($categories, &$moveBoardsTo));
  164. // With no category set to move the boards to, delete them all.
  165. if ($moveBoardsTo === null)
  166. {
  167. $request = $smcFunc['db_query']('', '
  168. SELECT id_board
  169. FROM {db_prefix}boards
  170. WHERE id_cat IN ({array_int:category_list})',
  171. array(
  172. 'category_list' => $categories,
  173. )
  174. );
  175. $boards_inside = array();
  176. while ($row = $smcFunc['db_fetch_assoc']($request))
  177. $boards_inside[] = $row['id_board'];
  178. $smcFunc['db_free_result']($request);
  179. if (!empty($boards_inside))
  180. deleteBoards($boards_inside, null);
  181. }
  182. // Make sure the safe category is really safe.
  183. elseif (in_array($moveBoardsTo, $categories))
  184. trigger_error('deleteCategories(): You cannot move the boards to a category that\'s being deleted', E_USER_ERROR);
  185. // Move the boards inside the categories to a safe category.
  186. else
  187. $smcFunc['db_query']('', '
  188. UPDATE {db_prefix}boards
  189. SET id_cat = {int:new_parent_cat}
  190. WHERE id_cat IN ({array_int:category_list})',
  191. array(
  192. 'category_list' => $categories,
  193. 'new_parent_cat' => $moveBoardsTo,
  194. )
  195. );
  196. // Noone will ever be able to collapse these categories anymore.
  197. $smcFunc['db_query']('', '
  198. DELETE FROM {db_prefix}collapsed_categories
  199. WHERE id_cat IN ({array_int:category_list})',
  200. array(
  201. 'category_list' => $categories,
  202. )
  203. );
  204. // Do the deletion of the category itself
  205. $smcFunc['db_query']('', '
  206. DELETE FROM {db_prefix}categories
  207. WHERE id_cat IN ({array_int:category_list})',
  208. array(
  209. 'category_list' => $categories,
  210. )
  211. );
  212. // Log what we've done.
  213. foreach ($categories as $category)
  214. logAction('delete_cat', array('catname' => $cat_tree[$category]['node']['name']), 'admin');
  215. // Get all boards back into the right order.
  216. reorderBoards();
  217. }
  218. /**
  219. * Collapse, expand or toggle one or more categories for one or more members.
  220. * if members is null, the category is collapsed/expanded for all members.
  221. * allows three changes to the status: 'expand', 'collapse' and 'toggle'.
  222. * if check_collapsable is set, only category allowed to be collapsed, will be collapsed.
  223. *
  224. * @param array $categories
  225. * @param string $new_status
  226. * @param array $members = null
  227. * @param bool $check_collapsable = true
  228. */
  229. function collapseCategories($categories, $new_status, $members = null, $check_collapsable = true)
  230. {
  231. global $smcFunc;
  232. // Collapse or expand the categories.
  233. if ($new_status === 'collapse' || $new_status === 'expand')
  234. {
  235. $smcFunc['db_query']('', '
  236. DELETE FROM {db_prefix}collapsed_categories
  237. WHERE id_cat IN ({array_int:category_list})' . ($members === null ? '' : '
  238. AND id_member IN ({array_int:member_list})'),
  239. array(
  240. 'category_list' => $categories,
  241. 'member_list' => $members,
  242. )
  243. );
  244. if ($new_status === 'collapse')
  245. $smcFunc['db_query']('', '
  246. INSERT INTO {db_prefix}collapsed_categories
  247. (id_cat, id_member)
  248. SELECT c.id_cat, mem.id_member
  249. FROM {db_prefix}categories AS c
  250. INNER JOIN {db_prefix}members AS mem ON (' . ($members === null ? '1=1' : '
  251. mem.id_member IN ({array_int:member_list})') . ')
  252. WHERE c.id_cat IN ({array_int:category_list})' . ($check_collapsable ? '
  253. AND c.can_collapse = {int:is_collapsible}' : ''),
  254. array(
  255. 'member_list' => $members,
  256. 'category_list' => $categories,
  257. 'is_collapsible' => 1,
  258. )
  259. );
  260. }
  261. // Toggle the categories: collapsed get expanded and expanded get collapsed.
  262. elseif ($new_status === 'toggle')
  263. {
  264. // Get the current state of the categories.
  265. $updates = array(
  266. 'insert' => array(),
  267. 'remove' => array(),
  268. );
  269. $request = $smcFunc['db_query']('', '
  270. SELECT mem.id_member, c.id_cat, IFNULL(cc.id_cat, 0) AS is_collapsed, c.can_collapse
  271. FROM {db_prefix}members AS mem
  272. INNER JOIN {db_prefix}categories AS c ON (c.id_cat IN ({array_int:category_list}))
  273. LEFT JOIN {db_prefix}collapsed_categories AS cc ON (cc.id_cat = c.id_cat AND cc.id_member = mem.id_member)
  274. ' . ($members === null ? '' : '
  275. WHERE mem.id_member IN ({array_int:member_list})'),
  276. array(
  277. 'category_list' => $categories,
  278. 'member_list' => $members,
  279. )
  280. );
  281. while ($row = $smcFunc['db_fetch_assoc']($request))
  282. {
  283. if (empty($row['is_collapsed']) && (!empty($row['can_collapse']) || !$check_collapsable))
  284. $updates['insert'][] = array($row['id_member'], $row['id_cat']);
  285. elseif (!empty($row['is_collapsed']))
  286. $updates['remove'][] = '(id_member = ' . $row['id_member'] . ' AND id_cat = ' . $row['id_cat'] . ')';
  287. }
  288. $smcFunc['db_free_result']($request);
  289. // Collapse the ones that were originally expanded...
  290. if (!empty($updates['insert']))
  291. $smcFunc['db_insert']('replace',
  292. '{db_prefix}collapsed_categories',
  293. array(
  294. 'id_cat' => 'int', 'id_member' => 'int',
  295. ),
  296. $updates['insert'],
  297. array('id_cat', 'id_member')
  298. );
  299. // And expand the ones that were originally collapsed.
  300. if (!empty($updates['remove']))
  301. $smcFunc['db_query']('', '
  302. DELETE FROM {db_prefix}collapsed_categories
  303. WHERE ' . implode(' OR ', $updates['remove']),
  304. array(
  305. )
  306. );
  307. }
  308. }
  309. ?>