Dotclear

source: admin/categories.php @ 1531:18a9e84c04e3

Revision 1531:18a9e84c04e3, 5.1 KB checked in by Nicolas <nikrou77@…>, 12 years ago (diff)

Issue #1541 : categories with entries could be deleted and entries can be moved to another category.

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'])) {
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# Reset order
58if (!empty($_POST['reset']))
59{
60     try
61     {
62          $core->blog->resetCategoriesOrder();
63          http::redirect('categories.php?reord=1');
64     }
65     catch (Exception $e)
66     {
67          $core->error->add($e->getMessage());
68     }
69}
70
71# Actions combo box
72$combo_action = array();
73if ($core->auth->check('categories',$core->blog->id)) {
74     $combo_action[__('Delete')] = 'delete';
75}
76
77
78/* Display
79-------------------------------------------------------- */
80dcPage::open(__('Categories'),
81     dcPage::jsToolMan()."\n".
82     dcPage::jsLoad('js/_categories.js'),
83     dcPage::breadcrumb(
84          array(
85               html::escapeHTML($core->blog->name) => '',
86               '<span class="page-title">'.__('Categories').'</span>' => ''
87          ))
88);
89
90if (!empty($_GET['del'])) {
91        dcPage::message(__('The category has been successfully removed.',
92                  'The categories have been successfully removed.',
93                  (int) $_GET['del']
94                  )
95               );
96}
97if (!empty($_GET['reord'])) {
98     dcPage::message(__('Categories have been successfully reordered.'));
99}
100
101$rs = $core->blog->getCategories(array('post_type'=>'post'));
102
103$categories_combo = array();
104if (!$rs->isEmpty())
105{
106     while ($rs->fetch()) {
107          $catparents_combo[] = $categories_combo[] = new formSelectOption(
108               str_repeat('&nbsp;&nbsp;',$rs->level-1).($rs->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($rs->cat_title),
109               $rs->cat_id
110          );
111     }
112}
113
114echo
115'<p class="top-add"><a class="button add" href="category.php">'.__('New category').'</a></p>';
116
117echo
118'<div class="col">';
119if ($rs->isEmpty())
120{
121     echo '<p>'.__('No category yet.').'</p>';
122}
123else
124{
125     echo
126     '<form action="categories.php" method="post" id="form-categories">'.
127     '<h3>'.__('Categories list').'</h3>'.
128     '<div id="categories">';
129
130     $ref_level = $level = $rs->level-1;
131     while ($rs->fetch())
132     {
133          $attr = 'id="cat'.$rs->cat_id.'"';
134          if ($rs->nb_total == 0) {
135               $attr .= ' class="deletable"';
136          }
137
138          if ($rs->level > $level) {
139               echo str_repeat('<ul><li '.$attr.'>',$rs->level - $level);
140          } elseif ($rs->level < $level) {
141               echo str_repeat('</li></ul>',-($rs->level - $level));
142          }
143
144          if ($rs->level <= $level) {
145               echo '</li><li '.$attr.'>';
146          }
147
148          echo
149          '<p>'.
150          form::checkbox(array('categories[]'),$rs->cat_id).
151          '<strong><a href="category.php?id='.$rs->cat_id.'">'.html::escapeHTML($rs->cat_title).'</a></strong>'.
152          ' (<a href="posts.php?cat_id='.$rs->cat_id.'">'.
153          sprintf(($rs->nb_post > 1 ? __('%d entries') : __('%d entry') ),$rs->nb_post).'</a>'.
154          ', '.__('total:').' '.$rs->nb_total.')</p>'.
155          '<p>'.__('URL:').' '.html::escapeHTML($rs->cat_url).'</p>';
156
157          $level = $rs->level;
158     }
159
160     if ($ref_level - $level < 0) {
161          echo str_repeat('</li></ul>',-($ref_level - $level));
162     }
163     echo
164     '</div>';
165
166     if (count($combo_action)>0) {
167           echo
168           '<div class="two-cols">'.
169           '<p class="col checkboxes-helpers"></p>'.
170           '<p class="col right"><label for="mov_cat">'.__('And choose the category which will receive its entries:').'</label> '.
171           form::combo('mov_cat',array_merge(array(__('(No cat)') => ''),$categories_combo),'','maximal').'</p> '.
172           '<p class="col right"><label for="action" class="classic">'.__('Selected categories action:').'</label> '.
173           form::combo('action',$combo_action).
174           $core->formNonce().
175           '<input type="submit" value="'.__('ok').'" /></p>'.
176           '</div>'.
177           '</form>';
178     }
179
180     echo
181     '<div class="col clear">'.
182     '<form action="categories.php" method="post" id="reset-order">'.
183     '<h3>'.__('Reorder categories').'</h3>'.
184     '<p>'.__('This will relocate all categories on the top level').'</p> '.
185     '<p><input class="reset" type="submit" value="'.__('Reorder').'" />'.
186     form::hidden(array('reset'),1).
187     $core->formNonce().'</p>'.
188     '</form>'.
189     '</div>';
190}
191
192echo '</div>';
193
194dcPage::helpBlock('core_categories');
195dcPage::close();
196?>
Note: See TracBrowser for help on using the repository browser.

Sites map