Page MenuHomePhabricator
Paste P1896

CountTransactionsHeraldAction.php
ActivePublic

Authored by epriestley on Nov 26 2015, 2:52 PM.
Tags
None
Referenced Files
F1004998: CountTransactionsHeraldAction.php
Nov 27 2015, 9:03 AM
F1004171: CountTransactionsHeraldAction.php
Nov 26 2015, 2:54 PM
F1004165: CountTransactionsHeraldAction.php
Nov 26 2015, 2:52 PM
Subscribers
<?php
/**
* Example of how to use a HeraldAction instead of an event listener.
*/
final class CountTransactionsHeraldAction
extends HeraldAction {
const ACTIONCONST = 'custom.transactions.count';
const DO_COUNT = 'do.count';
public function getHeraldActionName() {
return pht('Count task transactions');
}
public function getActionGroupKey() {
return HeraldUtilityActionGroup::ACTIONGROUPKEY;
}
public function supportsObject($object) {
// You can modify this method to let your rule run on other types of
// objects that support Herald, like Differential revisions.
return ($object instanceof ManiphestTask);
}
public function supportsRuleType($rule_type) {
switch ($rule_type) {
case HeraldRuleTypeConfig::RULE_TYPE_GLOBAL:
case HeraldRuleTypeConfig::RULE_TYPE_OBJECT:
case HeraldRuleTypeConfig::RULE_TYPE_PERSONAL:
return true;
}
}
public function applyEffect($object, HeraldEffect $effect) {
$adapter = $this->getAdapter();
// NOTE: $object is the object being edited. If you didn't change the
// implementation of `supportsObject()`, it's a ManiphestTask.
// Transactions which were just applied.
$applied_xactions = $adapter->getAppliedTransactions();
$comment_text = pht(
'This edit applied %s transactions.',
phutil_count($applied_xactions));
$comment = $adapter->newTransaction()
->getApplicationTransactionCommentObject()
->setContent($comment_text);
$xaction = $adapter->newTransaction()
->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
->attachComment($comment);
// Use queueTransaction() to queue up transactions to be applied.
$adapter->queueTransaction($xaction);
// This makes the effect observable in the Herald transcript.
$this->logEffect(self::DO_COUNT);
}
public function getHeraldActionStandardType() {
// You can change this to give your action an editable value in the UI,
// like how "Add Subscribers: ..." allows you to choose subscribers to add.
// This might let you make a more modular/flexible action.
return self::STANDARD_NONE;
}
public function renderActionDescription($value) {
return pht('Count task transactions');
}
protected function getActionEffectMap() {
return array(
self::DO_COUNT => array(
'icon' => 'fa-sort-numeric-asc',
'color' => 'violet',
'name' => pht('Counted transactions.'),
),
);
}
protected function renderActionEffectDescription($type, $data) {
switch ($type) {
case self::DO_COUNT:
return pht('Added comment.');
}
}
}