Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / showreport.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright (c)2004-2009 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 'showreport',
24 'showreport_attachment',
25 'showreport_comment',
26 'quicksearch',
27 'bugfield_static_text'
28 );
29
30
31 $focus['showreport'] = 'focus';
32
33 require_once('./global.php');
34 require_once('./includes/functions_product.php');
35
36 $bugid = $input->inputClean('bugid', TYPE_UINT);
37
38 // ###################################################################
39
40 if (empty($bugid) OR $_REQUEST['do'] == 'quicksearch')
41 {
42 if (!empty($bugid))
43 {
44 if ($db->queryFirst("SELECT bugid FROM " . TABLE_PREFIX . "bug WHERE bugid = $bugid"))
45 {
46 header("Location: showreport.php?bugid=$bugid");
47 exit;
48 }
49 else
50 {
51 $error = L_INVALID_ID;
52 }
53 }
54
55 $tpl = new BSTemplate('quicksearch');
56 $tpl->vars = array('error' => $error);
57 $tpl->evaluate()->flush();
58 exit;
59 }
60
61 // ###################################################################
62
63 // -------------------------------------------------------------------
64 // get the report
65 $bug = $db->queryFirst("
66 SELECT bug.*, user.email, user.displayname, user.showemail
67 FROM " . TABLE_PREFIX . "bug AS bug
68 LEFT JOIN " . TABLE_PREFIX . "user AS user
69 ON (bug.userid = user.userid)
70 WHERE bug.bugid = $bugid"
71 );
72
73 if (!is_array($bug))
74 {
75 $message->error(L_INVALID_ID);
76 }
77
78 if (!check_bug_permissions($bug))
79 {
80 $message->errorPermission();
81 }
82
83 $show['edit'] = ((can_perform('caneditown', $bug['product']) AND bugdar::$userinfo['userid'] == $bug['userid'] AND $bug['userid'] != 0) OR (can_perform('caneditother', $bug['product']) AND bugdar::$userinfo['userid'] != $bug['userid']));
84 $show['delete'] = can_perform('candeletedata', $bug['product']);
85
86 // ###################################################################
87 // edit display
88 if ($show['edit'])
89 {
90 $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', $bug['severity']);
91
92 $show['changestatus'] = (can_perform('canchangestatus', $bug['product']) ? true : false);
93 if (can_perform('canchangestatus', $bug['product']))
94 {
95 $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', $bug['priority']);
96 $select['status'] = construct_datastore_select('status', 'status', 'statusid', $bug['status']);
97 $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', $bug['resolution']);
98 }
99 else
100 {
101 $bug['status'] = bugdar::$datastore['status']["$bug[status]"]['status'];
102 $bug['resolution'] = bugdar::$datastore['resolution']["$bug[resolution]"]['resolution'];
103 $bug['severity'] = bugdar::$datastore['severity']["$bug[severity]"]['severity'];
104 $bug['priority'] = bugdar::$datastore['priority']["$bug[priority]"]['priority'];
105 }
106
107 $show['assign'] = (can_perform('canassign', $bug['product']));
108 if (can_perform('canassign', $bug['product']) && is_array(bugdar::$datastore['assignto']))
109 {
110 foreach (bugdar::$datastore['assignto'] as $dev)
111 {
112 $tpl = new BSTemplate('selectoption');
113 $tpl->vars = array(
114 'value' => $dev['userid'],
115 'label' => construct_user_display($dev, false),
116 'selected' => ($dev['userid'] == $bug['assignedto'])
117 );
118 $select['dev'] .= $tpl->evaluate()->getTemplate();
119 }
120 }
121
122 $productSelect = construct_product_select('canviewbugs', "$bug[product],$bug[component],$bug[version]");
123
124 if ($bug['duplicateof'])
125 {
126 $duplicate = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
127 }
128
129 if ($bug['dependency'])
130 {
131 $depends = array();
132 $dependencies = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($bug[dependency])");
133 foreach ($dependencies as $dependency)
134 {
135 $depends[] = "<a href=\"showreport.php?bugid=$dependency[bugid]\" title=\"$dependency[summary]\">$dependency[bugid]</a>";
136 }
137 $dependencies = implode(' ', $depends);
138 }
139
140 $select['automations'] = '';
141 $show['automations'] = false;
142 if (is_array(bugdar::$datastore['automation']))
143 {
144 foreach (bugdar::$datastore['automation'] as $action)
145 {
146 $tpl = new BSTemplate('selectoption');
147 $tpl->vars = array(
148 'label' => $action['name'],
149 'value' => $action['actionid'],
150 'selected' => false
151 );
152 $select['automation'] .= $tpl->evaluate()->getTemplate();
153 $show['automations'] = true;
154 }
155 if ($show['automations'])
156 {
157 $tpl = new BSTemplate('selectoption');
158 $tpl->vars = array(
159 'label' => '',
160 'value' => 0,
161 'selected' => true
162 );
163 $select['automation'] = $tpl->evaluate()->getTemplate() . $select['automation'];
164 }
165 }
166 }
167
168 // ###################################################################
169 // non-edit display
170 else
171 {
172 // -------------------------------------------------------------------
173 // prep display
174 $bug['status'] = bugdar::$datastore['status']["$bug[status]"]['status'];
175 $bug['resolution'] = bugdar::$datastore['resolution']["$bug[resolution]"]['resolution'];
176 $bug['severity'] = bugdar::$datastore['severity']["$bug[severity]"]['severity'];
177 $bug['priority'] = bugdar::$datastore['priority']["$bug[priority]"]['priority'];
178
179 $assigninfo = bugdar::$datastore['assignto']["$bug[assignedto]"];
180 $bug['assigninfo'] = ((is_array($assigninfo)) ? construct_user_display($assigninfo) : '');
181
182 $duplicateof = $db->queryFirst("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
183
184 $dupelist = array();
185 $duplicates = $db->query("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE duplicateof = $bug[bugid]");
186 foreach ($duplicates as $duplicate)
187 {
188 $dupelist[] = "<a href=\"showreport.php?bugid=$duplicate[bugid]\" target=\"_blank\">$duplicate[summary]</a>";
189 }
190 $dupelist = implode(', ', $dupelist);
191
192 if ($bug['dependency'])
193 {
194 $depends = array();
195 $dependencies = $db->query("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($bug[dependency])");
196 foreach ($dependencies as $dependency)
197 {
198 $depends[] = "<a href=\"showreport.php?bugid=$dependency[bugid]\" title=\"$dependency[summary]\" target=\"_blank\">$dependency[bugid]</a>";
199 }
200 $dependencies = implode(' ', $depends);
201 }
202 }
203
204 // ###################################################################
205 // global display items
206
207 $show['subscribe'] = can_perform('cansubscribe', $bug['product']);
208
209 $favorite = (bool)$db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE bugid = $bug[bugid] AND userid = " . bugdar::$userinfo['userid']);
210 $favoritetext = ($favorite ? T('Remove from Favorites') : T('Add to Favorites'));
211
212 $bug['userinfo'] = construct_user_display($bug);
213 $bug['datetime'] = $datef->format(bugdar::$options['dateformat'], $bug['dateline']);
214 $bug['productid'] = $bug['product'];
215 $bug['product'] = bugdar::$datastore['product']["$bug[product]"]['title'];
216 $bug['componentid'] = $bug['component'];
217 $bug['component'] = ($bug['component'] ? bugdar::$datastore['product']["$bug[component]"]['title'] : '');
218 $bug['versionid'] = $bug['version'];
219 $bug['version'] = bugdar::$datastore['version']["$bug[version]"]['version'];
220
221 // ###################################################################
222 // custom field output
223
224 $fields = construct_custom_fields($bug);
225 $i = 0;
226 foreach ($fields AS $field)
227 {
228 if ($i % 2 == 0)
229 {
230 $customfields['left'] .= $field;
231 }
232 else
233 {
234 $customfields['right'] .= $field;
235 }
236 $i++;
237 }
238
239 // ###################################################################
240 // other elements
241
242 // -------------------------------------------------------------------
243 // hilight
244 $words = explode(' ', $input->in['hilight']);
245 foreach ($words AS $word)
246 {
247 if (trim($word))
248 {
249 $word = preg_quote($input->unsanitize($word));
250 $hilight[] = $temp = trim(preg_replace('#[^0-9a-zA-Z_ ]#', '', $word));
251 }
252 }
253
254 // -------------------------------------------------------------------
255 // attachments
256 $show['getattachments'] = (can_perform('cangetattach', $bug['productid']) || can_perform('caneditattach', $bug['productid']));
257 $show['putattachments'] = (can_perform('canputattach', $bug['productid']) || can_perform('caneditattach', $bug['productid']));
258 $show['attachments'] = ($show['getattachments'] || $show['putattachments']);
259
260 if ($show['getattachments'] || $show['putattachments'])
261 {
262 $attachments_fetch = $db->query("
263 SELECT attachment.attachmentid, attachment.filename,
264 attachment.description, attachment.dateline,
265 attachment.userid, attachment.obsolete, user.email,
266 user.showemail, user.displayname
267 FROM " . TABLE_PREFIX . "attachment AS attachment
268 LEFT JOIN " . TABLE_PREFIX . "user AS user
269 ON (attachment.userid = user.userid)
270 WHERE attachment.bugid = $bug[bugid]
271 ORDER BY attachment.dateline"
272 );
273
274 $attaches = false;
275 foreach ($attachments_fetch as $attachment)
276 {
277 $attaches = true;
278 $show['editattach'] = (can_perform('caneditattach', $bug['productid']) || ($attachment['userid'] == bugdar::$userinfo['userid'] && can_perform('canputattach', $bug['productid'])));
279 $attachment['date'] = $datef->format(bugdar::$options['dateformat'], $attachment['dateline']);
280 $attachment['user'] = construct_user_display($attachment, false);
281
282 $tpl = new BSTemplate('showreport_attachment');
283 $tpl->vars = array(
284 'attachment' => $attachment
285 );
286 $attachments .= $tpl->evaluate()->getTemplate();
287 }
288
289 $show['attachments'] = ($show['putattachments'] && $attaches);
290 }
291
292 // -------------------------------------------------------------------
293 // votes
294
295 $vote = $db->queryFirst("SELECT *, FIND_IN_SET(" . bugdar::$userinfo['userid'] . ", userids) AS uservote FROM " . TABLE_PREFIX . "vote WHERE bugid = $bug[bugid]");
296
297 $vote['total'] = $vote['votefor'] + $vote['voteagainst'];
298 if ($vote['total'] != 0)
299 {
300 $vote['forpercent'] = round($vote['votefor'] / $vote['total'], 3) * 100;
301 $vote['againstpercent'] = round($vote['voteagainst'] / $vote['total'], 3) * 100;
302 }
303 else
304 {
305 $vote['forpercent'] = 0;
306 $vote['againstpercent'] = 0;
307 }
308
309 $show['vote'] = ((can_perform('canvote', $bug['productid']) AND !$vote['uservote']) ? true : false);
310
311 // -------------------------------------------------------------------
312 // get comments
313 $comments_fetch = $db->query("
314 SELECT comment.*, user.email, user.showemail, user.displayname
315 FROM " . TABLE_PREFIX . "comment AS comment
316 LEFT JOIN " . TABLE_PREFIX . "user AS user
317 ON (comment.userid = user.userid)
318 WHERE comment.bugid = $bug[bugid]" . (!can_perform('canviewhidden', $bug['productid']) ? "
319 AND !hidden" : '') . "
320 ORDER BY comment.dateline ASC"
321 );
322 $description = null;
323 foreach ($comments_fetch as $comment)
324 {
325 $comment['posttime'] = $datef->format(bugdar::$options['dateformat'], $comment['dateline']);
326 $comment['postby'] = construct_user_display($comment);
327 $show['editcomment'] = ((can_perform('caneditownreply', $bug['productid']) AND bugdar::$userinfo['userid'] == $comment['userid']) OR (can_perform('caneditotherreply', $bug['productid']) AND bugdar::$userinfo['userid'] != $comment['userid']));
328
329 BSApp::debug('can edit own replies: ' . (int)(can_perform('caneditownreply', $bug['productid']) AND bugdar::$userinfo['userid'] == $comment['userid']));
330 BSApp::debug('can edit other replies:' . (int)(can_perform('caneditotherreply', $bug['productid']) AND bugdar::$userinfo['userid'] != $comment['userid']));
331 BSApp::debug('$show[editcomment]: ' . $show['editcomment']);
332
333 if (is_array($hilight))
334 {
335 foreach ($hilight AS $id => $find)
336 {
337 $find = "#($find)#i";
338 $replace = "<span style=\"background-color: yellow; font-weight: bold; color: red;\">\\1</span>";
339 $comment['comment_parsed'] = preg_replace($find, $replace, $comment['comment_parsed']);
340 }
341 }
342
343 $tpl = new BSTemplate('showreport_comment');
344 $tpl->vars = array('comment' => $comment);
345 $temp = $tpl->evaluate()->getTemplate();
346 if ($description == null)
347 {
348 $description = $temp;
349 }
350 else
351 {
352 $comments .= $temp;
353 }
354 }
355
356 $show['newreply'] = (can_perform('canpostcomments', $bug['productid']) ? true : false);
357
358 $bug['summary_title'] = $bug['summary'];
359 if (is_array($hilight) AND !$show['edit'])
360 {
361 foreach ($hilight AS $id => $find)
362 {
363 $find = "#($find)#i";
364 $replace = "<span style=\"background-color: yellow; font-weight: bold; color: red;\">\\1</span>";
365 $bug['summary'] = preg_replace($find, $replace, $bug['summary']);
366 }
367 }
368
369 $tpl = new BSTemplate('showreport');
370 $tpl->vars = array(
371 'bug' => $bug,
372 'comments' => $comments,
373 'select' => $select,
374 'vote' => $vote,
375 'favoritetext' => $favoritetext,
376 'customfields' => $customfields,
377 'attachments' => $attachments,
378 'productSelect' => $productSelect
379 );
380 $tpl->evaluate()->flush();
381
382 ?>