Dotclear

source: admin/posts_actions.php @ 1471:136cc800ade6

Revision 1471:136cc800ade6, 10.2 KB checked in by Dsls, 11 years ago (diff)

Added entries list in posts_actions.php, ability to uncheck some entries in this page. see #1540
Added proper message when no entry selected, or no action set. closes #1527

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('usage,contentadmin');
16
17$params = array();
18
19class FieldsList {
20     protected $hidden;
21     protected $entries;
22     public function __construct() {
23          $this->hidden=array();
24          $this->entries =array();
25     }
26     public function addHidden($name,$value) {
27          $this->hidden[] = form::hidden($name,$value);
28          return $this;
29     }
30     public function addEntry($id,$title) {
31          $this->entries[$id]=$title;
32          return $this;
33     }
34
35     public function getHidden() {
36          return join('',$this->hidden);
37     }
38     
39     public function getEntries ($hidden=false) {
40          $ret = '';
41          if ($hidden) {
42               foreach ($this->entries as $id=> $e) {
43                    $ret .= form::hidden('entries[]',$id);
44               }
45          } else {
46               $ret = 
47                    '<table class="posts-list"><tr>'.
48                    '<th colspan="2">'.__('Title').'</th>'.
49                    '</tr>';
50               foreach ($this->entries as $id=>$title) {
51                    $ret .= 
52                         '<tr><td>'.
53                         form::checkbox(array('entries[]'),$id,true,'','').'</td>'.
54                         '<td>'.   $title.'</td></tr>';
55               }
56               $ret .= '</table>';
57          }
58          return $ret;
59     }
60     
61     public function __toString() {
62          return join('',$this->hidden).$this->getEntries(true);
63     }
64}
65
66
67function listEntries($titles) {
68     $ret = 
69          '<table class="posts-list"><tr>'.
70          '<th colspan="2">'.__('Title').'</th>'.
71          '</tr>';
72     foreach ($titles as $id=>$title) {
73          $ret .= 
74               '<tr><td>'.
75               form::checkbox(array('entries[]'),$id,true,'','').'</td>'.
76               '<td>'.   $title.'</td></tr>';
77     }
78     $ret .= '</table>';
79     return $ret;
80}
81
82/* Actions
83-------------------------------------------------------- */
84if (!empty($_POST['action']) && !empty($_POST['entries']))
85{
86     $entries = $_POST['entries'];
87     $action = $_POST['action'];
88     
89     if (isset($_POST['redir']) && strpos($_POST['redir'],'://') === false)
90     {
91          $redir = $_POST['redir'];
92     }
93     else
94     {
95          $redir =
96          'posts.php?user_id='.$_POST['user_id'].
97          '&cat_id='.$_POST['cat_id'].
98          '&status='.$_POST['status'].
99          '&selected='.$_POST['selected'].
100          '&month='.$_POST['month'].
101          '&lang='.$_POST['lang'].
102          '&sortby='.$_POST['sortby'].
103          '&order='.$_POST['order'].
104          '&page='.$_POST['page'].
105          '&nb='.$_POST['nb'];
106     }
107     
108     foreach ($entries as $k => $v) {
109          $entries[$k] = (integer) $v;
110     }
111     
112     $params['sql'] = 'AND P.post_id IN('.implode(',',$entries).') ';
113     
114     if (!isset($_POST['full_content']) || empty($_POST['full_content'])) {
115          $params['no_content'] = true;
116     }
117     
118     if (isset($_POST['post_type'])) {
119          $params['post_type'] = $_POST['post_type'];
120     }
121     
122     $posts = $core->blog->getPosts($params);
123     
124     $posts_ids = array();
125     while ($posts->fetch())  {
126          $posts_ids[] = $posts->post_id;
127     }
128     
129     # --BEHAVIOR-- adminPostsActions
130     $core->callBehavior('adminPostsActions',$core,$posts,$action,$redir);
131     
132     if (preg_match('/^(publish|unpublish|schedule|pending)$/',$action))
133     {
134          switch ($action) {
135               case 'unpublish' : $status = 0; break;
136               case 'schedule' : $status = -1; break;
137               case 'pending' : $status = -2; break;
138               default : $status = 1; break;
139          }
140         
141          try
142          {
143               $core->blog->updPostsStatus($posts_ids,$status);
144               
145               http::redirect($redir);
146          }
147          catch (Exception $e)
148          {
149               $core->error->add($e->getMessage());
150          }
151     }
152     elseif ($action == 'selected' || $action == 'unselected')
153     {
154          try
155          {
156               $core->blog->updPostsSelected($posts_ids,$action == 'selected');
157               
158               http::redirect($redir);
159          }
160          catch (Exception $e)
161          {
162               $core->error->add($e->getMessage());
163          }
164     }
165     elseif ($action == 'delete')
166     {
167          try
168          {
169               // Backward compatibility
170               foreach($posts_ids as $post_id)
171               {
172                    # --BEHAVIOR-- adminBeforePostDelete
173                    $core->callBehavior('adminBeforePostDelete',(integer) $post_id);
174               }
175               
176               # --BEHAVIOR-- adminBeforePostsDelete
177               $core->callBehavior('adminBeforePostsDelete',$posts_ids);
178               
179               $core->blog->delPosts($posts_ids);
180               
181               http::redirect($redir);
182          }
183          catch (Exception $e)
184          {
185               $core->error->add($e->getMessage());
186          }
187         
188     }
189     elseif ($action == 'category' && isset($_POST['new_cat_id']))
190     {
191          try
192          {
193               $core->blog->updPostsCategory($posts_ids,$_POST['new_cat_id']);
194               
195               http::redirect($redir);
196          }
197          catch (Exception $e)
198          {
199               $core->error->add($e->getMessage());
200          }
201     }
202     elseif ($action == 'author' && isset($_POST['new_auth_id'])
203     && $core->auth->check('admin',$core->blog->id))
204     {
205          $new_user_id = $_POST['new_auth_id'];
206         
207          try
208          {
209               if ($core->getUser($new_user_id)->isEmpty()) {
210                    throw new Exception(__('This user does not exist'));
211               }
212               
213               $cur = $core->con->openCursor($core->prefix.'post');
214               $cur->user_id = $new_user_id;
215               $cur->update('WHERE post_id '.$core->con->in($posts_ids));
216               
217               http::redirect($redir);
218          }
219          catch (Exception $e)
220          {
221               $core->error->add($e->getMessage());
222          }
223     }
224     elseif ($action == 'lang' && isset($_POST['new_lang']))
225     {
226          $new_lang = $_POST['new_lang'];
227          try
228          {
229               $cur = $core->con->openCursor($core->prefix.'post');
230               $cur->post_lang = $new_lang;
231               $cur->update('WHERE post_id '.$core->con->in($posts_ids));
232               
233               http::redirect($redir);
234          }
235          catch (Exception $e)
236          {
237               $core->error->add($e->getMessages());
238          }
239     }
240} else {
241     if (empty($_POST['entries'])) {
242          $core->error->add(__('At least one entry should be selected'));
243     } else {
244          $core->error->add(__('No action specified.'));
245     }
246     dcPage::open(
247          __('Entries'),'',dcPage::breadcrumb(
248          array(
249               html::escapeHTML($core->blog->name) => '',
250               __('Entries') => 'posts.php',
251               '<span class="page-title">'.__('Entries actions').'</span>' => ''
252          ))
253     );
254
255     dcPage::close();
256     exit;
257}
258/* DISPLAY
259-------------------------------------------------------- */
260// Get current users list
261$usersList = '';
262if ($action == 'author' && $core->auth->check('admin',$core->blog->id)) {
263     $params = array(
264          'limit' => 100,
265          'order' => 'nb_post DESC'
266          );
267     $rs = $core->getUsers($params);
268     while ($rs->fetch())
269     {
270          $usersList .= ($usersList != '' ? ',' : '').'"'.$rs->user_id.'"';
271     }
272}
273dcPage::open(
274     __('Entries'),
275     '<script type="text/javascript">'."\n".
276     "//<![CDATA[\n".
277     'usersList = ['.$usersList.']'."\n".
278     "\n//]]>\n".
279     "</script>\n".
280     dcPage::jsLoad('js/jquery/jquery.autocomplete.js').
281     dcPage::jsLoad('js/_posts_actions.js').
282     dcPage::jsMetaEditor().
283     # --BEHAVIOR-- adminBeforePostDelete
284     $core->callBehavior('adminPostsActionsHeaders')
285);
286
287if (!isset($action)) {
288     dcPage::close();
289     exit;
290}
291
292$fields = new FieldsList();
293while ($posts->fetch()) {
294     $fields->addEntry($posts->post_id,$posts->post_title);
295}
296
297if (isset($_POST['redir']) && strpos($_POST['redir'],'://') === false)
298{
299     $fields->addHidden(array('redir'),html::escapeURL($_POST['redir']));
300}
301else
302{
303     $fields
304          ->addHidden(array('user_id'),$_POST['user_id'])
305          ->addHidden(array('cat_id'),$_POST['cat_id'])
306          ->addHidden(array('status'),$_POST['status'])
307          ->addHidden(array('selected'),$_POST['selected'])
308          ->addHidden(array('month'),$_POST['month'])
309          ->addHidden(array('lang'),$_POST['lang'])
310          ->addHidden(array('sortby'),$_POST['sortby'])
311          ->addHidden(array('order'),$_POST['order'])
312          ->addHidden(array('page'),$_POST['page'])
313          ->addHidden(array('nb'),$_POST['nb'])
314     ;
315}
316
317if (isset($_POST['post_type'])) {
318     $fields->addHidden(array('post_type'),$_POST['post_type']);
319}
320
321# --BEHAVIOR-- adminPostsActionsContent
322$core->callBehavior('adminPostsActionsContent',$core,$action,$fields);
323
324if ($action == 'category')
325{
326     echo dcPage::breadcrumb(
327          array(
328               html::escapeHTML($core->blog->name) => '',
329               __('Entries') => 'posts.php',
330               __('Change category for entries') => ''
331     ));
332
333     # categories list
334     # Getting categories
335     $categories_combo = array('&nbsp;' => '');
336     try {
337          $categories = $core->blog->getCategories(array('post_type'=>'post'));
338          while ($categories->fetch()) {
339               $categories_combo[] = new formSelectOption(
340                    str_repeat('&nbsp;&nbsp;',$categories->level-1).
341                    ($categories->level-1 == 0 ? '' : '&bull; ').html::escapeHTML($categories->cat_title),
342                    $categories->cat_id
343               );
344          }
345     } catch (Exception $e) { }
346     
347     echo
348     '<form action="posts_actions.php" method="post">'.
349     $fields->getEntries().
350     '<p><label for="new_cat_id" class="classic">'.__('Category:').' '.
351     form::combo('new_cat_id',$categories_combo,'').
352     '</label> ';
353     
354     echo
355     $fields->getHidden().
356     $core->formNonce().
357     form::hidden(array('action'),'category').
358     '<input type="submit" value="'.__('Save').'" /></p>'.
359     '</form>';
360}
361elseif ($action == 'lang')
362{
363     echo dcPage::breadcrumb(
364          array(
365               html::escapeHTML($core->blog->name) => '',
366               __('Entries') => 'posts.php',
367               '<span class="page-title">'.__('Change language for entries').'</span>' => ''
368     ));
369     
370     # lang list
371     # Languages combo
372     $rs = $core->blog->getLangs(array('order'=>'asc'));
373     $all_langs = l10n::getISOcodes(0,1);
374     $lang_combo = array('' => '', __('Most used') => array(), __('Available') => l10n::getISOcodes(1,1));
375     while ($rs->fetch()) {
376          if (isset($all_langs[$rs->post_lang])) {
377               $lang_combo[__('Most used')][$all_langs[$rs->post_lang]] = $rs->post_lang;
378               unset($lang_combo[__('Available')][$all_langs[$rs->post_lang]]);
379          } else {
380               $lang_combo[__('Most used')][$rs->post_lang] = $rs->post_lang;
381          }
382     }
383     unset($all_langs);
384     unset($rs);
385     
386     echo
387     '<form action="posts_actions.php" method="post">'.
388     $fields->getEntries().   
389     '<p><label for="new_lang" class="classic">'.__('Entry lang:').' '.
390     form::combo('new_lang',$lang_combo,'').
391     '</label> ';
392     
393     echo
394     $fields->getHidden().
395     $core->formNonce().
396     form::hidden(array('action'),'lang').
397     '<input type="submit" value="'.__('Save').'" /></p>'.
398     '</form>';
399
400}
401elseif ($action == 'author' && $core->auth->check('admin',$core->blog->id))
402{
403     echo dcPage::breadcrumb(
404          array(
405               html::escapeHTML($core->blog->name) => '',
406               __('Entries') => 'posts.php',
407               '<span class="page-title">'.__('Change author for entries').'</span>' => ''
408     ));
409     
410     echo
411     '<form action="posts_actions.php" method="post">'.
412     $fields->getEntries().
413     '<p><label for="new_auth_id" class="classic">'.__('Author ID:').' '.
414     form::field('new_auth_id',20,255).
415     '</label> ';
416     
417     echo
418     $fields->getHidden().
419     $core->formNonce().
420     form::hidden(array('action'),'author').
421     '<input type="submit" value="'.__('Save').'" /></p>'.
422     '</form>';
423}
424
425echo '<p><a class="back" href="'.html::escapeURL($redir).'">'.__('back').'</a></p>';
426
427dcPage::close();
428?>
Note: See TracBrowser for help on using the repository browser.

Sites map