Dotclear

source: admin/categories.php @ 1594:2c7be9167efb

Revision 1594:2c7be9167efb, 6.8 KB checked in by Nicolas <nikrou77@…>, 11 years ago (diff)

Fix issue 1541 : make action without javascript
Refactoring : only one form

Line 
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
13require dirname(__FILE__).'/../inc/admin/prepend.php';
14
15dcPage::check('categories');
16
17# Remove a categories
18if (!empty($_POST['categories']) && !empty($_POST['delete'])) {
19     try {
20          # Check if category where to move posts exists
21          $mov_cat = (int) $_POST['mov_cat'];
22          $mov_cat = $mov_cat ? $mov_cat : null;
23          if ($mov_cat !== null) {
24               $c = $core->blog->getCategory($mov_cat);
25               if ($c->isEmpty()) {
26                    throw new Exception(__('Category where to move posts does not exist'));
27               }
28               unset($c);
29
30               if (in_array($mov_cat, $_POST['categories'])) {
31                    throw new Exception(__('The entries cannot be moved to the category you choose to delete.'));
32               }
33          }
34
35          foreach ($_POST['categories'] as $cat_id) {
36               # Check if category to delete exists
37               $c = $core->blog->getCategory((integer) $cat_id);
38               if ($c->isEmpty()) {
39                    continue;
40               }
41               unset($c);
42
43               # Move posts
44               if ($mov_cat != $cat_id) {
45                       $core->blog->changePostsCategory($cat_id,$mov_cat);
46               }
47
48               # Delete category
49               $core->blog->delCategory($cat_id);
50          }
51          http::redirect('categories.php?del='.count($_POST['categories']));
52     } catch (Exception $e) {
53          $core->error->add($e->getMessage());
54     }
55}
56
57# Update order
58if (!empty($_POST['save_order']))
59{
60     if (!empty($_POST['categories_order'])) { // js is enable
61         $categories = json_decode($_POST['categories_order']);
62
63         foreach ($categories as $category) {
64           if (!empty($category->item_id)) {
65          $core->blog->updCategoryPosition($category->item_id, $category->left, $category->right);
66           }
67         }
68     } elseif (!empty($_POST['cat_rank'])) {
69          function countChildren($t, $id) {
70               $c = 0;
71               foreach ($t as $k => $v) {
72                    if (preg_match('`^'.$id.'\..*`', $k)) {
73                         $c++;
74                    }
75               }
76
77               return $c;
78          }
79
80          $i = 1;
81          $ranks = array_flip($_POST['cat_rank']);
82          uksort($ranks, 'version_compare');
83          foreach ($ranks as $str => $id) {
84               $core->blog->updCategoryPosition($id, $i, ($i + 2 * countChildren($ranks, $str) + 1));
85               $i = $i+2;
86          }
87     }
88
89     http::redirect('categories.php?reord=1');
90}
91
92# Reset order
93if (!empty($_POST['reset']))
94{
95     try
96     {
97          $core->blog->resetCategoriesOrder();
98          http::redirect('categories.php?reord=1');
99     }
100     catch (Exception $e)
101     {
102          $core->error->add($e->getMessage());
103     }
104}
105
106
107/* Display
108-------------------------------------------------------- */
109$rs = $core->blog->getCategories(array('post_type'=>'post'));
110
111$starting_script = dcPage::jsToolMan()."\n";
112if (!$core->auth->user_prefs->accessibility->nodragdrop
113     && $core->auth->check('categories',$core->blog->id)
114     && $rs->count()>1) {
115        $starting_script .= dcPage::jsLoad('js/jquery/jquery-ui.custom.js');
116     $starting_script .= dcPage::jsLoad('js/jquery/jquery.mjs.nestedSortable.js');
117}
118$starting_script .= dcPage::jsLoad('js/_categories.js');
119
120dcPage::open(__('Categories'),$starting_script,
121     dcPage::breadcrumb(
122          array(
123               html::escapeHTML($core->blog->name) => '',
124               '<span class="page-title">'.__('Categories').'</span>' => ''
125          ))
126);
127
128if (!empty($_GET['del'])) {
129        dcPage::success(__('The category has been successfully removed.',
130                  'The categories have been successfully removed.',
131                  (int) $_GET['del']
132                  )
133               );
134}
135if (!empty($_GET['reord'])) {
136     dcPage::success(__('Categories have been successfully reordered.'));
137}
138
139$categories_combo = array();
140if (!$rs->isEmpty())
141{
142     while ($rs->fetch()) {
143          $catparents_combo[] = $categories_combo[] = new formSelectOption(
144               str_repeat('&nbsp;&nbsp;',$rs->level-1).($rs->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($rs->cat_title),
145               $rs->cat_id
146          );
147     }
148}
149
150echo
151'<p class="top-add"><a class="button add" href="category.php">'.__('New category').'</a></p>';
152
153echo
154'<div class="col">';
155if ($rs->isEmpty())
156{
157     echo '<p>'.__('No category yet.').'</p>';
158}
159else
160{
161     echo
162     '<form action="categories.php" method="post" id="form-categories">'.
163     '<h3>'.__('List of blog\'s categories').'</h3>'.
164     '<div id="categories">';
165
166     $ref_level = $level = $rs->level-1;
167     $parts = array();
168     while ($rs->fetch())
169     {
170          $attr = 'id="cat_'.$rs->cat_id.'"';
171
172          if ($rs->level > $level) {
173               echo str_repeat('<ul><li '.$attr.'>',$rs->level - $level);
174               $parts[] = 1;
175          } elseif ($rs->level < $level) {
176               echo str_repeat('</li></ul>',-($rs->level - $level));
177               $parts = array_slice($parts,0,(count($parts) - ($level - $rs->level)));
178               $parts[count($parts)-1]++;
179          } else {
180               $parts[count($parts)-1]++;
181          }
182
183          if ($rs->level <= $level) {
184               echo '</li><li '.$attr.'>';
185          }
186
187          $cat_rank = implode('.',$parts);
188          echo
189          '<p>'.
190          form::checkbox(array('categories[]','cat-'.$rs->cat_id),$rs->cat_id,null,$rs->nb_total>0?'notempty':'').
191          '<label class="classic" for="cat-'.$rs->cat_id.'"><a href="category.php?id='.$rs->cat_id.'">'.html::escapeHTML($rs->cat_title).'</a></label>'.
192          ' (<a href="posts.php?cat_id='.$rs->cat_id.'">'.
193          sprintf(($rs->nb_post > 1 ? __('%d entries') : __('%d entry') ),$rs->nb_post).'</a>'.
194          ', '.__('total:').' '.$rs->nb_total.') '.
195          '<span class="cat-url">'.__('URL:').' <code>'.html::escapeHTML($rs->cat_url).'</code></span>'.
196          form::field('cat_rank['.$rs->cat_id.']',10,10,$cat_rank,'cat-rank').
197          '</p>';
198
199          $level = $rs->level;
200     }
201
202     if ($ref_level - $level < 0) {
203          echo str_repeat('</li></ul>',-($ref_level - $level));
204     }
205     echo
206     '</div>';
207
208     echo
209     '<div class="two-cols">'.
210     '<p class="col checkboxes-helpers"></p>'.
211     '<p class="col right" id="mov-cat">'.
212     '<label for="mov_cat" class="classic">'.__('Category which will receive entries of deleted categories:').'</label> '.
213     form::combo('mov_cat',array_merge(array(__('(No cat)') => ''),$categories_combo),'','').
214     '</p>'.
215     '<p class="right">'.
216     '<input type="submit" name="delete" value="'.__('Delete selected categories').'"/>'.
217     '</p>'.
218     '</div>';
219
220     echo '<h3 class="clear">'.__('Categories order').'</h3>';
221
222     if ($core->auth->check('categories',$core->blog->id) && $rs->count()>1) {
223          if (!$core->auth->user_prefs->accessibility->nodragdrop) {
224               echo '<p class="no-js-hidden">'.__('To rearrange categories order, move items by drag and drop, then click on “Save categories order” button.').'</p>';
225          }
226          echo
227          '<p class="dragdrop-hidden">'.__('To rearrange categories order, change position number and click on “Save categories order” button.').'</p>'.
228          '<p>'.
229          '<input type="hidden" id="categories_order" name="categories_order" value=""/>'.
230          '<input type="submit" name="save_order" id="save-set-order" value="'.__('Save categories order').'" />'.
231          '</p>';
232     }
233
234     echo
235     '<p><input type="submit" name="reset" value="'.__('Reorder all categories on the top level').'" />'.
236     $core->formNonce().'</p>'.
237     '</form>';
238}
239
240echo '</div>';
241
242dcPage::helpBlock('core_categories');
243dcPage::close();
244?>
Note: See TracBrowser for help on using the repository browser.

Sites map