Squash-merging the ISSO3 branch back onto master
[bugdar.git] / search.php
index e22c3b6bb79a2e5c3868db4deac12a955b66db6b..4b323e2ef39fdcb9849b91808a6d022d96075417 100644 (file)
@@ -30,7 +30,6 @@ $fetchtemplates = array(
        'pagenav'
 );
 
-define('SVN', '$Id$');
 
 $focus['search'] = 'focus';
 
@@ -39,9 +38,6 @@ require_once('./includes/functions_product.php');
 require_once('./includes/class_sort.php');
 require_once('./includes/class_logging.php');
 require_once('./includes/api_bug.php');
-require_once('./includes/class_api_error.php');
-
-APIError(array($message, 'error'));
 
 if (!can_perform('cansearch'))
 {
@@ -52,7 +48,7 @@ define('MODE_ANY', 1);
 define('MODE_ALL', 2);
 define('MODE_RAW', 3);
 
-$var = $db->query_first("SHOW VARIABLES LIKE 'ft_min_word_len'");
+$var = $db->queryFirst("SHOW VARIABLES LIKE 'ft_min_word_len'");
 define('SEARCH_WORD_MIN', $var['Value']);
 
 $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = 0 AND dateline < " . (TIMENOW - 3600));
@@ -70,17 +66,17 @@ if (empty($_REQUEST['do']))
 
 if ($_REQUEST['do'] == 'search')
 {
-       if ($bugsys->in['new'])
+       if ($input->in['new'])
        {
                $newsearch = true;
        }
-       else if ($bugsys->in['searchid'])
+       else if ($input->in['searchid'])
        {
-               $cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
+               $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
        }
-       else if ($bugsys->userinfo['userid'])
+       else if (bugdar::$userinfo['userid'])
        {
-               $cachedsearch = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
+               $cachedsearch = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . bugdar::$userinfo['userid']);
        }
        else
        {
@@ -90,11 +86,11 @@ if ($_REQUEST['do'] == 'search')
        if ($cachedsearch)
        {
                $show['cached'] = true;
-               if ($cachedsearch['dateline'] < TIMENOW - 900 OR $bugsys->in['rerun'])
+               if ($cachedsearch['dateline'] < TIMENOW - 900 OR $input->in['rerun'])
                {
                        $_REQUEST['do'] = 'process';
-                       $bugsys->in = array_merge(unserialize($cachedsearch['query']), $bugsys->in);
-                       $bugsys->debug('rerunning the search');
+                       $input->in = array_merge(unserialize($cachedsearch['query']), $input->in);
+                       BSApp::debug('rerunning the search');
                }
                else
                {
@@ -109,18 +105,16 @@ if ($_REQUEST['do'] == 'search')
        
        if ($newsearch)
        {
-               if (!is_array($bugsys->datastore['product']))
+               if (!is_array(bugdar::$datastore['product']))
                {
                        $message->error(T('No products are setup, therefore there can be no bugs and thus search cannot function.'));
                }
                
-               if (!is_array($bugsys->datastore['version']))
+               if (!is_array(bugdar::$datastore['version']))
                {
                        $message->error(T('No versions have been added underneath your product(s), there can be no bugs and thus search cannot function.'));
                }
                
-               $productSelect = ConstructProductSelect();
-               
                // -------------------------------------------------------------------
                // custom fields
                $fields = construct_custom_fields(null, true, false, true);             
@@ -146,26 +140,39 @@ if ($_REQUEST['do'] == 'search')
                $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid');
                
                $searches = '';
-               if ($bugsys->userinfo['userid'])
+               if (bugdar::$userinfo['userid'])
                {
-                       $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . $bugsys->userinfo['userid']);
-                       while ($search = $db->fetch_array($searchesFetch))
+                       $searchesFetch = $db->query("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NOT NULL AND userid = " . bugdar::$userinfo['userid']);
+                       foreach ($searchesFetch as $search)
                        {
-                               $value = $search['searchid'];
-                               $label = $search['name'];
-                               eval('$searches .= "' . $template->fetch('selectoption') . '";');
+                               $tpl = new BSTemplate('selectoption');
+                               $tpl->vars = array(
+                                       'value' => $search['searchid'],
+                                       'label' => $search['name']
+                               );
+                               $searches .= $tpl->evaluate()->getTemplate();
                        }
                }
                
                $select['dev'] = '';
-               foreach ($bugsys->datastore['assignto'] AS $dev)
+               foreach (bugdar::$datastore['assignto'] AS $dev)
                {
-                       $value = $dev['userid'];
-                       $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)
+                       );
+                       $select['dev'] .= $tpl->evaluate()->getTemplate();
                }
-                               
-               eval('$template->flush("' . $template->fetch('search') . '");');
+               
+               $tpl = new BSTemplate('search');
+               $tpl->vars = array(
+                       'select'                => $select,
+                       'productSelect' => construct_product_select(),
+                       'customfields'  => $customfields,
+                       'searches'              => $searches
+               );
+               $tpl->evaluate()->flush();
        }
 }
 
