Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / editreport.php
index 97ceafc8b0bd9fb9e83c13023d817f0459adb5d9..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
+|| ###################################################################
 \*=====================================================================*/
 
-define('SVN', '$Id$');
 
 $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');
+       $message->error(L_INVALID_ID);
 }
 
-if (!(((can_perform('caneditown') AND $bugsys->userinfo['userid'] == $comment['userid']) OR can_perform('caneditothers')) AND can_perform('caneditinfo')))
+if (!check_bug_permissions($bug))
 {
-       $message->error_permission();
-}
-
-if ($bug['hidden'] AND !can_perform('canviewhidden'))
-{
-       $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 ($_POST['do'] == 'update')
+if ($_POST['do'] == 'kill')
 {
-       $pcv = parse_pcv_select($bugsys->in['pcv_select'], true);
-       
-       if (!$bugsys->in['summary'])
-       {
-               $message->error('you need to enter a summary');
-       }
-       if (!$pcv)
+       if (!can_perform('candeletedata', $bug['product']))
        {
-               $message->error('invalid product/component/version');
+               $message->errorPermission();
        }
        
-       $hist[0] = (array)$db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[bugid]");
-       $hist2[0] = (array)$temp = $noinitialcustom = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "bugvaluefill WHERE bugid = $bug[bugid]");
-       
-       // -------------------------------------------------------------------
-       // start updates
+       $bugapi->remove();
        
-       // auto action
-       $autoaction = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "autoaction WHERE actionid = " . intval($bugsys->in['autoaction']));
-       if ($autoaction)
+       $message->redirect(T('The entire bug has been deleted.'), 'index.php');
+}
+
+// ###################################################################
+
+if ($_REQUEST['do'] == 'delete')
+{
+       if (!can_perform('candeletedata', $bug['product']))
        {
-               $autoaction['fields'] = unserialize($autoaction['fieldchanges']);
-               
-               foreach ($autoaction['fields']['builtin'] AS $field => $value)
-               {
-                       $bugsys->in["$field"] = $value;
-               }
-               
-               foreach ($autoaction['fields']['custom'] AS $field => $value)
-               {
-                       $bugsys->in["field$field"] = $value;
-               }
+               $message->errorPermission();
        }
        
-       process_custom_fields($bug['bugid']);
-       
-       $dependencies = preg_split('#([^0-9].*?)#', $bugsys->in['dependency'], -1, PREG_SPLIT_NO_EMPTY);
-       $dependencies = ((count($dependencies) < 1) ? '' : implode(', ', $dependencies));
-       
-       // #*# need to put in permission checks here because we do not show the fields the user has no permission to change in the edit section so they'll be blank on update
+       $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->errorPermission();
+       }
        
-       $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] = (array)$db->query_first("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid = $bug[bugid]");
-       $hist2[1] = (array)$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)
-       {
-               $log->log($log->diff($key, $diff[0]["$key"], $diff[1]["$key"]));
-       }
-       
-       $diff2[0] = array_diff_assoc($hist2[0], $hist2[1]);
-       $diff2[1] = array_diff_assoc($hist2[1], $hist2[0]);
-       
-       if ($noinitialcustom === false)
+
+       // -------------------------------------------------------------------
+       // process comment stuff
+       if ($input->in['comment'] OR $automation['comment'])
        {
-               $canallowempty = true;
-               $checkbox = $db->query("SELECT * FROM " . TABLE_PREFIX . "bugfield WHERE type = 'input_checkbox'");
-               while ($box = $db->fetch_array($checkbox))
+               if (!empty($input->in['comment']) AND $automation['comment'])
                {
-                       $boxlist[] = 'field' . $box['fieldid'];
+                       $commenttext = $input->in['comment'] . "\n\n" . T('--------------- AUTOMATIC RESPONSE ---------------') . "\n" . $automation['comment'];
                }
-       }
-       
-       foreach ($diff2[1] AS $key => $value)
-       {
-               if (in_array($key, $boxlist) AND $canallowempty)
+               else if (empty($input->in['comment']) AND $automation['comment'])
                {
-                       $log->allowempty = true;
+                       $commenttext = $automation['comment'];
                }
                else
                {
-                       $log->allowempty = false;
+                       $commenttext = $input->in['comment'];
                }
                
-               if ($key == 'bugid')
+               $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'])))
                {
-                       continue;
+                       $bugapi->update();
+                       $notif->finalize();
+                       $message->redirect(T('Your reply has been added to the comment list.'), "showreport.php?bugid=$bug[bugid]");
                }
-               
-               $log->log($log->diff('custom_' . $key, $diff2[0]["$key"], $diff2[1]["$key"]));
        }
        
