r803: Implementing BugAPI
[bugdar.git] / editreport.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 [#]gpl[#] 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 define('SVN', '$Id$');
23
24 $focus['showreport'] = 'focus';
25
26 require_once('./global.php');
27 require_once('./includes/functions_product.php');
28 require_once('./includes/api_bug.php');
29 require_once('./includes/api_comment.php');
30
31 $bug = $db->query_first("
32 SELECT bugvaluefill.*, bug.*, user.email, user.displayname, user.showemail
33 FROM " . TABLE_PREFIX . "bug AS bug
34 LEFT JOIN " . TABLE_PREFIX . "user AS user
35 ON (bug.userid = user.userid)
36 LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugvaluefill
37 ON (bug.bugid = bugvaluefill.bugid)
38 WHERE bug.bugid = " . intval($bugsys->in['bugid'])
39 );
40
41 if (!((can_perform('caneditown', $bug['productid']) AND $bugsys->userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['productid']) AND $bugsys->userinfo['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['productid']))
42 {
43 $message->error_permission();
44 }
45
46 if (!$bug)
47 {
48 $message->error($lang->getlex('error_invalid_id'));
49 }
50
51 if ($bug['hidden'] AND !can_perform('canviewhidden', $bug['productid']))
52 {
53 $message->error_permission();
54 }
55
56 // setup logging
57 require_once('./includes/class_history.php');
58 $log = new History();
59 $log->bugid = $bug['bugid'];
60
61 // ###################################################################
62
63 if ($_POST['do'] == 'update')
64 {
65 // -------------------------------------------------------------------
66 // process comment stuff
67 if ($bugsys->in['comment'] AND !((can_perform('caneditown', $bug['productid']) AND $bugsys->userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['productid']) AND $bugsys->userinfo['userid'] != $bug['userid'])))
68 {
69 $comment = new CommentAPI($bugsys);
70 $comment->set('bugid', $bugsys->in['bugid']);
71 $comment->set('userid', $bugsys->userinfo['userid']);
72 $comment->set('comment', $_POST['comment']);
73 $comment->insert();
74
75 $time = $comment->values['dateline'];
76 $commentid = $comment->insertid;
77
78 $db->query("
79 UPDATE " . TABLE_PREFIX . "bug
80 SET lastposttime = $time,
81 lastpostby = " . $bugsys->userinfo['userid'] . ",
82 hiddenlastposttime = $time,
83 hiddenlastpostby = " . $bugsys->userinfo['userid'] . "
84 WHERE bugid = " . intval($bugsys->in['bugid'])
85 );
86
87 if ($hascomment)
88 {
89 $log->arguments = array($commentid);
90 $log->log();
91 }
92
93 $message->redirect($lang->string('Your reply has been added to the comment list.'), "showreport.php?bugid=$bug[bugid]");
94 }
95
96 // -------------------------------------------------------------------
97 // do update stuff
98 $pcv = parse_pcv_select($bugsys->in['pcv_select'], true);
99
100 $bugapi = new BugAPI($bugsys);
101 $bugapi->set('bugid', $bugsys->in['bugid']);
102 $bugapi->set_condition();
103 $bugapi->dorelations = array();
104 $bugapi->fetch();
105
106 $hist2[0] = (array)$temp = $noinitialcustom = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = $bug[bugid]");
107
108 // -------------------------------------------------------------------
109 // start updates
110
111 // auto action
112 $autoaction = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "autoaction WHERE actionid = " . intval($bugsys->in['autoaction']));
113 if ($autoaction)
114 {
115 $autoaction['fields'] = unserialize($autoaction['fieldchanges']);
116
117 foreach ($autoaction['fields']['builtin'] AS $field => $value)
118 {
119 $bugsys->in["$field"] = $value;
120 }
121
122 foreach ($autoaction['fields']['custom'] AS $field => $value)
123 {
124 $bugsys->in["field$field"] = $value;
125 }
126 }
127
128 process_custom_fields($bug['bugid']);
129
130 $dependencies = preg_split('#([^0-9].*?)#', $bugsys->in['dependency'], -1, PREG_SPLIT_NO_EMPTY);
131 $dependencies = ((count($dependencies) < 1) ? '' : implode(', ', $dependencies));
132
133 $bugapi->set('summary', $bugsys->in['summary']);
134 $bugapi->set('severity', $bugsys->in['severity']);
135 $bugapi->set('duplicateof', $bugsys->in['duplicateof']);
136 $bugapi->set('dependency', $dependencies);
137 $bugapi->set('productid', $pcv['product']);
138 $bugapi->set('componentid', $pcv['component']);
139 $bugapi->set('versionid', $pcv['version']);
140 $bugapi->set('hidden', $bugsys->in['hidden']);
141
142 if (can_perform('canchangestatus', $bug['productid']))
143 {
144 $bugapi->set('priority', $bugsys->in['priority']);
145 $bugapi->set('status', $bugsys->in['status']);
146 $bugapi->set('resolution', $bugsys->in['resolution']);
147 }
148 if (can_perform('canassign', $bug['productid']))
149 {
150 $bugapi->set('assignedto', $bugsys->in['assignedto']);
151 }
152
153 $bugapi->update();
154
155 // -------------------------------------------------------------------
156 // do diff history
157
158 $hist2[1] = (array)$db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = $bug[bugid]");
159
160 $diff[0] = array_diff_assoc($bugapi->objdata, $bugapi->values);
161 $diff[1] = array_diff_assoc($bugapi->values, $bugapi->objdata);
162
163 $lookupindex = array(
164 'status' => 'status',
165 'severity' => 'severity',
166 'priority' => 'priority',
167 'versionid' => 'version',
168 'assignedto' => 'assignto',
169 'resolution' => 'resolution',
170 'productid' => 'product',
171 'componentid' => 'product'
172 );
173
174 foreach ($diff AS $num => $diffs)
175 {
176 foreach ($diffs AS $key => $value)
177 {
178 if (!isset($lookupindex["$key"]))
179 {
180 continue;
181 }
182
183 $ref = $lookupindex["$key"];
184 $temp =& $bugsys->datastore["$ref"]["$value"];
185 $thevalue = $temp["$ref"];
186 $idbit = ' (id: ' . $temp["$ref" . 'id'] . ')';
187
188 if ($key == 'assignedto')
189 {
190 $thevalue = (($temp['userid']) ? construct_user_display($temp) : '');
191 $idbit = '';
192 }
193 else if ($key == 'productid' OR $key == 'componentid')
194 {
195 $ref = 'product';
196 $thevalue = $temp['title'];
197 }
198
199 $diff["$num"]["$key"] = (($thevalue) ? $thevalue . $idbit : '');
200 }
201 }
202
203 foreach ($diff[1] AS $key => $value)
204 {
205 $log->log($log->diff($key, $diff[0]["$key"], $diff[1]["$key"]));
206 }
207
208 $diff2[0] = array_diff_assoc($hist2[0], $hist2[1]);
209 $diff2[1] = array_diff_assoc($hist2[1], $hist2[0]);
210
211 if ($noinitialcustom === false)
212 {
213 $canallowempty = true;
214 $checkbox = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE type = 'input_checkbox'");
215 while ($box = $db->fetch_array($checkbox))
216 {
217 $boxlist[] = 'field' . $box['fieldid'];
218 }
219 }
220
221 foreach ($diff2[1] AS $key => $value)
222 {
223 if (in_array($key, $boxlist) AND $canallowempty)
224 {
225 $log->allowempty = true;
226 }
227 else
228 {
229 $log->allowempty = false;
230 }
231
232 if ($key == 'bugid')
233 {
234 continue;
235 }
236
237 $log->log($log->diff('custom_' . $key, $diff2[0]["$key"], $diff2[1]["$key"]));
238 }
239
240 $log->allowempty = false;
241
242 // -------------------------------------------------------------------
243 // more comment
244 $hascomment = (!empty($bugsys->in['comment'])) ? true : false;
245
246 if ($hascomment OR $autoaction['comment'])
247 {
248 if ($hascomment AND $autoaction['comment'])
249 {
250 $bugsys->in['comment'] .= "\n\n" . $lang->string('--------------- AUTOMATIC RESPONSE ---------------') . "\n" . $autoaction['comment'];
251 }
252 else if (!$hascomment AND $autoaction['comment'])
253 {
254 $bugsys->in['comment'] = $autoaction['comment'];
255 }
256
257 $comment = new CommentAPI($bugsys);
258 $comment->set('bugid', $bugsys->in['bugid']);
259 $comment->set('userid', $bugsys->userinfo['userid']);
260 $comment->set('comment', $bugsys->in['comment']);
261 $comment->insert();
262
263 $time = $comment->values['dateline'];
264 $commentid = $comment->insertid;
265
266 $db->query("
267 UPDATE " . TABLE_PREFIX . "bug
268 SET lastposttime = $time,
269 lastpostby = " . $bugsys->userinfo['userid'] . ",
270 hiddenlastposttime = $time,
271 hiddenlastpostby = " . $bugsys->userinfo['userid'] . "
272 WHERE bugid = " . intval($bugsys->in['bugid'])
273 );
274 }
275
276
277 $message->redirect($lang->string('Your changes to the bug have been saved.'), "showreport.php?bugid=$bug[bugid]");
278 }
279
280 /*=====================================================================*\
281 || ###################################################################
282 || # $HeadURL$
283 || # $Id$
284 || ###################################################################
285 \*=====================================================================*/
286 ?>