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