2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-01-08 07:37:50 +01:00
|
|
|
/**
|
2008-02-25 03:10:37 +01:00
|
|
|
* More advanced search form
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage search
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
class AdvancedSearchForm extends SearchForm {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* the constructor of a Simple/basic SearchForm
|
|
|
|
*/
|
|
|
|
function __construct($controller, $name, $fields = null, $actions = null) {
|
|
|
|
if(!$fields) {
|
|
|
|
$fields = new FieldSet(
|
|
|
|
$searchBy = new CompositeField(
|
2008-10-16 15:26:50 +02:00
|
|
|
new HeaderField('SearchByHeader',_t('AdvancedSearchForm.SEARCHBY', 'SEARCH BY')),
|
2008-02-25 03:10:37 +01:00
|
|
|
new TextField("+", _t('AdvancedSearchForm.ALLWORDS', 'All Words')),
|
|
|
|
new TextField("quote", _t('AdvancedSearchForm.EXACT', 'Exact Phrase')),
|
|
|
|
new TextField("any", _t('AdvancedSearchForm.ATLEAST', 'At Least One Of the Words')),
|
|
|
|
new TextField("-", _t('AdvancedSearchForm.WITHOUT', 'Without the Words'))
|
2007-07-19 12:40:28 +02:00
|
|
|
),
|
|
|
|
$sortBy = new CompositeField(
|
2008-10-16 15:26:50 +02:00
|
|
|
new HeaderField('SortByHeader',_t('AdvancedSearchForm.SORTBY', 'SORT RESULTS BY')),
|
2007-07-19 12:40:28 +02:00
|
|
|
new OptionsetField("sortby", "",
|
|
|
|
array(
|
2008-02-25 03:10:37 +01:00
|
|
|
'Relevance' => _t('AdvancedSearchForm.RELEVANCE', 'Relevance'),
|
|
|
|
'LastUpdated' => _t('AdvancedSearchForm.LASTUPDATED', 'Last Updated'),
|
|
|
|
'PageTitle' => _t('AdvancedSearchForm.PAGETITLE', 'Page Title'),
|
2007-07-19 12:40:28 +02:00
|
|
|
),
|
|
|
|
'Relevance'
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$chooseDate = new CompositeField(
|
2008-10-16 15:26:50 +02:00
|
|
|
new HeaderField('LastUpdatedHeader',_t('AdvancedSearchForm.LASTUPDATEDHEADER', 'LAST UPDATED')),
|
2008-02-25 03:10:37 +01:00
|
|
|
new CompositeDateField("From", _t('AdvancedSearchForm.FROM', 'From')),
|
|
|
|
new CompositeDateField("To", _t('AdvancedSearchForm.TO', 'To'))
|
2007-07-19 12:40:28 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$searchBy->ID = "AdvancedSearchForm_SearchBy";
|
|
|
|
$searchOnly->ID = "AdvancedSearchForm_SearchOnly";
|
|
|
|
$sortBy->ID = "AdvancedSearchForm_SortBy";
|
|
|
|
$chooseDate->ID = "AdvancedSearchForm_ChooseDate";
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!$actions) {
|
|
|
|
$actions = new FieldSet(
|
2008-02-25 03:10:37 +01:00
|
|
|
new FormAction("results", _t('AdvancedSearchForm.GO', 'Go'))
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
parent::__construct($controller, $name, $fields, $actions);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function forTemplate(){
|
|
|
|
return $this->renderWith(array("AdvancedSearchForm","Form"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return dataObjectSet of the results, using the form data.
|
|
|
|
*/
|
|
|
|
public function getResults($numPerPage = 10) {
|
|
|
|
$data = $this->getData();
|
|
|
|
|
|
|
|
if($data['+']) $keywords .= " +" . ereg_replace(" +", " +", trim($data['+']));
|
|
|
|
if($data['quote']) $keywords .= ' "' . $data['quote'] . '"';
|
|
|
|
if($data['any']) $keywords .= ' ' . $data['any'];
|
|
|
|
if($data['-']) $keywords .= " -" . ereg_replace(" +", " -", trim($data['-']));
|
|
|
|
$keywords = trim($keywords);
|
|
|
|
|
|
|
|
// This means that they want to just find pages where there's *no* match
|
|
|
|
|
|
|
|
if($keywords[0] == '-') {
|
|
|
|
$keywords = $data['-'];
|
|
|
|
$invertedMatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Limit search to various sections
|
|
|
|
if($_REQUEST['OnlyShow']) {
|
|
|
|
$pageList = array();
|
|
|
|
|
|
|
|
// Find the associated pages
|
|
|
|
foreach($_REQUEST['OnlyShow'] as $section => $checked) {
|
|
|
|
$items = explode(",", $section);
|
|
|
|
foreach($items as $item) {
|
2009-04-06 01:24:56 +02:00
|
|
|
$page = DataObject::get_one('SiteTree', "\"URLSegment\" = '" . DB::getConn()->addslashes($item) . "'");
|
2007-07-19 12:40:28 +02:00
|
|
|
$pageList[] = $page->ID;
|
|
|
|
if(!$page) user_error("Can't find a page called '$item'", E_USER_WARNING);
|
|
|
|
$page->loadDescendantIDListInto($pageList);
|
|
|
|
}
|
|
|
|
}
|
2008-11-24 00:28:16 +01:00
|
|
|
$contentFilter = "\"ID\" IN (" . implode(",", $pageList) . ")";
|
2007-07-19 12:40:28 +02:00
|
|
|
|
|
|
|
// Find the files associated with those pages
|
2008-11-24 00:28:16 +01:00
|
|
|
$fileList = DB::query("SELECT \"FileID\" FROM \"Page_ImageTracking\" WHERE \"PageID\" IN (" . implode(",", $pageList) . ")")->column();
|
|
|
|
if($fileList) $fileFilter = "\"ID\" IN (" . implode(",", $fileList) . ")";
|
2007-07-19 12:40:28 +02:00
|
|
|
else $fileFilter = " 1 = 2 ";
|
|
|
|
}
|
|
|
|
|
|
|
|
if($data['From']) {
|
2008-11-24 00:28:16 +01:00
|
|
|
$filter .= ($filter?" AND":"") . " \"LastEdited\" >= '$data[From]'";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
if($data['To']) {
|
2008-11-24 00:28:16 +01:00
|
|
|
$filter .= ($filter?" AND":"") . " \"LastEdited\" <= '$data[To]'";
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($filter) {
|
|
|
|
$contentFilter .= ($contentFilter?" AND":"") . $filter;
|
|
|
|
$fileFilter .= ($fileFilter?" AND":"") . $filter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($data['sortby']) {
|
|
|
|
$sorts = array(
|
2008-11-24 00:28:16 +01:00
|
|
|
'LastUpdated' => '"LastEdited" DESC',
|
|
|
|
'PageTitle' => '"Title" ASC',
|
|
|
|
'Relevance' => '"Relevance" DESC',
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
|
|
|
$sortBy = $sorts[$data['sortby']] ? $sorts[$data['sortby']] : $sorts['Relevance'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$keywords = $this->addStarsToKeywords($keywords);
|
|
|
|
|
|
|
|
return $this->searchEngine($keywords, $numPerPage, $sortBy, $contentFilter, true, $fileFilter, $invertedMatch);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSearchQuery() {
|
|
|
|
$data = $_REQUEST;
|
|
|
|
if($data['+']) $keywords .= " +" . ereg_replace(" +", " +", trim($data['+']));
|
|
|
|
if($data['quote']) $keywords .= ' "' . $data['quote'] . '"';
|
|
|
|
if($data['any']) $keywords .= ' ' . $data['any'];
|
|
|
|
if($data['-']) $keywords .= " -" . ereg_replace(" +", " -", trim($data['-']));
|
|
|
|
|
|
|
|
return trim($keywords);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|