@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 154 lines 3.9 kB view raw
1<?php 2 3final class FundInitiativeSearchEngine 4 extends PhabricatorApplicationSearchEngine { 5 6 public function getResultTypeDescription() { 7 return pht('Fund Initiatives'); 8 } 9 10 public function getApplicationClassName() { 11 return PhabricatorFundApplication::class; 12 } 13 14 public function newQuery() { 15 return new FundInitiativeQuery(); 16 } 17 18 protected function buildCustomSearchFields() { 19 return array( 20 id(new PhabricatorUsersSearchField()) 21 ->setKey('ownerPHIDs') 22 ->setAliases(array('owner', 'ownerPHID', 'owners')) 23 ->setLabel(pht('Owners')), 24 id(new PhabricatorSearchCheckboxesField()) 25 ->setKey('statuses') 26 ->setLabel(pht('Statuses')) 27 ->setOptions(FundInitiative::getStatusNameMap()), 28 ); 29 } 30 31 protected function buildQueryFromParameters(array $map) { 32 $query = $this->newQuery(); 33 34 if ($map['ownerPHIDs']) { 35 $query->withOwnerPHIDs($map['ownerPHIDs']); 36 } 37 38 if ($map['statuses']) { 39 $query->withStatuses($map['statuses']); 40 } 41 42 return $query; 43 } 44 45 protected function getURI($path) { 46 return '/fund/'.$path; 47 } 48 49 protected function getBuiltinQueryNames() { 50 $names = array(); 51 52 $names['open'] = pht('Open Initiatives'); 53 if ($this->requireViewer()->isLoggedIn()) { 54 $names['owned'] = pht('Owned Initiatives'); 55 } 56 $names['all'] = pht('All Initiatives'); 57 58 return $names; 59 } 60 61 public function buildSavedQueryFromBuiltin($query_key) { 62 $query = $this->newSavedQuery(); 63 $query->setQueryKey($query_key); 64 65 switch ($query_key) { 66 case 'all': 67 return $query; 68 case 'owned': 69 return $query->setParameter( 70 'ownerPHIDs', 71 array( 72 $this->requireViewer()->getPHID(), 73 )); 74 case 'open': 75 return $query->setParameter( 76 'statuses', 77 array( 78 FundInitiative::STATUS_OPEN, 79 )); 80 } 81 82 return parent::buildSavedQueryFromBuiltin($query_key); 83 } 84 85 protected function renderResultList( 86 array $initiatives, 87 PhabricatorSavedQuery $query, 88 array $handles) { 89 assert_instances_of($initiatives, 'FundInitiative'); 90 91 $viewer = $this->requireViewer(); 92 93 $load_phids = array(); 94 foreach ($initiatives as $initiative) { 95 $load_phids[] = $initiative->getOwnerPHID(); 96 } 97 98 if ($initiatives) { 99 $edge_query = id(new PhabricatorEdgeQuery()) 100 ->withSourcePHIDs(mpull($initiatives, 'getPHID')) 101 ->withEdgeTypes( 102 array( 103 PhabricatorProjectObjectHasProjectEdgeType::EDGECONST, 104 )); 105 106 $edge_query->execute(); 107 108 foreach ($edge_query->getDestinationPHIDs() as $phid) { 109 $load_phids[] = $phid; 110 } 111 } 112 113 $handles = $viewer->loadHandles($load_phids); 114 $handles = iterator_to_array($handles); 115 116 $list = new PHUIObjectItemListView(); 117 foreach ($initiatives as $initiative) { 118 $owner_handle = $handles[$initiative->getOwnerPHID()]; 119 120 $item = id(new PHUIObjectItemView()) 121 ->setObjectName($initiative->getMonogram()) 122 ->setHeader($initiative->getName()) 123 ->setHref('/'.$initiative->getMonogram()) 124 ->addByline(pht('Owner: %s', $owner_handle->renderLink())); 125 126 if ($initiative->isClosed()) { 127 $item->setDisabled(true); 128 } 129 130 $project_phids = $edge_query->getDestinationPHIDs( 131 array( 132 $initiative->getPHID(), 133 )); 134 135 $project_handles = array_select_keys($handles, $project_phids); 136 if ($project_handles) { 137 $item->addAttribute( 138 id(new PHUIHandleTagListView()) 139 ->setLimit(4) 140 ->setSlim(true) 141 ->setHandles($project_handles)); 142 } 143 144 $list->addItem($item); 145 } 146 147 $result = new PhabricatorApplicationSearchResultView(); 148 $result->setObjectList($list); 149 $result->setNoDataString(pht('No initiatives found.')); 150 151 return $result; 152 } 153 154}