[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/customfield/standard/ -> PhabricatorStandardCustomField.php (source)

   1  <?php
   2  
   3  abstract class PhabricatorStandardCustomField
   4    extends PhabricatorCustomField {
   5  
   6    private $rawKey;
   7    private $fieldKey;
   8    private $fieldName;
   9    private $fieldValue;
  10    private $fieldDescription;
  11    private $fieldConfig;
  12    private $applicationField;
  13    private $strings = array();
  14    private $caption;
  15    private $fieldError;
  16    private $required;
  17    private $default;
  18  
  19    abstract public function getFieldType();
  20  
  21    public static function buildStandardFields(
  22      PhabricatorCustomField $template,
  23      array $config) {
  24  
  25      $types = id(new PhutilSymbolLoader())
  26        ->setAncestorClass(__CLASS__)
  27        ->loadObjects();
  28      $types = mpull($types, null, 'getFieldType');
  29  
  30      $fields = array();
  31      foreach ($config as $key => $value) {
  32        $type = idx($value, 'type', 'text');
  33        if (empty($types[$type])) {
  34          // TODO: We should have better typechecking somewhere, and then make
  35          // this more serious.
  36          continue;
  37        }
  38  
  39        $namespace = $template->getStandardCustomFieldNamespace();
  40        $full_key = "std:{$namespace}:{$key}";
  41  
  42        $template = clone $template;
  43        $standard = id(clone $types[$type])
  44          ->setRawStandardFieldKey($key)
  45          ->setFieldKey($full_key)
  46          ->setFieldConfig($value)
  47          ->setApplicationField($template);
  48  
  49        $field = $template->setProxy($standard);
  50        $fields[] = $field;
  51      }
  52  
  53      return $fields;
  54    }
  55  
  56    public function setApplicationField(
  57      PhabricatorStandardCustomFieldInterface $application_field) {
  58      $this->applicationField = $application_field;
  59      return $this;
  60    }
  61  
  62    public function getApplicationField() {
  63      return $this->applicationField;
  64    }
  65  
  66    public function setFieldName($name) {
  67      $this->fieldName = $name;
  68      return $this;
  69    }
  70  
  71    public function getFieldValue() {
  72      return $this->fieldValue;
  73    }
  74  
  75    public function setFieldValue($value) {
  76      $this->fieldValue = $value;
  77      return $this;
  78    }
  79  
  80    public function setCaption($caption) {
  81      $this->caption = $caption;
  82      return $this;
  83    }
  84  
  85    public function getCaption() {
  86      return $this->caption;
  87    }
  88  
  89    public function setFieldDescription($description) {
  90      $this->fieldDescription = $description;
  91      return $this;
  92    }
  93  
  94    public function setFieldConfig(array $config) {
  95      foreach ($config as $key => $value) {
  96        switch ($key) {
  97          case 'name':
  98            $this->setFieldName($value);
  99            break;
 100          case 'description':
 101            $this->setFieldDescription($value);
 102            break;
 103          case 'strings':
 104            $this->setStrings($value);
 105            break;
 106          case 'caption':
 107            $this->setCaption($value);
 108            break;
 109          case 'required':
 110            if ($value) {
 111              $this->setRequired($value);
 112              $this->setFieldError(true);
 113            }
 114            break;
 115          case 'default':
 116            $this->setFieldValue($value);
 117            break;
 118          case 'type':
 119            // We set this earlier on.
 120            break;
 121        }
 122      }
 123      $this->fieldConfig = $config;
 124      return $this;
 125    }
 126  
 127    public function getFieldConfigValue($key, $default = null) {
 128      return idx($this->fieldConfig, $key, $default);
 129    }
 130  
 131    public function setFieldError($field_error) {
 132      $this->fieldError = $field_error;
 133      return $this;
 134    }
 135  
 136    public function getFieldError() {
 137      return $this->fieldError;
 138    }
 139  
 140    public function setRequired($required) {
 141      $this->required = $required;
 142      return $this;
 143    }
 144  
 145    public function getRequired() {
 146      return $this->required;
 147    }
 148  
 149    public function setRawStandardFieldKey($raw_key) {
 150      $this->rawKey = $raw_key;
 151      return $this;
 152    }
 153  
 154    public function getRawStandardFieldKey() {
 155      return $this->rawKey;
 156    }
 157  
 158  
 159  /* -(  PhabricatorCustomField  )--------------------------------------------- */
 160  
 161  
 162    public function setFieldKey($field_key) {
 163      $this->fieldKey = $field_key;
 164      return $this;
 165    }
 166  
 167    public function getFieldKey() {
 168      return $this->fieldKey;
 169    }
 170  
 171    public function getFieldName() {
 172      return coalesce($this->fieldName, parent::getFieldName());
 173    }
 174  
 175    public function getFieldDescription() {
 176      return coalesce($this->fieldDescription, parent::getFieldDescription());
 177    }
 178  
 179    public function setStrings(array $strings) {
 180      $this->strings = $strings;
 181      return;
 182    }
 183  
 184    public function getString($key, $default = null) {
 185      return idx($this->strings, $key, $default);
 186    }
 187  
 188    public function shouldUseStorage() {
 189      return true;
 190    }
 191  
 192    public function getValueForStorage() {
 193      return $this->getFieldValue();
 194    }
 195  
 196    public function setValueFromStorage($value) {
 197      return $this->setFieldValue($value);
 198    }
 199  
 200    public function shouldAppearInApplicationTransactions() {
 201      return true;
 202    }
 203  
 204    public function shouldAppearInEditView() {
 205      return $this->getFieldConfigValue('edit', true);
 206    }
 207  
 208    public function readValueFromRequest(AphrontRequest $request) {
 209      $value = $request->getStr($this->getFieldKey());
 210      if (!strlen($value)) {
 211        $value = null;
 212      }
 213      $this->setFieldValue($value);
 214    }
 215  
 216    public function getInstructionsForEdit() {
 217      return $this->getFieldConfigValue('instructions');
 218    }
 219  
 220    public function getPlaceholder() {
 221      return $this->getFieldConfigValue('placeholder', null);
 222    }
 223  
 224    public function renderEditControl(array $handles) {
 225      return id(new AphrontFormTextControl())
 226        ->setName($this->getFieldKey())
 227        ->setCaption($this->getCaption())
 228        ->setValue($this->getFieldValue())
 229        ->setError($this->getFieldError())
 230        ->setLabel($this->getFieldName())
 231        ->setPlaceholder($this->getPlaceholder());
 232    }
 233  
 234    public function newStorageObject() {
 235      return $this->getApplicationField()->newStorageObject();
 236    }
 237  
 238    public function shouldAppearInPropertyView() {
 239      return $this->getFieldConfigValue('view', true);
 240    }
 241  
 242    public function renderPropertyViewValue(array $handles) {
 243      if (!strlen($this->getFieldValue())) {
 244        return null;
 245      }
 246      return $this->getFieldValue();
 247    }
 248  
 249    public function shouldAppearInApplicationSearch() {
 250      return $this->getFieldConfigValue('search', false);
 251    }
 252  
 253    protected function newStringIndexStorage() {
 254      return $this->getApplicationField()->newStringIndexStorage();
 255    }
 256  
 257    protected function newNumericIndexStorage() {
 258      return $this->getApplicationField()->newNumericIndexStorage();
 259    }
 260  
 261    public function buildFieldIndexes() {
 262      return array();
 263    }
 264  
 265    public function buildOrderIndex() {
 266      return null;
 267    }
 268  
 269    public function readApplicationSearchValueFromRequest(
 270      PhabricatorApplicationSearchEngine $engine,
 271      AphrontRequest $request) {
 272      return;
 273    }
 274  
 275    public function applyApplicationSearchConstraintToQuery(
 276      PhabricatorApplicationSearchEngine $engine,
 277      PhabricatorCursorPagedPolicyAwareQuery $query,
 278      $value) {
 279      return;
 280    }
 281  
 282    public function appendToApplicationSearchForm(
 283      PhabricatorApplicationSearchEngine $engine,
 284      AphrontFormView $form,
 285      $value,
 286      array $handles) {
 287      return;
 288    }
 289  
 290    public function validateApplicationTransactions(
 291      PhabricatorApplicationTransactionEditor $editor,
 292      $type,
 293      array $xactions) {
 294  
 295      $this->setFieldError(null);
 296  
 297      $errors = parent::validateApplicationTransactions(
 298        $editor,
 299        $type,
 300        $xactions);
 301  
 302      if ($this->getRequired()) {
 303        $value = $this->getOldValueForApplicationTransactions();
 304  
 305        $transaction = null;
 306        foreach ($xactions as $xaction) {
 307          $value = $xaction->getNewValue();
 308          if (!$this->isValueEmpty($value)) {
 309            $transaction = $xaction;
 310            break;
 311          }
 312        }
 313        if ($this->isValueEmpty($value)) {
 314          $error = new PhabricatorApplicationTransactionValidationError(
 315            $type,
 316            pht('Required'),
 317            pht('%s is required.', $this->getFieldName()),
 318            $transaction);
 319          $error->setIsMissingFieldError(true);
 320          $errors[] = $error;
 321          $this->setFieldError(pht('Required'));
 322        }
 323      }
 324  
 325      return $errors;
 326    }
 327  
 328    protected function isValueEmpty($value) {
 329      if (is_array($value)) {
 330        return empty($value);
 331      }
 332      return !strlen($value);
 333    }
 334  
 335    public function getApplicationTransactionTitle(
 336      PhabricatorApplicationTransaction $xaction) {
 337      $author_phid = $xaction->getAuthorPHID();
 338      $old = $xaction->getOldValue();
 339      $new = $xaction->getNewValue();
 340  
 341      if (!$old) {
 342        return pht(
 343          '%s set %s to %s.',
 344          $xaction->renderHandleLink($author_phid),
 345          $this->getFieldName(),
 346          $new);
 347      } else if (!$new) {
 348        return pht(
 349          '%s removed %s.',
 350          $xaction->renderHandleLink($author_phid),
 351          $this->getFieldName());
 352      } else {
 353        return pht(
 354          '%s changed %s from %s to %s.',
 355          $xaction->renderHandleLink($author_phid),
 356          $this->getFieldName(),
 357          $old,
 358          $new);
 359      }
 360    }
 361  
 362    public function getApplicationTransactionTitleForFeed(
 363      PhabricatorApplicationTransaction $xaction,
 364      PhabricatorFeedStory $story) {
 365  
 366      $author_phid = $xaction->getAuthorPHID();
 367      $object_phid = $xaction->getObjectPHID();
 368  
 369      $old = $xaction->getOldValue();
 370      $new = $xaction->getNewValue();
 371  
 372      if (!$old) {
 373        return pht(
 374          '%s set %s to %s on %s.',
 375          $xaction->renderHandleLink($author_phid),
 376          $this->getFieldName(),
 377          $new,
 378          $xaction->renderHandleLink($object_phid));
 379      } else if (!$new) {
 380        return pht(
 381          '%s removed %s on %s.',
 382          $xaction->renderHandleLink($author_phid),
 383          $this->getFieldName(),
 384          $xaction->renderHandleLink($object_phid));
 385      } else {
 386        return pht(
 387          '%s changed %s from %s to %s on %s.',
 388          $xaction->renderHandleLink($author_phid),
 389          $this->getFieldName(),
 390          $old,
 391          $new,
 392          $xaction->renderHandleLink($object_phid));
 393      }
 394    }
 395  
 396    public function getHeraldFieldValue() {
 397      return $this->getFieldValue();
 398    }
 399  
 400    public function getFieldControlID($key = null) {
 401      $key = coalesce($key, $this->getRawStandardFieldKey());
 402      return 'std:control:'.$key;
 403    }
 404  
 405    public function shouldAppearInGlobalSearch() {
 406      return $this->getFieldConfigValue('fulltext', false);
 407    }
 408  
 409    public function updateAbstractDocument(
 410      PhabricatorSearchAbstractDocument $document) {
 411  
 412      $field_key = $this->getFieldConfigValue('fulltext');
 413  
 414      // If the caller or configuration didn't specify a valid field key,
 415      // generate one automatically from the field index.
 416      if (!is_string($field_key) || (strlen($field_key) != 4)) {
 417        $field_key = '!'.substr($this->getFieldIndex(), 0, 3);
 418      }
 419  
 420      $field_value = $this->getFieldValue();
 421      if (strlen($field_value)) {
 422        $document->addField($field_key, $field_value);
 423      }
 424    }
 425  
 426  }


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