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