/** * Confirm that the current search suggestion is valid. */ public function validate() { $result = parent::validate(); // Confirm that the current search suggestion matches the minimum autocomplete length and doesn't already exist. if ($result->valid() && strlen($this->Term) < 3) { $result->error('Minimum autocomplete length required!'); } else { if ($result->valid() && ExtensibleSearchSuggestion::get_one('ExtensibleSearchSuggestion', "ID != " . (int) $this->ID . " AND Term = '" . Convert::raw2sql($this->Term) . "' AND ExtensibleSearchPageID = " . (int) $this->ExtensibleSearchPageID)) { $result->error('Suggestion already exists!'); } } return $result; }
/** * Retrieve the most relevant search suggestions. * * @parameter <{SEARCH_TERM}> string * @parameter <{EXTENSIBLE_SEARCH_PAGE_ID}> integer * @parameter <{LIMIT}> integer * @parameter <{APPROVED_ONLY}> boolean * @return array */ public function getSuggestions($term, $pageID, $limit = 5, $approved = true) { // Make sure the search matches the minimum autocomplete length. if ($term && strlen($term) > 2) { // Make sure the current user has appropriate permission. $pageID = (int) $pageID; if (($page = ExtensibleSearchPage::get_by_id('ExtensibleSearchPage', $pageID)) && $page->canView()) { // Retrieve the search suggestions. $suggestions = ExtensibleSearchSuggestion::get()->filter(array('Term:StartsWith' => $term, 'Approved' => (int) $approved, 'ExtensibleSearchPageID' => $pageID))->sort('Frequency', 'DESC')->limit($limit); // Make sure the search suggestions are unique. return array_unique($suggestions->column('Term')); } } return null; }
/** * Confirm that the current search suggestion is valid. */ public function validate() { $result = parent::validate(); // Confirm that the current search suggestion matches the minimum autocomplete length and doesn't already exist. if ($result->valid() && strlen($this->Term) < 3) { $result->error('Minimum autocomplete length required!'); } else { if ($result->valid() && ExtensibleSearchSuggestion::get_one('ExtensibleSearchSuggestion', array('ID != ?' => $this->ID, 'Term = ?' => $this->Term, 'ExtensibleSearchPageID = ?' => $this->ExtensibleSearchPageID))) { $result->error('Suggestion already exists!'); } } // Allow extension customisation. $this->extend('validateExtensibleSearchSuggestion', $result); return $result; }