Path: blob/master/src/applications/almanac/editor/AlmanacInterfaceEditEngine.php
12256 views
<?php12final class AlmanacInterfaceEditEngine3extends PhabricatorEditEngine {45const ENGINECONST = 'almanac.interface';67private $device;89public function setDevice(AlmanacDevice $device) {10$this->device = $device;11return $this;12}1314public function getDevice() {15if (!$this->device) {16throw new PhutilInvalidStateException('setDevice');17}18return $this->device;19}2021public function isEngineConfigurable() {22return false;23}2425public function getEngineName() {26return pht('Almanac Interfaces');27}2829public function getSummaryHeader() {30return pht('Edit Almanac Interface Configurations');31}3233public function getSummaryText() {34return pht('This engine is used to edit Almanac interfaces.');35}3637public function getEngineApplicationClass() {38return 'PhabricatorAlmanacApplication';39}4041protected function newEditableObject() {42$interface = AlmanacInterface::initializeNewInterface();4344$device = $this->getDevice();45$interface46->setDevicePHID($device->getPHID())47->attachDevice($device);4849return $interface;50}5152protected function newEditableObjectForDocumentation() {53$this->setDevice(new AlmanacDevice());54return $this->newEditableObject();55}5657protected function newEditableObjectFromConduit(array $raw_xactions) {58$device_phid = null;59foreach ($raw_xactions as $raw_xaction) {60if ($raw_xaction['type'] !== 'device') {61continue;62}6364$device_phid = $raw_xaction['value'];65}6667if ($device_phid === null) {68throw new Exception(69pht(70'When creating a new Almanac interface via the Conduit API, you '.71'must provide a "device" transaction to select a device.'));72}7374$device = id(new AlmanacDeviceQuery())75->setViewer($this->getViewer())76->withPHIDs(array($device_phid))77->requireCapabilities(78array(79PhabricatorPolicyCapability::CAN_VIEW,80PhabricatorPolicyCapability::CAN_EDIT,81))82->executeOne();83if (!$device) {84throw new Exception(85pht(86'Device "%s" is unrecognized, restricted, or you do not have '.87'permission to edit it.',88$device_phid));89}9091$this->setDevice($device);9293return $this->newEditableObject();94}9596protected function newObjectQuery() {97return new AlmanacInterfaceQuery();98}99100protected function getObjectCreateTitleText($object) {101return pht('Create Interface');102}103104protected function getObjectCreateButtonText($object) {105return pht('Create Interface');106}107108protected function getObjectEditTitleText($object) {109return pht('Edit Interface');110}111112protected function getObjectEditShortText($object) {113return pht('Edit Interface');114}115116protected function getObjectCreateShortText() {117return pht('Create Interface');118}119120protected function getObjectName() {121return pht('Interface');122}123124protected function getEditorURI() {125return '/almanac/interface/edit/';126}127128protected function getObjectCreateCancelURI($object) {129if ($this->getDevice()) {130return $this->getDevice()->getURI();131}132return '/almanac/interface/';133}134135protected function getObjectViewURI($object) {136return $object->getDevice()->getURI();137}138139protected function buildCustomEditFields($object) {140$viewer = $this->getViewer();141142// TODO: Some day, this should be a datasource.143$networks = id(new AlmanacNetworkQuery())144->setViewer($viewer)145->execute();146$network_map = mpull($networks, 'getName', 'getPHID');147148return array(149id(new PhabricatorTextEditField())150->setKey('device')151->setLabel(pht('Device'))152->setIsFormField(false)153->setTransactionType(154AlmanacInterfaceDeviceTransaction::TRANSACTIONTYPE)155->setDescription(pht('When creating an interface, set the device.'))156->setConduitDescription(pht('Set the device.'))157->setConduitTypeDescription(pht('Device PHID.'))158->setValue($object->getDevicePHID()),159id(new PhabricatorSelectEditField())160->setKey('network')161->setLabel(pht('Network'))162->setDescription(pht('Network for the interface.'))163->setTransactionType(164AlmanacInterfaceNetworkTransaction::TRANSACTIONTYPE)165->setValue($object->getNetworkPHID())166->setOptions($network_map),167id(new PhabricatorTextEditField())168->setKey('address')169->setLabel(pht('Address'))170->setDescription(pht('Address of the service.'))171->setTransactionType(172AlmanacInterfaceAddressTransaction::TRANSACTIONTYPE)173->setIsRequired(true)174->setValue($object->getAddress()),175id(new PhabricatorIntEditField())176->setKey('port')177->setLabel(pht('Port'))178->setDescription(pht('Port of the service.'))179->setTransactionType(AlmanacInterfacePortTransaction::TRANSACTIONTYPE)180->setIsRequired(true)181->setValue($object->getPort()),182);183}184185}186187188