[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class PhabricatorStandardCustomFieldDate
   4    extends PhabricatorStandardCustomField {
   5  
   6    public function getFieldType() {
   7      return 'date';
   8    }
   9  
  10    public function buildFieldIndexes() {
  11      $indexes = array();
  12  
  13      $value = $this->getFieldValue();
  14      if (strlen($value)) {
  15        $indexes[] = $this->newNumericIndex((int)$value);
  16      }
  17  
  18      return $indexes;
  19    }
  20  
  21    public function buildOrderIndex() {
  22      return $this->newNumericIndex(0);
  23    }
  24  
  25    public function getValueForStorage() {
  26      $value = $this->getFieldValue();
  27      if (strlen($value)) {
  28        return (int)$value;
  29      } else {
  30        return null;
  31      }
  32    }
  33  
  34    public function setValueFromStorage($value) {
  35      if (strlen($value)) {
  36        $value = (int)$value;
  37      } else {
  38        $value = null;
  39      }
  40      return $this->setFieldValue($value);
  41    }
  42  
  43    public function renderEditControl(array $handles) {
  44      return $this->newDateControl();
  45    }
  46  
  47    public function readValueFromRequest(AphrontRequest $request) {
  48      $control = $this->newDateControl();
  49      $control->setUser($request->getUser());
  50      $value = $control->readValueFromRequest($request);
  51  
  52      $this->setFieldValue($value);
  53    }
  54  
  55    public function renderPropertyViewValue(array $handles) {
  56      $value = $this->getFieldValue();
  57      if (!$value) {
  58        return null;
  59      }
  60  
  61      return phabricator_datetime($value, $this->getViewer());
  62    }
  63  
  64    private function newDateControl() {
  65      $control = id(new AphrontFormDateControl())
  66        ->setLabel($this->getFieldName())
  67        ->setName($this->getFieldKey())
  68        ->setUser($this->getViewer())
  69        ->setCaption($this->getCaption())
  70        ->setAllowNull(!$this->getRequired());
  71  
  72      // If the value is already numeric, treat it as an epoch timestamp and set
  73      // it directly. Otherwise, it's likely a field default, which we let users
  74      // specify as a string. Parse the string into an epoch.
  75  
  76      $value = $this->getFieldValue();
  77      if (!ctype_digit($value)) {
  78        $value = PhabricatorTime::parseLocalTime($value, $this->getViewer());
  79      }
  80  
  81      // If we don't have anything valid, make sure we pass `null`, since the
  82      // control special-cases that.
  83      $control->setValue(nonempty($value, null));
  84  
  85      return $control;
  86    }
  87  
  88    public function readApplicationSearchValueFromRequest(
  89      PhabricatorApplicationSearchEngine $engine,
  90      AphrontRequest $request) {
  91  
  92      $key = $this->getFieldKey();
  93  
  94      return array(
  95        'min' => $request->getStr($key.'.min'),
  96        'max' => $request->getStr($key.'.max'),
  97      );
  98    }
  99  
 100    public function applyApplicationSearchConstraintToQuery(
 101      PhabricatorApplicationSearchEngine $engine,
 102      PhabricatorCursorPagedPolicyAwareQuery $query,
 103      $value) {
 104  
 105      $viewer = $this->getViewer();
 106  
 107      if (!is_array($value)) {
 108        $value = array();
 109      }
 110  
 111      $min_str = idx($value, 'min', '');
 112      if (strlen($min_str)) {
 113        $min = PhabricatorTime::parseLocalTime($min_str, $viewer);
 114      } else {
 115        $min = null;
 116      }
 117  
 118      $max_str = idx($value, 'max', '');
 119      if (strlen($max_str)) {
 120        $max = PhabricatorTime::parseLocalTime($max_str, $viewer);
 121      } else {
 122        $max = null;
 123      }
 124  
 125      if (($min !== null) || ($max !== null)) {
 126        $query->withApplicationSearchRangeConstraint(
 127          $this->newNumericIndex(null),
 128          $min,
 129          $max);
 130      }
 131    }
 132  
 133    public function appendToApplicationSearchForm(
 134      PhabricatorApplicationSearchEngine $engine,
 135      AphrontFormView $form,
 136      $value,
 137      array $handles) {
 138  
 139      if (!is_array($value)) {
 140        $value = array();
 141      }
 142  
 143      $form
 144        ->appendChild(
 145          id(new AphrontFormTextControl())
 146            ->setLabel(pht('%s After', $this->getFieldName()))
 147            ->setName($this->getFieldKey().'.min')
 148            ->setValue(idx($value, 'min', '')))
 149        ->appendChild(
 150          id(new AphrontFormTextControl())
 151            ->setLabel(pht('%s Before', $this->getFieldName()))
 152            ->setName($this->getFieldKey().'.max')
 153            ->setValue(idx($value, 'max', '')));
 154    }
 155  
 156    public function getApplicationTransactionTitle(
 157      PhabricatorApplicationTransaction $xaction) {
 158      $author_phid = $xaction->getAuthorPHID();
 159      $old = $xaction->getOldValue();
 160      $new = $xaction->getNewValue();
 161  
 162      $viewer = $this->getViewer();
 163  
 164      $old_date = null;
 165      if ($old) {
 166        $old_date = phabricator_datetime($old, $viewer);
 167      }
 168  
 169      $new_date = null;
 170      if ($new) {
 171        $new_date = phabricator_datetime($new, $viewer);
 172      }
 173  
 174      if (!$old) {
 175        return pht(
 176          '%s set %s to %s.',
 177          $xaction->renderHandleLink($author_phid),
 178          $this->getFieldName(),
 179          $new_date);
 180      } else if (!$new) {
 181        return pht(
 182          '%s removed %s.',
 183          $xaction->renderHandleLink($author_phid),
 184          $this->getFieldName());
 185      } else {
 186        return pht(
 187          '%s changed %s from %s to %s.',
 188          $xaction->renderHandleLink($author_phid),
 189          $this->getFieldName(),
 190          $old_date,
 191          $new_date);
 192      }
 193    }
 194  
 195  }


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