function perform()
 {
     $layout = grwd('layout', 'menus');
     $optionFunctions = array('pulldown' => 'getSelectPulldown', 'radio' => 'getRadioList', 'menus' => 'getSelectBox');
     $optionFunction = $optionFunctions[$layout];
     // Ensure we have the variable questionId
     $questionId = grwd('questionId', -1) + 0;
     $db = $_REQUEST['db'];
     $tsf = new TimeSpentFinder($db);
     $ptf = new PatronTypeFinder($db);
     $qff = new QuestionFormatFinder($db);
     $qtf = new QuestionTypeFinder($db);
     $if = new InitialsFinder($db);
     $lf = new LocationFinder($db);
     $userFinder = new UserFinder($db);
     $user = $userFinder->findById($_SESSION['userId']);
     if ($questionId != -1) {
         // to the edit form!
         $result = array('renderer' => 'template_renderer.inc', 'pageTitle' => SITE_NAME . ' : Edit Question', 'content' => 'content/questionEditForm.php');
         $result['optionFunction'] = $optionFunction;
         $libId = $user['library_id'];
         $result['timeSpentOpts'] = $tsf->findByLibrary($libId);
         $result['patronTypeOpts'] = $ptf->findByLibrary($libId);
         $result['questionTypeOpts'] = $qtf->findByLibrary($libId);
         $result['questionFormatOpts'] = $qff->findByLibrary($libId);
         $result['locationOpts'] = $lf->findByLibrary($libId);
         $result['locationId'] = $lf->getLastLocationId($_SERVER['REMOTE_ADDR'], $libId);
         $result['user'] = $user;
         $questionFinder = new QuestionFinder($db);
         $result['question'] = $questionFinder->getQuestion($questionId);
         $result['answer'] = $questionFinder->getQuestion($questionId);
         $result['delete_hide'] = gpwd('delete_hide');
         $result['origin'] = grwd('origin', 'questionAddForm.do');
     } else {
         echo "QuestionID {$questionId} not found";
         // Send somewere else
     }
     return $result;
 }
 function perform()
 {
     // set display requirements
     $result = array('renderer' => 'template_renderer.inc', 'pageTitle' => SITE_NAME . ' : Search Results', 'content' => 'content/questionSearchResults.php');
     // The number of questions and page offset we want
     $count = grwd('count', 50);
     $page = grwd('page', 1);
     $result['count'] = $count;
     $result['page'] = $page;
     // don't lose the db!
     $db = $_REQUEST['db'];
     $userFinder = new UserFinder($db);
     $user = $userFinder->findById($_SESSION['userId']);
     $result['user'] = $user;
     // Generate a base URL for this search -- can't have page or count data
     // cos we're going to build pagination links from this
     $criteriaArray = $_GET;
     unset($criteriaArray['page']);
     unset($criteriaArray['count']);
     $advancedSearchString = implode_assoc('=', '&', $criteriaArray, true, true);
     $result['criteria_array'] =& $criteriaArray;
     $baseUrl = 'advancedSearch.do?' . $advancedSearchString;
     $result['advanced_search_string'] = $advancedSearchString;
     // gather request data
     $date1 = grwd('date1');
     $date2 = grwd('date2');
     $library_id_post = grwd('library_id') + 0;
     $location_id_post = grwd('location_id') + 0;
     $initials = grwd('initials');
     $searchString = grwd('searchString');
     $result['searchWords'] = $searchString;
     $questionFinder = new QuestionFinder($db);
     $startDate = array('database_field' => 'questions.question_date', 'relation' => '>=', 'value' => $date1, 'type' => 'DATE');
     $endDate = array('database_field' => 'questions.question_date', 'relation' => '<=', 'value' => $date2, 'type' => 'DATE');
     $library_id = array('database_field' => 'questions.library_id', 'relation' => '=', 'value' => $library_id_post, 'type' => 'INT');
     $location_id = array('database_field' => 'questions.location_id', 'relation' => '=', 'value' => $location_id_post, 'type' => 'INT');
     $initials = array('database_field' => 'questions.initials', 'relation' => 'LIKE', 'value' => $initials, 'type' => 'TEXT');
     $searchCriteria = array('database_field' => array('question', 'answer'), 'relation' => 'fulltext', 'value' => $searchString, 'type' => 'FULLTEXT');
     // pull together all of the search criteria
     $criteria = array('start_date' => $startDate, 'end_date' => $endDate, 'library_id' => $library_id, 'location_id' => $location_id, 'initials' => $initials, 'search_criteria' => $searchCriteria);
     $sql = "";
     $param = array();
     foreach ($criteria as $value) {
         if (!$value["value"]) {
             continue;
         }
         if (count($param) != 0) {
             $sql .= ' AND ';
         }
         if ($value["type"] == "FULLTEXT") {
             // Track if this is the first fulltext in group
             $orNeeded = false;
             $sql .= ' ( ';
             foreach ($value["database_field"] as $field) {
                 if ($orNeeded) {
                     $sql .= " OR ";
                 }
                 $sql .= 'MATCH(' . $field . ") AGAINST(? IN BOOLEAN MODE)";
                 $param[] = mySqlFulltextString($value["value"]);
                 $orNeeded = true;
             }
             $sql .= ' ) ';
         } else {
             if ($value['type'] == "DATE") {
                 // Make dates sane; the only part of this that's special
                 $sDate = makeDateSane($value['value']);
                 $sDate = date('Y-m-d', strtotime($sDate));
                 $sql .= $value["database_field"] . ' ' . $value["relation"] . ' ? ';
                 $param[] = $sDate;
             } else {
                 $sql .= $value["database_field"] . ' ' . $value["relation"] . ' ? ';
                 $param[] = $value["value"];
             }
         }
     }
     $questionFinder = new QuestionFinder($db);
     $qList =& $questionFinder->getPagedList($count, $page, $sql, $param);
     $result['questionList'] =& $qList['list'];
     $result['list_meta'] = $qList['meta'];
     $libraryFinder = new LibraryFinder($db);
     $searchLibName = $libraryFinder->getLibraryName($library_id_post);
     if ($library_id_post == 0) {
         $searchLibName = "All Libraries";
     }
     if (isset($location_id_post)) {
         $locationFinder = new LocationFinder($db);
         $searchLocName = $locationFinder->getLocation($location_id_post);
     }
     $result['origin'] = $baseUrl . "&amp;page={$page}&amp;count={$count}";
     $result['base_url'] = $baseUrl;
     $result['date1'] = $date1;
     $result['date2'] = $date2;
     $result['library_id'] = $library_id;
     $result['library_name'] = $searchLibName;
     $result['search_library_id'] = $library_id_post;
     $result['location_id'] = $location_id;
     $result['location_name'] = $searchLocName;
     $result['criteria'] = $criteria;
     // $result['sql'] = $sql;
     return $result;
 }
 function perform()
 {
     // Get the resources we need to do this update
     $db = $_REQUEST['db'];
     $userFinder = new UserFinder($db);
     $user = $userFinder->findById($_SESSION['userId']);
     $questionFinder = new QuestionFinder($db);
     // Grok all the relevant data from the form
     $qHash = array();
     $qHash['library_id'] = $user['library_id'];
     $qHash['location_id'] = gpwd('location', null);
     $qHash['question_type_id'] = gpwd('questionType', null);
     $qHash['question_type_other'] = gpwd('questionTypeOther');
     $qHash['time_spent_id'] = gpwd('timeSpent', null);
     $qHash['patron_type_id'] = gpwd('patronType', null);
     $qHash['question_format_id'] = gpwd('questionFormat', null);
     $qHash['initials'] = gpwd('initials');
     $qHash['client_ip'] = getRemoteIp();
     $qHash['user_id'] = $_SESSION['userId'];
     $qHash['question'] = gpwd('question');
     $qHash['answer'] = gpwd('answer');
     $qHash['question'] = trim($qHash['question']);
     $qHash['answer'] = trim($qHash['answer']);
     $qHash['hide'] = 0;
     if ($qHash['question'] == '' && $qHash['answer'] == '') {
         $qHash['hide'] = 1;
     }
     // Do the date
     $qHash['question_date'] = trim(gpwd('mydate', 'now'));
     if ($qHash['question_date'] == '') {
         $qHash['question_date'] = 'now';
     }
     $stamp = strtotime($qHash['question_date']);
     if ($stamp != -1) {
         $qHash['question_date'] = date('Y-m-d H:i:s', $stamp);
     } else {
         $qHash['question_date'] = null;
     }
     $qHash['date_added'] = date('Y-m-d H:i:s');
     // Clean up qHash; make numbers really numeric. The dirty little
     // trick: add 0 to non-null values names .*_id
     foreach ($qHash as $key => $val) {
         if (strpos($key, '_id')) {
             if ($val != null) {
                 $qHash[$key] = $val + 0;
             }
         }
     }
     $target = "questionAddForm.do";
     $res = $questionFinder->addQuestion($qHash);
     if (!DB::isError($res)) {
         // Use a Location: header to fly back; we don't want to
         // be able to double-enter by mistake.... I think.
         $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
         $url = substr($url, 0, -strrchr($url, '/')) . $target;
         header("Location: {$url}");
         exit;
     } else {
         // A page error occurred!
         $_REQUEST['dbResult'] = $res;
         $act = new PageErrorAction();
         return $act->perform();
     }
 }
 function perform()
 {
     // Get the resources we need to do this update
     $db = $_REQUEST['db'];
     $userFinder = new UserFinder($db);
     $questionFinder = new QuestionFinder($db);
     $questionId = gpwd('questionId', 0) + 0;
     //Test for DELETE
     $delete = gpwd('deleteButton', '');
     $save = gpwd('saveButton', '');
     if ($save == "Save Question / Answer") {
         $delete = 0;
     } else {
         if ($delete == "Delete") {
             $delete = 1;
         }
     }
     // Grok all the relevant data from the form
     $qHash = array();
     $qHash['location_id'] = gpwd('location', null);
     $qHash['question_type_id'] = gpwd('questionType', null);
     $qHash['question_type_other'] = gpwd('questionTypeOther');
     $qHash['time_spent_id'] = gpwd('timeSpent', null);
     $qHash['patron_type_id'] = gpwd('patronType', null);
     $qHash['question_format_id'] = gpwd('questionFormat', null);
     $qHash['initials'] = gpwd('initials');
     $qHash['question'] = gpwd('question');
     $qHash['answer'] = gpwd('answer');
     $qHash['question'] = trim($qHash['question']);
     $qHash['answer'] = trim($qHash['answer']);
     $qHash['hide'] = 0;
     $qHash['delete_hide'] = $delete;
     if ($qHash['question'] == '' && $qHash['answer'] == '') {
         $qHash['hide'] = 1;
     }
     // Do the date -- if we can't parse it, don't change it!
     $qTime = trim(gpwd('questionDate', ''));
     if ($qTime != '') {
         $stamp = strtotime($qTime);
         if ($stamp != -1) {
             $qHash['question_date'] = date('Y-m-d H:i:s', $stamp);
         }
     }
     // Clean up qHash; make numbers really numeric. The dirty little
     // trick: add 0 to non-null values names .*_id
     foreach ($qHash as $key => $val) {
         if (strpos($key, '_id')) {
             if ($val != null) {
                 $qHash[$key] = $val + 0;
             }
         }
     }
     $res = $questionFinder->editQuestion($questionId, $qHash);
     if (!DB::isError($res)) {
         // Use a Location: header to fly back, to avoid people refreshing
         // and posting twice -- a common problem.
         $url = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
         $url = substr($url, 0, -strrchr($url, '/')) . $_REQUEST['origin'];
         header("Location: {$url}");
         exit;
     } else {
         echo "<pre>";
         var_dump($res);
         echo "</pre>";
     }
 }
 function perform()
 {
     $times = array();
     $start = 0;
     $end = 0;
     $result = array('renderer' => 'template_renderer.inc', 'pageTitle' => SITE_NAME . ' : Add Question', 'content' => 'content/questionAddForm.php');
     // The number of questions and page offset we want
     $count = grwd('count', 50);
     $page = grwd('page', 1);
     $result['count'] = $count;
     $result['page'] = $page;
     // Learn what kind of menus we need
     $layout = grwd('layout', 'menus');
     $optionFunction = array('pulldown' => 'getSelectPulldown', 'radio' => 'getRadioList', 'menus' => 'getSelectBox');
     $result['optionFunction'] = $optionFunction[$layout];
     $db = $_REQUEST['db'];
     $tsf = new TimeSpentFinder($db);
     $ptf = new PatronTypeFinder($db);
     $qff = new QuestionFormatFinder($db);
     $qtf = new QuestionTypeFinder($db);
     $if = new InitialsFinder($db);
     $lf = new LocationFinder($db);
     $userFinder = new UserFinder($db);
     $start = mTimeFloat();
     $user = $userFinder->findById($_SESSION['userId']);
     $end = mTimeFloat();
     $times['userfind'] = $end - $start;
     $clientIp = getRemoteIp();
     $libId = $user['library_id'];
     $start = mTimeFloat();
     $result['timeSpentOpts'] = $tsf->findByLibrary($libId);
     $result['patronTypeOpts'] = $ptf->findByLibrary($libId);
     $result['questionTypeOpts'] = $qtf->findByLibrary($libId);
     $result['questionFormatOpts'] = $qff->findByLibrary($libId);
     $result['locationOpts'] = $lf->findByLibrary($libId);
     $times['menus'] = mTimeFloat() - $start;
     $start = mTimeFloat();
     // Load the default (last used) values for this client
     $result['locationId'] = $lf->getLastLocationId($clientIp, $libId);
     if ($result['locationId'] == null) {
         $result['locationId'] = $result['locationOpts'][0]['location_id'];
     }
     $start = mTimeFloat();
     $result['lastInitials'] = $if->getLastInitials($clientIp);
     $times['lastInitials'] = mTimeFloat() - $start;
     $start = mTimeFloat();
     $result['lastTimeSpent'] = $tsf->getLast($clientIp);
     $times['lastTimeSpent'] = mTimeFloat() - $start;
     $start = mTimeFloat();
     $result['lastPatronType'] = $ptf->getLast($clientIp);
     $times['lastPatronType'] = mTimeFloat() - $start;
     $start = mTimeFloat();
     $result['lastQuestionType'] = $qtf->getLast($clientIp);
     $times['lastQuestionType'] = mTimeFloat() - $start;
     $start = mTimeFloat();
     $result['lastQuestionFormat'] = $qff->getLast($clientIp);
     $times['lastQuestionFormat'] = mTimeFloat() - $start;
     $result['user'] = $user;
     $questionFinder = new QuestionFinder($db);
     $start = mTimeFloat();
     $result['lastAdded'] = $questionFinder->getLastQuestionTime($clientIp, $libId);
     if ($result['lastAdded'] != null) {
         $result['lastAdded'] = date('n/d g:i A', strtotime($result['lastAdded']));
     }
     $times['lastAdded'] = mTimeFloat() - $start;
     $start = mTimeFloat();
     $qResult = $questionFinder->getPagedList($count, $page, 'questions.library_id = ?', array((int) $libId));
     $result['questionList'] =& $qResult['list'];
     $result['list_meta'] = $qResult['meta'];
     $result['questionCount'] = $qResult['meta']['totalQuestions'];
     $result['nonemptyQuestionCount'] = $qResult['meta']['nonemptyQuestions'];
     $times['getQuestions'] = mTimeFloat() - $start;
     $origin = "questionAddForm.do?page=" . $result['page'] . "&amp;count=" . $result['count'];
     $result['origin'] = $origin;
     $result['target'] = 'questionAddForm.do';
     // Disabled debugging output
     // echo "<!-- ";
     // var_dump($times);
     // echo " -->";
     return $result;
 }