Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / showhistory.php
index 5bb05b6b5b465e942d88ef5bdcefcc4c19649018..25bca98717f9f201f5ce4525d9b2ba7a33788659 100644 (file)
@@ -1,52 +1,76 @@
 <?php
 /*=====================================================================*\
-|| ################################################################### ||
-|| # BugStrike [#]version[#]
-|| # --------------------------------------------------------------- # ||
-|| # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
-|| # This file may not be reproduced in any way without permission.  # ||
-|| # --------------------------------------------------------------- # ||
-|| # User License Agreement at http://www.iris-studios.com/license/  # ||
-|| ################################################################### ||
+|| ###################################################################
+|| # Bugdar
+|| # Copyright (c)2004-2009 Blue Static
+|| #
+|| # This program is free software; you can redistribute it and/or modify
+|| # it under the terms of the GNU General Public License as published by
+|| # the Free Software Foundation; version 2 of the License.
+|| #
+|| # This program is distributed in the hope that it will be useful, but
+|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+|| # more details.
+|| #
+|| # You should have received a copy of the GNU General Public License along
+|| # with this program; if not, write to the Free Software Foundation, Inc.,
+|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+|| ###################################################################
 \*=====================================================================*/
 
 $fetchtemplates = array(
        'history',
-       'history_group',
        'history_bit'
 );
 
+
+$focus['showreport'] = 'focus';
+
 require_once('./global.php');
+require_once('./includes/api_user.php');
 
-$bug = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . intval($bugsys->in['bugid']));
+$bug = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = " . $input->inputClean('bugid', TYPE_UINT));
 if (!$bug)
 {
-       $message->error('alert: bad bug');
+       $message->error(L_INVALID_ID);
 }
 
-if ($bug['hidden'] AND !can_perform('canviewhidden'))
+if (!check_bug_permissions($bug))
 {
-       $message->error_permission();
+       $message->errorPermission();
 }
 
+$lookupfields = array(
+       'status'                => 'status',
+       'priority'              => 'priority',
+       'severity'              => 'severity',
+       'resolution'    => 'resolution',
+       'version'               => 'version',
+       'product'               => 'title',
+       'component'             => 'title'
+);
+
 // ###################################################################
 
 $customfields = $db->query("
-       SELECT bugfield.*
+       SELECT bugfield.*, MAX(permission.mask) AS mask
        FROM " . TABLE_PREFIX . "bugfield AS bugfield
        LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
                ON (bugfield.fieldid = permission.fieldid)
-       WHERE permission.mask <> 0
-       AND permission.usergroupid = {$bugsys->userinfo['usergroupid']}"
+       WHERE mask <> 0
+       AND permission.usergroupid IN (" . bugdar::$userinfo['usergroupid'] . (sizeof(bugdar::$userinfo['groupids']) != 0 ? ',' . implode(',', bugdar::$userinfo['groupids']) : '') . ")
+       GROUP BY (bugfield.fieldid)"
 );
 
-while ($field = $db->fetch_array($customfields))
+foreach ($customfields as $field)
 {
-       $fieldlist["custom_field$field[fieldid]"] = 'custom_field' . $field['fieldid'] . " (\"$field[name]\")";
+       $fieldlist["$field[fieldid]"] = sprintf(T('Custom Field %1$s (%2$s)'), $field['fieldid'], $field['name']);
 }
 
 // ###################################################################
 
