| 1 | <?php | 
|---|
| 2 | /** | 
|---|
| 3 | * @package Dotclear | 
|---|
| 4 | * @subpackage Backend | 
|---|
| 5 | * | 
|---|
| 6 | * @copyright Olivier Meunier & Association Dotclear | 
|---|
| 7 | * @copyright GPL-2.0-only | 
|---|
| 8 | */ | 
|---|
| 9 |  | 
|---|
| 10 | require dirname(__FILE__) . '/../inc/admin/prepend.php'; | 
|---|
| 11 |  | 
|---|
| 12 | dcPage::check('categories'); | 
|---|
| 13 |  | 
|---|
| 14 | # Remove a categories | 
|---|
| 15 | if (!empty($_POST['delete'])) { | 
|---|
| 16 | $keys   = array_keys($_POST['delete']); | 
|---|
| 17 | $cat_id = (int) $keys[0]; | 
|---|
| 18 |  | 
|---|
| 19 | # Check if category to delete exists | 
|---|
| 20 | $c = $core->blog->getCategory((integer) $cat_id); | 
|---|
| 21 | if ($c->isEmpty()) { | 
|---|
| 22 | dcPage::addErrorNotice(__('This category does not exist.')); | 
|---|
| 23 | $core->adminurl->redirect("admin.categories"); | 
|---|
| 24 | } | 
|---|
| 25 | $name = $c->cat_title; | 
|---|
| 26 | unset($c); | 
|---|
| 27 |  | 
|---|
| 28 | try { | 
|---|
| 29 | # Delete category | 
|---|
| 30 | $core->blog->delCategory($cat_id); | 
|---|
| 31 | dcPage::addSuccessNotice(sprintf(__('The category "%s" has been successfully deleted.'), html::escapeHTML($name))); | 
|---|
| 32 | $core->adminurl->redirect("admin.categories"); | 
|---|
| 33 | } catch (Exception $e) { | 
|---|
| 34 | $core->error->add($e->getMessage()); | 
|---|
| 35 | } | 
|---|
| 36 | } | 
|---|
| 37 |  | 
|---|
| 38 | # move post into a category | 
|---|
| 39 | if (!empty($_POST['mov']) && !empty($_POST['mov_cat'])) { | 
|---|
| 40 | try { | 
|---|
| 41 | # Check if category where to move posts exists | 
|---|
| 42 | $keys    = array_keys($_POST['mov']); | 
|---|
| 43 | $cat_id  = (int) $keys[0]; | 
|---|
| 44 | $mov_cat = (int) $_POST['mov_cat'][$cat_id]; | 
|---|
| 45 |  | 
|---|
| 46 | $mov_cat = $mov_cat ?: null; | 
|---|
| 47 | if ($mov_cat !== null) { | 
|---|
| 48 | $c = $core->blog->getCategory($mov_cat); | 
|---|
| 49 | if ($c->isEmpty()) { | 
|---|
| 50 | throw new Exception(__('Category where to move entries does not exist')); | 
|---|
| 51 | } | 
|---|
| 52 | $name = $c->cat_title; | 
|---|
| 53 | unset($c); | 
|---|
| 54 | } | 
|---|
| 55 | # Move posts | 
|---|
| 56 | if ($mov_cat != $cat_id) { | 
|---|
| 57 | $core->blog->changePostsCategory($cat_id, $mov_cat); | 
|---|
| 58 | } | 
|---|
| 59 | dcPage::addSuccessNotice(sprintf(__('The entries have been successfully moved to category "%s"'), | 
|---|
| 60 | html::escapeHTML($name))); | 
|---|
| 61 | $core->adminurl->redirect("admin.categories"); | 
|---|
| 62 | } catch (Exception $e) { | 
|---|
| 63 | $core->error->add($e->getMessage()); | 
|---|
| 64 | } | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | # Update order | 
|---|
| 68 | if (!empty($_POST['save_order']) && !empty($_POST['categories_order'])) { | 
|---|
| 69 | $categories = json_decode($_POST['categories_order']); | 
|---|
| 70 |  | 
|---|
| 71 | foreach ($categories as $category) { | 
|---|
| 72 | if (!empty($category->item_id) && !empty($category->left) && !empty($category->right)) { | 
|---|
| 73 | $core->blog->updCategoryPosition($category->item_id, $category->left, $category->right); | 
|---|
| 74 | } | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | dcPage::addSuccessNotice(__('Categories have been successfully reordered.')); | 
|---|
| 78 | $core->adminurl->redirect("admin.categories"); | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | # Reset order | 
|---|
| 82 | if (!empty($_POST['reset'])) { | 
|---|
| 83 | try | 
|---|
| 84 | { | 
|---|
| 85 | $core->blog->resetCategoriesOrder(); | 
|---|
| 86 | dcPage::addSuccessNotice(__('Categories order has been successfully reset.')); | 
|---|
| 87 | $core->adminurl->redirect("admin.categories"); | 
|---|
| 88 | } catch (Exception $e) { | 
|---|
| 89 | $core->error->add($e->getMessage()); | 
|---|
| 90 | } | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | /* Display | 
|---|
| 94 | -------------------------------------------------------- */ | 
|---|
| 95 | $rs = $core->blog->getCategories(); | 
|---|
| 96 |  | 
|---|
| 97 | $starting_script = ""; | 
|---|
| 98 |  | 
|---|
| 99 | $core->auth->user_prefs->addWorkspace('accessibility'); | 
|---|
| 100 | if (!$core->auth->user_prefs->accessibility->nodragdrop | 
|---|
| 101 | && $core->auth->check('categories', $core->blog->id) | 
|---|
| 102 | && $rs->count() > 1) { | 
|---|
| 103 | $starting_script .= dcPage::jsLoad('js/jquery/jquery-ui.custom.js'); | 
|---|
| 104 | $starting_script .= dcPage::jsLoad('js/jquery/jquery.ui.touch-punch.js'); | 
|---|
| 105 | $starting_script .= dcPage::jsLoad('js/jquery/jquery.mjs.nestedSortable.js'); | 
|---|
| 106 | } | 
|---|
| 107 | $starting_script .= dcPage::jsConfirmClose('form-categories'); | 
|---|
| 108 | $starting_script .= dcPage::jsLoad('js/_categories.js'); | 
|---|
| 109 |  | 
|---|
| 110 | dcPage::open(__('Categories'), $starting_script, | 
|---|
| 111 | dcPage::breadcrumb( | 
|---|
| 112 | [ | 
|---|
| 113 | html::escapeHTML($core->blog->name) => '', | 
|---|
| 114 | __('Categories')                    => '' | 
|---|
| 115 | ]) | 
|---|
| 116 | ); | 
|---|
| 117 |  | 
|---|
| 118 | if (!empty($_GET['del'])) { | 
|---|
| 119 | dcPage::success(__('The category has been successfully removed.')); | 
|---|
| 120 | } | 
|---|
| 121 | if (!empty($_GET['reord'])) { | 
|---|
| 122 | dcPage::success(__('Categories have been successfully reordered.')); | 
|---|
| 123 | } | 
|---|
| 124 | if (!empty($_GET['move'])) { | 
|---|
| 125 | dcPage::success(__('Entries have been successfully moved to the category you choose.')); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | $categories_combo = dcAdminCombos::getCategoriesCombo($rs); | 
|---|
| 129 |  | 
|---|
| 130 | echo | 
|---|
| 131 | '<p class="top-add"><a class="button add" href="' . $core->adminurl->get("admin.category") . '">' . __('New category') . '</a></p>'; | 
|---|
| 132 |  | 
|---|
| 133 | echo | 
|---|
| 134 | '<div class="col">'; | 
|---|
| 135 | if ($rs->isEmpty()) { | 
|---|
| 136 | echo '<p>' . __('No category so far.') . '</p>'; | 
|---|
| 137 | } else { | 
|---|
| 138 | echo | 
|---|
| 139 | '<form action="' . $core->adminurl->get("admin.categories") . '" method="post" id="form-categories">' . | 
|---|
| 140 | '<div id="categories">'; | 
|---|
| 141 |  | 
|---|
| 142 | $ref_level = $level = $rs->level - 1; | 
|---|
| 143 | while ($rs->fetch()) { | 
|---|
| 144 | $attr = 'id="cat_' . $rs->cat_id . '" class="cat-line clearfix"'; | 
|---|
| 145 |  | 
|---|
| 146 | if ($rs->level > $level) { | 
|---|
| 147 | echo str_repeat('<ul><li ' . $attr . '>', $rs->level - $level); | 
|---|
| 148 | } elseif ($rs->level < $level) { | 
|---|
| 149 | echo str_repeat('</li></ul>', -($rs->level - $level)); | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | if ($rs->level <= $level) { | 
|---|
| 153 | echo '</li><li ' . $attr . '>'; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | echo | 
|---|
| 157 | '<p class="cat-title"><label class="classic" for="cat_' . $rs->cat_id . '"><a href="' . | 
|---|
| 158 | $core->adminurl->get("admin.category", ['id' => $rs->cat_id]) . '">' . html::escapeHTML($rs->cat_title) . | 
|---|
| 159 | '</a></label> </p>' . | 
|---|
| 160 | '<p class="cat-nb-posts">(<a href="' . $core->adminurl->get("admin.posts", ['cat_id' => $rs->cat_id]) . '">' . | 
|---|
| 161 | sprintf(($rs->nb_post > 1 ? __('%d entries') : __('%d entry')), $rs->nb_post) . '</a>' . | 
|---|
| 162 | ', ' . __('total:') . ' ' . $rs->nb_total . ')</p>' . | 
|---|
| 163 | '<p class="cat-url">' . __('URL:') . ' <code>' . html::escapeHTML($rs->cat_url) . '</code></p>'; | 
|---|
| 164 |  | 
|---|
| 165 | echo | 
|---|
| 166 | '<p class="cat-buttons">'; | 
|---|
| 167 | if ($rs->nb_total > 0) { | 
|---|
| 168 | // remove current category | 
|---|
| 169 | echo | 
|---|
| 170 | '<label for="mov_cat_' . $rs->cat_id . '">' . __('Move entries to') . '</label> ' . | 
|---|
| 171 | form::combo(['mov_cat[' . $rs->cat_id . ']', 'mov_cat_' . $rs->cat_id], array_filter($categories_combo, | 
|---|
| 172 | function ($cat) {return $cat->value != $GLOBALS['rs']->cat_id;} | 
|---|
| 173 | ), '', '') . | 
|---|
| 174 | ' <input type="submit" class="reset" name="mov[' . $rs->cat_id . ']" value="' . __('OK') . '"/>'; | 
|---|
| 175 |  | 
|---|
| 176 | $attr_disabled = ' disabled="disabled"'; | 
|---|
| 177 | $input_class   = 'disabled '; | 
|---|
| 178 | } else { | 
|---|
| 179 | $attr_disabled = ''; | 
|---|
| 180 | $input_class   = ''; | 
|---|
| 181 | } | 
|---|
| 182 | echo | 
|---|
| 183 | ' <input type="submit"' . $attr_disabled . ' class="' . $input_class . 'delete" name="delete[' . $rs->cat_id . ']" value="' . __('Delete category') . '"/>' . | 
|---|
| 184 | '</p>'; | 
|---|
| 185 |  | 
|---|
| 186 | $level = $rs->level; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | if ($ref_level - $level < 0) { | 
|---|
| 190 | echo str_repeat('</li></ul>', -($ref_level - $level)); | 
|---|
| 191 | } | 
|---|
| 192 | echo | 
|---|
| 193 | '</div>'; | 
|---|
| 194 |  | 
|---|
| 195 | echo '<div class="clear">'; | 
|---|
| 196 |  | 
|---|
| 197 | if ($core->auth->check('categories', $core->blog->id) && $rs->count() > 1) { | 
|---|
| 198 | if (!$core->auth->user_prefs->accessibility->nodragdrop) { | 
|---|
| 199 | echo '<p class="form-note hidden-if-no-js">' . __('To rearrange categories order, move items by drag and drop, then click on “Save categories order” button.') . '</p>'; | 
|---|
| 200 | } | 
|---|
| 201 | echo | 
|---|
| 202 | '<p><span class="hidden-if-no-js">' . | 
|---|
| 203 | '<input type="hidden" id="categories_order" name="categories_order" value=""/>' . | 
|---|
| 204 | '<input type="submit" name="save_order" id="save-set-order" value="' . __('Save categories order') . '" />' . | 
|---|
| 205 | '</span> '; | 
|---|
| 206 | } else { | 
|---|
| 207 | echo '<p>'; | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | echo | 
|---|
| 211 | '<input type="submit" class="reset" name="reset" value="' . __('Reorder all categories on the top level') . '" />' . | 
|---|
| 212 | $core->formNonce() . '</p>' . | 
|---|
| 213 | '</div></form>'; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 | echo '</div>'; | 
|---|
| 217 |  | 
|---|
| 218 | dcPage::helpBlock('core_categories'); | 
|---|
| 219 | dcPage::close(); | 
|---|