Fix showhistory.php to show more than one log item
[bugdar.git] / showhistory.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Bugdar
5 || # Copyright ©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 'history',
24 'history_bit'
25 );
26
27 define('SVN', '$Id$');
28
29 $focus['showreport'] = 'focus';
30
31 require_once('./global.php');
32 require_once('./includes/api_user.php');
33
34 $bug = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . $input->inputClean('bugid', TYPE_UINT));
35 if (!$bug)
36 {
37 $message->error(L_INVALID_ID);
38 }
39
40 if (!check_bug_permissions($bug))
41 {
42 $message->errorPermission();
43 }
44
45 $lookupfields = array(
46 'status' => 'status',
47 'priority' => 'priority',
48 'severity' => 'severity',
49 'resolution' => 'resolution',
50 'version' => 'version',
51 'product' => 'title',
52 'component' => 'title'
53 );
54
55 // ###################################################################
56
57 $customfields = $db->query("
58 SELECT bugfield.*, MAX(permission.mask) AS mask
59 FROM " . TABLE_PREFIX . "bugfield AS bugfield
60 LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
61 ON (bugfield.fieldid = permission.fieldid)
62 WHERE mask <> 0
63 AND permission.usergroupid IN (" . bugdar::$userinfo['usergroupid'] . (sizeof(bugdar::$userinfo['groupids']) != 0 ? ',' . implode(',', bugdar::$userinfo['groupids']) : '') . ")
64 GROUP BY (bugfield.fieldid)"
65 );
66
67 foreach ($customfields as $field)
68 {
69 $fieldlist["$field[fieldid]"] = sprintf(T('Custom Field %1$s (%2$s)'), $field['fieldid'], $field['name']);
70 }
71
72 // ###################################################################
73
74 $logs = array();
75 $logs_fetch = $db->query("
76 SELECT history.*, user.userid, user.email, user.displayname, user.showemail
77 FROM " . TABLE_PREFIX . "history AS history
78 LEFT JOIN " . TABLE_PREFIX . "user AS user
79 ON (user.userid = history.userid)
80 WHERE bugid = $bug[bugid]"
81 );
82 foreach ($logs_fetch as $log)
83 {
84 $log['date'] = $datef->format(bugdar::$options['dateformat'], $log['dateline']);
85 $log['user'] = construct_user_display($log);
86
87 $logs["$log[dateline]"]["$log[historyid]"] = $log;
88 ksort($logs["$log[dateline]"]);
89 }
90
91 ksort($logs);
92
93 $toKill = array();
94
95 foreach ($logs AS $dateline => $logitems)
96 {
97 $show['group'] = true;
98
99 foreach ($logitems AS $log)
100 {
101 if ($log['field'] == '.' OR ($log['original'] == '' AND $log['changed'] == ''))
102 {
103 $toKill[] = $log['historyid'];
104 continue;
105 }
106
107 if (preg_match('#^(comment|attachment)\.(.*)#', $log['field'], $matches))
108 {
109 if ($matches[1] == 'comment')
110 {
111 $log['field'] = sprintf(T('Comment #%1$s %2$s'), $log['commentid'], ucwords($matches[2]));
112 }
113 else if ($matches[1] == 'attachment')
114 {
115 $log['field'] = sprintf(T('Attachment #2%1$s %2$s'), $log['attachmentid'], ucwords($matches[2]));
116 }
117 }
118 else if (preg_match('#^.?custom(.field)?([0-9]+?)#', $log['field'], $matches))
119 {
120 if ($fieldlist["$matches[2]"])
121 {
122 $log['field'] = $fieldlist["$matches[2]"];
123 }
124 else
125 {
126 continue;
127 }
128 }
129 else if (preg_match('#^\.(.*)#', $log['field'], $matches))
130 {
131 $log['field'] = ucwords($matches[1]);
132
133 if (isset($lookupfields["$matches[1]"]))
134 {
135 $lookup = $matches[1];
136 $log['original'] = bugdar::$datastore["$lookup"]["$log[original]"][ $lookupfields["$lookup"] ];
137 $log['changed'] = bugdar::$datastore["$lookup"]["$log[changed]"][ $lookupfields["$lookup"] ];
138 }
139 else if ($matches[1] == 'assignto' OR $matches[1] == 'assignedto')
140 {
141 if ($log['original'])
142 {
143 $user = new UserAPI();
144 $user->set('userid', $log['original']);
145 $user->setCondition();
146 $user->fetch();
147 $log['original'] = construct_user_display($user->record);
148 }
149
150 if ($log['changed'])
151 {
152 $user = new UserAPI();
153 $user->set('userid', $log['changed']);
154 $user->setCondition();
155 $user->fetch();
156 $log['changed'] = construct_user_display($user->record);
157 }
158 }
159 }
160
161 BSFunctions::swap_css_classes('', 'altcolor');
162
163 $tpl = new BSTemplate('history_bit');
164 $tpl->vars = array(
165 'bgcolor' => BSFunctions::$cssClass,
166 'log' => $log
167 );
168 $history .= $tpl->evaluate()->getTemplate();
169 $show['group'] = false;
170 }
171 }
172
173 // we can now remove all useless logs
174 if (sizeof($toKill) > 0)
175 {
176 $db->query("DELETE FROM " . TABLE_PREFIX . "history WHERE historyid IN (" . implode(',', $toKill) . ")");
177 }
178
179 $tpl = new BSTemplate('history');
180 $tpl->vars = array(
181 'history' => $history,
182 'bug' => $bug
183 );
184 $tpl->evaluate()->flush();
185
186 /*=====================================================================*\
187 || ###################################################################
188 || # $HeadURL$
189 || # $Id$
190 || ###################################################################
191 \*=====================================================================*/
192 ?>