PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/admin_categories.php

https://github.com/Dratone/EveBB
PHP | 266 lines | 199 code | 51 blank | 16 comment | 27 complexity | 04a673198ee4d88c1ab345abb0428e6b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Copyright (C) 2008-2010 FluxBB
  4. * based on code by Rickard Andersson copyright (C) 2002-2008 PunBB
  5. * License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  6. */
  7. // Tell header.php to use the admin template
  8. define('PUN_ADMIN_CONSOLE', 1);
  9. define('PUN_ROOT', dirname(__FILE__).'/');
  10. require PUN_ROOT.'include/common.php';
  11. require PUN_ROOT.'include/common_admin.php';
  12. if ($pun_user['g_id'] != PUN_ADMIN)
  13. message($lang_common['No permission']);
  14. // Load the admin_categories.php language file
  15. require PUN_ROOT.'lang/'.$admin_language.'/admin_categories.php';
  16. // Add a new category
  17. if (isset($_POST['add_cat']))
  18. {
  19. confirm_referrer('admin_categories.php');
  20. $new_cat_name = pun_trim($_POST['new_cat_name']);
  21. if ($new_cat_name == '')
  22. message($lang_admin_categories['Must enter name message']);
  23. $db->query('INSERT INTO '.$db->prefix.'categories (cat_name) VALUES(\''.$db->escape($new_cat_name).'\')') or error('Unable to create category', __FILE__, __LINE__, $db->error());
  24. redirect('admin_categories.php', $lang_admin_categories['Category added redirect']);
  25. }
  26. // Delete a category
  27. else if (isset($_POST['del_cat']) || isset($_POST['del_cat_comply']))
  28. {
  29. confirm_referrer('admin_categories.php');
  30. $cat_to_delete = intval($_POST['cat_to_delete']);
  31. if ($cat_to_delete < 1)
  32. message($lang_common['Bad request']);
  33. if (isset($_POST['del_cat_comply'])) // Delete a category with all forums and posts
  34. {
  35. @set_time_limit(0);
  36. $result = $db->query('SELECT id FROM '.$db->prefix.'forums WHERE cat_id='.$cat_to_delete) or error('Unable to fetch forum list', __FILE__, __LINE__, $db->error());
  37. $num_forums = $db->num_rows($result);
  38. for ($i = 0; $i < $num_forums; ++$i)
  39. {
  40. $cur_forum = $db->result($result, $i);
  41. // Prune all posts and topics
  42. prune($cur_forum, 1, -1);
  43. // Delete the forum
  44. $db->query('DELETE FROM '.$db->prefix.'forums WHERE id='.$cur_forum) or error('Unable to delete forum', __FILE__, __LINE__, $db->error());
  45. }
  46. // Locate any "orphaned redirect topics" and delete them
  47. $result = $db->query('SELECT t1.id FROM '.$db->prefix.'topics AS t1 LEFT JOIN '.$db->prefix.'topics AS t2 ON t1.moved_to=t2.id WHERE t2.id IS NULL AND t1.moved_to IS NOT NULL') or error('Unable to fetch redirect topics', __FILE__, __LINE__, $db->error());
  48. $num_orphans = $db->num_rows($result);
  49. if ($num_orphans)
  50. {
  51. for ($i = 0; $i < $num_orphans; ++$i)
  52. $orphans[] = $db->result($result, $i);
  53. $db->query('DELETE FROM '.$db->prefix.'topics WHERE id IN('.implode(',', $orphans).')') or error('Unable to delete redirect topics', __FILE__, __LINE__, $db->error());
  54. }
  55. // Delete the category
  56. $db->query('DELETE FROM '.$db->prefix.'categories WHERE id='.$cat_to_delete) or error('Unable to delete category', __FILE__, __LINE__, $db->error());
  57. // Regenerate the quick jump cache
  58. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  59. require PUN_ROOT.'include/cache.php';
  60. generate_quickjump_cache();
  61. redirect('admin_categories.php', $lang_admin_categories['Category deleted redirect']);
  62. }
  63. else // If the user hasn't comfirmed the delete
  64. {
  65. $result = $db->query('SELECT cat_name FROM '.$db->prefix.'categories WHERE id='.$cat_to_delete) or error('Unable to fetch category info', __FILE__, __LINE__, $db->error());
  66. $cat_name = $db->result($result);
  67. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_admin_common['Admin'], $lang_admin_common['Categories']);
  68. define('PUN_ACTIVE_PAGE', 'admin');
  69. require PUN_ROOT.'header.php';
  70. generate_admin_menu('categories');
  71. ?>
  72. <div class="blockform">
  73. <h2><span><?php echo $lang_admin_categories['Delete category head'] ?></span></h2>
  74. <div class="box">
  75. <form method="post" action="admin_categories.php">
  76. <div class="inform">
  77. <input type="hidden" name="cat_to_delete" value="<?php echo $cat_to_delete ?>" />
  78. <fieldset>
  79. <legend><?php echo $lang_admin_categories['Confirm delete subhead'] ?></legend>
  80. <div class="infldset">
  81. <p><?php printf($lang_admin_categories['Confirm delete info'], pun_htmlspecialchars($cat_name)) ?></p>
  82. <p class="warntext"><?php echo $lang_admin_categories['Delete category warn'] ?></p>
  83. </div>
  84. </fieldset>
  85. </div>
  86. <p class="buttons"><input type="submit" name="del_cat_comply" value="<?php echo $lang_admin_common['Delete'] ?>" /><a href="javascript:history.go(-1)"><?php echo $lang_admin_common['Go back'] ?></a></p>
  87. </form>
  88. </div>
  89. </div>
  90. <div class="clearer"></div>
  91. </div>
  92. <?php
  93. require PUN_ROOT.'footer.php';
  94. }
  95. }
  96. else if (isset($_POST['update'])) // Change position and name of the categories
  97. {
  98. confirm_referrer('admin_categories.php');
  99. $categories = $_POST['cat'];
  100. if (empty($categories))
  101. message($lang_common['Bad request']);
  102. foreach ($categories as $cat_id => $cur_cat)
  103. {
  104. $cur_cat['name'] = pun_trim($cur_cat['name']);
  105. $cur_cat['order'] = trim($cur_cat['order']);
  106. if ($cur_cat['name'] == '')
  107. message($lang_admin_categories['Must enter name message']);
  108. if ($cur_cat['order'] == '' || preg_match('/[^0-9]/', $cur_cat['order']))
  109. message($lang_admin_categories['Must enter integer message']);
  110. $db->query('UPDATE '.$db->prefix.'categories SET cat_name=\''.$db->escape($cur_cat['name']).'\', disp_position='.$cur_cat['order'].' WHERE id='.intval($cat_id)) or error('Unable to update category', __FILE__, __LINE__, $db->error());
  111. }
  112. // Regenerate the quick jump cache
  113. if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  114. require PUN_ROOT.'include/cache.php';
  115. generate_quickjump_cache();
  116. redirect('admin_categories.php', $lang_admin_categories['Categories updated redirect']);
  117. }
  118. // Generate an array with all categories
  119. $result = $db->query('SELECT id, cat_name, disp_position FROM '.$db->prefix.'categories ORDER BY disp_position') or error('Unable to fetch category list', __FILE__, __LINE__, $db->error());
  120. $num_cats = $db->num_rows($result);
  121. for ($i = 0; $i < $num_cats; ++$i)
  122. $cat_list[] = $db->fetch_assoc($result);
  123. $page_title = array(pun_htmlspecialchars($pun_config['o_board_title']), $lang_admin_common['Admin'], $lang_admin_common['Categories']);
  124. define('PUN_ACTIVE_PAGE', 'admin');
  125. require PUN_ROOT.'header.php';
  126. generate_admin_menu('categories');
  127. ?>
  128. <div class="blockform">
  129. <h2><span><?php echo $lang_admin_categories['Add categories head'] ?></span></h2>
  130. <div class="box">
  131. <form method="post" action="admin_categories.php">
  132. <div class="inform">
  133. <fieldset>
  134. <legend><?php echo $lang_admin_categories['Add categories subhead'] ?></legend>
  135. <div class="infldset">
  136. <table class="aligntop" cellspacing="0">
  137. <tr>
  138. <th scope="row"><?php echo $lang_admin_categories['Add category label'] ?><div><input type="submit" name="add_cat" value="<?php echo $lang_admin_categories['Add new submit'] ?>" tabindex="2" /></div></th>
  139. <td>
  140. <input type="text" name="new_cat_name" size="35" maxlength="80" tabindex="1" />
  141. <span><?php printf($lang_admin_categories['Add category help'], '<a href="admin_forums.php">'.$lang_admin_common['Forums'].'</a>') ?></span>
  142. </td>
  143. </tr>
  144. </table>
  145. </div>
  146. </fieldset>
  147. </div>
  148. </form>
  149. </div>
  150. <?php if ($num_cats): ?> <h2 class="block2"><span><?php echo $lang_admin_categories['Delete categories head'] ?></span></h2>
  151. <div class="box">
  152. <form method="post" action="admin_categories.php">
  153. <div class="inform">
  154. <fieldset>
  155. <legend><?php echo $lang_admin_categories['Delete categories subhead'] ?></legend>
  156. <div class="infldset">
  157. <table class="aligntop" cellspacing="0">
  158. <tr>
  159. <th scope="row"><?php echo $lang_admin_categories['Delete category label'] ?><div><input type="submit" name="del_cat" value="<?php echo $lang_admin_common['Delete'] ?>" tabindex="4" /></div></th>
  160. <td>
  161. <select name="cat_to_delete" tabindex="3">
  162. <?php
  163. foreach ($cat_list as $cur_cat)
  164. echo "\t\t\t\t\t\t\t\t\t\t\t".'<option value="'.$cur_cat['id'].'">'.pun_htmlspecialchars($cur_cat['cat_name']).'</option>'."\n";
  165. ?>
  166. </select>
  167. <span><?php echo $lang_admin_categories['Delete category help'] ?></span>
  168. </td>
  169. </tr>
  170. </table>
  171. </div>
  172. </fieldset>
  173. </div>
  174. </form>
  175. </div>
  176. <?php endif; ?>
  177. <?php if ($num_cats): ?> <h2 class="block2"><span><?php echo $lang_admin_categories['Edit categories head'] ?></span></h2>
  178. <div class="box">
  179. <form method="post" action="admin_categories.php">
  180. <div class="inform">
  181. <fieldset>
  182. <legend><?php echo $lang_admin_categories['Edit categories subhead'] ?></legend>
  183. <div class="infldset">
  184. <table id="categoryedit" cellspacing="0" >
  185. <thead>
  186. <tr>
  187. <th class="tcl" scope="col"><?php echo $lang_admin_categories['Category name label'] ?></th>
  188. <th scope="col"><?php echo $lang_admin_categories['Category position label'] ?></th>
  189. </tr>
  190. </thead>
  191. <tbody>
  192. <?php
  193. foreach ($cat_list as $cur_cat)
  194. {
  195. ?>
  196. <tr>
  197. <td class="tcl"><input type="text" name="cat[<?php echo $cur_cat['id'] ?>][name]" value="<?php echo pun_htmlspecialchars($cur_cat['cat_name']) ?>" size="35" maxlength="80" /></td>
  198. <td><input type="text" name="cat[<?php echo $cur_cat['id'] ?>][order]" value="<?php echo $cur_cat['disp_position'] ?>" size="3" maxlength="3" /></td>
  199. </tr>
  200. <?php
  201. }
  202. ?>
  203. </tbody>
  204. </table>
  205. <div class="fsetsubmit"><input type="submit" name="update" value="<?php echo $lang_admin_common['Update'] ?>" /></div>
  206. </div>
  207. </fieldset>
  208. </div>
  209. </form>
  210. </div>
  211. <?php endif; ?> </div>
  212. <div class="clearer"></div>
  213. </div>
  214. <?php
  215. require PUN_ROOT.'footer.php';