[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/audit/storage/ -> PhabricatorAuditTransaction.php (source)

   1  <?php
   2  
   3  final class PhabricatorAuditTransaction
   4    extends PhabricatorApplicationTransaction {
   5  
   6    const TYPE_COMMIT = 'audit:commit';
   7  
   8    const MAILTAG_ACTION_CONCERN = 'audit-action-concern';
   9    const MAILTAG_ACTION_ACCEPT  = 'audit-action-accept';
  10    const MAILTAG_ACTION_RESIGN  = 'audit-action-resign';
  11    const MAILTAG_ACTION_CLOSE   = 'audit-action-close';
  12    const MAILTAG_ADD_AUDITORS   = 'audit-add-auditors';
  13    const MAILTAG_ADD_CCS        = 'audit-add-ccs';
  14    const MAILTAG_COMMENT        = 'audit-comment';
  15    const MAILTAG_COMMIT         = 'audit-commit';
  16    const MAILTAG_PROJECTS       = 'audit-projects';
  17    const MAILTAG_OTHER          = 'audit-other';
  18  
  19    public function getApplicationName() {
  20      return 'audit';
  21    }
  22  
  23    public function getApplicationTransactionType() {
  24      return PhabricatorRepositoryCommitPHIDType::TYPECONST;
  25    }
  26  
  27    public function getApplicationTransactionCommentObject() {
  28      return new PhabricatorAuditTransactionComment();
  29    }
  30  
  31    public function getRemarkupBlocks() {
  32      $blocks = parent::getRemarkupBlocks();
  33  
  34      switch ($this->getTransactionType()) {
  35      case self::TYPE_COMMIT:
  36        $data = $this->getNewValue();
  37        $blocks[] = $data['description'];
  38        break;
  39      }
  40  
  41      return $blocks;
  42    }
  43  
  44    public function getRequiredHandlePHIDs() {
  45      $phids = parent::getRequiredHandlePHIDs();
  46  
  47      $type = $this->getTransactionType();
  48  
  49      switch ($type) {
  50        case self::TYPE_COMMIT:
  51          $phids[] = $this->getObjectPHID();
  52          $data = $this->getNewValue();
  53          if ($data['authorPHID']) {
  54            $phids[] = $data['authorPHID'];
  55          }
  56          if ($data['committerPHID']) {
  57            $phids[] = $data['committerPHID'];
  58          }
  59          break;
  60        case PhabricatorAuditActionConstants::ADD_CCS:
  61        case PhabricatorAuditActionConstants::ADD_AUDITORS:
  62          $old = $this->getOldValue();
  63          $new = $this->getNewValue();
  64  
  65          if (!is_array($old)) {
  66            $old = array();
  67          }
  68          if (!is_array($new)) {
  69            $new = array();
  70          }
  71  
  72          foreach (array_keys($old + $new) as $phid) {
  73            $phids[] = $phid;
  74          }
  75          break;
  76      }
  77  
  78      return $phids;
  79    }
  80  
  81    public function getActionName() {
  82  
  83      switch ($this->getTransactionType()) {
  84        case PhabricatorAuditActionConstants::ACTION:
  85          switch ($this->getNewValue()) {
  86            case PhabricatorAuditActionConstants::CONCERN:
  87              return pht('Raised Concern');
  88            case PhabricatorAuditActionConstants::ACCEPT:
  89              return pht('Accepted');
  90            case PhabricatorAuditActionConstants::RESIGN:
  91              return pht('Resigned');
  92            case PhabricatorAuditActionConstants::CLOSE:
  93              return pht('Closed');
  94          }
  95          break;
  96        case PhabricatorAuditActionConstants::ADD_AUDITORS:
  97          return pht('Added Auditors');
  98        case self::TYPE_COMMIT:
  99          return pht('Committed');
 100      }
 101  
 102      return parent::getActionName();
 103    }
 104  
 105    public function getColor() {
 106  
 107      $type = $this->getTransactionType();
 108  
 109      switch ($type) {
 110        case PhabricatorAuditActionConstants::ACTION:
 111          switch ($this->getNewValue()) {
 112            case PhabricatorAuditActionConstants::CONCERN:
 113              return 'red';
 114            case PhabricatorAuditActionConstants::ACCEPT:
 115              return 'green';
 116          }
 117      }
 118  
 119      return parent::getColor();
 120    }
 121  
 122    public function getTitle() {
 123      $old = $this->getOldValue();
 124      $new = $this->getNewValue();
 125  
 126      $author_handle = $this->renderHandleLink($this->getAuthorPHID());
 127  
 128      $type = $this->getTransactionType();
 129  
 130      switch ($type) {
 131        case PhabricatorAuditActionConstants::ADD_CCS:
 132        case PhabricatorAuditActionConstants::ADD_AUDITORS:
 133          if (!is_array($old)) {
 134            $old = array();
 135          }
 136          if (!is_array($new)) {
 137            $new = array();
 138          }
 139          $add = array_keys(array_diff_key($new, $old));
 140          $rem = array_keys(array_diff_key($old, $new));
 141          break;
 142      }
 143  
 144      switch ($type) {
 145        case self::TYPE_COMMIT:
 146          $author = null;
 147          if ($new['authorPHID']) {
 148            $author = $this->renderHandleLink($new['authorPHID']);
 149          } else {
 150            $author = $new['authorName'];
 151          }
 152  
 153          $committer = null;
 154          if ($new['committerPHID']) {
 155            $committer = $this->renderHandleLink($new['committerPHID']);
 156          } else if ($new['committerName']) {
 157            $committer = $new['committerName'];
 158          }
 159  
 160          $commit = $this->renderHandleLink($this->getObjectPHID());
 161  
 162          if (!$committer) {
 163            $committer = $author;
 164            $author = null;
 165          }
 166  
 167          if ($author) {
 168            $title = pht(
 169              '%s committed %s (authored by %s).',
 170              $committer,
 171              $commit,
 172              $author);
 173          } else {
 174            $title = pht(
 175              '%s committed %s.',
 176              $committer,
 177              $commit);
 178          }
 179          return $title;
 180  
 181        case PhabricatorAuditActionConstants::INLINE:
 182          return pht(
 183            '%s added inline comments.',
 184            $author_handle);
 185  
 186        case PhabricatorAuditActionConstants::ADD_CCS:
 187          if ($add && $rem) {
 188            return pht(
 189              '%s edited subscribers; added: %s, removed: %s.',
 190              $author_handle,
 191              $this->renderHandleList($add),
 192              $this->renderHandleList($rem));
 193          } else if ($add) {
 194            return pht(
 195              '%s added subscribers: %s.',
 196              $author_handle,
 197              $this->renderHandleList($add));
 198          } else if ($rem) {
 199            return pht(
 200              '%s removed subscribers: %s.',
 201              $author_handle,
 202              $this->renderHandleList($rem));
 203          } else {
 204            return pht(
 205              '%s added subscribers...',
 206              $author_handle);
 207          }
 208  
 209        case PhabricatorAuditActionConstants::ADD_AUDITORS:
 210          if ($add && $rem) {
 211            return pht(
 212              '%s edited auditors; added: %s, removed: %s.',
 213              $author_handle,
 214              $this->renderHandleList($add),
 215              $this->renderHandleList($rem));
 216          } else if ($add) {
 217            return pht(
 218              '%s added auditors: %s.',
 219              $author_handle,
 220              $this->renderHandleList($add));
 221          } else if ($rem) {
 222            return pht(
 223              '%s removed auditors: %s.',
 224              $author_handle,
 225              $this->renderHandleList($rem));
 226          } else {
 227            return pht(
 228              '%s added auditors...',
 229              $author_handle);
 230          }
 231  
 232        case PhabricatorAuditActionConstants::ACTION:
 233          switch ($new) {
 234            case PhabricatorAuditActionConstants::ACCEPT:
 235              return pht(
 236                '%s accepted this commit.',
 237                $author_handle);
 238            case PhabricatorAuditActionConstants::CONCERN:
 239              return pht(
 240                '%s raised a concern with this commit.',
 241                $author_handle);
 242            case PhabricatorAuditActionConstants::RESIGN:
 243              return pht(
 244                '%s resigned from this audit.',
 245                $author_handle);
 246            case PhabricatorAuditActionConstants::CLOSE:
 247              return pht(
 248                '%s closed this audit.',
 249                $author_handle);
 250          }
 251  
 252      }
 253  
 254      return parent::getTitle();
 255    }
 256  
 257    public function getTitleForFeed(PhabricatorFeedStory $story) {
 258      $old = $this->getOldValue();
 259      $new = $this->getNewValue();
 260  
 261      $author_handle = $this->renderHandleLink($this->getAuthorPHID());
 262      $object_handle = $this->renderHandleLink($this->getObjectPHID());
 263  
 264      $type = $this->getTransactionType();
 265  
 266      switch ($type) {
 267        case PhabricatorAuditActionConstants::ADD_CCS:
 268        case PhabricatorAuditActionConstants::ADD_AUDITORS:
 269          if (!is_array($old)) {
 270            $old = array();
 271          }
 272          if (!is_array($new)) {
 273            $new = array();
 274          }
 275          $add = array_keys(array_diff_key($new, $old));
 276          $rem = array_keys(array_diff_key($old, $new));
 277          break;
 278      }
 279  
 280      switch ($type) {
 281        case self::TYPE_COMMIT:
 282          $author = null;
 283          if ($new['authorPHID']) {
 284            $author = $this->renderHandleLink($new['authorPHID']);
 285          } else {
 286            $author = $new['authorName'];
 287          }
 288  
 289          $committer = null;
 290          if ($new['committerPHID']) {
 291            $committer = $this->renderHandleLink($new['committerPHID']);
 292          } else if ($new['committerName']) {
 293            $committer = $new['committerName'];
 294          }
 295  
 296          if (!$committer) {
 297            $committer = $author;
 298            $author = null;
 299          }
 300  
 301          if ($author) {
 302            $title = pht(
 303              '%s committed %s (authored by %s).',
 304              $committer,
 305              $object_handle,
 306              $author);
 307          } else {
 308            $title = pht(
 309              '%s committed %s.',
 310              $committer,
 311              $object_handle);
 312          }
 313          return $title;
 314  
 315        case PhabricatorAuditActionConstants::INLINE:
 316          return pht(
 317            '%s added inline comments to %s.',
 318            $author_handle,
 319            $object_handle);
 320  
 321        case PhabricatorAuditActionConstants::ADD_AUDITORS:
 322          if ($add && $rem) {
 323            return pht(
 324              '%s edited auditors for %s; added: %s, removed: %s.',
 325              $author_handle,
 326              $object_handle,
 327              $this->renderHandleList($add),
 328              $this->renderHandleList($rem));
 329          } else if ($add) {
 330            return pht(
 331              '%s added auditors to %s: %s.',
 332              $author_handle,
 333              $object_handle,
 334              $this->renderHandleList($add));
 335          } else if ($rem) {
 336            return pht(
 337              '%s removed auditors from %s: %s.',
 338              $author_handle,
 339              $object_handle,
 340              $this->renderHandleList($rem));
 341          } else {
 342            return pht(
 343              '%s added auditors to %s...',
 344              $author_handle,
 345              $object_handle);
 346          }
 347  
 348        case PhabricatorAuditActionConstants::ACTION:
 349          switch ($new) {
 350            case PhabricatorAuditActionConstants::ACCEPT:
 351              return pht(
 352                '%s accepted %s.',
 353                $author_handle,
 354                $object_handle);
 355            case PhabricatorAuditActionConstants::CONCERN:
 356              return pht(
 357                '%s raised a concern with %s.',
 358                $author_handle,
 359                $object_handle);
 360            case PhabricatorAuditActionConstants::RESIGN:
 361              return pht(
 362                '%s resigned from auditing %s.',
 363                $author_handle,
 364                $object_handle);
 365            case PhabricatorAuditActionConstants::CLOSE:
 366              return pht(
 367                '%s closed the audit of %s.',
 368                $author_handle,
 369                $object_handle);
 370          }
 371  
 372      }
 373  
 374      return parent::getTitleForFeed($story);
 375    }
 376  
 377    public function getBodyForFeed(PhabricatorFeedStory $story) {
 378      switch ($this->getTransactionType()) {
 379        case self::TYPE_COMMIT:
 380          $data = $this->getNewValue();
 381          return $story->renderSummary($data['summary']);
 382      }
 383      return parent::getBodyForFeed($story);
 384    }
 385  
 386  
 387    // TODO: These two mail methods can likely be abstracted by introducing a
 388    // formal concept of "inline comment" transactions.
 389  
 390    public function shouldHideForMail(array $xactions) {
 391      $type_inline = PhabricatorAuditActionConstants::INLINE;
 392      switch ($this->getTransactionType()) {
 393        case $type_inline:
 394          foreach ($xactions as $xaction) {
 395            if ($xaction->getTransactionType() != $type_inline) {
 396              return true;
 397            }
 398          }
 399          return ($this !== head($xactions));
 400      }
 401  
 402      return parent::shouldHideForMail($xactions);
 403    }
 404  
 405    public function getBodyForMail() {
 406      switch ($this->getTransactionType()) {
 407        case PhabricatorAuditActionConstants::INLINE:
 408          return null;
 409        case self::TYPE_COMMIT:
 410          $data = $this->getNewValue();
 411          return $data['description'];
 412      }
 413  
 414      return parent::getBodyForMail();
 415    }
 416  
 417    public function getMailTags() {
 418      $tags = array();
 419      switch ($this->getTransactionType()) {
 420        case PhabricatorAuditActionConstants::ACTION:
 421          switch ($this->getNewValue()) {
 422            case PhabricatorAuditActionConstants::CONCERN:
 423              $tags[] = self::MAILTAG_ACTION_CONCERN;
 424              break;
 425            case PhabricatorAuditActionConstants::ACCEPT:
 426              $tags[] = self::MAILTAG_ACTION_ACCEPT;
 427              break;
 428            case PhabricatorAuditActionConstants::RESIGN:
 429              $tags[] = self::MAILTAG_ACTION_RESIGN;
 430              break;
 431            case PhabricatorAuditActionConstants::CLOSE:
 432              $tags[] = self::MAILTAG_ACTION_CLOSE;
 433              break;
 434          }
 435          break;
 436        case PhabricatorAuditActionConstants::ADD_AUDITORS:
 437          $tags[] = self::MAILTAG_ADD_AUDITORS;
 438          break;
 439        case PhabricatorAuditActionConstants::ADD_CCS:
 440          $tags[] = self::MAILTAG_ADD_CCS;
 441          break;
 442        case PhabricatorAuditActionConstants::INLINE:
 443        case PhabricatorTransactions::TYPE_COMMENT:
 444          $tags[] = self::MAILTAG_COMMENT;
 445          break;
 446        case self::TYPE_COMMIT:
 447          $tags[] = self::MAILTAG_COMMIT;
 448          break;
 449        case PhabricatorTransactions::TYPE_EDGE:
 450          switch ($this->getMetadataValue('edge:type')) {
 451            case PhabricatorProjectObjectHasProjectEdgeType::EDGECONST:
 452              $tags[] = self::MAILTAG_PROJECTS;
 453              break;
 454            case PhabricatorEdgeConfig::TYPE_OBJECT_HAS_SUBSCRIBER:
 455              $tags[] = self::MAILTAG_ADD_CCS;
 456              break;
 457            default:
 458              $tags[] = self::MAILTAG_OTHER;
 459              break;
 460          }
 461          break;
 462        default:
 463          $tags[] = self::MAILTAG_OTHER;
 464          break;
 465      }
 466      return $tags;
 467    }
 468  }


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