@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 177 lines 4.9 kB view raw
1<?php 2 3final class HeraldRuleSearchEngine extends PhabricatorApplicationSearchEngine { 4 5 public function getResultTypeDescription() { 6 return pht('Herald Rules'); 7 } 8 9 public function getApplicationClassName() { 10 return PhabricatorHeraldApplication::class; 11 } 12 13 public function newQuery() { 14 return id(new HeraldRuleQuery()) 15 ->needValidateAuthors(true); 16 } 17 18 protected function buildCustomSearchFields() { 19 $viewer = $this->requireViewer(); 20 21 $rule_types = HeraldRuleTypeConfig::getRuleTypeMap(); 22 $content_types = HeraldAdapter::getEnabledAdapterMap($viewer); 23 24 return array( 25 id(new PhabricatorUsersSearchField()) 26 ->setLabel(pht('Authors')) 27 ->setKey('authorPHIDs') 28 ->setAliases(array('author', 'authors', 'authorPHID')) 29 ->setDescription( 30 pht('Search for rules with given authors.')), 31 id(new PhabricatorSearchCheckboxesField()) 32 ->setKey('ruleTypes') 33 ->setAliases(array('ruleType')) 34 ->setLabel(pht('Rule Type')) 35 ->setDescription( 36 pht('Search for rules of given types.')) 37 ->setOptions($rule_types), 38 id(new PhabricatorSearchCheckboxesField()) 39 ->setKey('contentTypes') 40 ->setLabel(pht('Content Type')) 41 ->setDescription( 42 pht('Search for rules affecting given types of content.')) 43 ->setOptions($content_types), 44 id(new PhabricatorSearchThreeStateField()) 45 ->setLabel(pht('Active Rules')) 46 ->setKey('active') 47 ->setOptions( 48 pht('(Show All)'), 49 pht('Show Only Active Rules'), 50 pht('Show Only Inactive Rules')), 51 id(new PhabricatorSearchThreeStateField()) 52 ->setLabel(pht('Disabled Rules')) 53 ->setKey('disabled') 54 ->setOptions( 55 pht('(Show All)'), 56 pht('Show Only Disabled Rules'), 57 pht('Show Only Enabled Rules')), 58 id(new PhabricatorPHIDsSearchField()) 59 ->setLabel(pht('Affected Objects')) 60 ->setKey('affectedPHIDs') 61 ->setAliases(array('affectedPHID')), 62 ); 63 } 64 65 protected function buildQueryFromParameters(array $map) { 66 $query = $this->newQuery(); 67 68 if ($map['authorPHIDs']) { 69 $query->withAuthorPHIDs($map['authorPHIDs']); 70 } 71 72 if ($map['contentTypes']) { 73 $query->withContentTypes($map['contentTypes']); 74 } 75 76 if ($map['ruleTypes']) { 77 $query->withRuleTypes($map['ruleTypes']); 78 } 79 80 if ($map['disabled'] !== null) { 81 $query->withDisabled($map['disabled']); 82 } 83 84 if ($map['active'] !== null) { 85 $query->withActive($map['active']); 86 } 87 88 if ($map['affectedPHIDs']) { 89 $query->withAffectedObjectPHIDs($map['affectedPHIDs']); 90 } 91 92 return $query; 93 } 94 95 protected function getURI($path) { 96 return '/herald/'.$path; 97 } 98 99 protected function getBuiltinQueryNames() { 100 $names = array(); 101 102 if ($this->requireViewer()->isLoggedIn()) { 103 $names['authored'] = pht('Authored'); 104 } 105 106 $names['active'] = pht('Active'); 107 $names['all'] = pht('All'); 108 109 return $names; 110 } 111 112 public function buildSavedQueryFromBuiltin($query_key) { 113 $query = $this->newSavedQuery(); 114 $query->setQueryKey($query_key); 115 116 $viewer_phid = $this->requireViewer()->getPHID(); 117 118 switch ($query_key) { 119 case 'all': 120 return $query; 121 case 'active': 122 return $query 123 ->setParameter('active', true); 124 case 'authored': 125 return $query 126 ->setParameter('authorPHIDs', array($viewer_phid)) 127 ->setParameter('disabled', false); 128 } 129 130 return parent::buildSavedQueryFromBuiltin($query_key); 131 } 132 133 /** 134 * @param array<HeraldRule> $rules 135 * @param PhabricatorSavedQuery $query 136 * @param array<PhabricatorObjectHandle> $handles 137 */ 138 protected function renderResultList( 139 array $rules, 140 PhabricatorSavedQuery $query, 141 array $handles) { 142 assert_instances_of($rules, HeraldRule::class); 143 $viewer = $this->requireViewer(); 144 145 $list = id(new HeraldRuleListView()) 146 ->setViewer($viewer) 147 ->setRules($rules) 148 ->newObjectList(); 149 150 $result = new PhabricatorApplicationSearchResultView(); 151 $result->setObjectList($list); 152 $result->setNoDataString(pht('No rules found.')); 153 154 return $result; 155 } 156 157 protected function getNewUserBody() { 158 $create_button = id(new PHUIButtonView()) 159 ->setTag('a') 160 ->setText(pht('Create Herald Rule')) 161 ->setHref('/herald/create/') 162 ->setColor(PHUIButtonView::GREEN); 163 164 $icon = $this->getApplication()->getIcon(); 165 $app_name = $this->getApplication()->getName(); 166 $view = id(new PHUIBigInfoView()) 167 ->setIcon($icon) 168 ->setTitle(pht('Welcome to %s', $app_name)) 169 ->setDescription( 170 pht('A flexible rules engine that can notify and act on '. 171 'other actions such as tasks, diffs, and commits.')) 172 ->addAction($create_button); 173 174 return $view; 175 } 176 177}