Dotclear

source: admin/posts_actions.php @ 1473:be1c768e98cc

Revision 1473:be1c768e98cc, 12.9 KB checked in by Dsls, 12 years ago (diff)

Fusion

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

Sites map