Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / showreport.php
index e47dea738717c053a6d0e1d1f7a8617d0fc8c05a..8d0c9ba8d66cf3885a4a4754b1f4ff307f957e07 100644 (file)
@@ -1,13 +1,22 @@
 <?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(
@@ -15,25 +24,16 @@ $fetchtemplates = array(
        'showreport_attachment',
        'showreport_comment',
        'quicksearch',
-       'pcv_select_row',
-       'customfield_bit',
        'bugfield_static_text'
 );
 
-define('SVN', '$Id$');
 
 $focus['showreport'] = 'focus';
 
 require_once('./global.php');
+require_once('./includes/functions_product.php');
 
-if (!can_perform('canviewbugs'))
-{
-       $message->error_permission();
-}
-
-$show['edit'] = (((can_perform('caneditown') AND $bugsys->userinfo['userid'] == $bug['userid']) OR can_perform('caneditothers')) AND can_perform('caneditinfo')) ? true : false;
-
-$bugid = intval($bugsys->in['bugid']);
+$bugid = $input->inputClean('bugid', TYPE_UINT);
 
 // ###################################################################
 
@@ -41,18 +41,20 @@ if (empty($bugid) OR $_REQUEST['do'] == 'quicksearch')
 {
        if (!empty($bugid))
        {
-               if ($db->query_first("SELECT bugid FROM " . TABLE_PREFIX . "bug WHERE bugid = $bugid"))
+               if ($db->queryFirst("SELECT bugid FROM " . TABLE_PREFIX . "bug WHERE bugid = $bugid"))
                {
                        header("Location: showreport.php?bugid=$bugid");
                        exit;
                }
                else
                {
-                       $error = lang::p('error_invalid_id');
+                       $error = L_INVALID_ID;
                }
        }
        
-       eval('$template->flush("' . $template->fetch('quicksearch') . '");');
+       $tpl = new BSTemplate('quicksearch');
+       $tpl->vars = array('error' => $error);
+       $tpl->evaluate()->flush();
        exit;
 }
 
@@ -60,34 +62,26 @@ if (empty($bugid) OR $_REQUEST['do'] == 'quicksearch')
 
 // -------------------------------------------------------------------
 // get the report
-$bug = $db->query_first("
-       SELECT bugvaluefill.*, bug.*, user.email, user.displayname, user.showemail
+$bug = $db->queryFirst("
+       SELECT bug.*, user.email, user.displayname, user.showemail
        FROM " . TABLE_PREFIX . "bug AS bug
        LEFT JOIN " . TABLE_PREFIX . "user AS user
                ON (bug.userid = user.userid)
-       LEFT JOIN " . TABLE_PREFIX . "bugvaluefill AS bugvaluefill
-               ON (bug.bugid = bugvaluefill.bugid)
-       WHERE bug.bugid = " . intval($bugsys->in['bugid'])
+       WHERE bug.bugid = $bugid"
 );
 
 if (!is_array($bug))
 {
-       $message->error(lang::p('error_invalid_id'));
+       $message->error(L_INVALID_ID);
 }
 
-if ($bug['hidden'] AND !can_perform('canviewhidden'))
+if (!check_bug_permissions($bug))
 {
-       $message->error_permission();
+       $message->errorPermission();
 }
 
-// ###################################################################
-// global display items
-
-$favourite = (bool)$db->query_first("SELECT * FROM " . TABLE_PREFIX . "favourite WHERE bugid = $bug[bugid] AND userid = " . $bugsys->userinfo['userid']);
-$favouritetext = (($favourite) ? 'Remove from Favourites' : 'Add to Favourites');
-
-$bug['userinfo'] = construct_user_display($bug);
-$bug['datetime'] = $datef->format($bugsys->options['dateformat'], $bug['dateline']);
+$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']));
+$show['delete'] = can_perform('candeletedata', $bug['product']);
 
 // ###################################################################
 // edit display
@@ -95,62 +89,79 @@ if ($show['edit'])
 {
        $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', $bug['severity']);
        
-       $show['changestatus'] = ((can_perform('canchangestatus')) ? true : false);
-       if (can_perform('canchangestatus'))
+       $show['changestatus'] = (can_perform('canchangestatus', $bug['product']) ? true : false);
+       if (can_perform('canchangestatus', $bug['product']))
        {
                $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', $bug['priority']);
                $select['status'] = construct_datastore_select('status', 'status', 'statusid', $bug['status']);
                $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', $bug['resolution']);             
        }
+       else
+       {
+               $bug['status'] = bugdar::$datastore['status']["$bug[status]"]['status'];
+               $bug['resolution'] = bugdar::$datastore['resolution']["$bug[resolution]"]['resolution'];
+               $bug['severity'] = bugdar::$datastore['severity']["$bug[severity]"]['severity'];
+               $bug['priority'] = bugdar::$datastore['priority']["$bug[priority]"]['priority'];
+       }
        
-       $show['assign'] = ((can_perform('canassign')) ? true : false);
-       if (can_perform('canassign'))
+       $show['assign'] = (can_perform('canassign', $bug['product']));
+       if (can_perform('canassign', $bug['product']) && is_array(bugdar::$datastore['assignto']))
        {
-               foreach ($bugsys->datastore['assignto'] AS $dev)
+               foreach (bugdar::$datastore['assignto'] as $dev)
                {
-                       $value = $dev['userid'];
-                       $selected = (($dev['userid'] == $bug['assignedto']) ? true : false);
-                       $label = construct_user_display($dev, false);
-                       eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
+                       $tpl = new BSTemplate('selectoption');
+                       $tpl->vars = array(
+                               'value'         => $dev['userid'],
+                               'label'         => construct_user_display($dev, false),
+                               'selected'      => ($dev['userid'] == $bug['assignedto'])
+                       );
+                       $select['dev'] .= $tpl->evaluate()->getTemplate();
                }
        }
        
-       $pcv_select = construct_pcv_select("p$bug[productid]c$bug[componentid]v$bug[versionid]");
+       $productSelect = construct_product_select('canviewbugs', "$bug[product],$bug[component],$bug[version]");
        
-       $firstcomment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "comment WHERE bugid = $bug[bugid] ORDER BY dateline ASC LIMIT 1");
-               
        if ($bug['duplicateof'])
        {
-               $duplicate = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
+               $duplicate = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
        }
        
        if ($bug['dependency'])
        {
+               $depends = array();
                $dependencies = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($bug[dependency])");
-               while ($dependency = $db->fetch_array($dependencies))
+               foreach ($dependencies as $dependency)
                {
                        $depends[] = "<a href=\"showreport.php?bugid=$dependency[bugid]\" title=\"$dependency[summary]\">$dependency[bugid]</a>";
                }
                $dependencies = implode(' ', $depends);
        }
        
-       $actions = $db->query("SELECT * FROM " . TABLE_PREFIX . "autoaction ORDER BY name ASC");
-       $select['autoactions'] = '';
-       $show['autoactions'] = false;
-       while ($action = $db->fetch_array($actions))
-       {
-               $label = $action['name'];
-               $value = $action['actionid'];
-               $selected = false;
-               eval('$select[autoaction] .= "' . $template->fetch('selectoption') . '";');
-               $show['autoactions'] = true;
-       }
-       if ($show['autoactions'])
+       $select['automations'] = '';
+       $show['automations'] = false;
+       if (is_array(bugdar::$datastore['automation']))
        {
-               $label = '';
-               $value = 0;
-               $selected = true;
-               eval('$select[autoaction] = "' . $template->fetch('selectoption') . '" . $select[autoaction];');
+               foreach (bugdar::$datastore['automation'] as $action)
+               {
+                       $tpl = new BSTemplate('selectoption');
+                       $tpl->vars = array(
+                               'label'         => $action['name'],
+                               'value'         => $action['actionid'],
+                               'selected'      => false
+                       );
+                       $select['automation'] .= $tpl->evaluate()->getTemplate();
+                       $show['automations'] = true;
+               }
+               if ($show['automations'])
+               {
+                       $tpl = new BSTemplate('selectoption');
+                       $tpl->vars = array(
+                               'label'         => '',
+                               'value'         => 0,
+                               'selected'      => true
+                       );
+                       $select['automation'] = $tpl->evaluate()->getTemplate() . $select['automation'];
+               }
        }
 }
 
@@ -160,21 +171,19 @@ else
 {
        // -------------------------------------------------------------------
        // prep display
-       $bug['product'] = $bugsys->datastore['product']["$bug[productid]"]['title'];
-       $bug['component'] = (($bug['componentid']) ? $bugsys->datastore['product']["$bug[componentid]"]['title'] : '');
-       $bug['version'] = $bugsys->datastore['version']["$bug[versionid]"]['version'];
-       $bug['status'] = $bugsys->datastore['status']["$bug[status]"]['status'];
-       $bug['resolution'] = $bugsys->datastore['resolution']["$bug[resolution]"]['resolution'];
-       $bug['severity'] = $bugsys->datastore['severity']["$bug[severity]"]['severity'];
-       $bug['priority'] = $bugsys->datastore['priority']["$bug[priority]"]['priority'];
+       $bug['status'] = bugdar::$datastore['status']["$bug[status]"]['status'];
+       $bug['resolution'] = bugdar::$datastore['resolution']["$bug[resolution]"]['resolution'];
+       $bug['severity'] = bugdar::$datastore['severity']["$bug[severity]"]['severity'];
+       $bug['priority'] = bugdar::$datastore['priority']["$bug[priority]"]['priority'];
        
-       $assigninfo = $bugsys->datastore['assignto']["$bug[assignedto]"];
+       $assigninfo = bugdar::$datastore['assignto']["$bug[assignedto]"];
        $bug['assigninfo'] = ((is_array($assigninfo)) ? construct_user_display($assigninfo) : '');
                
-       $duplicateof = $db->query_first("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
+       $duplicateof = $db->queryFirst("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
        
+       $dupelist = array();
        $duplicates = $db->query("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE duplicateof = $bug[bugid]");
-       while ($duplicate = $db->fetch_array($duplicates))
+       foreach ($duplicates as $duplicate)
        {
                $dupelist[] = "<a href=\"showreport.php?bugid=$duplicate[bugid]\" target=\"_blank\">$duplicate[summary]</a>";
        }
@@ -182,8 +191,9 @@ else
        
        if ($bug['dependency'])
        {
+               $depends = array();
                $dependencies = $db->query("SELECT bugid, summary FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($bug[dependency])");
-               while ($dependency = $db->fetch_array($dependencies))
+               foreach ($dependencies as $dependency)
                {
                        $depends[] = "<a href=\"showreport.php?bugid=$dependency[bugid]\" title=\"$dependency[summary]\" target=\"_blank\">$dependency[bugid]</a>";
                }
@@ -191,87 +201,112 @@ else
        }
 }
 
+// ###################################################################
+// global display items
+
+$show['subscribe'] = can_perform('cansubscribe', $bug['product']);
+
+$favorite = (bool)$db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE bugid = $bug[bugid] AND userid = " . bugdar::$userinfo['userid']);
+$favoritetext = ($favorite ? T('Remove from Favorites') : T('Add to Favorites'));
+
+$bug['userinfo'] = construct_user_display($bug);
+$bug['datetime'] = $datef->format(bugdar::$options['dateformat'], $bug['dateline']);
+$bug['productid'] = $bug['product'];
+$bug['product'] = bugdar::$datastore['product']["$bug[product]"]['title'];
+$bug['componentid'] = $bug['component'];
+$bug['component'] = ($bug['component'] ? bugdar::$datastore['product']["$bug[component]"]['title'] : '');
+$bug['versionid'] = $bug['version'];
+$bug['version'] = bugdar::$datastore['version']["$bug[version]"]['version'];
+
 // ###################################################################
 // custom field output
 
 $fields = construct_custom_fields($bug);
-
-$bugsys->debug(count($fields) % 2);
-$i = 1;
+$i = 0;
 foreach ($fields AS $field)
 {
-       $bugsys->debug("i = $i");
-       if ($i == 1)
+       if ($i % 2 == 0)
        {
-               $left = $field;
+               $customfields['left'] .= $field;
        }
-       else if ($i == 2)
+       else
        {
-               $right = $field;
-               eval('$customfields .= "' . $template->fetch('customfield_bit') . '";');
-               $i = 0;
+               $customfields['right'] .= $field;
        }
        $i++;
 }
 
-if ((count($fields) % 2) != 0)
-{
-       $bugsys->debug('no modulo for you');
-       $right = '';
-       eval('$customfields .= "' . $template->fetch('customfield_bit') . '";');
-}
-
 // ###################################################################
 // other elements
 
 // -------------------------------------------------------------------
 // hilight
-$words = explode(' ', $bugsys->in['hilight']);
+$words = explode(' ', $input->in['hilight']);
 foreach ($words AS $word)
 {
        if (trim($word))
        {
-               $word = preg_quote($bugsys->unsanitize($word));
+               $word = preg_quote($input->unsanitize($word));
                $hilight[] = $temp = trim(preg_replace('#[^0-9a-zA-Z_ ]#', '', $word));
        }
 }
 
 // -------------------------------------------------------------------
 // attachments
-$show['getattachments'] = ((can_perform('cangetattach') OR can_perform('caneditattach')) ? true : false);
-$show['putattachments'] = ((can_perform('canputattach') OR can_perform('caneditattach')) ? true : false);
-$show['attachments'] = ($show['getattachments'] OR $show['putattachments']) ? true : false;
+$show['getattachments'] = (can_perform('cangetattach', $bug['productid']) || can_perform('caneditattach', $bug['productid']));
+$show['putattachments'] = (can_perform('canputattach', $bug['productid']) || can_perform('caneditattach', $bug['productid']));
+$show['attachments'] = ($show['getattachments'] || $show['putattachments']);
 
-if ($show['getattachments'] OR $show['putattachments'])
+if ($show['getattachments'] || $show['putattachments'])
 {
        $attachments_fetch = $db->query("
-               SELECT attachment.*, user.email, user.showemail,
-                       user.displayname
+               SELECT attachment.attachmentid, attachment.filename,
+                       attachment.description, attachment.dateline,
+                       attachment.userid, attachment.obsolete, user.email,
+                       user.showemail, user.displayname
                FROM " . TABLE_PREFIX . "attachment AS attachment
                LEFT JOIN " . TABLE_PREFIX . "user AS user
                        ON (attachment.userid = user.userid)
                WHERE attachment.bugid = $bug[bugid]
                ORDER BY attachment.dateline"
        );
-       while ($attachment = $db->fetch_array($attachments_fetch))
+       
+       $attaches = false;
+       foreach ($attachments_fetch as $attachment)
        {
-               $show['editattach'] = ((can_perform('caneditattach') OR ($attachment['userid'] == $bugsys->userinfo['userid'] AND can_perform('canputattach'))) ? true : false);
-               $attachment['date'] = $datef->format($bugsys->options['dateformat'], $attachment['dateline']);
+               $attaches = true;
+               $show['editattach'] = (can_perform('caneditattach', $bug['productid']) || ($attachment['userid'] == bugdar::$userinfo['userid'] && can_perform('canputattach', $bug['productid'])));
+               $attachment['date'] = $datef->format(bugdar::$options['dateformat'], $attachment['dateline']);
                $attachment['user'] = construct_user_display($attachment, false);
-               eval('$attachments .= "' . $template->fetch('showreport_attachment') . '";');
+               
+               $tpl = new BSTemplate('showreport_attachment');
+               $tpl->vars = array(
+                       'attachment'    => $attachment
+               );
+               $attachments .= $tpl->evaluate()->getTemplate();
        }
+       
+       $show['attachments'] = ($show['putattachments'] && $attaches);
 }
 
 // -------------------------------------------------------------------
 // votes
 
-$vote = $db->query_first("SELECT *, FIND_IN_SET(" . $bugsys->userinfo['userid'] . ", userids) AS uservote FROM " . TABLE_PREFIX . "vote WHERE bugid = $bug[bugid]");
+$vote = $db->queryFirst("SELECT *, FIND_IN_SET(" . bugdar::$userinfo['userid'] . ", userids) AS uservote FROM " . TABLE_PREFIX . "vote WHERE bugid = $bug[bugid]");
 
 $vote['total'] = $vote['votefor'] + $vote['voteagainst'];
-$vote['forpercent'] = round($vote['votefor'] / $vote['total'], 3) * 100;
-$vote['againstpercent'] = round($vote['voteagainst'] / $vote['total'], 3) * 100;
+if ($vote['total'] != 0)
+{
+       $vote['forpercent'] = round($vote['votefor'] / $vote['total'], 3) * 100;
+       $vote['againstpercent'] = round($vote['voteagainst'] / $vote['total'], 3) * 100;
+}
+else
+{
+       $vote['forpercent'] = 0;
+       $vote['againstpercent'] = 0;
+}
 
-$show['vote'] = ((can_perform('canvote') AND !$vote['uservote']) ? true : false);
+$show['vote'] = ((can_perform('canvote', $bug['productid']) AND !$vote['uservote']) ? true : false);
 
 // -------------------------------------------------------------------
 // get comments
@@ -280,47 +315,68 @@ $comments_fetch = $db->query("
        FROM " . TABLE_PREFIX . "comment AS comment
        LEFT JOIN " . TABLE_PREFIX . "user AS user
                ON (comment.userid = user.userid)
-       WHERE comment.bugid = $bug[bugid]" . ((!can_perform('canviewhidden')) ? "
+       WHERE comment.bugid = $bug[bugid]" . (!can_perform('canviewhidden', $bug['productid']) ? "
                AND !hidden" : '') . "
        ORDER BY comment.dateline ASC"
 );
-while ($comment = $db->fetch_array($comments_fetch))
+$description = null;
+foreach ($comments_fetch as $comment)
 {
-       $comment['posttime'] = $datef->format($bugsys->options['dateformat'], $comment['dateline']);
+       $comment['posttime'] = $datef->format(bugdar::$options['dateformat'], $comment['dateline']);
        $comment['postby'] = construct_user_display($comment);
-       $show['editcomment'] = (((can_perform('caneditown') AND $bugsys->userinfo['userid'] == $comment['userid']) OR can_perform('caneditothers')) ? true : false);
+       $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']));
+       
+       BSApp::debug('can edit own replies: ' . (int)(can_perform('caneditownreply', $bug['productid']) AND bugdar::$userinfo['userid'] == $comment['userid']));
+       BSApp::debug('can edit other replies:' . (int)(can_perform('caneditotherreply', $bug['productid']) AND bugdar::$userinfo['userid'] != $comment['userid']));
+       BSApp::debug('$show[editcomment]: ' . $show['editcomment']);
        
        if (is_array($hilight))
        {
                foreach ($hilight AS $id => $find)
                {
-                       $find = "#($find)#";
+                       $find = "#($find)#i";
                        $replace = "<span style=\"background-color: yellow; font-weight: bold; color: red;\">\\1</span>";
                        $comment['comment_parsed'] = preg_replace($find, $replace, $comment['comment_parsed']);
                }
        }
        
-       eval('$comments .= "' . $template->fetch('showreport_comment') . '";');
+       $tpl = new BSTemplate('showreport_comment');
+       $tpl->vars = array('comment' => $comment);
+       $temp = $tpl->evaluate()->getTemplate();
+       if ($description == null)
+       {
+               $description = $temp;
+       }
+       else
+       {
+               $comments .= $temp;
+       }
 }
 
-$show['newreply'] = ((can_perform('canpostcomments')) ? true : false);
+$show['newreply'] = (can_perform('canpostcomments', $bug['productid']) ? true : false);
 
-if (is_array($hilight))
+$bug['summary_title'] = $bug['summary'];
+if (is_array($hilight) AND !$show['edit'])
 {
        foreach ($hilight AS $id => $find)
        {
-               $find = "#($find)#";
+               $find = "#($find)#i";
                $replace = "<span style=\"background-color: yellow; font-weight: bold; color: red;\">\\1</span>";
                $bug['summary'] = preg_replace($find, $replace, $bug['summary']);
        }
 }
 
-eval('$template->flush("' . $template->fetch('showreport') . '");');
+$tpl = new BSTemplate('showreport');
+$tpl->vars = array(
+       'bug'                   => $bug,
+       'comments'              => $comments,
+       'select'                => $select,
+       'vote'                  => $vote,
+       'favoritetext'  => $favoritetext,
+       'customfields'  => $customfields,
+       'attachments'   => $attachments,
+       'productSelect' => $productSelect
+);
+$tpl->evaluate()->flush();
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file