-       $log->allowempty = false;
+       // -------------------------------------------------------------------
+       // do update stuff      
+       $dependencies = preg_split('#([^0-9].*?)#', $input->in['dependency'], -1, PREG_SPLIT_NO_EMPTY);
+       $dependencies = ((sizeof($dependencies) < 1) ? '' : implode(', ', $dependencies));
+       
+       if ((can_perform('caneditown', $bug['product']) && bugdar::$userinfo['userid'] == $bug['userid']) || (can_perform('caneditother', $bug['product']) && bugdar::$userinfo['userid'] != $bug['userid']))
+       {
+               $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]);
+       }
+       
+       if (can_perform('canchangestatus', $bug['product']))
+       {
+               $bugapi->set('priority',        $input->in['priority']);
+               $bugapi->set('status',          $input->in['status']);
+               $bugapi->set('resolution',      $input->in['resolution']);
+       }
+       if (can_perform('canassign', $bug['product']))
+       {
+               $bugapi->set('assignedto',      $input->in['assignedto']);
+       }
+       
+       process_custom_fields($bugapi, $message, false);
        
        // -------------------------------------------------------------------
-       // process comment stuff
+       // handle logging and perform updates
        
-       $hascomment = (!empty($bugsys->in['comment'])) ? true : false;
+       $notif->setBugData($bugapi->record, array_merge($bugapi->record, $bugapi->values));
+       $log->addData(false, $bugapi->values, $log->getCommonFields(), true);
        
-       if ($hascomment OR $autoaction['comment'])
+       if (!$message->hasErrors())
        {
-               if ($hascomment AND $autoaction['comment'])
-               {
-                       $bugsys->in['comment'] .= "\n\n--------------- AUTOMATIC RESPONSE ---------------\n" . $autoaction['comment'];
-               }
-               else if (!$hascomment AND $autoaction['comment'])
-               {
-                       $bugsys->in['comment'] = $autoaction['comment'];
-               }
-               
-               $bugsys->in['comment_parsed'] = $bugsys->in['comment'];
-               
-               if (!$bugsys->options['allowhtml'])
-               {
-                       $bugsys->in['comment_parsed'] = $bugsys->sanitize($bugsys->in['comment_parsed']);
-               }
-               
-               $time = TIMENOW;
-               
-               $db->query("
-                       INSERT INTO " . TABLE_PREFIX . "comment
-                               (bugid, userid, dateline, comment, comment_parsed)
-                       VALUES
-                               (" . intval($bugsys->in['bugid']) . ", " . $bugsys->userinfo['userid'] . ",
-                               $time, '" . $bugsys->in['comment'] . "',
-                               '" . nl2br($bugsys->in['comment_parsed']) . "'
-                       )"
-               );
-               
-               $commentid = $db->insert_id();
-               
-               $db->query("
-                       UPDATE " . TABLE_PREFIX . "bug
-                       SET lastposttime = $time,
-                               lastpostby = " . $bugsys->userinfo['userid'] . ",
-                               hiddenlastposttime = $time,
-                               hiddenlastpostby = " . $bugsys->userinfo['userid'] . "
-                       WHERE bugid = " . intval($bugsys->in['bugid'])
-               );
-               
-               if ($hascomment)
-               {
-                       $log->language = 'log_new_comment';
-                       $log->arguments = array($commentid);
-                       $log->log();
-               }
+               $bugapi->update();
        }
+       else
+       {
+               $message->error();
+       }
+       
+       // -------------------------------------------------------------------
+       // do diff history
 
-       $message->redirect('done with update bug', "showreport.php?bugid=$bug[bugid]");
+       $log->updateHistory();
+       
+       $notif->sendBugChangeNotice();
+       
+       $notif->finalize();
+       
+       $message->redirect(T('Your changes to the bug have been saved.'), "showreport.php?bugid=$bug[bugid]");
 }
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file