public function handleRequest(AphrontRequest $request)
 {
     $viewer = $this->getViewer();
     if ($request->getStr('jump') != 'no') {
         $response = PhabricatorJumpNavHandler::getJumpResponse($viewer, $request->getStr('query'));
         if ($response) {
             return $response;
         }
     }
     $engine = new PhabricatorSearchApplicationSearchEngine();
     $engine->setViewer($viewer);
     // If we're coming from primary search, do some special handling to
     // interpret the scope selector and query.
     if ($request->getBool('search:primary')) {
         // If there's no query, just take the user to advanced search.
         if (!strlen($request->getStr('query'))) {
             $advanced_uri = '/search/query/advanced/';
             return id(new AphrontRedirectResponse())->setURI($advanced_uri);
         }
         // First, load or construct a template for the search by examining
         // the current search scope.
         $scope = $request->getStr('search:scope');
         $saved = null;
         if ($scope == self::SCOPE_CURRENT_APPLICATION) {
             $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)->setParameter('statuses', array('open'));
                 }
             }
         }
         if (!$saved && !$engine->isBuiltinQuery($scope)) {
             $saved = id(new PhabricatorSavedQueryQuery())->setViewer($viewer)->withQueryKeys(array($scope))->executeOne();
         }
         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'));
         $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
         try {
             $saved->setID(null)->save();
         } catch (AphrontDuplicateKeyQueryException $ex) {
             // Ignore, this is just a repeated search.
         }
         unset($unguarded);
         $query_key = $saved->getQueryKey();
         $results_uri = $engine->getQueryResultsPageURI($query_key) . '#R';
         return id(new AphrontRedirectResponse())->setURI($results_uri);
     }
     $controller = id(new PhabricatorApplicationSearchController())->setQueryKey($request->getURIData('queryKey'))->setSearchEngine($engine)->setNavigation($this->buildSideNavView());
     return $this->delegateToController($controller);
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $viewer = $request->getUser();
     if ($request->getStr('jump') != 'no') {
         $pref_jump = PhabricatorUserPreferences::PREFERENCE_SEARCHBAR_JUMP;
         if ($viewer->loadPreferences($pref_jump, 1)) {
             $response = PhabricatorJumpNavHandler::getJumpResponse($viewer, $request->getStr('query'));
             if ($response) {
                 return $response;
             }
         }
     }
     $engine = new PhabricatorSearchApplicationSearchEngine();
     $engine->setViewer($viewer);
     // NOTE: This is a little weird. If we're coming from primary search, we
     // load the user's first search filter and overwrite the "query" part of
     // 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')) {
         $named_queries = $engine->loadEnabledNamedQueries();
         if ($named_queries) {
             $named = head($named_queries);
             $query_key = $named->getQueryKey();
             $saved = null;
             if ($engine->isBuiltinQuery($query_key)) {
                 $saved = $engine->buildSavedQueryFromBuiltin($query_key);
             } else {
                 $saved = id(new PhabricatorSavedQueryQuery())->setViewer($viewer)->withQueryKeys(array($query_key))->executeOne();
             }
             if ($saved) {
                 $saved->setParameter('query', $request->getStr('query'));
                 $unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
                 try {
                     $saved->setID(null)->save();
                 } catch (AphrontDuplicateKeyQueryException $ex) {
                     // Ignore, this is just a repeated search.
                 }
                 unset($unguarded);
                 $results_uri = $engine->getQueryResultsPageURI($saved->getQueryKey()) . '#R';
                 return id(new AphrontRedirectResponse())->setURI($results_uri);
             }
         }
     }
     $controller = id(new PhabricatorApplicationSearchController($request))->setQueryKey($this->queryKey)->setSearchEngine($engine)->setNavigation($this->buildSideNavView());
     return $this->delegateToController($controller);
 }
 private function buildJumpResponse($nav)
 {
     $request = $this->getRequest();
     $jump = $request->getStr('jump');
     $response = PhabricatorJumpNavHandler::jumpPostResponse($jump);
     if ($response) {
         return $response;
     } else {
         if ($request->isFormPost()) {
             $query = new PhabricatorSearchQuery();
             $query->setQuery($jump);
             $query->save();
             return id(new AphrontRedirectResponse())->setURI('/search/' . $query->getQueryKey() . '/');
         }
     }
     $nav->appendChild($this->buildJumpPanel($jump));
     return $this->buildStandardPageResponse($nav, array('title' => 'Jump Nav'));
 }
 public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     if ($this->key) {
         $query = id(new PhabricatorSearchQuery())->loadOneWhere('queryKey = %s', $this->key);
         if (!$query) {
             return new Aphront404Response();
         }
     } else {
         $query = new PhabricatorSearchQuery();
         if ($request->isFormPost()) {
             $query_str = $request->getStr('query');
             $pref_jump = PhabricatorUserPreferences::PREFERENCE_SEARCHBAR_JUMP;
             if ($request->getStr('jump') != 'no' && $user && $user->loadPreferences()->getPreference($pref_jump, 1)) {
                 $response = PhabricatorJumpNavHandler::jumpPostResponse($query_str);
             } else {
                 $response = null;
             }
             if ($response) {
                 return $response;
             } else {
                 $query->setQuery($query_str);
                 if ($request->getStr('scope')) {
                     switch ($request->getStr('scope')) {
                         case PhabricatorSearchScope::SCOPE_OPEN_REVISIONS:
                             $query->setParameter('open', 1);
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_DREV);
                             break;
                         case PhabricatorSearchScope::SCOPE_OPEN_TASKS:
                             $query->setParameter('open', 1);
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_TASK);
                             break;
                         case PhabricatorSearchScope::SCOPE_WIKI:
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_WIKI);
                             break;
                         case PhabricatorSearchScope::SCOPE_COMMITS:
                             $query->setParameter('type', PhabricatorPHIDConstants::PHID_TYPE_CMIT);
                             break;
                         default:
                             break;
                     }
                 } else {
                     if (strlen($request->getStr('type'))) {
                         $query->setParameter('type', $request->getStr('type'));
                     }
                     if ($request->getArr('author')) {
                         $query->setParameter('author', $request->getArr('author'));
                     }
                     if ($request->getArr('owner')) {
                         $query->setParameter('owner', $request->getArr('owner'));
                     }
                     if ($request->getInt('open')) {
                         $query->setParameter('open', $request->getInt('open'));
                     }
                     if ($request->getArr('project')) {
                         $query->setParameter('project', $request->getArr('project'));
                     }
                 }
                 $query->save();
                 return id(new AphrontRedirectResponse())->setURI('/search/' . $query->getQueryKey() . '/');
             }
         }
     }
     $options = array('' => 'All Documents') + PhabricatorSearchAbstractDocument::getSupportedTypes();
     $status_options = array(0 => 'Open and Closed Documents', 1 => 'Open Documents');
     $phids = array_merge($query->getParameter('author', array()), $query->getParameter('owner', array()), $query->getParameter('project', array()));
     $handles = $this->loadViewerHandles($phids);
     $author_value = array_select_keys($handles, $query->getParameter('author', array()));
     $author_value = mpull($author_value, 'getFullName', 'getPHID');
     $owner_value = array_select_keys($handles, $query->getParameter('owner', array()));
     $owner_value = mpull($owner_value, 'getFullName', 'getPHID');
     $project_value = array_select_keys($handles, $query->getParameter('project', array()));
     $project_value = mpull($project_value, 'getFullName', 'getPHID');
     $search_form = new AphrontFormView();
     $search_form->setUser($user)->setAction('/search/')->appendChild(phutil_render_tag('input', array('type' => 'hidden', 'name' => 'jump', 'value' => 'no')))->appendChild(id(new AphrontFormTextControl())->setLabel('Search')->setName('query')->setValue($query->getQuery()))->appendChild(id(new AphrontFormSelectControl())->setLabel('Document Type')->setName('type')->setOptions($options)->setValue($query->getParameter('type')))->appendChild(id(new AphrontFormSelectControl())->setLabel('Document Status')->setName('open')->setOptions($status_options)->setValue($query->getParameter('open')))->appendChild(id(new AphrontFormTokenizerControl())->setName('author')->setLabel('Author')->setDatasource('/typeahead/common/users/')->setValue($author_value))->appendChild(id(new AphrontFormTokenizerControl())->setName('owner')->setLabel('Owner')->setDatasource('/typeahead/common/searchowner/')->setValue($owner_value)->setCaption('Tip: search for "Up For Grabs" to find unowned documents.'))->appendChild(id(new AphrontFormTokenizerControl())->setName('project')->setLabel('Project')->setDatasource('/typeahead/common/projects/')->setValue($project_value))->appendChild(id(new AphrontFormSubmitControl())->setValue('Search'));
     $search_panel = new AphrontPanelView();
     $search_panel->setHeader('Search Phabricator');
     $search_panel->appendChild($search_form);
     require_celerity_resource('phabricator-search-results-css');
     if ($query->getID()) {
         $limit = 20;
         $pager = new AphrontPagerView();
         $pager->setURI($request->getRequestURI(), 'page');
         $pager->setPageSize($limit);
         $pager->setOffset($request->getInt('page'));
         $query->setParameter('limit', $limit + 1);
         $query->setParameter('offset', $pager->getOffset());
         $engine = PhabricatorSearchEngineSelector::newSelector()->newEngine();
         $results = $engine->executeSearch($query);
         $results = $pager->sliceResults($results);
         if (!$request->getInt('page')) {
             $jump = PhabricatorPHID::fromObjectName($query->getQuery());
             if ($jump) {
                 array_unshift($results, $jump);
             }
         }
         if ($results) {
             $loader = new PhabricatorObjectHandleData($results);
             $handles = $loader->loadHandles();
             $objects = $loader->loadObjects();
             $results = array();
             foreach ($handles as $phid => $handle) {
                 $view = new PhabricatorSearchResultView();
                 $view->setHandle($handle);
                 $view->setQuery($query);
                 $view->setObject(idx($objects, $phid));
                 $results[] = $view->render();
             }
             $results = '<div class="phabricator-search-result-list">' . implode("\n", $results) . '<div class="search-results-pager">' . $pager->render() . '</div>' . '</div>';
         } else {
             $results = '<div class="phabricator-search-result-list">' . '<p class="phabricator-search-no-results">No search results.</p>' . '</div>';
         }
     } else {
         $results = null;
     }
     return $this->buildStandardPageResponse(array($search_panel, $results), array('title' => 'Search Results'));
 }