Dotclear

source: inc/admin/actions/class.dcactioncomments.php @ 3707:3a350757c847

Revision 3707:3a350757c847, 7.8 KB checked in by franck <carnet.franck.paul@…>, 8 years ago (diff)

Use array form of optionnal parameters for form::checkbox() where is relevant, 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 dcCommentsActionsPage 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('type', 'author', 'status',
20            'sortby', 'ip', 'order', 'page', 'nb', 'section');
21        $this->field_entries = 'comments';
22        $this->title_cb      = __('Comments');
23        $this->loadDefaults();
24        $core->callBehavior('adminCommentsActionsPage', $core, $this);
25    }
26
27    protected function loadDefaults()
28    {
29        // We could have added a behavior here, but we want default action
30        // to be setup first
31        dcDefaultCommentActions::adminCommentsActionsPage($this->core, $this);
32    }
33
34    public function beginPage($breadcrumb = '', $head = '')
35    {
36        if ($this->in_plugin) {
37            echo '<html><head><title>' . __('Comments') . '</title>' .
38            dcPage::jsLoad('js/_comments_actions.js') .
39                $head .
40                '</script></head><body>' .
41                $breadcrumb;
42        } else {
43            dcPage::open(
44                __('Comments'),
45                dcPage::jsLoad('js/_comments_actions.js') .
46                $head,
47                $breadcrumb
48            );
49
50        }
51        echo '<p><a class="back" href="' . $this->getRedirection(true) . '">' . __('Back to comments list') . '</a></p>';
52    }
53
54    public function endPage()
55    {
56        dcPage::close();
57    }
58
59    public function error(Exception $e)
60    {
61        $this->core->error->add($e->getMessage());
62        $this->beginPage(dcPage::breadcrumb(
63            array(
64                html::escapeHTML($this->core->blog->name) => '',
65                __('Comments')                            => $this->core->adminurl->get('admin.comments'),
66                __('Comments actions')                    => ''
67            ))
68        );
69        $this->endPage();
70    }
71
72    /**
73     * getcheckboxes -returns html code for selected entries
74     *             as a table containing entries checkboxes
75     *
76     * @access public
77     *
78     * @return string the html code for checkboxes
79     */
80    public function getCheckboxes()
81    {
82        $ret =
83        '<table class="posts-list"><tr>' .
84        '<th colspan="2">' . __('Author') . '</th><th>' . __('Title') . '</th>' .
85            '</tr>';
86        foreach ($this->entries as $id => $title) {
87            $ret .=
88            '<tr><td class="minimal">' .
89            form::checkbox(array($this->field_entries . '[]'), $id,
90                array(
91                    'checked' => true
92                )) .
93            '</td>' .
94            '<td>' . $title['author'] . '</td><td>' . $title['title'] . '</td></tr>';
95        }
96        $ret .= '</table>';
97        return $ret;
98    }
99
100    protected function fetchEntries($from)
101    {
102        $params = array();
103        if (!empty($from['comments'])) {
104            $comments = $from['comments'];
105
106            foreach ($comments as $k => $v) {
107                $comments[$k] = (integer) $v;
108            }
109
110            $params['sql'] = 'AND C.comment_id IN(' . implode(',', $comments) . ') ';
111        } else {
112            $params['sql'] = 'AND 1=0 ';
113        }
114
115        if (!isset($from['full_content']) || empty($from['full_content'])) {
116            $params['no_content'] = true;
117        }
118        $co = $this->core->blog->getComments($params);
119        while ($co->fetch()) {
120            $this->entries[$co->comment_id] = array(
121                'title'  => $co->post_title,
122                'author' => $co->comment_author
123            );
124        }
125        $this->rs = $co;
126    }
127}
128
129class dcDefaultCommentActions
130{
131    public static function adminCommentsActionsPage($core, dcCommentsActionsPage $ap)
132    {
133        if ($core->auth->check('publish,contentadmin', $core->blog->id)) {
134            $ap->addAction(
135                array(__('Status') => array(
136                    __('Publish')         => 'publish',
137                    __('Unpublish')       => 'unpublish',
138                    __('Mark as pending') => 'pending',
139                    __('Mark as junk')    => 'junk'
140                )),
141                array('dcDefaultCommentActions', 'doChangeCommentStatus')
142            );
143        }
144
145        if ($core->auth->check('delete,contentadmin', $core->blog->id)) {
146            $ap->addAction(
147                array(__('Delete') => array(
148                    __('Delete') => 'delete')),
149                array('dcDefaultCommentActions', 'doDeleteComment')
150            );
151        }
152
153        $ip_filter_active = true;
154        if ($core->blog->settings->antispam->antispam_filters !== null) {
155            $filters_opt = $core->blog->settings->antispam->antispam_filters;
156            if (is_array($filters_opt)) {
157                $ip_filter_active = isset($filters_opt['dcFilterIP']) && is_array($filters_opt['dcFilterIP']) && $filters_opt['dcFilterIP'][0] == 1;
158            }
159        }
160
161        if ($ip_filter_active) {
162            $blacklist_actions = array(__('Blacklist IP') => 'blacklist');
163            if ($core->auth->isSuperAdmin()) {
164                $blacklist_actions[__('Blacklist IP (global)')] = 'blacklist_global';
165            }
166
167            $ap->addAction(
168                array(__('IP address') => $blacklist_actions),
169                array('dcDefaultCommentActions', 'doBlacklistIP')
170            );
171        }
172    }
173
174    public static function doChangeCommentStatus($core, dcCommentsActionsPage $ap, $post)
175    {
176        $action = $ap->getAction();
177        $co_ids = $ap->getIDs();
178        if (empty($co_ids)) {
179            throw new Exception(__('No comment selected'));
180        }
181        switch ($action) {
182            case 'unpublish':$status = 0;
183                break;
184            case 'pending':$status = -1;
185                break;
186            case 'junk':$status = -2;
187                break;
188            default:$status = 1;
189                break;
190        }
191
192        $core->blog->updCommentsStatus($co_ids, $status);
193
194        dcPage::addSuccessNotice(__('Selected comments have been successfully updated.'));
195        $ap->redirect(true);
196    }
197
198    public static function doDeleteComment($core, dcCommentsActionsPage $ap, $post)
199    {
200        $co_ids = $ap->getIDs();
201        if (empty($co_ids)) {
202            throw new Exception(__('No comment selected'));
203        }
204        // Backward compatibility
205        foreach ($co_ids as $comment_id) {
206            # --BEHAVIOR-- adminBeforeCommentDelete
207            $core->callBehavior('adminBeforeCommentDelete', $comment_id);
208        }
209
210        # --BEHAVIOR-- adminBeforeCommentsDelete
211        $core->callBehavior('adminBeforeCommentsDelete', $co_ids);
212
213        $core->blog->delComments($co_ids);
214        dcPage::addSuccessNotice(__('Selected comments have been successfully deleted.'));
215        $ap->redirect(false);
216    }
217
218    public static function doBlacklistIP($core, dcCommentsActionsPage $ap, $post)
219    {
220        $action = $ap->getAction();
221        $co_ids = $ap->getIDs();
222        if (empty($co_ids)) {
223            throw new Exception(__('No comment selected'));
224        }
225
226        $global = !empty($action) && $action == 'blacklist_global' && $core->auth->isSuperAdmin();
227
228        $ip_filter = new dcFilterIP($core);
229        $rs        = $ap->getRS();
230        while ($rs->fetch()) {
231            $ip_filter->addIP('black', $rs->comment_ip, $global);
232        }
233
234        dcPage::addSuccessNotice(__('IP addresses for selected comments have been blacklisted.'));
235        $ap->redirect(true);
236    }
237}
Note: See TracBrowser for help on using the repository browser.

Sites map