@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
1<?php
2
3final class PhabricatorProjectTriggerSearchEngine
4 extends PhabricatorApplicationSearchEngine {
5
6 public function getResultTypeDescription() {
7 return pht('Triggers');
8 }
9
10 public function getApplicationClassName() {
11 return PhabricatorProjectApplication::class;
12 }
13
14 public function newQuery() {
15 return id(new PhabricatorProjectTriggerQuery())
16 ->needUsage(true);
17 }
18
19 protected function buildCustomSearchFields() {
20 return array(
21 id(new PhabricatorSearchThreeStateField())
22 ->setLabel(pht('Active'))
23 ->setKey('isActive')
24 ->setOptions(
25 pht('(Show All)'),
26 pht('Show Only Active Triggers'),
27 pht('Show Only Inactive Triggers')),
28 );
29 }
30
31 protected function buildQueryFromParameters(array $map) {
32 $query = $this->newQuery();
33
34 if ($map['isActive'] !== null) {
35 if ($map['isActive']) {
36 $query->withActiveColumnCountBetween(1, null);
37 } else {
38 $query->withActiveColumnCountBetween(null, 0);
39 }
40 }
41
42 return $query;
43 }
44
45 protected function getURI($path) {
46 return '/project/trigger/'.$path;
47 }
48
49 protected function getBuiltinQueryNames() {
50 $names = array();
51
52 $names['active'] = pht('Active Triggers');
53 $names['all'] = pht('All Triggers');
54
55 return $names;
56 }
57
58 public function buildSavedQueryFromBuiltin($query_key) {
59 $query = $this->newSavedQuery();
60 $query->setQueryKey($query_key);
61
62 switch ($query_key) {
63 case 'active':
64 return $query->setParameter('isActive', true);
65 case 'all':
66 return $query;
67 }
68
69 return parent::buildSavedQueryFromBuiltin($query_key);
70 }
71
72 /**
73 * @param array<PhabricatorProjectTrigger> $triggers
74 * @param PhabricatorSavedQuery $query
75 * @param array<PhabricatorObjectHandle> $handles
76 */
77 protected function renderResultList(
78 array $triggers,
79 PhabricatorSavedQuery $query,
80 array $handles) {
81 assert_instances_of($triggers, PhabricatorProjectTrigger::class);
82 $viewer = $this->requireViewer();
83
84 $example_phids = array();
85 foreach ($triggers as $trigger) {
86 $example_phid = $trigger->getUsage()->getExamplePHID();
87 if ($example_phid) {
88 $example_phids[] = $example_phid;
89 }
90 }
91
92 $handles = $viewer->loadHandles($example_phids);
93
94 $list = id(new PHUIObjectItemListView())
95 ->setViewer($viewer);
96 foreach ($triggers as $trigger) {
97 $usage = $trigger->getUsage();
98
99 $column_handle = null;
100 $have_column = false;
101 $example_phid = $usage->getExamplePHID();
102 if ($example_phid) {
103 $column_handle = $handles[$example_phid];
104 if ($column_handle->isComplete()) {
105 if (!$column_handle->getPolicyFiltered()) {
106 $have_column = true;
107 }
108 }
109 }
110
111 $column_count = $usage->getColumnCount();
112 $active_count = $usage->getActiveColumnCount();
113
114 if ($have_column) {
115 if ($active_count > 1) {
116 $usage_description = pht(
117 'Used on %s and %s other active column(s).',
118 $column_handle->renderLink(),
119 new PhutilNumber($active_count - 1));
120 } else if ($column_count > 1) {
121 $usage_description = pht(
122 'Used on %s and %s other column(s).',
123 $column_handle->renderLink(),
124 new PhutilNumber($column_count - 1));
125 } else {
126 $usage_description = pht(
127 'Used on %s.',
128 $column_handle->renderLink());
129 }
130 } else {
131 if ($active_count) {
132 $usage_description = pht(
133 'Used on %s active column(s).',
134 new PhutilNumber($active_count));
135 } else if ($column_count) {
136 $usage_description = pht(
137 'Used on %s column(s).',
138 new PhutilNumber($column_count));
139 } else {
140 $usage_description = pht(
141 'Unused trigger.');
142 }
143 }
144
145 $item = id(new PHUIObjectItemView())
146 ->setObjectName($trigger->getObjectName())
147 ->setHeader($trigger->getDisplayName())
148 ->setHref($trigger->getURI())
149 ->addAttribute($usage_description)
150 ->setDisabled(!$active_count);
151
152 $list->addItem($item);
153 }
154
155 return id(new PhabricatorApplicationSearchResultView())
156 ->setObjectList($list)
157 ->setNoDataString(pht('No triggers found.'));
158 }
159
160}