Changeset View
Changeset View
Standalone View
Standalone View
src/applications/search/controller/PhabricatorSearchController.php
| <?php | <?php | ||||
| final class PhabricatorSearchController | final class PhabricatorSearchController | ||||
| extends PhabricatorSearchBaseController { | extends PhabricatorSearchBaseController { | ||||
| const SCOPE_CURRENT_APPLICATION = 'application'; | |||||
| private $queryKey; | private $queryKey; | ||||
| public function shouldAllowPublic() { | public function shouldAllowPublic() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| public function willProcessRequest(array $data) { | public function willProcessRequest(array $data) { | ||||
| $this->queryKey = idx($data, 'queryKey'); | $this->queryKey = idx($data, 'queryKey'); | ||||
| Show All 13 Lines | if ($request->getStr('jump') != 'no') { | ||||
| return $response; | return $response; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| $engine = new PhabricatorSearchApplicationSearchEngine(); | $engine = new PhabricatorSearchApplicationSearchEngine(); | ||||
| $engine->setViewer($viewer); | $engine->setViewer($viewer); | ||||
| // NOTE: This is a little weird. If we're coming from primary search, we | // If we're coming from primary search, do some special handling to | ||||
| // load the user's first search filter and overwrite the "query" part of | // interpret the scope selector and query. | ||||
| // it, then send them to that result page. This is sort of odd, but lets | |||||
| // users choose a default query like "Open Tasks" in a reasonable way, | |||||
| // with only this piece of somewhat-sketchy code. See discussion in T4365. | |||||
| if ($request->getBool('search:primary')) { | if ($request->getBool('search:primary')) { | ||||
| // If there's no query, just take the user to advanced search. | |||||
| if (!strlen($request->getStr('query'))) { | if (!strlen($request->getStr('query'))) { | ||||
| $advanced_uri = '/search/query/advanced/'; | $advanced_uri = '/search/query/advanced/'; | ||||
| return id(new AphrontRedirectResponse())->setURI($advanced_uri); | return id(new AphrontRedirectResponse())->setURI($advanced_uri); | ||||
| } | } | ||||
| $named_queries = $engine->loadEnabledNamedQueries(); | // First, load or construct a template for the search by examining | ||||
| if ($named_queries) { | // the current search scope. | ||||
| $named = head($named_queries); | $scope = $request->getStr('search:scope'); | ||||
| $query_key = $named->getQueryKey(); | |||||
| $saved = null; | $saved = null; | ||||
| if ($engine->isBuiltinQuery($query_key)) { | |||||
| $saved = $engine->buildSavedQueryFromBuiltin($query_key); | if ($scope == self::SCOPE_CURRENT_APPLICATION) { | ||||
| } else { | $application = id(new PhabricatorApplicationQuery()) | ||||
| ->setViewer($viewer) | |||||
| ->withClasses(array($request->getStr('search:application'))) | |||||
| ->executeOne(); | |||||
| if ($application) { | |||||
| $types = $application->getApplicationSearchDocumentTypes(); | |||||
| if ($types) { | |||||
| $saved = id(new PhabricatorSavedQuery()) | |||||
| ->setEngineClassName(get_class($engine)) | |||||
| ->setParameter('types', $types); | |||||
| } | |||||
| } | |||||
| } | |||||
| if (!$saved && !$engine->isBuiltinQuery($scope)) { | |||||
| $saved = id(new PhabricatorSavedQueryQuery()) | $saved = id(new PhabricatorSavedQueryQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withQueryKeys(array($query_key)) | ->withQueryKeys(array($scope)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| } | } | ||||
| if ($saved) { | if (!$saved) { | ||||
| if (!$engine->isBuiltinQuery($scope)) { | |||||
| $scope = 'all'; | |||||
| } | |||||
| $saved = $engine->buildSavedQueryFromBuiltin($scope); | |||||
| } | |||||
| // Add the user's query, then save this as a new saved query and send | |||||
| // the user to the results page. | |||||
| $saved->setParameter('query', $request->getStr('query')); | $saved->setParameter('query', $request->getStr('query')); | ||||
| $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites(); | ||||
| try { | try { | ||||
| $saved->setID(null)->save(); | $saved->setID(null)->save(); | ||||
| } catch (AphrontDuplicateKeyQueryException $ex) { | } catch (AphrontDuplicateKeyQueryException $ex) { | ||||
| // Ignore, this is just a repeated search. | // Ignore, this is just a repeated search. | ||||
| } | } | ||||
| unset($unguarded); | unset($unguarded); | ||||
| $results_uri = $engine->getQueryResultsPageURI( | $query_key = $saved->getQueryKey(); | ||||
| $saved->getQueryKey()).'#R'; | $results_uri = $engine->getQueryResultsPageURI($query_key).'#R'; | ||||
| return id(new AphrontRedirectResponse())->setURI($results_uri); | return id(new AphrontRedirectResponse())->setURI($results_uri); | ||||
| } | } | ||||
| } | |||||
| } | |||||
| $controller = id(new PhabricatorApplicationSearchController()) | $controller = id(new PhabricatorApplicationSearchController()) | ||||
| ->setQueryKey($this->queryKey) | ->setQueryKey($this->queryKey) | ||||
| ->setSearchEngine($engine) | ->setSearchEngine($engine) | ||||
| ->setNavigation($this->buildSideNavView()); | ->setNavigation($this->buildSideNavView()); | ||||
| return $this->delegateToController($controller); | return $this->delegateToController($controller); | ||||
| } | } | ||||
| Show All 17 Lines | |||||