@@ -175,9 +182,9 @@ if ($_REQUEST['do'] == 'process')
 {
        // -------------------------------------------------------------------
        // handle keywords
-       if ($bugsys->in['summary'])
+       if ($input->in['summary'])
        {
-               $keywords = preg_split('#\s+#', $bugsys->in['summary']);
+               $keywords = preg_split('#\s+#', $input->in['summary']);
                
                // TODO - need to have some str to bool conversions
                
@@ -188,7 +195,7 @@ if ($_REQUEST['do'] == 'process')
                                continue;
                        }
                        
-                       if ($bugsys->in['mode'] == MODE_ALL)
+                       if ($input->in['mode'] == MODE_ALL)
                        {
                                $querybuild['text'] .= " +$word";
                        }
@@ -209,7 +216,7 @@ if ($_REQUEST['do'] == 'process')
                
                $temp = trim($querybuild['text']);
                
-               if ($bugsys->in['mode'] == MODE_ALL OR $bugsys->in['mode'] == MODE_RAW)
+               if ($input->in['mode'] == MODE_ALL OR $input->in['mode'] == MODE_RAW)
                {
                        $bool_flag = ' IN BOOLEAN MODE';
                }
@@ -219,11 +226,11 @@ if ($_REQUEST['do'] == 'process')
        
        // -------------------------------------------------------------------
        // reporter
-       if ($bugsys->in['reporter'])
+       if ($input->in['reporter'])
        {
                // force email or name?? make a distinction?
                // more elegant way to do this? probably
-               $user = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $bugsys->input_escape('reporter')) . "%'");
+               $user = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "user WHERE email LIKE '%" . str_replace('%', '\%', $input->inputEscape('reporter')) . "' OR displayname LIKE '%" . str_replace('%', '\%', $input->inputEscape('reporter')) . "%'");
                if ($user['userid'])
                {
                        $querybuild['reporter'] = "AND bug.userid = $user[userid] OR comment.userid = $user[userid]";
@@ -232,12 +239,12 @@ if ($_REQUEST['do'] == 'process')
        
        // -------------------------------------------------------------------
        // product/component/version stuff
-       if (is_array($bugsys->in['product']))
+       if (is_array($input->in['product']))
        {
-               foreach ($bugsys->in['product'] AS $prod)
+               foreach ($input->in['product'] AS $prod)
                {
                        $product = explode(',', $prod);
-                       $product = $bugsys->clean($product, TYPE_UINT);
+                       $product = $input->clean($product, TYPE_UINT);
                        $products[] = $product[0];
                        $components[] = $product[1];
                        $versions[] = $product[2];
@@ -249,72 +256,72 @@ if ($_REQUEST['do'] == 'process')
        // severity, priority, status, resolution, assignedto
        
        // severity
-       if ($bugsys->in['severity'])
+       if ($input->in['severity'])
        {
-               $bugsys->input_clean('severity', TYPE_UINT);
-               $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $bugsys->in['severity']) . ")";
+               $input->inputClean('severity', TYPE_UINT);
+               $querybuild['severity'] = "AND bug.severity IN (" . implode(',', $input->in['severity']) . ")";
        }
        
        // priority
-       if ($bugsys->in['priority'])
+       if ($input->in['priority'])
        {
-               $bugsys->input_clean('priority', TYPE_UINT);
-               $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $bugsys->in['priority']) . ")";
+               $input->inputClean('priority', TYPE_UINT);
+               $querybuild['priority'] = "AND bug.priority IN (" . implode(',', $input->in['priority']) . ")";
        }
        
        // status
-       if ($bugsys->in['status'])
+       if ($input->in['status'])
        {
-               $bugsys->input_clean('status', TYPE_UINT);
-               $querybuild['status'] = "AND bug.status IN (" . implode(',', $bugsys->in['status']) . ")";
+               $input->inputClean('status', TYPE_UINT);
+               $querybuild['status'] = "AND bug.status IN (" . implode(',', $input->in['status']) . ")";
        }
        
        // resolution
-       if ($bugsys->in['resolution'])
+       if ($input->in['resolution'])
        {
-               $bugsys->input_clean('resolution', TYPE_UINT);
-               $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $bugsys->in['resolution']) . ")";
+               $input->inputClean('resolution', TYPE_UINT);
+               $querybuild['resolution'] = "AND bug.resolution IN (" . implode(',', $input->in['resolution']) . ")";
        }
        
        // assignment
