Migrate 4 builtin fields to fields2: priority, resolution, severity, status.
[bugdar.git] / editcomment.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2002-2007 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 $fetchtemplates = array(
23 'editcomment'
24 );
25
26
27 $focus['showreport'] = 'focus';
28
29 require_once('./global.php');
30 require_once('./includes/api_comment.php');
31
32 require_once('./includes/class_api_error.php');
33 APIError(array(new API_Error_Handler($message), 'user_cumulative'));
34
35 $commentapi = new CommentAPI($bugsys);
36 $commentapi->set('commentid', $bugsys->in['commentid']);
37 $commentapi->set_condition();
38 $commentapi->fetch();
39
40 $comment =& $commentapi->objdata;
41
42 $bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $comment[bugid]");
43
44 if (!check_bug_permissions($bug))
45 {
46 $message->errorPermission();
47 }
48
49 // ###################################################################
50
51 if (empty($_REQUEST['do']))
52 {
53 $_REQUEST['do'] = 'edit';
54 }
55
56 // ###################################################################
57
58 if ($_POST['do'] == 'kill')
59 {
60 if (!can_perform('candeletedata', $bug['product']))
61 {
62 $message->errorPermission();
63 }
64
65 // get the last post (as in prior to deletion)
66 $lastpost = $db->query_first("SELECT commentid FROM " . TABLE_PREFIX . "comment WHERE bugid = $bug[bugid] ORDER BY dateline DESC");
67
68 $commentapi->delete();
69
70 // check to see if we need to rebuild the lastpost information
71 if ($lastpost['commentid'] == $comment['commentid'])
72 {
73 $lastgoodpublic = $db->query_first("
74 SELECT comment.* AS comment, user.displayname AS username
75 FROM " . TABLE_PREFIX . "comment AS comment
76 LEFT JOIN " . TABLE_PREFIX . "user AS user
77 ON (user.userid = comment.userid)
78 WHERE bugid = $bug[bugid]
79 AND !hidden ORDER BY dateline DESC
80 ");
81 $lastgoodprivate = $db->query_first("
82 SELECT comment.* AS comment, user.displayname AS username
83 FROM " . TABLE_PREFIX . "comment AS comment
84 LEFT JOIN " . TABLE_PREFIX . "user AS user
85 ON (user.userid = comment.userid)
86 WHERE bugid = $bug[bugid]
87 ORDER BY dateline DESC
88 ");
89 $db->query("
90 UPDATE " . TABLE_PREFIX . "bug
91 SET hiddenlastposttime = $lastgoodpublic[dateline],
92 hiddenlastpostby = $lastgoodpublic[userid],
93 hiddenlastpostbyname = '" . $db->escape_string($lastgoodpublic['username']) . "',
94 lastposttime = $lastgoodprivate[dateline],
95 lastpostby = $lastgoodprivate[userid],
96 lastpostbyname = '" . $db->escape_string($lastgoodprivate['username']) . "'
97 WHERE bugid = $bug[bugid]"
98 );
99 }
100
101 $message->redirect(T('The comment has been deleted. You will be redirected back to the bug.'), 'showreport.php?bugid=' . $bug['bugid']);
102 }
103
104 // ###################################################################
105
106 if ($_REQUEST['do'] == 'delete')
107 {
108 if (!can_perform('candeletedata', $bug['product']))
109 {
110 $message->errorPermission();
111 }
112
113 if ($bug['initialreport'] == $comment['commentid'])
114 {
115 $message->error(T('You cannot delete this comment because it is attached to the bug as the first comment. You have to delete the entire bug instead (which is not recommended unless it is spam).'));
116 }
117
118 $message->confirm(T('Are you sure you want to delete this comment? After you do so, the data <strong>will</strong> be lost forever. We recommend only deleting spam comments and nothing else.'), 'editcomment.php', 'kill', T('Delete Comment'), 'showreport.php?bugid=' . $bug['bugid'], array('commentid' => $comment['commentid']));
119 }
120
121 // ###################################################################
122
123 if ($_POST['do'] == 'update')
124 {
125 if (!((can_perform('caneditownreply', $bug['product']) AND $bugsys->userinfo['userid'] == $comment['userid']) OR (can_perform('caneditotherreply', $bug['product']) AND $bugsys->userinfo['userid'] != $comment['userid'])))
126 {
127 $message->errorPermission();
128 }
129
130 $commentapi->set('comment', $bugsys->in['comment']);
131 $commentapi->set('parselinks', $bugsys->in['parselinks']);
132 $commentapi->set('hidden', $bugsys->in['hidden']);
133
134 if ($bugsys->in['commentid'] == $bug['initialreport'] AND $bugsys->in['hidden'])
135 {
136 $message->addError(T('You cannot hide the first comment/initial report of a bug. Instead, hide the entire bug.'));
137 }
138
139 if (!$message->hasErrors())
140 {
141 $commentapi->update();
142
143 // setup logging
144 require_once('./includes/class_logging.php');
145 $log = new Logging;
146 $log->set_bugid($bug['bugid']);
147 $log->set_commentid($comment['commentid']);
148
149 $log->add_data(true, $commentapi->objdata, array('comment', 'hidden'), false, 'comment');
150 $log->add_data(false, $commentapi->values, array('comment', 'hidden'), false, 'comment');
151
152 $lastgood = $db->query_first("
153 SELECT comment.* AS comment, user.displayname AS username
154 FROM " . TABLE_PREFIX . "comment AS comment
155 LEFT JOIN " . TABLE_PREFIX . "user AS user
156 ON (user.userid = comment.userid)
157 WHERE bugid = $bug[bugid]
158 AND !hidden ORDER BY dateline DESC
159 ");
160 $db->query("
161 UPDATE " . TABLE_PREFIX . "bug
162 SET hiddenlastposttime = $lastgood[dateline],
163 hiddenlastpostby = $lastgood[userid],
164 hiddenlastpostbyname = '" . $db->escape_string($lastgood['username']) . "'
165 WHERE bugid = $bug[bugid]"
166 );
167
168 $log->update_history();
169
170 $message->redirect(T('The comment was modified successfully.'), "showreport.php?bugid=$bug[bugid]");
171 }
172 else
173 {
174 $show['errors'] = true;
175 $_REQUEST['do'] = 'edit';
176 $comment['comment'] = $bugsys->in['comment'];
177 $comment['hidden'] = $bugsys->in['hidden'];
178 }
179 }
180
181 // ###################################################################
182
183 if ($_REQUEST['do'] == 'edit')
184 {
185 if (!((can_perform('caneditownreply', $bug['product']) AND $bugsys->userinfo['userid'] == $comment['userid']) OR (can_perform('caneditotherreply', $bug['product']) AND $bugsys->userinfo['userid'] != $comment['userid'])))
186 {
187 $message->errorPermission();
188 }
189
190 $comment['posttime'] = $datef->format($bugsys->options['dateformat'], $comment['dateline']);
191 if ($comment['userid'])
192 {
193 $commenter = new UserAPI($bugsys);
194 $commenter->set('userid', $comment['userid']);
195 $commenter->set_condition();
196 $commenter->fetch();
197 $commenter = $commenter->objdata;
198 }
199 $comment['postby'] = construct_user_display($commenter);
200 $comment['comment'] = $bugsys->sanitize($comment['comment']);
201 $show['hide'] = ($bug['initialreport'] != $comment['commentid'] ? true : false);
202 eval('$template->flush("' . $template->fetch('editcomment') . '");');
203 }
204