Subs-Categories.php 10.0 KB

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