-       if ($bugsys->in['assignedto'])
+       if ($input->in['assignedto'])
        {
-               $bugsys->input_clean('assignedto', TYPE_UINT);
-               $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $bugsys->in['assignedto']) . ")";
+               $input->inputClean('assignedto', TYPE_UINT);
+               $querybuild['assignedto'] = "AND bug.assignedto IN (" . implode(',', $input->in['assignedto']) . ")";
        }
        
        // -------------------------------------------------------------------
        // date
-       if ($bugsys->in['date'])
+       if ($input->in['date'])
        {
                // now - (seconds/day * number of days)
-               $dateline = time() - ($bugsys->input_clean('date', TYPE_INT) * 3600);
+               $dateline = time() - ($input->inputClean('date', TYPE_INT) * 3600);
                $querybuild['date'] = "AND bug.dateline >= $dateline";
        }
        
        // -------------------------------------------------------------------
        // favorites
-       $bugsys->input_clean('favorite', TYPE_INT);
-       if ($bugsys->in['favorite'] != 0 AND $bugsys->userinfo['userid'])
+       $input->inputClean('favorite', TYPE_INT);
+       if ($input->in['favorite'] != 0 AND bugdar::$userinfo['userid'])
        {
-               $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . $bugsys->userinfo['userid']);
-               while ($favorite = $db->fetch_array($favorites))
+               $favorites = $db->query("SELECT * FROM " . TABLE_PREFIX . "favorite WHERE userid = " . bugdar::$userinfo['userid']);
+               foreach ($favorites as $favorite)
                {
                        $ids[] = $favorite['bugid'];
                }
-               $querybuild['favorites'] = "AND bug.bugid " . ($bugsys->in['favorite'] > 0 ? "IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
+               $querybuild['favorites'] = "AND bug.bugid " . ($input->in['favorite'] > 0 ? "IN" : "NOT IN") . " (" . implode(', ', $ids) . ")";
        }
        
        // -------------------------------------------------------------------
        // sort by
        $sortby = array('bugid', 'severity', 'priority', 'status', 'resolution', 'dateline');
        $orderby = array('ASC', 'DESC');
