Switch the 'modify' code of admin/field.php to use templates
[bugdar.git] / newreport.php
index 3beb5668477bb6d8d77ed786b3280737a304eea6..d4c9d3865c8ba5acf503175cd22891496395fc50 100755 (executable)
@@ -1,21 +1,42 @@
 <?php
 /*=====================================================================*\
-|| ################################################################### ||
-|| # [#]app[#] [#]version[#]
-|| # --------------------------------------------------------------- # ||
-|| # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No     # || 
-|| # part of this file may be reproduced in any way: part or whole.  # ||
-|| # --------------------------------------------------------------- # ||
-|| # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com   # ||
-|| ################################################################### ||
+|| ###################################################################
+|| # 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(
+       'newreport',
+);
+
+
+$focus['newreport'] = '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');
+
 
-if (!can_perform('cansubmitbugs'))
+$product = explode(',', $input->in['product']);
+if (!can_perform('cansubmitbugs', $product[0]))
 {
-       echo 'NO permission';
-       exit;
+       $message->errorPermission();
 }
 
 // ###################################################################
@@ -29,301 +50,150 @@ if (empty($_REQUEST['do']))
 
 if ($_POST['do'] == 'insert')
 {
-       sanitize(array(
-               'product' => INT,
-               'component' => INT,
-               'version' => INT,
-               'summary' => STR_NOHTML,
-               'severity' => INT,
-               'priority' => INT,
-               'status' => INT,
-               'resolution' => INT,
-               'assignedto' => INT,
-               'comment' => STR)
-       );
+       $bug = new BugAPI();
+       $comment = new CommentAPI();
+       
+       $notif = new NotificationCenter();
+               
+       $bug->set('userid',                     bugdar::$userinfo['userid']);
+       $bug->set('username',           bugdar::$userinfo['displayname']);
+       $bug->set('summary',            $input->in['summary']);
+       $bug->set('severity',           $input->in['severity']);
        
+       $comment->set('userid',         bugdar::$userinfo['userid']);
+       $comment->set('comment',        $input->in['comment']);
+       $comment->set('parselinks',     $input->in['parselinks']);
+       
+       // -------------------------------------------------------------------
        // check permissions on various input values
-       if (!can_perform('canchangestatus'))
+       if (!can_perform('canchangestatus', $input->in['product']))
        {
-               $vars['priority'] = $bugsys->options['defaultpriority'];
-               $vars['status'] = $bugsys->options['defaultstatus'];
-               $vars['resolution'] = $bugsys->options['defaultresolve'];
+               $bug->set('priority',   bugdar::$options['defaultpriority']);
+               $bug->set('status',             bugdar::$options['defaultstatus']);
+               $bug->set('resolution', bugdar::$options['defaultresolve']);
        }
        else
        {
-               if (!$bugsys->datastore['priority']["$vars[priority]"])
-               {
-                       $vars['priority'] = $bugsys->options['defaultpriority'];
-               }
-               if (!$bugsys->datastore['status']["$vars[status]"])
-               {
-                       $vars['status'] = $bugsys->options['defaultstatus'];
-               }
-               if (!$bugsys->datastore['resolution']["$vars[resolution]"])
-               {
-                       $vars['resolution'] = $bugsys->options['defaultresolve'];
-               }
+               $bug->set('priority',   $input->in['priority']);
+               $bug->set('status',             $input->in['status']);
+               $bug->set('resolution', $input->in['resolution']);
        }
-       if (!can_perform('canassign'))
+       if (!can_perform('canassign', $input->in['product']))
        {
-               $vars['assignedto'] = $bugsys->options['defaultassign'];
+               $bug->set('assignedto', bugdar::$options['defaultassign']);
        }
        else
        {
                // assigned person is not a dev or a valid user
-               if (!$bugsys->datastore['assignto']["$vars[assignedto]"]['userid'])
-               {
-                       $vars['assignedto'] = $bugsys->options['defaultassign'];
-               }
+               $bug->set('assignedto', $input->in['assignedto']);
        }
+               
+       $product = explode(',', $input->in['product']);
+       $bug->set('product',            $product[0]);
+       $bug->set('component',          $product[1]);
+       $bug->set('version',            $product[2]);
        
-       if (!$vars['product'] OR !$vars['component'] OR !$vars['version'])
-       {
-               echo 'there was a problem selecting the product, component, or version';
-               exit;
-       }
-       if (!$vars['summary'])
-       {
-               echo 'please enter a bug title';
-               exit;
-       }
-       if (!$vars['comment'])
-       {
-               echo 'please enter a bug description';
-               exit;
-       }
-       $product = $bugsys->datastore['product']["$vars[product]"];
-       if (!$product)
-       {
-               echo 'please select a valid product';
-               exit;
-       }
-       $version = $bugsys->datastore['version']["$vars[version]"];
-       if (!$version)
-       {
-               echo 'please select a valid version';
-               exit;
-       }
-       // no component
-       if ($vars['component'] == -1)
+       process_custom_fields($bug, $message, true);
+       
+       if (!$message->hasErrors())
        {
-               // not global version and version.productid != product.productid
-               if ($version['productid'] != 0 AND $version['productid'] != $product['productid'])
-               {
-                       echo 'invalid version specified';
-                       exit;
-               }
+               $bug->insert();
+               
+               $comment->set('bugid',                  $bug->insertid);
+               $comment->insert();
+               
+               $notif->sendNewBugNotice($bug->values, $comment->values);
+               
+               $bug = new BugAPI(); // need to destroy because update will think the insert fields need to be changed, too
+               $bug->set('bugid',                                      $comment->values['bugid']);
+               $bug->fetch();
+               $bug->set('dateline',                           $comment->values['dateline']);
+               $bug->set('initialreport',                      $comment->insertid);
+               $bug->set('lastposttime',                       $comment->values['dateline']);
+               $bug->set('lastpostby',                         bugdar::$userinfo['userid']);
+               $bug->set('lastpostbyname',                     bugdar::$userinfo['displayname']);
+               $bug->set('hiddenlastposttime',         $comment->values['dateline']);
+               $bug->set('hiddenlastpostby',           bugdar::$userinfo['userid']);
+               $bug->set('hiddenlastpostbyname',       bugdar::$userinfo['displayname']);
+               $bug->update();
+               
+               $notif->setBugData($bug->record);
+               
+               $notif->finalize();
+               
+               $message->redirect(T('The bug has been added to the database.'), ($input->in['submit_reload'] == '' ? "showreport.php?bugid=" . $bug->values['bugid'] : 'newreport.php'));
        }
-       // using a component
        else
        {
-               $component = $bugsys->datastore['product']["$vars[component]"];
-               // component has the right mother
-               if ($component['componentmother'] == $product['productid'])
-               {
-                       // version.productid != {component.productid | product.productid}
-                       if ($version['productid'] != $component['productid'] AND $version['productid'] != $product['productid'])
-                       {
-                               echo 'invalid version specified';
-                               exit;
-                       }
-               }
-               else
-               {
-                       echo 'invalid component specified';
-                       exit;
-               }
+               $show['errors'] = true;
+               $_REQUEST['do'] = 'add';
        }
-       
-       $vars['comment_parsed'] = $vars['comment'];
-       
-       if (!$bugsys->options['allowhtml'])
-       {
-               $vars['comment_parsed'] = htmlspecialcharslike($vars['comment_parsed']);
-       }
-       
-       // create the bug report
-       $DB_sql->query("
-               INSERT INTO " . TABLE_PREFIX . "bug
-                       (userid, productid, componentid, versionid, summary, severity, priority, status, assignedto, resolution)
-               VALUES
-                       (" . $bugsys->userinfo['userid'] . ", $vars[product], $vars[component], $vars[version],
-                       '" . addslasheslike($vars['summary']) . "', $vars[severity], $vars[priority], $vars[status], $vars[assignedto], $vars[resolution]
-               )"
-       );
-       
-       $bugid = $DB_sql->insert_id();
-       
-       // insert the comment to the database
-       $DB_sql->query("
-               INSERT INTO " . TABLE_PREFIX . "comment
-                       (bugid, userid, dateline, comment, comment_parsed 
-                       )
-               VALUES
-                       ($bugid, " . $bugsys->userinfo['userid'] . ",
-                       " . time() . ", '" . addslasheslike($vars['comment']) . "',
-                       '" . addslasheslike(nl2br($vars['comment_parsed'])) . "'
-               )"
-       );
-       
-       echo 'bug is done!';
 }
 
 // ###################################################################
 
 if ($_REQUEST['do'] == 'add')
 {
-       sanitize(array(
-               'product' => INT,
-               'component' => INT,
-               'version' => INT)
-       );
-       
-       // the user can hit the back button without reposting data...
-       if (!$vars['product'] OR !$vars['component'] OR !$vars['version'])
+       if (!is_array(bugdar::$datastore['product']))
        {
-               $method = 'get';
+               $message->error(T('No products have been setup, therefore no bugs can be added.'));
        }
-       else
+       if (!is_array(bugdar::$datastore['version']))
        {
-               $method = 'post';
+               $message->error(T('No versions have been setup underneath your product(s), therefore no bugs can be added.'));
        }
        
-       echo '<form name="newbug" action="newreport.php" method="' . $method . '">';
-       
-       $do = 'add';
+       $select['severity'] = construct_datastore_select('severity', 'severity', 'severityid', ($input->in['severity'] ? $input->in['severity'] : bugdar::$options['defaultseverity']));
+
+       $show['changestatus'] = can_perform('canchangestatus');
        
-       if (!$vars['product'])
+       if (can_perform('canchangestatus'))
        {
-               echo '<strong>Product:</strong> <select name="product">';
-               $products = $DB_sql->query("SELECT * FROM " . TABLE_PREFIX . "product WHERE !componentmother ORDER BY displayorder ASC");
-               while ($product = $DB_sql->fetch_array($products))
-               {
-                       echo "<option value=\"$product[productid]\">$product[title]</option>";
-               }
-               echo '</select>';
+               $select['priority'] = construct_datastore_select('priority', 'priority', 'priorityid', ($input->in['priority'] ? $input->in['priority'] : bugdar::$options['defaultpriority']));
+               $select['status'] = construct_datastore_select('status', 'status', 'statusid', ($input->in['status'] ? $input->in['status'] : bugdar::$options['defaultstatus']));
+               $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', ($input->in['resolution'] ? $input->in['resolution'] : bugdar::$options['defaultresolve']));
        }
-       else if (!$vars['component'])
+       
+       $show['assign'] = can_perform('canassign');
+       
+       if (can_perform('canassign'))
        {
-               echo '<strong>Component:</strong> <select name="component"><option value="-1">No Component</option>';
-               $components = $DB_sql->query("SELECT * FROM " . TABLE_PREFIX . "product WHERE componentmother IN ($vars[product]) ORDER BY displayorder ASC");
-               while ($component = $DB_sql->fetch_array($components))
+               foreach (bugdar::$datastore['assignto'] as $dev)
                {
-                       echo "<option value=\"$component[productid]\">$component[title]</option>";
+                       $tpl = new BSTemplate('selectoption');
+                       $tpl->vars = array(
+                               'value'         => $dev['userid'],
+                               'label'         => construct_user_display($dev, false),
+                               'selected'      => ($input->in['assignedto'] ? ($input->in['assignedto'] == $dev['userid']) : ($dev['userid'] == bugdar::$options['defaultassign']))
+                       );
+                       $select['dev'] = $tpl->evaluate()->getTemplate();
                }
-               echo '</select>';
-               echo '<input type="hidden" name="product" value="' . $vars['product'] . '" />';
        }
-       else if (!$vars['version'])
-       {
-               echo '<strong>Version:</strong> <select name="version">';
-               $versions = $DB_sql->query("
-                       SELECT version.*, product.componentmother, product.title AS productname
-                       FROM " . TABLE_PREFIX . "version AS version
-                       LEFT JOIN " . TABLE_PREFIX . "product ON (product.productid = version.productid)
-                       WHERE version.productid IN (0, $vars[product]" . iff($vars['component'] != -1, ", $vars[component]", '') . ")
-                       ORDER BY version.productid, version.displayorder ASC"
-               );
-               
-               while ($version = $DB_sql->fetch_array($versions))
-               {
-                       $versionlist["$version[productid]"][] = $version;
-                       $lookup["$version[productid]"] = array('componentmother' => $version['componentmother'], 'productname' => $version['productname']);
-               }
-               
-               foreach ($versionlist AS $productid => $versions)
-               {
-                       $prepend = '-- ';
-                       // global version
-                       if ($productid == 0)
-                       {
-                               echo '<optgroup label="Global Versions">';
-                       }
-                       // component
-                       else if ($lookup["$productid"]['componentmother'])
-                       {
-                               echo '<optgroup label="' . $lookup["$productid"]['productname'] . '">';
-                       }
-                       else
-                       {
-                               echo '<optgroup label="' . $lookup["$productid"]['productname'] . '">';
-                       }
-                       
-                       foreach ($versions AS $version)
-                       {
-                               echo '<option value="' . $version['versionid'] . '">' . $prepend . $version['version'] . '</option>';
-                       }
-                       
-                       echo '</optgroup>';
-               }
-               
-               echo '</select>';
-               echo '<input type="hidden" name="product" value="' . $vars['product'] . '" />';
-               echo '<input type="hidden" name="component" value="' . $vars['component'] . '" />';
-       }
-       else
+       
+       // custom fields
+       $fields = construct_custom_fields($input->in, true);            
+       $i = 0;
+       foreach ($fields AS $field)
        {
-               $do = 'insert';
-               echo '<div><strong>Summary/Title:</strong> <input type="text" name="summary" size="25" /></div>';
-               
-               echo '<div><strong>Severity:</strong> <select name="severity">';
-               foreach ($bugsys->datastore['severity'] AS $severity)
-               {
-                       echo '<option value="' . $severity['severityid'] . '">' . $severity['severity'] . '</option>';
-               }
-               echo '</select></div>';
-               
-               if (can_perform('canchangestatus'))
+               if ($i % 2 == 0)
                {
-                       echo '<div><strong>Priority:</strong> <select name="priority">';
-                       foreach ($bugsys->datastore['priority'] AS $priority)
-                       {
-                               echo '<option value="' . $priority['priorityid'] . '">' . $priority['priority'] . '</option>';
-                       }
-                       echo '</select></div>';
-                       
-                       echo '<div><strong>Status:</strong> <select name="status">';
-                       foreach ($bugsys->datastore['status'] AS $status)
-                       {
-                               echo '<option value="' . $status['statusid'] . '">' . $status['status'] . '</option>';
-                       }
-                       echo '</select></div>';
-                       
-                       echo '<div><strong>Resolution:</strong> <select name="resolution">';
-                       foreach ($bugsys->datastore['resolution'] AS $resolution)
-                       {
-                               echo '<option value="' . $resolution['resolutionid'] . '">' . $resolution['resolution'] . '</option>';
-                       }
-                       echo '</select></div>';
+                       $customfields['left'] .= $field;
                }
-               
-               if (can_perform('canassign'))
+               else
                {
-                       echo '<div><strong>Assigned to:</strong> <select name="assignedto"><option value="0">No Assignment</option>';
-                       foreach ($bugsys->datastore['assignto'] AS $dev)
-                       {
-                               fetch_user_display_name($dev);
-                               echo '<option value="' . $dev['userid'] . '">' . $dev['displayname'] . iff($dev['showemail'], ' <' . $dev['email'] . '>', '') . '</option>';
-                       }
-                       echo '</select></div>';
+                       $customfields['right'] .= $field;
                }
-               
-               echo '<div><strong>Detailed description:</strong><div><textarea name="comment" rows="15" cols="75"></textarea></div></div>';
-               
-               echo '<input type="hidden" name="product" value="' . $vars['product'] . '" />';
-               echo '<input type="hidden" name="component" value="' . $vars['component'] . '" />';
-               echo '<input type="hidden" name="version" value="' . $vars['version'] . '" />';
+               $i++;
        }
        
-       echo '<div><input type="hidden" name="do" value="' . $do . '" /><input type="submit" name="submit" value="  Proceed  " /></div>';
-       
-       echo '</form>';
+       $tpl = new BSTemplate('newreport');
+       $tpl->vars = array(
+               'reporter'              => construct_user_display(bugdar::$userinfo),
+               'productSelect' => construct_product_select('cansubmitbugs', $input->in['product'], false),
+               'select'                => $select,
+               'customfields'  => $customfields
+       );
+       $tpl->evaluate()->flush();
 }
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file