Example #1
0
 public function Execute(Template $template, Session $session, $request)
 {
     /* Fool around with the session and the request variables so pagination works throughout */
     if (isset($request['keywords']) || isset($request['username'])) {
         $session->OffsetSet('search_results', $request);
     } else {
         $session['search_results'] = isset($session['search_results']) ? $session['search_results'] : $request;
     }
     $request = $session['search_results'];
     /* Set the template */
     $template->content = array('file' => 'search_results.html');
     /* Set the ancestors bar */
     $template = CreateAncestors($template, $template['L_SEARCHRESULTS']);
     /* Do the search */
     if (isset($request['keywords']) && $request['keywords'] || isset($request['username']) && $request['username']) {
         /* Set a variable to the column that we will search against */
         $to_use = '';
         /* Check to find out which field we want to search for */
         if ($request['keywords'] != '' && $request['username'] != '') {
             $to_use = 1;
         }
         if ($to_use == '') {
             $to_use = $request['keywords'] != '' ? 1 : 2;
         }
         /* Auto quote out invalid characters */
         foreach ($request as $key => $val) {
             if ($key != 'forums') {
                 $request[$key] = $this->dba->Quote($val);
             }
         }
         $this->exact = isset($request['exact']) ? TRUE : FALSE;
         /* Get the forums to search in */
         $forums = isset($request['forums']) ? $request['forums'] : array();
         /* Check if the user has actually selected any forums to search in */
         if (count($forums) == 0) {
             return new Error($template['L_FORUMDOESNTEXIST'], $template);
         }
         /* Set up this section of the query */
         $query_users = '';
         $query_posts = '';
         /* If we are searching using keywords */
         if ($to_use == 1) {
             $keywords = htmlspecialchars($request['keywords']);
             $template['search_terms'] = $template['L_KEYWORDS'] . ': ' . $keywords;
             $field = intval($request['search_where']) == 1 ? 'body_text' : 'name';
             $query_posts = " lower(" . $field . ") LIKE lower('%" . $keywords . "%') ";
             /* If we are searching by poster names */
         } else {
             if ($to_use == 2) {
                 $template['search_terms'] = $template['L_USERNAME'] . ': ' . $request['username'];
                 /* Get the user(s) */
                 $users = $this->GetUsers($this->dba->Quote($request['username']));
                 $i = 1;
                 if ($users instanceof SetError) {
                     return new Error($users->message, $template);
                 } else {
                     /* Loop through the users */
                     foreach ($users as $user) {
                         /* Make this section of the query */
                         $query_users .= $i != $this->user_count ? "poster_name = '" . $user['name'] . "' OR " : "poster_name = '" . $user['name'] . "'";
                         /* increment the $i variable */
                         $i++;
                     }
                     /* If we are just looking for threads by the user */
                     if (intval($request['user_where']) == '2') {
                         $query_users .= " AND row_type = 2 ";
                     }
                 }
             }
         }
         $query_forums = '';
         /* Loop the forums and make that part of the query */
         for ($f = 0; $f < count($forums); $f++) {
             /* Make the forums part of the query */
             $query_forums .= $f != count($forums) - 1 ? "id = " . $forums[$f] . " OR " : "id = " . $forums[$f];
         }
         $at_least = '';
         //$at_least = intval($request['at_least']) == 0 ? '<=' : '>=';
         //$at_least = " AND (right-left-1)/2 $at_least ". intval(@$request['num_posts']) ." ";
         /* set the display order */
         $order = intval($request['sort']) == 1 ? ' ORDER BY created DESC' : ' ORDER BY created ASC';
         /* Set from how long ago the posts will be */
         $oldnew = intval($request['posts_oldnew']) == 1 ? '>=' : '<=';
         $from = " AND created {$oldnew} '" . intval($request['posts_from']) . "' ";
         $template['postlimit'] = 30;
         $template['total_posts'] = DBA::Open()->GetValue("SELECT COUNT(*) FROM " . FORUMS . " WHERE {$query_forums}");
         $template->search_results = new SearchResultsIterator($query_forums, $query_posts, $query_users, $order, $at_least, $from);
     } else {
         /* Return an error if they have not put anything to search for */
         return new Error($template['L_MUSTDEFINESEARCH'], $template);
     }
     /* Set the number of queries */
     $template['num_queries'] = $session->dba->num_queries;
     return TRUE;
 }