Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / editreport.php
index 4492deedb6e487a65273d54aadbfc4e7d0d04c5b..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'
-);
-
-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 (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'])
-       {
-               $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
-       
-       // auto action
-       $autoaction = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "autoaction WHERE actionid = " . intval($bugsys->in['autoaction']));
-       if ($autoaction)
+       $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']))
        {
-               $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
-       
-       $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;
-       
        // -------------------------------------------------------------------
-       // process comment stuff
-       /*
-       if (!$bugsys->in['firstcomment'])
+       // 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']))
        {
-               $message->error('you need to enter some text in the first comment');
+               $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]);
        }
        
-       $bugsys->in['comment_parsed'] = $bugsys->in['firstcomment'];
-       
-       if (!$bugsys->options['allowhtml'])
+       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']))
        {
-               $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]"
-       );
-       */
        // -------------------------------------------------------------------
-       // auto action comment
+       // handle logging and perform updates
        
-       if ($autoaction['comment'])
+       $notif->setBugData($bugapi->record, array_merge($bugapi->record, $bugapi->values));
+       $log->addData(false, $bugapi->values, $log->getCommonFields(), true);
+       
+       if (!$message->hasErrors())
        {
-               $comment = $autoaction['comment'];
-               if (!$bugsys->options['allowhtml'])
-               {
-                       $comment_parsed = $bugsys->sanitize($comment);
-               }
-               
-               $db->query("
-                       INSERT INTO comment
-                               (bugid, userid, dateline, comment, comment_parsed)
-                       VALUES
-                               ($bug[bugid], " . $bugsys->userinfo['userid'] . ", " . LOG_TIME . ",
-                               '" . $bugsys->escape($comment) . "',
-                               '" . $bugsys->escape($comment_parsed) . "'
-                       )"
-               );
-               
-               $db->query("
-                       UPDATE " . TABLE_PREFIX . "bug
-                       SET lastposttime = " . LOG_TIME . ",
-                               lastpostby = " . $bugsys->userinfo['userid'] . ",
-                               hiddenlastposttime = " . LOG_TIME . ",
-                               hiddenlastpostby = " . $bugsys->userinfo['userid'] . "
-                       WHERE bugid = $bug[bugid]"
-               );
+               $bugapi->update();
+       }
+       else
+       {
+               $message->error();
        }
        
-       $message->redirect('done with update bug', "showreport.php?bugid=$bug[bugid]");
+       // -------------------------------------------------------------------
+       // do diff history
+
+       $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