@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 AlmanacInterfaceEditEngine
4 extends PhabricatorEditEngine {
5
6 const ENGINECONST = 'almanac.interface';
7
8 private $device;
9
10 public function setDevice(AlmanacDevice $device) {
11 $this->device = $device;
12 return $this;
13 }
14
15 public function getDevice() {
16 if (!$this->device) {
17 throw new PhutilInvalidStateException('setDevice');
18 }
19 return $this->device;
20 }
21
22 public function isEngineConfigurable() {
23 return false;
24 }
25
26 public function getEngineName() {
27 return pht('Almanac Interfaces');
28 }
29
30 public function getSummaryHeader() {
31 return pht('Edit Almanac Interface Configurations');
32 }
33
34 public function getSummaryText() {
35 return pht('This engine is used to edit Almanac interfaces.');
36 }
37
38 public function getEngineApplicationClass() {
39 return PhabricatorAlmanacApplication::class;
40 }
41
42 protected function newEditableObject() {
43 $interface = AlmanacInterface::initializeNewInterface();
44
45 $device = $this->getDevice();
46 $interface
47 ->setDevicePHID($device->getPHID())
48 ->attachDevice($device);
49
50 return $interface;
51 }
52
53 protected function newEditableObjectForDocumentation() {
54 $this->setDevice(new AlmanacDevice());
55 return $this->newEditableObject();
56 }
57
58 protected function newEditableObjectFromConduit(array $raw_xactions) {
59 $device_phid = null;
60 foreach ($raw_xactions as $raw_xaction) {
61 if ($raw_xaction['type'] !== 'device') {
62 continue;
63 }
64
65 $device_phid = $raw_xaction['value'];
66 }
67
68 if ($device_phid === null) {
69 throw new Exception(
70 pht(
71 'When creating a new Almanac interface via the Conduit API, you '.
72 'must provide a "device" transaction to select a device.'));
73 }
74
75 $device = id(new AlmanacDeviceQuery())
76 ->setViewer($this->getViewer())
77 ->withPHIDs(array($device_phid))
78 ->requireCapabilities(
79 array(
80 PhabricatorPolicyCapability::CAN_VIEW,
81 PhabricatorPolicyCapability::CAN_EDIT,
82 ))
83 ->executeOne();
84 if (!$device) {
85 throw new Exception(
86 pht(
87 'Device "%s" is unrecognized, restricted, or you do not have '.
88 'permission to edit it.',
89 $device_phid));
90 }
91
92 $this->setDevice($device);
93
94 return $this->newEditableObject();
95 }
96
97 protected function newObjectQuery() {
98 return new AlmanacInterfaceQuery();
99 }
100
101 protected function getObjectCreateTitleText($object) {
102 return pht('Create Interface');
103 }
104
105 protected function getObjectCreateButtonText($object) {
106 return pht('Create Interface');
107 }
108
109 protected function getObjectEditTitleText($object) {
110 return pht('Edit Interface');
111 }
112
113 protected function getObjectEditShortText($object) {
114 return pht('Edit Interface');
115 }
116
117 protected function getObjectCreateShortText() {
118 return pht('Create Interface');
119 }
120
121 protected function getObjectName() {
122 return pht('Interface');
123 }
124
125 protected function getEditorURI() {
126 return '/almanac/interface/edit/';
127 }
128
129 protected function getObjectCreateCancelURI($object) {
130 if ($this->getDevice()) {
131 return $this->getDevice()->getURI();
132 }
133 return '/almanac/interface/';
134 }
135
136 protected function getObjectViewURI($object) {
137 return $object->getDevice()->getURI();
138 }
139
140 protected function buildCustomEditFields($object) {
141 $viewer = $this->getViewer();
142
143 // TODO: Some day, this should be a datasource.
144 $networks = id(new AlmanacNetworkQuery())
145 ->setViewer($viewer)
146 ->execute();
147 $network_map = mpull($networks, 'getName', 'getPHID');
148
149 return array(
150 id(new PhabricatorTextEditField())
151 ->setKey('device')
152 ->setLabel(pht('Device'))
153 ->setIsFormField(false)
154 ->setTransactionType(
155 AlmanacInterfaceDeviceTransaction::TRANSACTIONTYPE)
156 ->setDescription(pht('When creating an interface, set the device.'))
157 ->setConduitDescription(pht('Set the device.'))
158 ->setConduitTypeDescription(pht('Device PHID.'))
159 ->setValue($object->getDevicePHID()),
160 id(new PhabricatorSelectEditField())
161 ->setKey('network')
162 ->setLabel(pht('Network'))
163 ->setDescription(pht('Network for the interface.'))
164 ->setTransactionType(
165 AlmanacInterfaceNetworkTransaction::TRANSACTIONTYPE)
166 ->setValue($object->getNetworkPHID())
167 ->setOptions($network_map),
168 id(new PhabricatorTextEditField())
169 ->setKey('address')
170 ->setLabel(pht('Address'))
171 ->setDescription(pht('Address of the service.'))
172 ->setTransactionType(
173 AlmanacInterfaceAddressTransaction::TRANSACTIONTYPE)
174 ->setIsRequired(true)
175 ->setValue($object->getAddress()),
176 id(new PhabricatorIntEditField())
177 ->setKey('port')
178 ->setLabel(pht('Port'))
179 ->setDescription(pht('Port of the service.'))
180 ->setTransactionType(AlmanacInterfacePortTransaction::TRANSACTIONTYPE)
181 ->setIsRequired(true)
182 ->setValue($object->getPort()),
183 );
184 }
185
186}