Ejemplo n.º 1
0
 protected function loadPage()
 {
     $table = new AlmanacDevice();
     $conn_r = $table->establishConnection('r');
     $data = queryfx_all($conn_r, 'SELECT * FROM %T %Q %Q %Q', $table->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r));
     return $table->loadAllFromArray($data);
 }
 private function buildServicesTable(AlmanacDevice $device)
 {
     $viewer = $this->getViewer();
     // NOTE: We're loading all services so we can show hidden, locked services.
     // In general, we let you know about all the things the device is bound to,
     // even if you don't have permission to see their details. This is similar
     // to exposing the existence of edges in other applications, with the
     // addition of always letting you see that locks exist.
     $services = id(new AlmanacServiceQuery())->setViewer(PhabricatorUser::getOmnipotentUser())->withDevicePHIDs(array($device->getPHID()))->execute();
     $handles = $viewer->loadHandles(mpull($services, 'getPHID'));
     $icon_cluster = id(new PHUIIconView())->setIcon('fa-sitemap');
     $rows = array();
     foreach ($services as $service) {
         $rows[] = array($service->isClusterService() ? $icon_cluster : null, $handles->renderHandle($service->getPHID()));
     }
     $table = id(new AphrontTableView($rows))->setNoDataString(pht('No services are bound to this device.'))->setHeaders(array(null, pht('Service')))->setColumnClasses(array(null, 'wide pri'));
     return id(new PHUIObjectBoxView())->setHeaderText(pht('BOUND SERVICES'))->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)->setTable($table);
 }
 protected function newEditableObject()
 {
     return AlmanacDevice::initializeNewDevice();
 }
 public function handleRequest(AphrontRequest $request)
 {
     $viewer = $request->getViewer();
     $list_uri = $this->getApplicationURI('device/');
     $id = $request->getURIData('id');
     if ($id) {
         $device = id(new AlmanacDeviceQuery())->setViewer($viewer)->withIDs(array($id))->requireCapabilities(array(PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT))->executeOne();
         if (!$device) {
             return new Aphront404Response();
         }
         $is_new = false;
         $device_uri = $device->getURI();
         $cancel_uri = $device_uri;
         $title = pht('Edit Device');
         $save_button = pht('Save Changes');
     } else {
         $this->requireApplicationCapability(AlmanacCreateDevicesCapability::CAPABILITY);
         $device = AlmanacDevice::initializeNewDevice();
         $is_new = true;
         $cancel_uri = $list_uri;
         $title = pht('Create Device');
         $save_button = pht('Create Device');
     }
     $v_name = $device->getName();
     $e_name = true;
     $validation_exception = null;
     if ($is_new) {
         $v_projects = array();
     } else {
         $v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs($device->getPHID(), PhabricatorProjectObjectHasProjectEdgeType::EDGECONST);
         $v_projects = array_reverse($v_projects);
     }
     if ($request->isFormPost()) {
         $v_name = $request->getStr('name');
         $v_view = $request->getStr('viewPolicy');
         $v_edit = $request->getStr('editPolicy');
         $v_projects = $request->getArr('projects');
         $type_name = AlmanacDeviceTransaction::TYPE_NAME;
         $type_view = PhabricatorTransactions::TYPE_VIEW_POLICY;
         $type_edit = PhabricatorTransactions::TYPE_EDIT_POLICY;
         $xactions = array();
         $xactions[] = id(new AlmanacDeviceTransaction())->setTransactionType($type_name)->setNewValue($v_name);
         $xactions[] = id(new AlmanacDeviceTransaction())->setTransactionType($type_view)->setNewValue($v_view);
         $xactions[] = id(new AlmanacDeviceTransaction())->setTransactionType($type_edit)->setNewValue($v_edit);
         $proj_edge_type = PhabricatorProjectObjectHasProjectEdgeType::EDGECONST;
         $xactions[] = id(new AlmanacDeviceTransaction())->setTransactionType(PhabricatorTransactions::TYPE_EDGE)->setMetadataValue('edge:type', $proj_edge_type)->setNewValue(array('=' => array_fuse($v_projects)));
         $editor = id(new AlmanacDeviceEditor())->setActor($viewer)->setContentSourceFromRequest($request)->setContinueOnNoEffect(true);
         try {
             $editor->applyTransactions($device, $xactions);
             $device_uri = $device->getURI();
             return id(new AphrontRedirectResponse())->setURI($device_uri);
         } catch (PhabricatorApplicationTransactionValidationException $ex) {
             $validation_exception = $ex;
             $e_name = $ex->getShortMessage($type_name);
             $device->setViewPolicy($v_view);
             $device->setEditPolicy($v_edit);
         }
     }
     $policies = id(new PhabricatorPolicyQuery())->setViewer($viewer)->setObject($device)->execute();
     $form = id(new AphrontFormView())->setUser($viewer)->appendChild(id(new AphrontFormTextControl())->setLabel(pht('Name'))->setName('name')->setValue($v_name)->setError($e_name))->appendChild(id(new AphrontFormPolicyControl())->setName('viewPolicy')->setPolicyObject($device)->setCapability(PhabricatorPolicyCapability::CAN_VIEW)->setPolicies($policies))->appendChild(id(new AphrontFormPolicyControl())->setName('editPolicy')->setPolicyObject($device)->setCapability(PhabricatorPolicyCapability::CAN_EDIT)->setPolicies($policies))->appendControl(id(new AphrontFormTokenizerControl())->setLabel(pht('Projects'))->setName('projects')->setValue($v_projects)->setDatasource(new PhabricatorProjectDatasource()))->appendChild(id(new AphrontFormSubmitControl())->addCancelButton($cancel_uri)->setValue($save_button));
     $box = id(new PHUIObjectBoxView())->setValidationException($validation_exception)->setHeaderText($title)->setForm($form);
     $crumbs = $this->buildApplicationCrumbs();
     if ($is_new) {
         $crumbs->addTextCrumb(pht('Create Device'));
     } else {
         $crumbs->addTextCrumb($device->getName(), $device_uri);
         $crumbs->addTextCrumb(pht('Edit'));
     }
     return $this->newPage()->setTitle($title)->setCrumbs($crumbs)->appendChild(array($box));
 }
 protected function getDeviceDictionary(AlmanacDevice $device)
 {
     return array('id' => (int) $device->getID(), 'phid' => $device->getPHID(), 'name' => $device->getName(), 'properties' => $this->getPropertiesDictionary($device));
 }