/** * If there is a search encoded in the link, go ahead and log it. * This happens when you click through on a search suggestion */ public function onAfterInit() { $req = $this->owner->getRequest(); $src = $req->requestVar(Config::inst()->get('ShopSearch', 'qs_source')); if ($src) { $qs_q = Config::inst()->get('ShopSearch', 'qs_query'); $qs_f = Config::inst()->get('ShopSearch', 'qs_filters'); $vars = json_decode(base64_decode($src), true); // log the search $log = SearchLog::create(array('Query' => strtolower($vars[$qs_q]), 'Link' => $req->getURL(false), 'NumResults' => $vars['total'], 'MemberID' => Member::currentUserID(), 'Filters' => !empty($vars[$qs_f]) ? json_encode($vars[$qs_f]) : null)); $log->write(); // redirect to the clean page $this->owner->redirect($req->getURL(false)); } }
/** * The result will contain at least the following: * Matches - SS_List of results * TotalMatches - total # of results, unlimited * Query - query string * Also saves a log record. * * @param array $vars * @param bool $logSearch [optional] * @param bool $useFacets [optional] * @param int $start [optional] * @param int $limit [optional] * @return ArrayData */ public function search(array $vars, $logSearch = true, $useFacets = true, $start = -1, $limit = -1) { $qs_q = $this->config()->get('qs_query'); $qs_f = $this->config()->get('qs_filters'); $qs_ps = $this->config()->get('qs_parent_search'); $qs_t = $this->config()->get('qs_title'); $qs_sort = $this->config()->get('qs_sort'); if ($limit < 0) { $limit = $this->config()->get('page_size'); } if ($start < 0) { $start = !empty($vars['start']) ? (int) $vars['start'] : 0; } // as far as i can see, fulltextsearch hard codes 'start' $facets = $useFacets ? $this->config()->get('facets') : array(); if (!is_array($facets)) { $facets = array(); } if (empty($limit)) { $limit = -1; } // figure out and scrub the sort $sortOptions = $this->config()->get('sort_options'); $sort = !empty($vars[$qs_sort]) ? $vars[$qs_sort] : ''; if (!isset($sortOptions[$sort])) { $sort = current(array_keys($sortOptions)); } // figure out and scrub the filters $filters = !empty($vars[$qs_f]) ? FacetHelper::inst()->scrubFilters($vars[$qs_f]) : array(); // do the search $keywords = !empty($vars[$qs_q]) ? $vars[$qs_q] : ''; if ($keywordRegex = $this->config()->get('keyword_filter_regex')) { $keywords = preg_replace($keywordRegex, '', $keywords); } $results = self::adapter()->searchFromVars($keywords, $filters, $facets, $start, $limit, $sort); // massage the results a bit if (!empty($keywords) && !$results->hasValue('Query')) { $results->Query = $keywords; } if (!empty($filters) && !$results->hasValue('Filters')) { $results->Filters = new ArrayData($filters); } if (!$results->hasValue('Sort')) { $results->Sort = $sort; } if (!$results->hasValue('TotalMatches')) { $results->TotalMatches = $results->Matches->hasMethod('getTotalItems') ? $results->Matches->getTotalItems() : $results->Matches->count(); } // for some types of facets, update the state if ($results->hasValue('Facets')) { FacetHelper::inst()->transformHierarchies($results->Facets); FacetHelper::inst()->updateFacetState($results->Facets, $filters); } // make a hash of the search so we can know if we've already logged it this session $loggedFilters = !empty($filters) ? json_encode($filters) : null; $loggedQuery = strtolower($results->Query); // $searchHash = md5($loggedFilters . $loggedQuery); // $sessSearches = Session::get('loggedSearches'); // if (!is_array($sessSearches)) $sessSearches = array(); // Debug::dump($searchHash, $sessSearches); // save the log record if ($start == 0 && $logSearch && (!empty($keywords) || !empty($filters))) { // && !in_array($searchHash, $sessSearches)) { $log = SearchLog::create(array('Query' => $loggedQuery, 'Title' => !empty($vars[$qs_t]) ? $vars[$qs_t] : '', 'Link' => Controller::curr()->getRequest()->getURL(true), 'NumResults' => $results->TotalMatches, 'MemberID' => Member::currentUserID(), 'Filters' => $loggedFilters, 'ParentSearchID' => !empty($vars[$qs_ps]) ? $vars[$qs_ps] : 0)); $log->write(); $results->SearchLogID = $log->ID; $results->SearchBreadcrumbs = $log->getBreadcrumbs(); // $sessSearches[] = $searchHash; // Session::set('loggedSearches', $sessSearches); } return $results; }