Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / editreport.php
index 3f49d177fa27815d14671543ea78f82c5de27e13..09d941e46e1c4a76a984cab299131140433c990f 100644 (file)
 <?php
 /*=====================================================================*\
-|| ################################################################### ||
-|| # BugStrike [#]version[#]
-|| # --------------------------------------------------------------- # ||
-|| # All parts of this file are ©2002-[#]year[#] Iris Studios, Inc. No     # || 
-|| # part of this file may be reproduced in any way: part or whole.  # ||
-|| # --------------------------------------------------------------- # ||
-|| # ©2002 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com   # ||
-|| ################################################################### ||
+|| ###################################################################
+|| # Bugdar
+|| # Copyright 2002-2007 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(
-       'editreport',
-       'pcv_select_row'
-);
+
+$focus['showreport'] = 'focus';
 
 require_once('./global.php');
+require_once('./includes/functions_product.php');
+require_once('./includes/class_notification.php');
+require_once('./includes/api_bug.php');
+require_once('./includes/api_comment.php');
 
-$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 = " . $input->inputClean('bugid', TYPE_UINT)
 );
 
 if (!$bug)
 {
-       $message->error('alert: bad bug');
-}
-
-if (!(((can_perform('caneditown') AND $bugsys->userinfo['userid'] == $comment['userid']) OR can_perform('caneditothers')) AND can_perform('caneditinfo')))
-{
-       $message->error_permission();
+       $message->error(L_INVALID_ID);
 }
 
-if ($bug['hidden'] AND !can_perform('canviewhidden'))
+if (!check_bug_permissions($bug))
 {
-       $message->error_permission();
+       $message->errorPermission();
 }
 
 // setup logging
-require_once('./includes/class_history.php');
-$log = new History();
-$log->bugid = $bug['bugid'];
+require_once('./includes/class_logging.php');
+$log = new Logging();
+$log->setBugId($bug['bugid']);
+
+$notif = new NotificationCenter();
+
+$bugapi = new BugAPI();
+$bugapi->set('bugid', $input->in['bugid']);
+$bugapi->fetch();
 
 // ###################################################################
 
-if (empty($_REQUEST['do']))
+if ($_POST['do'] == 'kill')
 {
-       $_REQUEST['do'] = 'edit';
+       if (!can_perform('candeletedata', $bug['product']))
+       {
+               $message->errorPermission();
+       }
+       
+       $bugapi->remove();
+       
+       $message->redirect(T('The entire bug has been deleted.'), 'index.php');
 }
 
 // ###################################################################
 
-if ($_POST['do'] == 'update')
+if ($_REQUEST['do'] == 'delete')
 {
-       $pcv = parse_pcv_select($bugsys->in['pcv_select'], true);
-       
-       if (!$bugsys->in['summary'])
+       if (!can_perform('candeletedata', $bug['product']))
        {
-               $message->error('you need to enter a summary');
+               $message->errorPermission();
        }
-       if (!$pcv)
+       
+       $message->confirm(T('Are you sure you want to delete this bug? Doing so will <strong>destroy</strong> all associated data, including comments, attachments, and votes. We strongly recommend only deleting span records and nothing else as users may wish to go back and look at any bug to check its status.'), 'editreport.php', 'kill', T('Delete Bug Permanently'), 'showreport.php?bugid=' . $bug['bugid'], array('bugid' => $bug['bugid']));
+}
+
+// ###################################################################
+
+if ($_POST['do'] == 'update')
+{
+       if (!((can_perform('caneditown', $bug['product']) && bugdar::$userinfo['userid'] == $bug['userid']) || (can_perform('caneditother', $bug['product']) && bugdar::$userinfo['userid'] != $bug['userid'])) && !can_perform('canpostcomments', $bug['product']))
        {
-               $message->error('invalid product/component/version');
+               $message->errorPermission();
        }
        
-       $hist[0] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[bugid]");
-       $hist2[0] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = $bug[bugid]");
-       
-       process_custom_fields($bug['bugid']);
-       
-       $dependencies = preg_split('#([^0-9].*?)#', $bugsys->in['dependency'], -1, PREG_SPLIT_NO_EMPTY);
-       $dependencies = ((count($dependencies) < 1) ? '' : implode(', ', $dependencies));
-       
-       $db->query("
-               UPDATE " . TABLE_PREFIX . "bug
-               SET summary = '" . $bugsys->in['summary'] . "',
-                       priority = " . intval($bugsys->in['priority']) . ",
-                       status = " . intval($bugsys->in['status']) . ",
-                       severity = " . intval($bugsys->in['severity']) . ",
-                       resolution = " . intval($bugsys->in['resolution']) . ",
-                       assignedto = " . intval($bugsys->in['assignedto']) . ",
-                       duplicateof = " . intval($bugsys->in['duplicateof']) . ",
-                       dependency = '$dependencies',
-                       productid = " . $pcv['product'] . ",
-                       componentid = " . $pcv['component'] . ",
-                       versionid = " . $pcv['version'] . ",
-                       hidden = " . intval($bugsys->in['hidden']) . "
-               WHERE bugid = $bug[bugid]"
-       );
+       $log->addData(true, $bugapi->record, $log->getCommonFields(), true);
        
        // -------------------------------------------------------------------
-       // do diff history
-       
-       $hist[1] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[bugid]");
-       $hist2[1] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = $bug[bugid]");
-       
-       $diff[0] = array_diff_assoc($hist[0], $hist[1]);
-       $diff[1] = array_diff_assoc($hist[1], $hist[0]);
-       
-       $lookupindex = array(
-               'status' => 'status',
-               'severity' => 'severity',
-               'priority' => 'priority',
-               'versionid' => 'version',
-               'assignedto' => 'assignto',
-               'resolution' => 'resolution',
-               'productid' => 'product',
-               'componentid' => 'product'
-       );
-       
-       $log->language = 'log_update_bug';
-       
-       foreach ($diff AS $num => $diffs)
+       // handle automations
+       if ($input->in['automation'])
        {
-               foreach ($diffs AS $key => $value)
+               $automation = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "automation WHERE actionid = " . $input->inputClean('automation', TYPE_UINT));
+               if ($automation)
                {
-                       if (!isset($lookupindex["$key"]))
-                       {
-                               continue;
-                       }
-                       
-                       $ref = $lookupindex["$key"];
-                       $temp =& $bugsys->datastore["$ref"]["$value"];
-                       $thevalue = $temp["$ref"];
-                       $idbit = ' (id: ' . $temp["$ref" . 'id'] . ')';
-                       
-                       if ($key == 'assignedto')
+                       $automation['fields'] = unserialize($automation['fieldchanges']);
+
+                       if (is_array($automation['fields']['builtin']))
                        {
-                               $thevalue = (($temp['userid']) ? construct_user_display($temp) : '');
-                               $idbit = '';
+                               foreach ($automation['fields']['builtin'] AS $field => $value)
+                               {
+                                       $input->in["$field"] = $value;
+                               }
                        }
-                       else if ($key == 'productid' OR $key == 'componentid')
+
+                       if (is_array($automation['fields']['custom']))
                        {
-                               $ref = 'product';
-                               $thevalue = $temp['title'];
+                               foreach ($automation['fields']['custom'] AS $field => $value)
+                               {
+                                       $input->in["custom$field"] = $value;
+                               }
                        }
-                       
-                       $diff["$num"]["$key"] = (($thevalue) ? $thevalue . $idbit : '');
                }
        }
-       
-       foreach ($diff[1] AS $key => $value)
+
+       // -------------------------------------------------------------------
+       // process comment stuff
+       if ($input->in['comment'] OR $automation['comment'])
        {
-               $log->log($log->diff($key, $diff[0]["$key"], $diff[1]["$key"]));
+               if (!empty($input->in['comment']) AND $automation['comment'])
+               {
+                       $commenttext = $input->in['comment'] . "\n\n" . T('--------------- AUTOMATIC RESPONSE ---------------') . "\n" . $automation['comment'];
+               }
+               else if (empty($input->in['comment']) AND $automation['comment'])
+               {
+                       $commenttext = $automation['comment'];
+               }
+               else
+               {
+                       $commenttext = $input->in['comment'];
+               }
+               
+               $comment = new CommentAPI();
+               $comment->set('bugid',          $input->in['bugid']);
+               $comment->set('userid',         bugdar::$userinfo['userid']);
+               $comment->set('comment',        $commenttext);
+               $comment->set('parselinks',     $input->in['parselinks']);
+               $comment->insert();
+               
+               // we redefine the bug data later, but it needs to be here in order to generate the user list
+               $notif->setBugData($bugapi->record);
+               $notif->sendNewCommentNotice($comment->values);
+               
+               $bugapi->set('lastposttime',                    $comment->values['dateline']);
+               $bugapi->set('lastpostby',                              bugdar::$userinfo['userid']);
+               $bugapi->set('lastpostbyname',                  bugdar::$userinfo['displayname']);
+               $bugapi->set('hiddenlastposttime',              $comment->values['dateline']);
+               $bugapi->set('hiddenlastpostby',                bugdar::$userinfo['userid']);
+               $bugapi->set('hiddenlastpostbyname',    bugdar::$userinfo['displayname']);
+               
+               if (!((can_perform('caneditown', $bug['product']) AND bugdar::$userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND bugdar::$userinfo['userid'] != $bug['userid'])))
+               {
+                       $bugapi->update();
+                       $notif->finalize();
+                       $message->redirect(T('Your reply has been added to the comment list.'), "showreport.php?bugid=$bug[bugid]");
+               }
        }
        
-       $diff2[0] = array_diff_assoc($hist2[0], $hist2[1]);
-       $diff2[1] = array_diff_assoc($hist2[1], $hist2[0]);
+       // -------------------------------------------------------------------
+       // do update stuff      
+       $dependencies = preg_split('#([^0-9].*?)#', $input->in['dependency'], -1, PREG_SPLIT_NO_EMPTY);
+       $dependencies = ((sizeof($dependencies) < 1) ? '' : implode(', ', $dependencies));
        
-       foreach ($diff2[1] AS $key => $value)
+       if ((can_perform('caneditown', $bug['product']) && bugdar::$userinfo['userid'] == $bug['userid']) || (can_perform('caneditother', $bug['product']) && bugdar::$userinfo['userid'] != $bug['userid']))
        {
-               $log->log($log->diff('custom_' . $key, $diff2[0]["$key"], $diff2[1]["$key"]));
+               $bugapi->set('summary',         $input->in['summary']);
+               $bugapi->set('severity',        $input->in['severity']);
+               $bugapi->set('duplicateof',     $input->in['duplicateof']);
+               $bugapi->set('dependency',      $dependencies);
+               $bugapi->set('hidden',          $input->in['hidden']);
+       
+               $product = explode(',',         $input->in['product']);
+               $bugapi->set('product',         $product[0]);
+               $bugapi->set('component',       $product[1]);
+               $bugapi->set('version',         $product[2]);
        }
        
-       // -------------------------------------------------------------------
-       // process comment stuff
-       
-       if (!$bugsys->in['firstcomment'])
+       if (can_perform('canchangestatus', $bug['product']))
        {
-               $message->error('you need to enter some text in the first comment');
+               $bugapi->set('priority',        $input->in['priority']);
+               $bugapi->set('status',          $input->in['status']);
+               $bugapi->set('resolution',      $input->in['resolution']);
        }
-       
-       $bugsys->in['comment_parsed'] = $bugsys->in['firstcomment'];
-       
-       if (!$bugsys->options['allowhtml'])
+       if (can_perform('canassign', $bug['product']))
        {
-               $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
+               $bugapi->set('assignedto',      $input->in['assignedto']);
        }
        
-       // we could pass this as a GET param, but that's unsafe
-       $firstcomment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "comment WHERE bugid = $bug[bugid] ORDER BY dateline ASC LIMIT 1");
+       process_custom_fields($bugapi, $message, false);
        
-       $db->query("
-               UPDATE " . TABLE_PREFIX . "comment
-               SET comment = '" . $bugsys->in['firstcomment'] . "',
-                       comment_parsed = '" . nl2br($bugsys->in['comment_parsed']) . "'
-               WHERE commentid = $firstcomment[commentid]"
-       );
+       // -------------------------------------------------------------------
+       // handle logging and perform updates
        
-       $message->redirect('done with update bug', "showreport.php?bugid=$bug[bugid]");
-}
-
-// ###################################################################
-
-if ($_REQUEST['do'] == 'edit')
-{
-       $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', $bug['severity']);
+       $notif->setBugData($bugapi->record, array_merge($bugapi->record, $bugapi->values));
+       $log->addData(false, $bugapi->values, $log->getCommonFields(), true);
        
-       $show['changestatus'] = ((can_perform('canchangestatus')) ? true : false);
-       if (can_perform('canchangestatus'))
+       if (!$message->hasErrors())
        {
-               $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']);             
+               $bugapi->update();
        }
-       
-       $show['assign'] = ((can_perform('canassign')) ? true : false);
-       if (can_perform('canassign'))
+       else
        {
-               foreach ($bugsys->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') . '";');
-               }
+               $message->error();
        }
        
-       $pcv_select = construct_pcv_select("p$bug[productid]c$bug[componentid]v$bug[versionid]");
-       
-       $firstcomment = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "comment WHERE bugid = $bug[bugid] ORDER BY dateline ASC LIMIT 1");
-       
-       $customfields = construct_custom_fields($bug);
+       // -------------------------------------------------------------------
+       // do diff history
+
+       $log->updateHistory();
        
-       if ($bug['duplicateof'])
-       {
-               $duplicate = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[duplicateof]");
-       }
+       $notif->sendBugChangeNotice();
        
-       if ($bug['dependency'])
-       {
-               $dependencies = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($bug[dependency])");
-               while ($dependency = $db->fetch_array($dependencies))
-               {
-                       $depends[] = "<a href=\"showreport.php?bugid=$dependency[bugid]\" alt=\"$dependency[summary]\">$dependency[bugid]</a>";
-               }
-               $dependencies = implode(' ', $depends);
-       }
+       $notif->finalize();
        
-       eval('$template->flush("' . $template->fetch('editreport') . '");');
+       $message->redirect(T('Your changes to the bug have been saved.'), "showreport.php?bugid=$bug[bugid]");
 }
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file