PhabricatorStandardCustomFieldPHIDs.php 7.33 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
<?php

/**
 * Common code for standard field types which store lists of PHIDs.
 */
abstract class PhabricatorStandardCustomFieldPHIDs
  extends PhabricatorStandardCustomField {

  public function buildFieldIndexes() {
    $indexes = array();

    $value = $this->getFieldValue();
    if (is_array($value)) {
      foreach ($value as $phid) {
        $indexes[] = $this->newStringIndex($phid);
      }
    }

    return $indexes;
  }

  public function readValueFromRequest(AphrontRequest $request) {
    $value = $request->getArr($this->getFieldKey());
    $this->setFieldValue($value);
  }

  public function getValueForStorage() {
    $value = $this->getFieldValue();
    if (!$value) {
      return null;
    }

    return json_encode(array_values($value));
  }

  public function setValueFromStorage($value) {
37 38 39 40 41
    // NOTE: We're accepting either a JSON string (a real storage value) or
    // an array (from HTTP parameter prefilling). This is a little hacky, but
    // should hold until this can get cleaned up more thoroughly.
    // TODO: Clean this up.

42
    $result = array();
43
    if (!is_array($value)) {
44 45 46 47 48
      $value = json_decode($value, true);
      if (is_array($value)) {
        $result = array_values($value);
      }
    }
49

50
    $this->setFieldValue($value);
51

52
    return $this;
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  }

  public function readApplicationSearchValueFromRequest(
    PhabricatorApplicationSearchEngine $engine,
    AphrontRequest $request) {
    return $request->getArr($this->getFieldKey());
  }

  public function applyApplicationSearchConstraintToQuery(
    PhabricatorApplicationSearchEngine $engine,
    PhabricatorCursorPagedPolicyAwareQuery $query,
    $value) {
    if ($value) {
      $query->withApplicationSearchContainsConstraint(
        $this->newStringIndex(null),
        $value);
    }
  }

72
  public function getRequiredHandlePHIDsForPropertyView() {
73 74 75 76 77 78 79 80
    $value = $this->getFieldValue();
    if ($value) {
      return $value;
    }
    return array();
  }

  public function renderPropertyViewValue(array $handles) {
81 82 83 84 85
    $value = $this->getFieldValue();
    if (!$value) {
      return null;
    }

86
    $handles = mpull($handles, 'renderHovercardLink');
87 88 89 90
    $handles = phutil_implode_html(', ', $handles);
    return $handles;
  }

91 92 93 94 95 96 97 98 99 100 101 102
  public function getRequiredHandlePHIDsForEdit() {
    $value = $this->getFieldValue();
    if ($value) {
      return $value;
    } else {
      return array();
    }
  }

  public function getApplicationTransactionRequiredHandlePHIDs(
    PhabricatorApplicationTransaction $xaction) {

103 104
    $old = $this->decodeValue($xaction->getOldValue());
    $new = $this->decodeValue($xaction->getNewValue());
105 106 107 108 109 110 111 112 113 114 115

    $add = array_diff($new, $old);
    $rem = array_diff($old, $new);

    return array_merge($add, $rem);
  }

  public function getApplicationTransactionTitle(
    PhabricatorApplicationTransaction $xaction) {
    $author_phid = $xaction->getAuthorPHID();

116 117
    $old = $this->decodeValue($xaction->getOldValue());
    $new = $this->decodeValue($xaction->getNewValue());
118 119 120 121 122 123 124 125 126

    $add = array_diff($new, $old);
    $rem = array_diff($old, $new);

    if ($add && !$rem) {
      return pht(
        '%s updated %s, added %d: %s.',
        $xaction->renderHandleLink($author_phid),
        $this->getFieldName(),
127
        phutil_count($add),
128 129 130
        $xaction->renderHandleList($add));
    } else if ($rem && !$add) {
      return pht(
131
        '%s updated %s, removed %s: %s.',
132 133
        $xaction->renderHandleLink($author_phid),
        $this->getFieldName(),
134
        phutil_count($rem),
135 136 137
        $xaction->renderHandleList($rem));
    } else {
      return pht(
138
        '%s updated %s, added %s: %s; removed %s: %s.',
139 140
        $xaction->renderHandleLink($author_phid),
        $this->getFieldName(),
141
        phutil_count($add),
142
        $xaction->renderHandleList($add),
143
        phutil_count($rem),
144 145 146 147
        $xaction->renderHandleList($rem));
    }
  }

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  public function getApplicationTransactionTitleForFeed(
    PhabricatorApplicationTransaction $xaction) {
    $author_phid = $xaction->getAuthorPHID();
    $object_phid = $xaction->getObjectPHID();

    $old = $this->decodeValue($xaction->getOldValue());
    $new = $this->decodeValue($xaction->getNewValue());

    $add = array_diff($new, $old);
    $rem = array_diff($old, $new);

    if ($add && !$rem) {
      return pht(
        '%s updated %s for %s, added %d: %s.',
        $xaction->renderHandleLink($author_phid),
        $this->getFieldName(),
        $xaction->renderHandleLink($object_phid),
        phutil_count($add),
        $xaction->renderHandleList($add));
    } else if ($rem && !$add) {
      return pht(
        '%s updated %s for %s, removed %s: %s.',
        $xaction->renderHandleLink($author_phid),
        $this->getFieldName(),
        $xaction->renderHandleLink($object_phid),
        phutil_count($rem),
        $xaction->renderHandleList($rem));
    } else {
      return pht(
        '%s updated %s for %s, added %s: %s; removed %s: %s.',
        $xaction->renderHandleLink($author_phid),
        $this->getFieldName(),
        $xaction->renderHandleLink($object_phid),
        phutil_count($add),
        $xaction->renderHandleList($add),
        phutil_count($rem),
        $xaction->renderHandleList($rem));
    }
  }

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  public function validateApplicationTransactions(
    PhabricatorApplicationTransactionEditor $editor,
    $type,
    array $xactions) {

    $errors = parent::validateApplicationTransactions(
      $editor,
      $type,
      $xactions);

    // If the user is adding PHIDs, make sure the new PHIDs are valid and
    // visible to the actor. It's OK for a user to edit a field which includes
    // some invalid or restricted values, but they can't add new ones.

    foreach ($xactions as $xaction) {
203 204
      $old = $this->decodeValue($xaction->getOldValue());
      $new = $this->decodeValue($xaction->getNewValue());
205 206 207

      $add = array_diff($new, $old);

208 209 210
      $invalid = PhabricatorObjectQuery::loadInvalidPHIDsForViewer(
        $editor->getActor(),
        $add);
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

      if ($invalid) {
        $error = new PhabricatorApplicationTransactionValidationError(
          $type,
          pht('Invalid'),
          pht(
            'Some of the selected PHIDs in field "%s" are invalid or '.
            'restricted: %s.',
            $this->getFieldName(),
            implode(', ', $invalid)),
          $xaction);
        $errors[] = $error;
        $this->setFieldError(pht('Invalid'));
      }
    }

    return $errors;
  }

230 231 232 233 234 235 236 237 238 239 240 241 242 243
  public function shouldAppearInHerald() {
    return true;
  }

  public function getHeraldFieldConditions() {
    return array(
      HeraldAdapter::CONDITION_INCLUDE_ALL,
      HeraldAdapter::CONDITION_INCLUDE_ANY,
      HeraldAdapter::CONDITION_INCLUDE_NONE,
      HeraldAdapter::CONDITION_EXISTS,
      HeraldAdapter::CONDITION_NOT_EXISTS,
    );
  }

244 245 246 247
  public function getHeraldFieldStandardType() {
    return HeraldField::STANDARD_PHID_NULLABLE;
  }

248 249 250 251 252 253 254 255 256 257
  public function getHeraldFieldValue() {
    // If the field has a `null` value, make sure we hand an `array()` to
    // Herald.
    $value = parent::getHeraldFieldValue();
    if ($value) {
      return $value;
    }
    return array();
  }

258
  protected function decodeValue($value) {
259 260 261 262
    if (is_array($value)) {
      return $value;
    }

263 264 265 266 267 268 269 270
    $value = json_decode($value);
    if (!is_array($value)) {
      $value = array();
    }

    return $value;
  }

271 272 273 274
  protected function getHTTPParameterType() {
    return new AphrontPHIDListHTTPParameterType();
  }

275
}