Version: current

      1 <?php
      2 /**
      3  * More advanced search form
      4  * @package sapphire
      5  * @subpackage search
      6  */
      7 class AdvancedSearchForm extends SearchForm {
      8
      9     /**
     10      * the constructor of a Simple/basic SearchForm
     11      */
     12     function __construct($controller, $name, $fields = null, $actions = null) {
     13         if(!$fields) {
     14             $fields = new FieldSet(
     15                 $searchBy = new CompositeField(
     16                     new HeaderField('SearchByHeader',_t('AdvancedSearchForm.SEARCHBY', 'SEARCH BY')),
     17                     new TextField("+", _t('AdvancedSearchForm.ALLWORDS', 'All Words')),
     18                     new TextField("quote", _t('AdvancedSearchForm.EXACT', 'Exact Phrase')),
     19                     new TextField("any", _t('AdvancedSearchForm.ATLEAST', 'At Least One Of the Words')),
     20                     new TextField("-", _t('AdvancedSearchForm.WITHOUT', 'Without the Words'))
     21                 ),
     22                 $sortBy = new CompositeField(
     23                     new HeaderField('SortByHeader',_t('AdvancedSearchForm.SORTBY', 'SORT RESULTS BY')),
     24                     new OptionsetField("sortby", "",
     25                         array(
     26                                 'Relevance' => _t('AdvancedSearchForm.RELEVANCE', 'Relevance'),
     27                             'LastUpdated' => _t('AdvancedSearchForm.LASTUPDATED', 'Last Updated'),
     28                             'PageTitle' => _t('AdvancedSearchForm.PAGETITLE', 'Page Title'),
     29                         ),
     30                         'Relevance'
     31                     )
     32                 ),
     33                 $chooseDate = new CompositeField(
     34                     new HeaderField('LastUpdatedHeader',_t('AdvancedSearchForm.LASTUPDATEDHEADER', 'LAST UPDATED')),
     35                     new DateField("From", _t('AdvancedSearchForm.FROM', 'From')),
     36                     new DateField("To", _t('AdvancedSearchForm.TO', 'To'))
     37                 )
     38             );
     39
     40             $searchBy->ID = "AdvancedSearchForm_SearchBy";
     41             $searchOnly->ID = "AdvancedSearchForm_SearchOnly";
     42             $sortBy->ID = "AdvancedSearchForm_SortBy";
     43             $chooseDate->ID = "AdvancedSearchForm_ChooseDate";
     44         }
     45
     46         if(!$actions) {
     47             $actions = new FieldSet(
     48                 new FormAction("results", _t('AdvancedSearchForm.GO', 'Go'))
     49             );
     50         }
     51         parent::__construct($controller, $name, $fields, $actions);
     52     }
     53
     54     public function forTemplate(){
     55         return $this->renderWith(array("AdvancedSearchForm","Form"));
     56     }
     57
     58     /* Return dataObjectSet of the results, using the form data.
     59      */
     60     public function getResults($numPerPage = 10) {
     61         $data = $this->getData();
     62
     63         if($data['+']) $keywords .= " +" . ereg_replace(" +", " +", trim($data['+']));
     64         if($data['quote']) $keywords .= ' "' . $data['quote'] . '"';
     65         if($data['any']) $keywords .= ' ' . $data['any'];
     66         if($data['-']) $keywords .= " -" . ereg_replace(" +", " -", trim($data['-']));
     67         $keywords = trim($keywords);
     68
     69         // This means that they want to just find pages where there's *no* match
     70
     71         if($keywords[0] == '-') {
     72             $keywords = $data['-'];
     73             $invertedMatch = true;
     74         }
     75
     76
     77         // Limit search to various sections
     78         if($_REQUEST['OnlyShow']) {
     79             $pageList = array();
     80
     81             // Find the associated pages
     82             foreach($_REQUEST['OnlyShow'] as $section => $checked) {
     83                 $items = explode(",", $section);
     84                 foreach($items as $item) {
     85                     $page = DataObject::get_one('SiteTree', "\"URLSegment\" = '" . DB::getConn()->addslashes($item) . "'");
     86                     $pageList[] = $page->ID;
     87                     if(!$page) user_error("Can't find a page called '$item'", E_USER_WARNING);
     88                     $page->loadDescendantIDListInto($pageList);
     89                 }
     90             }
     91             $contentFilter = "\"ID\" IN (" . implode(",", $pageList) . ")";
     92
     93             // Find the files associated with those pages
     94             $fileList = DB::query("SELECT \"FileID\" FROM \"Page_ImageTracking\" WHERE \"PageID\" IN (" . implode(",", $pageList) . ")")->column();
     95             if($fileList) $fileFilter = "\"ID\" IN (" . implode(",", $fileList) . ")";
     96             else $fileFilter = " 1 = 2 ";
     97         }
     98
     99         if($data['From']) {
    100             $filter .= ($filter?" AND":"") . " \"LastEdited\" >= '$data[From]'";
    101         }
    102         if($data['To']) {
    103             $filter .= ($filter?" AND":"") . " \"LastEdited\" <= '$data[To]'";
    104         }
    105
    106         if($filter) {
    107             $contentFilter .= ($contentFilter?" AND":"") . $filter;
    108             $fileFilter .= ($fileFilter?" AND":"") . $filter;
    109         }
    110
    111         if($data['sortby']) {
    112             $sorts = array(
    113                 'LastUpdated' => '"LastEdited" DESC',
    114                 'PageTitle' => '"Title" ASC',
    115                 'Relevance' => '"Relevance" DESC',
    116             );
    117             $sortBy = $sorts[$data['sortby']] ? $sorts[$data['sortby']] : $sorts['Relevance'];
    118         }
    119
    120         $keywords = $this->addStarsToKeywords($keywords);
    121
    122         return $this->searchEngine($keywords, $numPerPage, $sortBy, $contentFilter, true, $fileFilter, $invertedMatch);
    123     }
    124
    125     function getSearchQuery() {
    126         $data = $_REQUEST;
    127         if($data['+']) $keywords .= " +" . ereg_replace(" +", " +", trim($data['+']));
    128         if($data['quote']) $keywords .= ' "' . $data['quote'] . '"';
    129         if($data['any']) $keywords .= ' ' . $data['any'];
    130         if($data['-']) $keywords .= " -" . ereg_replace(" +", " -", trim($data['-']));
    131
    132         return trim($keywords);
    133     }
    134
    135 }
    136
    137 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -