Dotclear

source: inc/admin/actions/class.dcactionposts.php @ 3703:53c8bef8608a

Revision 3703:53c8bef8608a, 16.5 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Use array form of optionnal parameters for form::combo(), code formatting (PSR-2)

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 -----------------------------------------
12if (!defined('DC_RC_PATH')) {return;}
13
14class dcPostsActionsPage extends dcActionsPage
15{
16    public function __construct($core, $uri, $redirect_args = array())
17    {
18        parent::__construct($core, $uri, $redirect_args);
19        $this->redirect_fields = array('user_id', 'cat_id', 'status',
20            'selected', 'attachment', 'month', 'lang', 'sortby', 'order', 'page', 'nb');
21        $this->loadDefaults();
22    }
23
24    protected function loadDefaults()
25    {
26        // We could have added a behavior here, but we want default action
27        // to be setup first
28        dcDefaultPostActions::adminPostsActionsPage($this->core, $this);
29        $this->core->callBehavior('adminPostsActionsPage', $this->core, $this);
30
31    }
32
33    public function beginPage($breadcrumb = '', $head = '')
34    {
35        if ($this->in_plugin) {
36            echo '<html><head><title>' . __('Entries') . '</title>' .
37            dcPage::jsLoad('js/_posts_actions.js') .
38                $head .
39                '</script></head><body>' .
40                $breadcrumb;
41        } else {
42            dcPage::open(
43                __('Entries'),
44                dcPage::jsLoad('js/_posts_actions.js') .
45                $head,
46                $breadcrumb
47            );
48        }
49        echo '<p><a class="back" href="' . $this->getRedirection(true) . '">' . __('Back to entries list') . '</a></p>';
50    }
51
52    public function endPage()
53    {
54        if ($this->in_plugin) {
55            echo '</body></html>';
56        } else {
57            dcPage::close();
58        }
59    }
60
61    public function error(Exception $e)
62    {
63        $this->core->error->add($e->getMessage());
64        $this->beginPage(dcPage::breadcrumb(
65            array(
66                html::escapeHTML($this->core->blog->name) => '',
67                $this->getCallerTitle()                   => $this->getRedirection(true),
68                __('Entries actions')                     => ''
69            ))
70        );
71        $this->endPage();
72    }
73
74    protected function fetchEntries($from)
75    {
76        $params = array();
77        if (!empty($from['entries'])) {
78            $entries = $from['entries'];
79
80            foreach ($entries as $k => $v) {
81                $entries[$k] = (integer) $v;
82            }
83
84            $params['sql'] = 'AND P.post_id IN(' . implode(',', $entries) . ') ';
85        } else {
86            $params['sql'] = 'AND 1=0 ';
87        }
88
89        if (!isset($from['full_content']) || empty($from['full_content'])) {
90            $params['no_content'] = true;
91        }
92
93        if (isset($from['post_type'])) {
94            $params['post_type'] = $from['post_type'];
95        }
96
97        $posts = $this->core->blog->getPosts($params);
98        while ($posts->fetch()) {
99            $this->entries[$posts->post_id] = $posts->post_title;
100        }
101        $this->rs = $posts;
102    }
103}
104
105class dcDefaultPostActions
106{
107    public static function adminPostsActionsPage($core, $ap)
108    {
109        if ($core->auth->check('publish,contentadmin', $core->blog->id)) {
110            $ap->addAction(
111                array(__('Status') => array(
112                    __('Publish')         => 'publish',
113                    __('Unpublish')       => 'unpublish',
114                    __('Schedule')        => 'schedule',
115                    __('Mark as pending') => 'pending'
116                )),
117                array('dcDefaultPostActions', 'doChangePostStatus')
118            );
119        }
120        $ap->addAction(
121            array(__('Mark') => array(
122                __('Mark as selected')   => 'selected',
123                __('Mark as unselected') => 'unselected'
124            )),
125            array('dcDefaultPostActions', 'doUpdateSelectedPost')
126        );
127        $ap->addAction(
128            array(__('Change') => array(
129                __('Change category') => 'category'
130            )),
131            array('dcDefaultPostActions', 'doChangePostCategory')
132        );
133        $ap->addAction(
134            array(__('Change') => array(
135                __('Change language') => 'lang'
136            )),
137            array('dcDefaultPostActions', 'doChangePostLang')
138        );
139        if ($core->auth->check('admin', $core->blog->id)) {
140            $ap->addAction(
141                array(__('Change') => array(
142                    __('Change author') => 'author')),
143                array('dcDefaultPostActions', 'doChangePostAuthor')
144            );
145        }
146        if ($core->auth->check('delete,contentadmin', $core->blog->id)) {
147            $ap->addAction(
148                array(__('Delete') => array(
149                    __('Delete') => 'delete')),
150                array('dcDefaultPostActions', 'doDeletePost')
151            );
152        }
153    }
154
155    public static function doChangePostStatus($core, dcPostsActionsPage $ap, $post)
156    {
157        switch ($ap->getAction()) {
158            case 'unpublish':$status = 0;
159                break;
160            case 'schedule':$status = -1;
161                break;
162            case 'pending':$status = -2;
163                break;
164            default:$status = 1;
165                break;
166        }
167        $posts_ids = $ap->getIDs();
168        if (empty($posts_ids)) {
169            throw new Exception(__('No entry selected'));
170        }
171        $core->blog->updPostsStatus($posts_ids, $status);
172        dcPage::addSuccessNotice(sprintf(
173            __(
174                '%d entry has been successfully updated to status : "%s"',
175                '%d entries have been successfully updated to status : "%s"',
176                count($posts_ids)
177            ),
178            count($posts_ids),
179            $core->blog->getPostStatus($status))
180        );
181        $ap->redirect(true);
182    }
183
184    public static function doUpdateSelectedPost($core, dcPostsActionsPage $ap, $post)
185    {
186        $posts_ids = $ap->getIDs();
187        if (empty($posts_ids)) {
188            throw new Exception(__('No entry selected'));
189        }
190        $action = $ap->getAction();
191        $core->blog->updPostsSelected($posts_ids, $action == 'selected');
192        if ($action == 'selected') {
193            dcPage::addSuccessNotice(sprintf(
194                __(
195                    '%d entry has been successfully marked as selected',
196                    '%d entries have been successfully marked as selected',
197                    count($posts_ids)
198                ),
199                count($posts_ids))
200            );
201        } else {
202            dcPage::addSuccessNotice(sprintf(
203                __(
204                    '%d entry has been successfully marked as unselected',
205                    '%d entries have been successfully marked as unselected',
206                    count($posts_ids)
207                ),
208                count($posts_ids))
209            );
210        }
211        $ap->redirect(true);
212    }
213
214    public static function doDeletePost($core, dcPostsActionsPage $ap, $post)
215    {
216
217        $posts_ids = $ap->getIDs();
218        if (empty($posts_ids)) {
219            throw new Exception(__('No entry selected'));
220        }
221        // Backward compatibility
222        foreach ($posts_ids as $post_id) {
223            # --BEHAVIOR-- adminBeforePostDelete
224            $core->callBehavior('adminBeforePostDelete', (integer) $post_id);
225        }
226
227        # --BEHAVIOR-- adminBeforePostsDelete
228        $core->callBehavior('adminBeforePostsDelete', $posts_ids);
229
230        $core->blog->delPosts($posts_ids);
231        dcPage::addSuccessNotice(sprintf(
232            __(
233                '%d entry has been successfully deleted',
234                '%d entries have been successfully deleted',
235                count($posts_ids)
236            ),
237            count($posts_ids))
238        );
239
240        $ap->redirect(false);
241    }
242
243    public static function doChangePostCategory($core, dcPostsActionsPage $ap, $post)
244    {
245        if (isset($post['new_cat_id'])) {
246            $posts_ids = $ap->getIDs();
247            if (empty($posts_ids)) {
248                throw new Exception(__('No entry selected'));
249            }
250            $new_cat_id = $post['new_cat_id'];
251            if (!empty($post['new_cat_title']) && $core->auth->check('categories', $core->blog->id)) {
252                $cur_cat            = $core->con->openCursor($core->prefix . 'category');
253                $cur_cat->cat_title = $post['new_cat_title'];
254                $cur_cat->cat_url   = '';
255                $title              = $cur_cat->cat_title;
256
257                $parent_cat = !empty($post['new_cat_parent']) ? $post['new_cat_parent'] : '';
258
259                # --BEHAVIOR-- adminBeforeCategoryCreate
260                $core->callBehavior('adminBeforeCategoryCreate', $cur_cat);
261
262                $new_cat_id = $core->blog->addCategory($cur_cat, (integer) $parent_cat);
263
264                # --BEHAVIOR-- adminAfterCategoryCreate
265                $core->callBehavior('adminAfterCategoryCreate', $cur_cat, $new_cat_id);
266            }
267
268            $core->blog->updPostsCategory($posts_ids, $new_cat_id);
269            $title = $core->blog->getCategory($new_cat_id);
270            dcPage::addSuccessNotice(sprintf(
271                __(
272                    '%d entry has been successfully moved to category "%s"',
273                    '%d entries have been successfully moved to category "%s"',
274                    count($posts_ids)
275                ),
276                count($posts_ids),
277                html::escapeHTML($title->cat_title))
278            );
279
280            $ap->redirect(true);
281        } else {
282
283            $ap->beginPage(
284                dcPage::breadcrumb(
285                    array(
286                        html::escapeHTML($core->blog->name)      => '',
287                        $ap->getCallerTitle()                    => $ap->getRedirection(true),
288                        __('Change category for this selection') => ''
289                    )));
290            # categories list
291            # Getting categories
292            $categories_combo = dcAdminCombos::getCategoriesCombo(
293                $core->blog->getCategories()
294            );
295            echo
296            '<form action="' . $ap->getURI() . '" method="post">' .
297            $ap->getCheckboxes() .
298            '<p><label for="new_cat_id" class="classic">' . __('Category:') . '</label> ' .
299            form::combo(array('new_cat_id'), $categories_combo);
300
301            if ($core->auth->check('categories', $core->blog->id)) {
302                echo
303                '<div>' .
304                '<p id="new_cat">' . __('Create a new category for the post(s)') . '</p>' .
305                '<p><label for="new_cat_title">' . __('Title:') . '</label> ' .
306                form::field('new_cat_title', 30, 255, '', '') . '</p>' .
307                '<p><label for="new_cat_parent">' . __('Parent:') . '</label> ' .
308                form::combo('new_cat_parent', $categories_combo) .
309                    '</p>' .
310                    '</div>';
311            }
312
313            echo
314            $core->formNonce() .
315            $ap->getHiddenFields() .
316            form::hidden(array('action'), 'category') .
317            '<input type="submit" value="' . __('Save') . '" /></p>' .
318                '</form>';
319            $ap->endPage();
320
321        }
322
323    }
324    public static function doChangePostAuthor($core, dcPostsActionsPage $ap, $post)
325    {
326        if (isset($post['new_auth_id']) && $core->auth->check('admin', $core->blog->id)) {
327            $new_user_id = $post['new_auth_id'];
328            $posts_ids   = $ap->getIDs();
329            if (empty($posts_ids)) {
330                throw new Exception(__('No entry selected'));
331            }
332            if ($core->getUser($new_user_id)->isEmpty()) {
333                throw new Exception(__('This user does not exist'));
334            }
335
336            $cur          = $core->con->openCursor($core->prefix . 'post');
337            $cur->user_id = $new_user_id;
338            $cur->update('WHERE post_id ' . $core->con->in($posts_ids));
339            dcPage::addSuccessNotice(sprintf(
340                __(
341                    '%d entry has been successfully set to user "%s"',
342                    '%d entries have been successfully set to user "%s"',
343                    count($posts_ids)
344                ),
345                count($posts_ids),
346                html::escapeHTML($new_user_id))
347            );
348
349            $ap->redirect(true);
350        } else {
351            $usersList = '';
352            if ($core->auth->check('admin', $core->blog->id)) {
353                $params = array(
354                    'limit' => 100,
355                    'order' => 'nb_post DESC'
356                );
357                $rs       = $core->getUsers($params);
358                $rsStatic = $rs->toStatic();
359                $rsStatic->extend('rsExtUser');
360                $rsStatic = $rsStatic->toExtStatic();
361                $rsStatic->lexicalSort('user_id');
362                while ($rsStatic->fetch()) {
363                    $usersList .= ($usersList != '' ? ',' : '') . '"' . $rsStatic->user_id . '"';
364                }
365            }
366            $ap->beginPage(
367                dcPage::breadcrumb(
368                    array(
369                        html::escapeHTML($core->blog->name)    => '',
370                        $ap->getCallerTitle()                  => $ap->getRedirection(true),
371                        __('Change author for this selection') => '')),
372                dcPage::jsLoad('js/jquery/jquery.autocomplete.js') .
373                '<script type="text/javascript">' . "\n" .
374                'usersList = [' . $usersList . ']' . "\n" .
375                "</script>\n"
376            );
377
378            echo
379            '<form action="' . $ap->getURI() . '" method="post">' .
380            $ap->getCheckboxes() .
381            '<p><label for="new_auth_id" class="classic">' . __('New author (author ID):') . '</label> ' .
382            form::field('new_auth_id', 20, 255);
383
384            echo
385            $core->formNonce() . $ap->getHiddenFields() .
386            form::hidden(array('action'), 'author') .
387            '<input type="submit" value="' . __('Save') . '" /></p>' .
388                '</form>';
389            $ap->endPage();
390        }
391    }
392    public static function doChangePostLang($core, dcPostsActionsPage $ap, $post)
393    {
394        $posts_ids = $ap->getIDs();
395        if (empty($posts_ids)) {
396            throw new Exception(__('No entry selected'));
397        }
398        if (isset($post['new_lang'])) {
399            $new_lang       = $post['new_lang'];
400            $cur            = $core->con->openCursor($core->prefix . 'post');
401            $cur->post_lang = $new_lang;
402            $cur->update('WHERE post_id ' . $core->con->in($posts_ids));
403            dcPage::addSuccessNotice(sprintf(
404                __(
405                    '%d entry has been successfully set to language "%s"',
406                    '%d entries have been successfully set to language "%s"',
407                    count($posts_ids)
408                ),
409                count($posts_ids),
410                html::escapeHTML(l10n::getLanguageName($new_lang)))
411            );
412            $ap->redirect(true);
413        } else {
414            $ap->beginPage(
415                dcPage::breadcrumb(
416                    array(
417                        html::escapeHTML($core->blog->name)      => '',
418                        $ap->getCallerTitle()                    => $ap->getRedirection(true),
419                        __('Change language for this selection') => ''
420                    )));
421            # lang list
422            # Languages combo
423            $rs         = $core->blog->getLangs(array('order' => 'asc'));
424            $all_langs  = l10n::getISOcodes(0, 1);
425            $lang_combo = array('' => '', __('Most used') => array(), __('Available') => l10n::getISOcodes(1, 1));
426            while ($rs->fetch()) {
427                if (isset($all_langs[$rs->post_lang])) {
428                    $lang_combo[__('Most used')][$all_langs[$rs->post_lang]] = $rs->post_lang;
429                    unset($lang_combo[__('Available')][$all_langs[$rs->post_lang]]);
430                } else {
431                    $lang_combo[__('Most used')][$rs->post_lang] = $rs->post_lang;
432                }
433            }
434            unset($all_langs);
435            unset($rs);
436
437            echo
438            '<form action="' . $ap->getURI() . '" method="post">' .
439            $ap->getCheckboxes() .
440
441            '<p><label for="new_lang" class="classic">' . __('Entry language:') . '</label> ' .
442            form::combo('new_lang', $lang_combo);
443
444            echo
445            $core->formNonce() . $ap->getHiddenFields() .
446            form::hidden(array('action'), 'lang') .
447            '<input type="submit" value="' . __('Save') . '" /></p>' .
448                '</form>';
449            $ap->endPage();
450        }
451    }
452}
Note: See TracBrowser for help on using the repository browser.

Sites map