[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phlux/controller/ -> PhluxEditController.php (source)

   1  <?php
   2  
   3  final class PhluxEditController extends PhluxController {
   4  
   5    private $key;
   6  
   7    public function willProcessRequest(array $data) {
   8      $this->key = idx($data, 'key');
   9    }
  10  
  11    public function processRequest() {
  12      $request = $this->getRequest();
  13      $user = $request->getUser();
  14  
  15      $is_new = ($this->key === null);
  16      if ($is_new) {
  17        $var = new PhluxVariable();
  18        $var->setViewPolicy(PhabricatorPolicies::POLICY_USER);
  19        $var->setEditPolicy(PhabricatorPolicies::POLICY_USER);
  20      } else {
  21        $var = id(new PhluxVariableQuery())
  22          ->setViewer($user)
  23          ->requireCapabilities(
  24            array(
  25              PhabricatorPolicyCapability::CAN_VIEW,
  26              PhabricatorPolicyCapability::CAN_EDIT,
  27            ))
  28          ->withKeys(array($this->key))
  29          ->executeOne();
  30        if (!$var) {
  31          return new Aphront404Response();
  32        }
  33        $view_uri = $this->getApplicationURI('/view/'.$this->key.'/');
  34      }
  35  
  36      $e_key = ($is_new ? true : null);
  37      $e_value = true;
  38      $errors = array();
  39  
  40      $key = $var->getVariableKey();
  41  
  42      $display_value = null;
  43      $value = $var->getVariableValue();
  44  
  45      if ($request->isFormPost()) {
  46        if ($is_new) {
  47          $key = $request->getStr('key');
  48          if (!strlen($key)) {
  49            $errors[] = pht('Variable key is required.');
  50            $e_key = pht('Required');
  51          } else if (!preg_match('/^[a-z0-9.-]+\z/', $key)) {
  52            $errors[] = pht(
  53              'Variable key "%s" must contain only lowercase letters, digits, '.
  54              'period, and hyphen.',
  55              $key);
  56            $e_key = pht('Invalid');
  57          }
  58        }
  59  
  60        $raw_value = $request->getStr('value');
  61        $value = json_decode($raw_value, true);
  62        if ($value === null && strtolower($raw_value) !== 'null') {
  63          $e_value = pht('Invalid');
  64          $errors[] = pht('Variable value must be valid JSON.');
  65          $display_value = $raw_value;
  66        }
  67  
  68        if (!$errors) {
  69          $editor = id(new PhluxVariableEditor())
  70            ->setActor($user)
  71            ->setContinueOnNoEffect(true)
  72            ->setContentSourceFromRequest($request);
  73  
  74          $xactions = array();
  75          $xactions[] = id(new PhluxTransaction())
  76            ->setTransactionType(PhluxTransaction::TYPE_EDIT_KEY)
  77            ->setNewValue($key);
  78  
  79          $xactions[] = id(new PhluxTransaction())
  80            ->setTransactionType(PhluxTransaction::TYPE_EDIT_VALUE)
  81            ->setNewValue($value);
  82  
  83          $xactions[] = id(new PhluxTransaction())
  84            ->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
  85            ->setNewValue($request->getStr('viewPolicy'));
  86  
  87          $xactions[] = id(new PhluxTransaction())
  88            ->setTransactionType(PhabricatorTransactions::TYPE_EDIT_POLICY)
  89            ->setNewValue($request->getStr('editPolicy'));
  90  
  91          try {
  92            $editor->applyTransactions($var, $xactions);
  93            $view_uri = $this->getApplicationURI('/view/'.$key.'/');
  94            return id(new AphrontRedirectResponse())->setURI($view_uri);
  95          } catch (AphrontDuplicateKeyQueryException $ex) {
  96            $e_key = pht('Not Unique');
  97            $errors[] = pht('Variable key must be unique.');
  98          }
  99        }
 100      }
 101  
 102      if ($display_value === null) {
 103        if (is_array($value) &&
 104            (array_keys($value) !== array_keys(array_values($value)))) {
 105          $json = new PhutilJSON();
 106          $display_value = $json->encodeFormatted($value);
 107        } else {
 108          $display_value = json_encode($value);
 109        }
 110      }
 111  
 112      $policies = id(new PhabricatorPolicyQuery())
 113        ->setViewer($user)
 114        ->setObject($var)
 115        ->execute();
 116  
 117      $form = id(new AphrontFormView())
 118        ->setUser($user)
 119        ->appendChild(
 120          id(new AphrontFormTextControl())
 121            ->setValue($var->getVariableKey())
 122            ->setLabel(pht('Key'))
 123            ->setName('key')
 124            ->setError($e_key)
 125            ->setCaption(pht('Lowercase letters, digits, dot and hyphen only.'))
 126            ->setDisabled(!$is_new))
 127        ->appendChild(
 128          id(new AphrontFormTextAreaControl())
 129            ->setValue($display_value)
 130            ->setLabel(pht('Value'))
 131            ->setName('value')
 132            ->setCaption(pht('Enter value as JSON.'))
 133            ->setError($e_value))
 134        ->appendChild(
 135          id(new AphrontFormPolicyControl())
 136            ->setName('viewPolicy')
 137            ->setPolicyObject($var)
 138            ->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
 139            ->setPolicies($policies))
 140        ->appendChild(
 141          id(new AphrontFormPolicyControl())
 142            ->setName('editPolicy')
 143            ->setPolicyObject($var)
 144            ->setCapability(PhabricatorPolicyCapability::CAN_EDIT)
 145            ->setPolicies($policies));
 146  
 147      if ($is_new) {
 148        $form->appendChild(
 149          id(new AphrontFormSubmitControl())
 150            ->setValue(pht('Create Variable')));
 151      } else {
 152        $form->appendChild(
 153          id(new AphrontFormSubmitControl())
 154            ->setValue(pht('Update Variable'))
 155            ->addCancelButton($view_uri));
 156      }
 157  
 158      $crumbs = $this->buildApplicationCrumbs();
 159  
 160      if ($is_new) {
 161        $title = pht('Create Variable');
 162        $crumbs->addTextCrumb($title, $request->getRequestURI());
 163      } else {
 164        $title = pht('Edit %s', $this->key);
 165        $crumbs->addTextCrumb($title, $request->getRequestURI());
 166      }
 167  
 168      $form_box = id(new PHUIObjectBoxView())
 169        ->setHeaderText($title)
 170        ->setFormErrors($errors)
 171        ->setForm($form);
 172  
 173      return $this->buildApplicationPage(
 174        array(
 175          $crumbs,
 176          $form_box,
 177        ),
 178        array(
 179          'title' => $title,
 180        ));
 181    }
 182  
 183  }


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