@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 recaptime-dev/main 171 lines 4.7 kB view raw
1<?php 2 3final class AlmanacDeviceSearchEngine 4 extends PhabricatorApplicationSearchEngine { 5 6 public function getResultTypeDescription() { 7 return pht('Almanac Devices'); 8 } 9 10 public function getApplicationClassName() { 11 return PhabricatorAlmanacApplication::class; 12 } 13 14 public function newQuery() { 15 return new AlmanacDeviceQuery(); 16 } 17 18 protected function buildCustomSearchFields() { 19 $status_options = AlmanacDeviceStatus::getStatusMap(); 20 $status_options = mpull($status_options, 'getName'); 21 22 return array( 23 id(new PhabricatorSearchTextField()) 24 ->setLabel(pht('Name Contains')) 25 ->setKey('match') 26 ->setDescription(pht('Search for devices by name substring.')), 27 id(new PhabricatorSearchStringListField()) 28 ->setLabel(pht('Exact Names')) 29 ->setKey('names') 30 ->setDescription(pht('Search for devices with specific names.')), 31 id(new PhabricatorSearchCheckboxesField()) 32 ->setLabel(pht('Statuses')) 33 ->setKey('statuses') 34 ->setDescription(pht('Search for devices with given statuses.')) 35 ->setOptions($status_options), 36 id(new PhabricatorSearchThreeStateField()) 37 ->setLabel(pht('Cluster Device')) 38 ->setKey('isClusterDevice') 39 ->setOptions( 40 pht('Both Cluster and Non-cluster Devices'), 41 pht('Cluster Devices Only'), 42 pht('Non-cluster Devices Only')), 43 ); 44 } 45 46 protected function buildQueryFromParameters(array $map) { 47 $query = $this->newQuery(); 48 49 if ($map['match'] !== null) { 50 $query->withNameNgrams($map['match']); 51 } 52 53 if ($map['names']) { 54 $query->withNames($map['names']); 55 } 56 57 if ($map['isClusterDevice'] !== null) { 58 $query->withIsClusterDevice($map['isClusterDevice']); 59 } 60 61 if ($map['statuses']) { 62 $query->withStatuses($map['statuses']); 63 } 64 65 return $query; 66 } 67 68 protected function getURI($path) { 69 return '/almanac/device/'.$path; 70 } 71 72 protected function getBuiltinQueryNames() { 73 $names = array( 74 'active' => pht('Active Devices'), 75 'all' => pht('All Devices'), 76 ); 77 78 return $names; 79 } 80 81 public function buildSavedQueryFromBuiltin($query_key) { 82 $query = $this->newSavedQuery(); 83 $query->setQueryKey($query_key); 84 85 switch ($query_key) { 86 case 'active': 87 $active_statuses = AlmanacDeviceStatus::getActiveStatusList(); 88 return $query->setParameter('statuses', $active_statuses); 89 case 'all': 90 return $query; 91 } 92 93 return parent::buildSavedQueryFromBuiltin($query_key); 94 } 95 96 /** 97 * @param array<AlmanacDevice> $devices 98 * @param PhabricatorSavedQuery $query 99 * @param array<PhabricatorObjectHandle> $handles 100 */ 101 protected function renderResultList( 102 array $devices, 103 PhabricatorSavedQuery $query, 104 array $handles) { 105 assert_instances_of($devices, AlmanacDevice::class); 106 107 $viewer = $this->requireViewer(); 108 109 $list = new PHUIObjectItemListView(); 110 $list->setUser($viewer); 111 foreach ($devices as $device) { 112 $item = id(new PHUIObjectItemView()) 113 ->setObjectName(pht('Device %d', $device->getID())) 114 ->setHeader($device->getName()) 115 ->setHref($device->getURI()) 116 ->setObject($device); 117 118 if ($device->isClusterDevice()) { 119 $item->addIcon('fa-sitemap', pht('Cluster Device')); 120 } 121 122 if ($device->isDisabled()) { 123 $item->setDisabled(true); 124 } 125 126 $status = $device->getStatusObject(); 127 $icon_icon = $status->getIconIcon(); 128 $icon_color = $status->getIconColor(); 129 $icon_label = $status->getName(); 130 131 $item->setStatusIcon( 132 "{$icon_icon} {$icon_color}", 133 $icon_label); 134 135 $list->addItem($item); 136 } 137 138 $result = new PhabricatorApplicationSearchResultView(); 139 $result->setObjectList($list); 140 $result->setNoDataString(pht('No Almanac Devices found.')); 141 142 return $result; 143 } 144 145 protected function getNewUserBody() { 146 $see_network = id(new PHUIButtonView()) 147 ->setTag('a') 148 ->setText(pht('See Networks')) 149 ->setHref('/almanac/network/'); 150 151 $create_button = id(new PHUIButtonView()) 152 ->setTag('a') 153 ->setText(pht('Create a Device')) 154 ->setHref('/almanac/device/edit/') 155 ->setIcon('fa-plus') 156 ->setColor(PHUIButtonView::GREEN); 157 158 $app_name = pht('Devices'); 159 $view = id(new PHUIBigInfoView()) 160 ->setIcon('fa-server') 161 ->setTitle(pht('Welcome to %s', $app_name)) 162 ->setDescription( 163 pht( 164 'Use Almanac devices to catalogue your build hosts '. 165 'and their SSH ports your network, and more.')) 166 ->addAction($see_network) 167 ->addAction($create_button); 168 169 return $view; 170 } 171}