Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / search / controller / PhabricatorSearchRelationshipSourceController.php
blob4a96727a294cc714a23e5ebcad5717db4c9f85bb
1 <?php
3 final class PhabricatorSearchRelationshipSourceController
4 extends PhabricatorSearchBaseController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
9 $object = $this->loadRelationshipObject();
10 if (!$object) {
11 return new Aphront404Response();
14 $relationship = $this->loadRelationship($object);
15 if (!$relationship) {
16 return new Aphront404Response();
19 $source = $relationship->newSource();
20 $query = new PhabricatorSavedQuery();
22 $action = $request->getURIData('action');
23 $query_str = $request->getStr('query');
24 $filter = $request->getStr('filter');
26 $query->setEngineClassName('PhabricatorSearchApplicationSearchEngine');
27 $query->setParameter('query', $query_str);
29 $types = $source->getResultPHIDTypes();
30 $query->setParameter('types', $types);
32 $status_open = PhabricatorSearchRelationship::RELATIONSHIP_OPEN;
34 switch ($filter) {
35 case 'assigned':
36 $query->setParameter('ownerPHIDs', array($viewer->getPHID()));
37 $query->setParameter('statuses', array($status_open));
38 break;
39 case 'created';
40 $query->setParameter('authorPHIDs', array($viewer->getPHID()));
41 $query->setParameter('statuses', array($status_open));
42 break;
43 case 'open':
44 $query->setParameter('statuses', array($status_open));
45 break;
48 $query->setParameter('excludePHIDs', array($request->getStr('exclude')));
50 $capabilities = $relationship->getRequiredRelationshipCapabilities();
52 $results = id(new PhabricatorSearchDocumentQuery())
53 ->setViewer($viewer)
54 ->requireObjectCapabilities($capabilities)
55 ->withSavedQuery($query)
56 ->setOffset(0)
57 ->setLimit(100)
58 ->execute();
60 $phids = array_fill_keys(mpull($results, 'getPHID'), true);
61 $phids = $this->queryObjectNames($query, $capabilities) + $phids;
63 $phids = array_keys($phids);
64 $handles = $viewer->loadHandles($phids);
66 $data = array();
67 foreach ($handles as $handle) {
68 $view = new PhabricatorHandleObjectSelectorDataView($handle);
69 $data[] = $view->renderData();
72 return id(new AphrontAjaxResponse())->setContent($data);
75 private function queryObjectNames(
76 PhabricatorSavedQuery $query,
77 array $capabilities) {
79 $request = $this->getRequest();
80 $viewer = $request->getUser();
82 $types = $query->getParameter('types');
83 $match = $query->getParameter('query');
85 $objects = id(new PhabricatorObjectQuery())
86 ->setViewer($viewer)
87 ->requireCapabilities($capabilities)
88 ->withTypes($query->getParameter('types'))
89 ->withNames(array($match))
90 ->execute();
92 return mpull($objects, 'getPHID');