-       $bugsys->in['orderby'] = strtoupper($bugsys->in['orderby']);
-       if (in_array($bugsys->in['sortby'], $sortby) AND in_array($bugsys->in['orderby'], $orderby))
+       $input->in['orderby'] = strtoupper($input->in['orderby']);
+       if (in_array($input->in['sortby'], $sortby) AND in_array($input->in['orderby'], $orderby))
        {
-               $sortclause = "ORDER BY " . $bugsys->in['sortby'] . ' ' . $bugsys->in['orderby'];
+               $sortclause = "ORDER BY " . $input->in['sortby'] . ' ' . $input->in['orderby'];
        }
-       else if ($bugsys->in['sortby'] == 'relevance')
+       else if ($input->in['sortby'] == 'relevance')
        {
                $sortclause = '';
        }
@@ -325,32 +332,32 @@ if ($_REQUEST['do'] == 'process')
        
        // -------------------------------------------------------------------
        // custom fields
-       $fields_fetch = $bugsys->db->query("
+       $fields_fetch = $db->query("
                SELECT bugfield.*, MAX(permission.mask) AS mask
                FROM " . TABLE_PREFIX . "bugfield AS bugfield
                LEFT JOIN " . TABLE_PREFIX . "bugfieldpermission AS permission
                        ON (bugfield.fieldid = permission.fieldid)
                WHERE mask <> 0
-               AND permission.usergroupid IN ({$bugsys->userinfo['usergroupid']}" . (sizeof($bugsys->userinfo['groupids']) != 0 ? ',' . implode(',', $bugsys->userinfo['groupids']) : '') . ")
+               AND permission.usergroupid IN (" . bugdar::$userinfo['usergroupid'] . (sizeof(bugdar::$userinfo['groupids']) != 0 ? ',' . implode(',', bugdar::$userinfo['groupids']) : '') . ")
                AND bugfield.cansearch = 1
                GROUP BY (bugfield.fieldid)"
        );
-       while ($field = $bugsys->db->fetch_array($fields_fetch))
+       foreach ($fields_fetch as $field)
        {
-               if (!empty($bugsys->in["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($bugsys->in["custom$field[fieldid]"])))
+               if (!empty($input->in["custom$field[fieldid]"]) OR ($field['type'] == 'select_single' AND isset($input->in["custom$field[fieldid]"])))
                {
-                       if ($field['type'] == 'input_checkbox' AND $bugsys->input_clean("custom$field[fieldid]", TYPE_INT) != 0)
+                       if ($field['type'] == 'input_checkbox' AND $input->inputClean("custom$field[fieldid]", TYPE_INT) != 0)
                        {
-                               $querybuild[] = "AND bug.custom$field[fieldid] = " . ($bugsys->in["custom$field[fieldid]"] > 0 ? 1 : 0);
+                               $querybuild[] = "AND bug.custom$field[fieldid] = " . ($input->in["custom$field[fieldid]"] > 0 ? 1 : 0);
                        }
                        else if ($field['type'] == 'input_text')
                        {
-                               $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%" . $bugsys->in["custom$field[fieldid]"] . "%'";
+                               $querybuild[] = "AND bug.custom$field[fieldid] LIKE '%" . $input->in["custom$field[fieldid]"] . "%'";
                        }
-                       else if ($field['type'] == 'select_single' AND $bugsys->in["custom$field[fieldid]"] != -1)
+                       else if ($field['type'] == 'select_single' AND $input->in["custom$field[fieldid]"] != -1)
                        {
                                $temp = unserialize($field['selects']);
-                               $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($bugsys->in["custom$field[fieldid]"]) ]) . "'";
+                               $querybuild[] = "AND bug.custom$field[fieldid] = '" . trim($temp[ intval($input->in["custom$field[fieldid]"]) ]) . "'";
                        }
                }
        }
@@ -378,33 +385,33 @@ if ($_REQUEST['do'] == 'process')
                                OR
                        (bug.hidden AND bug.product IN (" . fetch_on_bits('canviewhidden') . "))" . (can_perform('canviewownhidden') ? "
                                OR
-                       (bug.hidden AND bug.userid = " . $bugsys->userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
+                       (bug.hidden AND bug.userid = " . bugdar::$userinfo['userid'] . " AND bug.product IN (" . fetch_on_bits('canviewonhidden') . "))" : "") . "
                )
                " . implode("\n\t\t", $querybuild) . "
                GROUP BY bug.bugid
                $sortclause
        ");
        
-       $numrows = $db->num_rows($search);
+       $numrows = $search->size();
        
        if ($numrows < 1)
        {
                $message->error(T('No search results were returned that matched your criteria. Please <a href="search.php?new=1">try again</a> with different search requirements.'));
        }
        
-       while ($result = $db->fetch_array($search))
+       foreach ($search as $result)
        {
                $ids[] = $result['bugid'];
                $results[] = $result;
        }
                
-       if ($bugsys->userinfo['userid'] AND !$cachedsearch AND !$bugsys->in['rerun'])
+       if (bugdar::$userinfo['userid'] AND !$cachedsearch AND !$input->in['rerun'])
        {
-               $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . $bugsys->userinfo['userid'] . " AND name IS NULL");
+               $db->query("DELETE FROM " . TABLE_PREFIX . "search WHERE userid = " . bugdar::$userinfo['userid'] . " AND name IS NULL");
        }
        
        // store the search params
-       $params = $bugsys->in;
+       $params = $input->in;
        foreach ($_COOKIE AS $key => $value)
        {
                unset($params["$key"]);
@@ -423,14 +430,14 @@ if ($_REQUEST['do'] == 'process')
                        INSERT INTO " . TABLE_PREFIX . "search
                                (userid, dateline, query, ids, orderby, hilight, resultcount)
                        VALUES
-                               (" . $bugsys->userinfo['userid'] . ",
-                               " . TIMENOW . ", '" . $bugsys->escape(serialize($params)) . "',
-                               '" . implode(',', $ids) . "', '" . $bugsys->escape($sortclause) . "',
-                               '" . $bugsys->escape($hilight) . "',
+                               (" . bugdar::$userinfo['userid'] . ",
+                               " . TIMENOW . ", '" . $input->escape(serialize($params)) . "',
+                               '" . implode(',', $ids) . "', '" . $input->escape($sortclause) . "',
+                               '" . $input->escape($hilight) . "',
                                " . sizeof($results) . "
                        )"
                );
-               $search = array('searchid' => $db->insert_id(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
+               $search = array('searchid' => $db->insertId(), 'ids' => implode(',', $ids), 'orderby' => $sortclause, 'hilight' => $hilight, 'resultcount' => sizeof($results));
        }
        
        $_POST['do'] = 'results';
@@ -440,14 +447,12 @@ if ($_REQUEST['do'] == 'process')
 
 if ($_REQUEST['do'] == 'update')
 {
-       $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
+       $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
        if (!$search OR !can_perform('caneditother'))
        {
                $message->errorPermission();
        }
        
-       $productSelect = ConstructProductSelect();
-       
        $show['update'] = true;
        
        // -------------------------------------------------------------------
@@ -474,27 +479,39 @@ if ($_REQUEST['do'] == 'update')
        $select['status'] = construct_datastore_select('status', 'status', 'statusid', 0, 0);
        $select['resolution'] = construct_datastore_select('resolution', 'resolution', 'resolutionid', 0, 0);
        
-       $select['dev'] = '';
-       $value = '0';
-       $label = '';
-       $selected = true;
-       eval('$select[dev] .= "' . $template->fetch('selectoption') . '";');
-       $selected = false;
-       foreach ($bugsys->datastore['assignto'] AS $dev)
+       $tpl = new BSTemplate('selectoption');
+       $tpl->vars = array(
+               'value'         => 0,
+               'label'         => '',
+               'selected'      => true
+       );
+       $select['dev'] = $tpl->evaluate()->getTemplate();
+
+       foreach (bugdar::$datastore['assignto'] as $dev)
        {
-               $value = $dev['userid'];
-               $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)
+               );
+               $select['dev'] .= $tpl->evaluate()->getTemplate();
        }
-               
-       eval('$template->flush("' . $template->fetch('search_update') . '");');
+       
+       $tpl = new BSTemplate('search_update');
+       $tpl->vars = array(
+               'search'                => $search,
+               'select'                => $select,
+               'customfields'  => $customfields,
+               'productSelect' => construct_product_select()
+       );
+       $tpl->evaluate()->flush();
 }
 
 // ###################################################################
 
 if ($_POST['do'] == 'doupdate')
 {
-       $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
+       $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
        if (!$search OR !can_perform('caneditother'))
        {
                $message->errorPermission();
@@ -509,48 +526,47 @@ if ($_POST['do'] == 'doupdate')
                        (
                                product IN (" . fetch_on_bits('caneditother') . ")
                                        OR
-                               (userid = " . $bugsys->userinfo['userid'] . " AND product IN (" . fetch_on_bits('caneditown') . "))
+                               (userid = " . bugdar::$userinfo['userid'] . " AND product IN (" . fetch_on_bits('caneditown') . "))
                        )
        ");
-       while ($bug = $db->fetch_array($bugs))
+       foreach ($bugs as $bug)
        {
-               if (!((can_perform('caneditown', $bug['product']) AND $bugsys->userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND $bugsys->userinfo['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['product']))
+               if (!((can_perform('caneditown', $bug['product']) AND bugdar::$userinfo['userid'] == $bug['userid']) OR (can_perform('caneditother', $bug['product']) AND bugdar::$userinfo['userid'] != $bug['userid'])) AND !can_perform('canpostcomments', $bug['product']))
                {
                        continue;
                }
                                
-               $api = new BugApi($bugsys);
+               $api = new BugApi();
                $api->set('bugid', $bug['bugid']);
-               $api->set_condition();
                $api->values = $bug;
                                
                $log = new Logging();
-               $log->set_bugid($bug['bugid']);
-               $log->add_data(true, $bug, $log->getCommonFields(), true);
+               $log->setBugId($bug['bugid']);
+               $log->addData(true, $bug, $log->getCommonFields(), true);
 
-               if ($bugsys->in['status'] AND can_perform('canchangestatus', $bug['product']))
+               if ($input->in['status'] AND can_perform('canchangestatus', $bug['product']))
                {
-                       $api->set('status',             $bugsys->in['status']);
+                       $api->set('status',             $input->in['status']);
                }
-               if ($bugsys->in['priority'] AND can_perform('canchangestatus', $bug['product']))
+               if ($input->in['priority'] AND can_perform('canchangestatus', $bug['product']))
                {
-                       $api->set('priority',   $bugsys->in['priority']);
+                       $api->set('priority',   $input->in['priority']);
                }
-               if ($bugsys->in['severity'])
+               if ($input->in['severity'])
                {
-                       $api->set('severity',   $bugsys->in['severity']);
+                       $api->set('severity',   $input->in['severity']);
                }
-               if ($bugsys->in['resolution'] AND can_perform('canchangestatus', $bug['product']))
+               if ($input->in['resolution'] AND can_perform('canchangestatus', $bug['product']))
                {
-                       $api->set('resolution', $bugsys->in['resolution']);
+                       $api->set('resolution', $input->in['resolution']);
                }
-               if ($bugsys->in['assignedto'] AND can_perform('canassign', $bug['product']))
+               if ($input->in['assignedto'] AND can_perform('canassign', $bug['product']))
                {
-                       $api->set('assignedto', $bugsys->in['assignedto']);
+                       $api->set('assignedto', $input->in['assignedto']);
                }
-               if ($bugsys->in['product'])
+               if ($input->in['product'])
                {
-                       $product = explode(',', $bugsys->in['product']);
+                       $product = explode(',', $input->in['product']);
                        $api->set('product',    $product[0]);
                        $api->set('component',  $product[1]);
                        $api->set('version',    $product[2]);
@@ -558,26 +574,26 @@ if ($_POST['do'] == 'doupdate')
                
                process_custom_fields($api, $message, false, true);
                
-               $log->add_data(false, $api->values, $log->getCommonFields(), true);
+               $log->addData(false, $api->values, $log->getCommonFields(), true);
                                
                $api->update();
-               $log->update_history();
+               $log->updateHistory();
        }
        
-       $message->redirect(T('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $bugsys->in['searchid']);
+       $message->redirect(T('The specified bugs have been updated and you will now return to your search results.'), 'search.php?searchid=' . $input->in['searchid']);
 }
 
 // ###################################################################
 
 if ($_REQUEST['do'] == 'export')
 {
-       if (!$bugsys->in['searchid'] AND $bugsys->userinfo['userid'])
+       if (!$input->in['searchid'] AND bugdar::$userinfo['userid'])
        {
-               $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . $bugsys->userinfo['userid']);
+               $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE name IS NULL AND userid = " . bugdar::$userinfo['userid']);
        }
-       else if ($bugsys->in['searchid'])
+       else if ($input->in['searchid'])
        {
-               $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
+               $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
        }
        else
        {
@@ -593,9 +609,9 @@ if ($_REQUEST['do'] == 'export')
        
        $xml = '<?xml version="1.0" encoding="' . $language['charset'] . '"?>
 
-<bugdarExport user="' . $bugsys->unsanitize(construct_user_display($bugsys->userinfo, false)) . '" date="' . $datef->format('r', TIMENOW) . '" searchid="' . $search['searchid'] . '">';
+<bugdarExport user="' . $input->unsanitize(construct_user_display(bugdar::$userinfo, false)) . '" date="' . $datef->format('r', TIMENOW) . '" searchid="' . $search['searchid'] . '">';
 
-       while ($bug = $db->fetch_array($bugs))
+       foreach ($bugs as $bug)
        {
                $xml .= "\n\t<bug>";
                
@@ -605,7 +621,7 @@ if ($_REQUEST['do'] == 'export')
                $xml .= "\n\t\t<dateReported>" . $datef->format('r', $bug['dateline']) . "</dateReported>";
                if ($bug['userid'])
                {
-                       $xml .= "\n\t\t<reporter>" . construct_user_display($db->query_first("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $bug[userid]"), false) . "</reporter>";
+                       $xml .= "\n\t\t<reporter>" . construct_user_display($db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "user WHERE userid = $bug[userid]"), false) . "</reporter>";
                }
                $xml .= "\n\t\t<summary>" . $bug['summary'] . "</summary>";
                $xml .= "\n\t\t<product>" . $bug['product'] . "</product>";
@@ -617,7 +633,7 @@ if ($_REQUEST['do'] == 'export')
                $xml .= "\n\t\t<status>" . $bug['status'] . "</status>";
                if ($bug['assignedto'])
                {
-                       $xml .= "\n\t\t<assignedTo>" . construct_user_display($bugsys->datastore['assignto']["$bug[assignedto]"], false) . "</assignedTo>";
+                       $xml .= "\n\t\t<assignedTo>" . construct_user_display(bugdar::$datastore['assignto']["$bug[assignedto]"], false) . "</assignedTo>";
                }
                $xml .= "\n\t\t<resolution>" . $bug['resolution'] . "</resolution>";
                $xml .= "\n\t\t<severity>" . $bug['severity'] . "</severity>";
@@ -628,14 +644,14 @@ if ($_REQUEST['do'] == 'export')
        
        $xml .= "\n</bugdarExport>";
        
-       $funct->download_file($xml, T('bugdar-search-' . $search['searchid'] . '.xml'), true);
+       BSFunctions::download_file($xml, T('bugdar-search-' . $search['searchid'] . '.xml'), true);
 }
 
 // ###################################################################
 
 if ($_POST['do'] == 'dosave')
 {
-       $search = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $bugsys->input_clean('searchid', TYPE_UINT) . " AND userid = " . $bugsys->userinfo['userid']);
+       $search = $db->queryFirst("SELECT * FROM " . TABLE_PREFIX . "search WHERE searchid = " . $input->inputClean('searchid', TYPE_UINT) . " AND userid = " . bugdar::$userinfo['userid']);
        if (!$search)
        {
                $message->addError(T('The search does not exist.'));
@@ -644,14 +660,14 @@ if ($_POST['do'] == 'dosave')
        {
                $message->addError(sprintf(T('This search has already been named "%1$s".'), $search['name']));
        }
-       if (empty($bugsys->in['name']))
+       if (empty($input->in['name']))
        {
                $message->addError(T('The name cannot be empty.'));
        }
        
        if (!$message->hasErrors())
        {
-               $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $bugsys->input_escape('name') . "' WHERE searchid = " . $bugsys->in['searchid']);
+               $db->query("UPDATE " . TABLE_PREFIX . "search SET name = '" . $input->inputEscape('name') . "' WHERE searchid = " . $input->in['searchid']);
                $message->redirect(T('Your search has been saved.'), 'search.php?searchid=' . $search['searchid']);
        }
        else
@@ -665,12 +681,18 @@ if ($_POST['do'] == 'dosave')
 
 if ($_REQUEST['do'] == 'save')
 {
-       if (!$bugsys->userinfo['userid'])
+       if (!bugdar::$userinfo['userid'])
        {
                $message->errorPermission();
        }
        
-       eval('$template->flush("' . $template->fetch('search_save') . '");');
+       $tpl = new BSTemplate('search_save');
+       $tpl->vars = array(
+               'message'       => $message,
+               'searchid'      => $input->in['searchid'],
+               'name'          => $input->in['name']
+       );
+       $tpl->evaluate()->flush();
 }
 
 // ###################################################################
@@ -684,22 +706,24 @@ if ($_POST['do'] == 'results')
        
        $searchid = $search['searchid'];
        $hilight = $search['hilight'];
-               
-       LoadPaginationFramework();
+       
+       require_once 'includes/pagination.php';
+       $pagination = new Pagination();
+       $pagination->processIncomingData();
        $pagination->setTotal($search['resultcount']);
        $pagination->splitPages();
        
        $sort = new ListSorter('search');
        
-       $show['save'] = ($bugsys->userinfo['userid'] AND !$search['name']);
+       $show['save'] = (bugdar::$userinfo['userid'] AND !$search['name']);
        $show['update'] = can_perform('caneditother');
        
        $bugs = '';
        $search = $db->query("SELECT * FROM " . TABLE_PREFIX . "bug WHERE bugid IN ($search[ids]) $search[orderby] LIMIT " . $pagination->fetchLimit($pagination->getPage() - 1) . ", " . $pagination->getPerPage());
-       while ($bug = $db->fetch_array($search))
+       foreach ($search as $bug)
        {
-               $funct->exec_swap_bg('altcolor', '');
-               $bug = ProcessBugDataForDisplay($bug, $funct->bgcolour);
+               BSFunctions::swap_css_classes('altcolor', '');
+               $bug = ProcessBugDataForDisplay($bug, BSFunctions::$cssClass);
                $bugs .= $sort->constructRow($bug, "&amp;hilight=$hilight");
        }
        
@@ -707,13 +731,13 @@ if ($_POST['do'] == 'results')
        $show['pagenav'] = ($pagination->getPageCount() > 1);
        $pagenav = $pagination->constructPageNav('search.php?searchid=' . $searchid);
        
-       eval('$template->flush("' . $template->fetch('search_results') . '");');
+       $tpl = new BSTemplate('search_results');
+       $tpl->vars = array(
+               'columnHeads'   => $columnHeads,
+               'bugs'                  => $bugs,
+               'searchid'              => $searchid
+       );
+       $tpl->evaluate()->flush();
 }
 
-/*=====================================================================*\
-|| ###################################################################
-|| # $HeadURL$
-|| # $Id$
-|| ###################################################################
-\*=====================================================================*/
 ?>
\ No newline at end of file