source: extensions/CommentEditor/classes/ce_plugin.class.php @ 3426

Last change on this file since 3426 was 3426, checked in by Criss, 15 years ago

CommentEditor plugin creation

  • Property svn:eol-style set to LF
File size: 8.6 KB
Line 
1<?php
2/* $Id: ce_plugin.class.php,v 1.3 2009/06/17 19:08:25 Criss Exp $ */
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5ce_require_class("CE_Comment");
6
7/**
8 * CE_Plugin class
9 */
10class CE_Plugin {
11
12    /* ************************ */
13    /* ** Trigger management ** */
14    /* ************************ */
15
16    /**
17     * Check whether the page contains comments.
18     * Add an edit link if user is allowed to edit comment
19     */
20    function loc_begin_page_header() {
21        global $template;
22        $all_comments = &$template->get_template_vars('comments');
23        for ($i=0; $i < count($all_comments) ; $i++) {
24            $comment = &$all_comments[$i];
25            if (isset($comment['U_DELETE'])) {
26                $comment['U_ID'] =  $this->getCommentId($comment['U_DELETE']);
27
28                $comment['DATE'] .= ' - <a href="' . $this->getEditUrlPrefix() . $comment['U_ID'] . '">';
29                $comment['DATE'] .= 'Edit';
30                $comment['DATE'] .= '</a>';
31            }
32        }
33    }
34
35    /**
36     * Check whether a comment edit or update is requested.
37     */
38    function loc_end_index() {
39        $comment_id = "";
40        $action = $this->isCommentEdit($_SERVER['QUERY_STRING'], $comment_id);
41        switch ($action) {
42            case CE_ACTION_EDIT:
43                // Edit
44                $this->editComment($comment_id);
45                break;
46            case CE_ACTION_UPDATE:
47                $this->updateComment();
48                break;
49            case CE_ACTION_NONE:
50                // Not an edit mode
51                break;
52        }
53    }
54
55    /* ************************ */
56    /* ** Private functions  ** */
57    /* ************************ */
58
59    function getTemplate($p_template = 'edit.tpl') {
60        return realpath(CE_TEMPLATE . $p_template);
61    }
62
63    function isCommentEdit($p_query_string, &$p_comment_id) {
64        $params = split('\&', $p_query_string);
65        $action = CE_ACTION_NONE;
66        $comment_id = "";
67        foreach ($params as $current_param) {
68            $key_value = split('=', $current_param);
69            if (isset($key_value[1])) {
70                if (CE_ACTION == $key_value[0] && CE_ACTION_NONE == $action) {
71                    //CE_ACTION_EDIT == $lKeyVal[1]
72                    switch ($key_value[1]) {
73                        case CE_ACTION_EDIT:
74                        case CE_ACTION_UPDATE:
75                            $action = $key_value[1];
76                            break;
77                    }
78                }
79                if (CE_ID == $key_value[0]) {
80                    $comment_id = $key_value[1];
81                }
82            }
83        }
84        if (CE_ACTION_EDIT == $action) {
85            $p_comment_id = $comment_id;
86        }
87        return $action;
88    }
89
90    function isEditAllowed($p_comment) {
91        if (is_admin()) {
92            return true;
93        }
94        global $user;
95        return $p_comment->isAuthor($user['username']);
96    }
97
98    function editComment($p_comment_id) {
99        //  Include language advices
100        load_language('plugin.lang', CE_PATH);
101
102        $comment = new CE_Comment($p_comment_id);
103        $infos = array();
104        if (!$this->isEditAllowed($comment)) {
105            // Not allowed
106            array_push($infos, l10n('comment_edit_forbidden'));
107            $this->displayError($infos);
108            return;
109        }
110        $this->displayComment($comment);
111    }
112
113    function updateComment() {
114        //  Include language advices
115        load_language('plugin.lang', CE_PATH);
116
117        $infos = array();
118        if (!isset($_POST['commentid'])) {
119            array_push($infos, l10n('comment_access_invalid'));
120            $this->displayError($infos);
121            return;
122        }
123        $comment = new CE_Comment($_POST['commentid']);
124
125        if (!$this->isEditAllowed($comment)) {
126            array_push($infos, l10n('comment_edit_forbidden'));
127            $this->displayError($infos);
128            return;
129        }
130        if (isset($_POST['author'])) {
131            $comment->setInfo('author', $_POST['author']);
132        } else if (isset($_POST['freeauthorck'])) {
133            // Free author
134            $new_author = $comment->validateAuthor(trim(stripslashes($_POST['freeauthor'])));
135            $comment->setInfo('author', $new_author);
136        }
137
138        $comment_array = array(
139            'author'        => trim( stripslashes($comment->getInfo('author')) ),
140            'content'       => trim( stripslashes($_POST['content']) ),
141            'image_id'      => $_POST['imageid'],
142            'comment_id'    => $_POST['commentid'],
143        );
144        $comment_action = update_user_comment($comment_array, @$_POST['key'], $infos );
145
146        switch ($comment_action) {
147            case 'moderate':
148              array_push( $infos, l10n('comment_to_validate') );
149            case 'validate':
150              array_push( $infos, l10n('comment_added'));
151              break;
152            case 'reject':
153              set_status_header(403);
154              array_push($infos, l10n('comment_not_added') );
155              break;
156            default:
157              trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
158        }
159        // allow plugins to notify what's going on
160        trigger_action( 'user_comment_insertion', array_merge($comment_array, array('action'=>$comment_action)));
161        if ($comment_action=='reject') {
162            $this->displayError($infos);
163        } else {
164            $this->displayInfo($infos);
165        }
166    }
167
168    function getCommentId($p_string_delete) {
169        $comment_id = "";
170        $elements = split('\?', $p_string_delete);
171        if (count($elements) > 1) {
172            $elem_var = split('\&amp;', $elements[1]);
173            foreach ($elem_var as $var) {
174                $key_value = split('=', $var);
175                if (count($key_value) > 1) {
176                    switch ($key_value[0]) {
177                        case 'comment_to_delete':
178                        case 'delete':
179                            $comment_id = $key_value[1];
180                            break;
181                        default:
182                            // No ID
183                    }
184                }
185            }
186        }
187        return $comment_id;
188    }
189
190    function displayError($p_error) {
191        $this->displayMessage($p_error, CE_TYPE_ERROR, CE_ERRORS);
192    }
193
194    function displayInfo($p_info) {
195        $this->displayMessage($p_info, CE_TYPE_INFO, CE_INFOS);
196    }
197
198    function displayMessage($p_message, $p_type, $p_image) {
199        //  Include language advices
200        load_language('plugin.lang', CE_PATH);
201
202        $messages = array();
203        foreach ($p_message as $message) {
204            array_push($messages, $message);
205        }
206        $message_array = array(
207            'TYPE'  => $p_type,
208            'IMG'   => $p_image,
209            'TITLE' => l10n($p_type),
210            'MSG'   => $messages
211        );
212
213        global $template;
214        $template->set_filenames(array('ce_message'=>$this->getTemplate('message.tpl')));
215        $template->block_html_head('', '<link rel="stylesheet" type="text/css" href="'.CE_STYLE.'">', $smarty, $repeat);
216        $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
217        $template->assign('message', $message_array);
218        $old_begin = $template->get_template_vars($begin);
219        $template->assign($begin, $template->parse('ce_message', true));
220        $template->concat($begin, $old_begin);
221    }
222
223    function displayComment($p_comment) {
224        global $template;
225
226        $template->set_filenames(array('ce_edit'=>$this->getTemplate()));
227        $template->block_html_head('', '<link rel="stylesheet" type="text/css" href="'.CE_STYLE.'">', $smarty, $repeat);
228        $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
229        $end = 'PLUGIN_INDEX_CONTENT_AFTER';
230        $template->assign($begin, null);
231        $template->assign($end, null);
232        $tplComment = array(
233            'CONTENT'   => $p_comment->getInfo('content'),
234            'KEY'       => $p_comment->getInfo('comment_id'),
235            'AUTHOR'    => $p_comment->getInfo('author'),
236            'IMAGE'     => $p_comment->getInfo('image_id'),
237            'DISABLED'  => !is_admin(),
238            'USERS'     => CE_Comment::getUsers(),
239            'F_ACTION'  => $this->getUpdateUrlPrefix(),
240            'FREEAUTHOR'=> ($p_comment->isKnownAuthor())?'':'checked="checked"'
241        );
242        $template->assign('comment', $tplComment);
243        $template->concat($begin, $template->parse('ce_edit', true));
244    }
245
246    function getEditUrlPrefix() {
247        return get_root_url() . 'index.php?' . CE_ACTION . '=' . CE_ACTION_EDIT . '&' . CE_ID . '=';
248    }
249
250    function getUpdateUrlPrefix() {
251        return get_root_url() . 'index.php?' . CE_ACTION . '=' . CE_ACTION_UPDATE;
252    }
253
254}
255?>
Note: See TracBrowser for help on using the repository browser.