+$logs = array();
 $logs_fetch = $db->query("
        SELECT history.*, user.userid, user.email, user.displayname, user.showemail
        FROM " . TABLE_PREFIX . "history AS history
@@ -54,10 +78,9 @@ $logs_fetch = $db->query("
                ON (user.userid = history.userid)
        WHERE bugid = $bug[bugid]"
 );
-while ($log = $db->fetch_array($logs_fetch))
+foreach ($logs_fetch as $log)
 {
-       $log['formatted'] = call_user_func_array(array($bugsys->lang, 'p'), array_merge(array($log['language']), unserialize($log['arguments'])));
-       $log['date'] = datelike('standard', $log['dateline']);
+       $log['date'] = $datef->format(bugdar::$options['dateformat'], $log['dateline']);
        $log['user'] = construct_user_display($log);
        
        $logs["$log[dateline]"]["$log[historyid]"] = $log;
@@ -66,46 +89,97 @@ while ($log = $db->fetch_array($logs_fetch))
 
 ksort($logs);
 
+$toKill = array();
+
 foreach ($logs AS $dateline => $logitems)
 {
-       $rowspan = 1;
-       $changes = '';
-       $show['changes'] = false;
-       $messages = array();
+       $show['group'] = true;
+       
        foreach ($logitems AS $log)
        {
-               $hasvalues = ((empty($log['original']) AND (empty($log['changed']) AND $log['changed'] != 0)) ? false : true);
-               
-               if ($newlog = $fieldlist["$log[field]"])
+               if ($log['field'] == '.' OR ($log['original'] == '' AND $log['changed'] == ''))
                {
-                       $log['field'] = $newlog;
+                       $toKill[] = $log['historyid'];
+                       continue;
                }
                
-               $show['changes'] = (($hasvalues) ? true : $show['changes']);
-               
-               if ($hasvalues)
+               if (preg_match('#^(comment|attachment)\.(.*)#', $log['field'], $matches))
                {
-                       $rowspan++;
-                       eval('$changes .= "' . $template->fetch('history_bit') . '";');
+                       if ($matches[1] == 'comment')
+                       {
+                               $log['field'] = sprintf(T('Comment #%1$s %2$s'), $log['commentid'], ucwords($matches[2]));
+                       }
+                       else if ($matches[1] == 'attachment')
+                       {
+                               $log['field'] = sprintf(T('Attachment #2%1$s %2$s'), $log['attachmentid'], ucwords($matches[2]));
+                       }
                }
-               
-               if (!in_array($log['formatted'], $messages))
+               else if (preg_match('#^.?custom(.field)?([0-9]+?)#', $log['field'], $matches))
+               {
+                       if ($fieldlist["$matches[2]"])
+                       {
+                               $log['field'] = $fieldlist["$matches[2]"];
+                       }
+                       else
+                       {
+                               continue;
+                       }
+               }
+               else if (preg_match('#^\.(.*)#', $log['field'], $matches))
                {
-                       $messages[] = $log['formatted'];
+                       $log['field'] = ucwords($matches[1]);
+                       
+                       if (isset($lookupfields["$matches[1]"]))
+                       {
+                               $lookup = $matches[1];
+                               $log['original'] = bugdar::$datastore["$lookup"]["$log[original]"][ $lookupfields["$lookup"] ];
+                               $log['changed'] = bugdar::$datastore["$lookup"]["$log[changed]"][ $lookupfields["$lookup"] ];
+                       }
+                       else if ($matches[1] == 'assignto' OR $matches[1] == 'assignedto')
+                       {
+                               if ($log['original'])
+                               {
+                                       $user = new UserAPI();
+                                       $user->set('userid', $log['original']);
+                                       $user->setCondition();
+                                       $user->fetch();
+                                       $log['original'] = construct_user_display($user->record);
+                               }
+                               
+                               if ($log['changed'])
+                               {
+                                       $user = new UserAPI();
+                                       $user->set('userid', $log['changed']);
+                                       $user->setCondition();
+                                       $user->fetch();
+                                       $log['changed'] = construct_user_display($user->record);
+                               }
+                       }
                }
+               
+               BSFunctions::swap_css_classes('', 'altcolor');
+               
+               $tpl = new BSTemplate('history_bit');
+               $tpl->vars = array(
+                       'bgcolor'       => BSFunctions::$cssClass,
+                       'log'           => $log
+               );
+               $history .= $tpl->evaluate()->getTemplate();
+               $show['group'] = false;
        }
-       
-       $log['messages'] = implode("</div>\n<div>", $messages);
-       
-       eval('$history .= "' . $template->fetch('history_group') . '";');
 }
 
-eval('$template->flush("' . $template->fetch('history') . '");');
+// we can now remove all useless logs
+if (sizeof($toKill) > 0)
+{
+       $db->query("DELETE FROM " . TABLE_PREFIX . "history WHERE historyid IN (" . implode(',', $toKill) . ")");
+}
+
+$tpl = new BSTemplate('history');
+$tpl->vars = array(
+       'history'       => $history,
+       'bug'           => $bug
+);
+$tpl->evaluate()->flush();
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file