[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/almanac/controller/ -> AlmanacPropertyEditController.php (source)

   1  <?php
   2  
   3  final class AlmanacPropertyEditController
   4    extends AlmanacDeviceController {
   5  
   6    public function handleRequest(AphrontRequest $request) {
   7      $viewer = $request->getViewer();
   8  
   9      $id = $request->getURIData('id');
  10      if ($id) {
  11        $property = id(new AlmanacPropertyQuery())
  12          ->setViewer($viewer)
  13          ->withIDs(array($id))
  14          ->requireCapabilities(
  15            array(
  16              PhabricatorPolicyCapability::CAN_VIEW,
  17              PhabricatorPolicyCapability::CAN_EDIT,
  18            ))
  19          ->executeOne();
  20        if (!$property) {
  21          return new Aphront404Response();
  22        }
  23  
  24        $object = $property->getObject();
  25  
  26        $is_new = false;
  27        $title = pht('Edit Property');
  28        $save_button = pht('Save Changes');
  29      } else {
  30        $object = id(new PhabricatorObjectQuery())
  31          ->setViewer($viewer)
  32          ->withPHIDs(array($request->getStr('objectPHID')))
  33          ->requireCapabilities(
  34            array(
  35              PhabricatorPolicyCapability::CAN_VIEW,
  36              PhabricatorPolicyCapability::CAN_EDIT,
  37            ))
  38          ->executeOne();
  39        if (!$object) {
  40          return new Aphront404Response();
  41        }
  42  
  43        $is_new = true;
  44        $title = pht('Add Property');
  45        $save_button = pht('Add Property');
  46      }
  47  
  48      if (!($object instanceof AlmanacPropertyInterface)) {
  49        return new Aphront404Response();
  50      }
  51  
  52      $cancel_uri = $object->getURI();
  53  
  54      if ($is_new) {
  55        $errors = array();
  56        $property = null;
  57  
  58        $v_name = null;
  59        $e_name = true;
  60  
  61        if ($request->isFormPost()) {
  62          $name = $request->getStr('name');
  63          if (!strlen($name)) {
  64            $e_name = pht('Required');
  65            $errors[] = pht('You must provide a property name.');
  66          } else {
  67            $caught = null;
  68            try {
  69              AlmanacNames::validateServiceOrDeviceName($name);
  70            } catch (Exception $ex) {
  71              $caught = $ex;
  72            }
  73            if ($caught) {
  74              $e_name = pht('Invalid');
  75              $errors[] = $caught->getMessage();
  76            }
  77          }
  78  
  79          if (!$errors) {
  80            $property = id(new AlmanacPropertyQuery())
  81              ->setViewer($viewer)
  82              ->withObjectPHIDs(array($object->getPHID()))
  83              ->withNames(array($name))
  84              ->requireCapabilities(
  85                array(
  86                  PhabricatorPolicyCapability::CAN_VIEW,
  87                  PhabricatorPolicyCapability::CAN_EDIT,
  88                ))
  89              ->executeOne();
  90            if (!$property) {
  91              $property = id(new AlmanacProperty())
  92                ->setObjectPHID($object->getPHID())
  93                ->setFieldName($name);
  94            }
  95          }
  96        }
  97  
  98        if (!$property) {
  99          $form = id(new AphrontFormView())
 100            ->setUser($viewer)
 101            ->appendChild(
 102              id(new AphrontFormTextControl())
 103                ->setName('name')
 104                ->setLabel(pht('Name'))
 105                ->setValue($v_name)
 106                ->setError($e_name));
 107  
 108          return $this->newDialog()
 109            ->setTitle($title)
 110            ->setErrors($errors)
 111            ->addHiddenInput('objectPHID', $request->getStr('objectPHID'))
 112            ->appendForm($form)
 113            ->addSubmitButton(pht('Continue'))
 114            ->addCancelButton($cancel_uri);
 115        }
 116      }
 117  
 118      $v_name = $property->getFieldName();
 119      $e_name = true;
 120  
 121      $v_value = $property->getFieldValue();
 122      $e_value = null;
 123  
 124      $object->attachAlmanacProperties(array($property));
 125  
 126      $field_list = PhabricatorCustomField::getObjectFields(
 127        $object,
 128        PhabricatorCustomField::ROLE_EDIT);
 129      $field_list
 130        ->setViewer($viewer)
 131        ->readFieldsFromStorage($object);
 132  
 133      $validation_exception = null;
 134      if ($request->isFormPost() && $request->getStr('isValueEdit')) {
 135        $xactions = $field_list->buildFieldTransactionsFromRequest(
 136          $object->getApplicationTransactionTemplate(),
 137          $request);
 138  
 139        $editor = $object->getApplicationTransactionEditor()
 140          ->setActor($viewer)
 141          ->setContentSourceFromRequest($request)
 142          ->setContinueOnNoEffect(true)
 143          ->setContinueOnMissingFields(true);
 144  
 145        try {
 146          $editor->applyTransactions($object, $xactions);
 147          return id(new AphrontRedirectResponse())->setURI($cancel_uri);
 148        } catch (PhabricatorApplicationTransactionValidationException $ex) {
 149          $validation_exception = $ex;
 150        }
 151      }
 152  
 153      $form = id(new AphrontFormView())
 154        ->setUser($viewer)
 155        ->addHiddenInput('objectPHID', $request->getStr('objectPHID'))
 156        ->addHiddenInput('name', $request->getStr('name'))
 157        ->addHiddenInput('isValueEdit', true);
 158  
 159      $field_list->appendFieldsToForm($form);
 160  
 161      return $this->newDialog()
 162        ->setTitle($title)
 163        ->setValidationException($validation_exception)
 164        ->appendForm($form)
 165        ->addSubmitButton($save_button)
 166        ->addCancelButton($cancel_uri);
 167    }
 168  
 169  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1