| 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 | $cat_id = ''; |
|---|
| 15 | $cat_title = ''; |
|---|
| 16 | $cat_url = ''; |
|---|
| 17 | $cat_desc = ''; |
|---|
| 18 | $cat_position = ''; |
|---|
| 19 | |
|---|
| 20 | # Getting existing category |
|---|
| 21 | if (!empty($_REQUEST['id'])) { |
|---|
| 22 | try { |
|---|
| 23 | $rs = $core->blog->getCategory($_REQUEST['id']); |
|---|
| 24 | } catch (Exception $e) { |
|---|
| 25 | $core->error->add($e->getMessage()); |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | if (!$core->error->flag() && !$rs->isEmpty()) { |
|---|
| 29 | $cat_id = (integer) $rs->cat_id; |
|---|
| 30 | $cat_title = $rs->cat_title; |
|---|
| 31 | $cat_url = $rs->cat_url; |
|---|
| 32 | $cat_desc = $rs->cat_desc; |
|---|
| 33 | } |
|---|
| 34 | unset($rs); |
|---|
| 35 | |
|---|
| 36 | # Getting hierarchy information |
|---|
| 37 | $parents = $core->blog->getCategoryParents($cat_id); |
|---|
| 38 | $rs = $core->blog->getCategoryParent($cat_id); |
|---|
| 39 | $cat_parent = $rs->isEmpty() ? 0 : (integer) $rs->cat_id; |
|---|
| 40 | unset($rs); |
|---|
| 41 | |
|---|
| 42 | # Allowed parents list |
|---|
| 43 | $children = $core->blog->getCategories(array('start' => $cat_id)); |
|---|
| 44 | $allowed_parents = array(__('Top level') => 0); |
|---|
| 45 | |
|---|
| 46 | $p = array(); |
|---|
| 47 | while ($children->fetch()) { |
|---|
| 48 | $p[$children->cat_id] = 1; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | $rs = $core->blog->getCategories(); |
|---|
| 52 | while ($rs->fetch()) { |
|---|
| 53 | if (!isset($p[$rs->cat_id])) { |
|---|
| 54 | $allowed_parents[] = new formSelectOption( |
|---|
| 55 | str_repeat(' ', $rs->level - 1) . ($rs->level - 1 == 0 ? '' : '• ') . html::escapeHTML($rs->cat_title), |
|---|
| 56 | $rs->cat_id |
|---|
| 57 | ); |
|---|
| 58 | } |
|---|
| 59 | } |
|---|
| 60 | unset($rs); |
|---|
| 61 | |
|---|
| 62 | # Allowed siblings list |
|---|
| 63 | $siblings = array(); |
|---|
| 64 | $rs = $core->blog->getCategoryFirstChildren($cat_parent); |
|---|
| 65 | while ($rs->fetch()) { |
|---|
| 66 | if ($rs->cat_id != $cat_id) { |
|---|
| 67 | $siblings[html::escapeHTML($rs->cat_title)] = $rs->cat_id; |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | unset($rs); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | # Changing parent |
|---|
| 74 | if ($cat_id && isset($_POST['cat_parent'])) { |
|---|
| 75 | $new_parent = (integer) $_POST['cat_parent']; |
|---|
| 76 | if ($cat_parent != $new_parent) { |
|---|
| 77 | try { |
|---|
| 78 | $core->blog->setCategoryParent($cat_id, $new_parent); |
|---|
| 79 | dcPage::addSuccessNotice(__('The category has been successfully moved')); |
|---|
| 80 | $core->adminurl->redirect("admin.categories"); |
|---|
| 81 | } catch (Exception $e) { |
|---|
| 82 | $core->error->add($e->getMessage()); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | # Changing sibling |
|---|
| 88 | if ($cat_id && isset($_POST['cat_sibling'])) { |
|---|
| 89 | try { |
|---|
| 90 | $core->blog->setCategoryPosition($cat_id, (integer) $_POST['cat_sibling'], $_POST['cat_move']); |
|---|
| 91 | dcPage::addSuccessNotice(__('The category has been successfully moved')); |
|---|
| 92 | $core->adminurl->redirect("admin.categories"); |
|---|
| 93 | } catch (Exception $e) { |
|---|
| 94 | $core->error->add($e->getMessage()); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | # Create or update a category |
|---|
| 99 | if (isset($_POST['cat_title'])) { |
|---|
| 100 | $cur = $core->con->openCursor($core->prefix . 'category'); |
|---|
| 101 | |
|---|
| 102 | $cur->cat_title = $cat_title = $_POST['cat_title']; |
|---|
| 103 | |
|---|
| 104 | if (isset($_POST['cat_desc'])) { |
|---|
| 105 | $cur->cat_desc = $cat_desc = $_POST['cat_desc']; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | if (isset($_POST['cat_url'])) { |
|---|
| 109 | $cur->cat_url = $cat_url = $_POST['cat_url']; |
|---|
| 110 | } else { |
|---|
| 111 | $cur->cat_url = $cat_url; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | try |
|---|
| 115 | { |
|---|
| 116 | # Update category |
|---|
| 117 | if ($cat_id) { |
|---|
| 118 | # --BEHAVIOR-- adminBeforeCategoryUpdate |
|---|
| 119 | $core->callBehavior('adminBeforeCategoryUpdate', $cur, $cat_id); |
|---|
| 120 | |
|---|
| 121 | $core->blog->updCategory($_POST['id'], $cur); |
|---|
| 122 | |
|---|
| 123 | # --BEHAVIOR-- adminAfterCategoryUpdate |
|---|
| 124 | $core->callBehavior('adminAfterCategoryUpdate', $cur, $cat_id); |
|---|
| 125 | |
|---|
| 126 | dcPage::addSuccessNotice(__('The category has been successfully updated.')); |
|---|
| 127 | |
|---|
| 128 | $core->adminurl->redirect("admin.category", array('id' => $_POST['id'])); |
|---|
| 129 | } |
|---|
| 130 | # Create category |
|---|
| 131 | else { |
|---|
| 132 | # --BEHAVIOR-- adminBeforeCategoryCreate |
|---|
| 133 | $core->callBehavior('adminBeforeCategoryCreate', $cur); |
|---|
| 134 | |
|---|
| 135 | $id = $core->blog->addCategory($cur, (integer) $_POST['new_cat_parent']); |
|---|
| 136 | |
|---|
| 137 | # --BEHAVIOR-- adminAfterCategoryCreate |
|---|
| 138 | $core->callBehavior('adminAfterCategoryCreate', $cur, $id); |
|---|
| 139 | |
|---|
| 140 | dcPage::addSuccessNotice(sprintf(__('The category "%s" has been successfully created.'), |
|---|
| 141 | html::escapeHTML($cur->cat_title))); |
|---|
| 142 | $core->adminurl->redirect("admin.categories"); |
|---|
| 143 | } |
|---|
| 144 | } catch (Exception $e) { |
|---|
| 145 | $core->error->add($e->getMessage()); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | $title = $cat_id ? html::escapeHTML($cat_title) : __('New category'); |
|---|
| 150 | |
|---|
| 151 | $elements = array( |
|---|
| 152 | html::escapeHTML($core->blog->name) => '', |
|---|
| 153 | __('Categories') => $core->adminurl->get("admin.categories") |
|---|
| 154 | ); |
|---|
| 155 | if ($cat_id) { |
|---|
| 156 | while ($parents->fetch()) { |
|---|
| 157 | $elements[html::escapeHTML($parents->cat_title)] = $core->adminurl->get("admin.category", array('id' => $parents->cat_id)); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | $elements[$title] = ''; |
|---|
| 161 | |
|---|
| 162 | $category_editor = $core->auth->getOption('editor'); |
|---|
| 163 | $rte_flag = true; |
|---|
| 164 | $rte_flags = @$core->auth->user_prefs->interface->rte_flags; |
|---|
| 165 | if (is_array($rte_flags) && in_array('cat_descr', $rte_flags)) { |
|---|
| 166 | $rte_flag = $rte_flags['cat_descr']; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | dcPage::open($title, |
|---|
| 170 | dcPage::jsConfirmClose('category-form') . |
|---|
| 171 | dcPage::jsLoad('js/_category.js') . |
|---|
| 172 | ($rte_flag ? $core->callBehavior('adminPostEditor', $category_editor['xhtml'], 'category', array('#cat_desc'), 'xhtml') : ''), |
|---|
| 173 | dcPage::breadcrumb($elements) |
|---|
| 174 | ); |
|---|
| 175 | |
|---|
| 176 | if (!empty($_GET['upd'])) { |
|---|
| 177 | dcPage::success(__('Category has been successfully updated.')); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | echo |
|---|
| 181 | '<form action="' . $core->adminurl->get("admin.category") . '" method="post" id="category-form">' . |
|---|
| 182 | '<h3>' . __('Category information') . '</h3>' . |
|---|
| 183 | '<p><label class="required" for="cat_title"><abbr title="' . __('Required field') . '">*</abbr> ' . __('Name:') . '</label> ' . |
|---|
| 184 | form::field('cat_title', 40, 255, array( |
|---|
| 185 | 'default' => html::escapeHTML($cat_title), |
|---|
| 186 | 'extra_html' => 'required placeholder="' . __('Name') . '"' |
|---|
| 187 | )) . |
|---|
| 188 | '</p>'; |
|---|
| 189 | if (!$cat_id) { |
|---|
| 190 | $rs = $core->blog->getCategories(); |
|---|
| 191 | echo |
|---|
| 192 | '<p><label for="new_cat_parent">' . __('Parent:') . ' ' . |
|---|
| 193 | '<select id="new_cat_parent" name="new_cat_parent" >' . |
|---|
| 194 | '<option value="0">' . __('(none)') . '</option>'; |
|---|
| 195 | while ($rs->fetch()) { |
|---|
| 196 | echo '<option value="' . $rs->cat_id . '" ' . (!empty($_POST['new_cat_parent']) && $_POST['new_cat_parent'] == $rs->cat_id ? 'selected="selected"' : '') . '>' . |
|---|
| 197 | str_repeat(' ', $rs->level - 1) . ($rs->level - 1 == 0 ? '' : '• ') . html::escapeHTML($rs->cat_title) . '</option>'; |
|---|
| 198 | } |
|---|
| 199 | echo |
|---|
| 200 | '</select></label></p>'; |
|---|
| 201 | unset($rs); |
|---|
| 202 | } |
|---|
| 203 | echo |
|---|
| 204 | '<div class="lockable">' . |
|---|
| 205 | '<p><label for="cat_url">' . __('URL:') . '</label> ' |
|---|
| 206 | . form::field('cat_url', 40, 255, html::escapeHTML($cat_url)) . |
|---|
| 207 | '</p>' . |
|---|
| 208 | '<p class="form-note warn" id="note-cat-url">' . |
|---|
| 209 | __('Warning: If you set the URL manually, it may conflict with another category.') . '</p>' . |
|---|
| 210 | '</div>' . |
|---|
| 211 | |
|---|
| 212 | '<p class="area"><label for="cat_desc">' . __('Description:') . '</label> ' . |
|---|
| 213 | form::textarea('cat_desc', 50, 8, html::escapeHTML($cat_desc)) . |
|---|
| 214 | '</p>' . |
|---|
| 215 | |
|---|
| 216 | '<p><input type="submit" accesskey="s" value="' . __('Save') . '" />' . |
|---|
| 217 | ($cat_id ? form::hidden('id', $cat_id) : '') . |
|---|
| 218 | $core->formNonce() . |
|---|
| 219 | '</p>' . |
|---|
| 220 | '</form>'; |
|---|
| 221 | |
|---|
| 222 | if ($cat_id) { |
|---|
| 223 | echo |
|---|
| 224 | '<h3 class="border-top">' . __('Move this category') . '</h3>' . |
|---|
| 225 | '<div class="two-cols">' . |
|---|
| 226 | '<div class="col">' . |
|---|
| 227 | |
|---|
| 228 | '<form action="' . $core->adminurl->get("admin.category") . '" method="post" class="fieldset">' . |
|---|
| 229 | '<h4>' . __('Category parent') . '</h4>' . |
|---|
| 230 | '<p><label for="cat_parent" class="classic">' . __('Parent:') . '</label> ' . |
|---|
| 231 | form::combo('cat_parent', $allowed_parents, $cat_parent) . '</p>' . |
|---|
| 232 | '<p><input type="submit" accesskey="s" value="' . __('Save') . '" />' . |
|---|
| 233 | form::hidden(array('id'), $cat_id) . $core->formNonce() . '</p>' . |
|---|
| 234 | '</form>' . |
|---|
| 235 | '</div>'; |
|---|
| 236 | |
|---|
| 237 | if (count($siblings) > 0) { |
|---|
| 238 | echo |
|---|
| 239 | '<div class="col">' . |
|---|
| 240 | '<form action="' . $core->adminurl->get("admin.category") . '" method="post" class="fieldset">' . |
|---|
| 241 | '<h4>' . __('Category sibling') . '</h4>' . |
|---|
| 242 | '<p><label class="classic" for="cat_sibling">' . __('Move current category') . '</label> ' . |
|---|
| 243 | form::combo('cat_move', array(__('before') => 'before', __('after') => 'after'), |
|---|
| 244 | array('extra_html' => 'title="' . __('position: ') . '"')) . ' ' . |
|---|
| 245 | form::combo('cat_sibling', $siblings) . '</p>' . |
|---|
| 246 | '<p><input type="submit" accesskey="s" value="' . __('Save') . '" />' . |
|---|
| 247 | form::hidden(array('id'), $cat_id) . $core->formNonce() . '</p>' . |
|---|
| 248 | '</form>' . |
|---|
| 249 | '</div>'; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | echo '</div>'; |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | dcPage::helpBlock('core_category'); |
|---|
| 256 | dcPage::close(); |
|---|