Generate file attachment transactions for explicit Remarkup attachments on common...
[phabricator.git] / src / applications / legalpad / herald / LegalpadRequireSignatureHeraldAction.php
blobb477c2ce35bc31555c9d8ca871a5096213442e05
1 <?php
3 final class LegalpadRequireSignatureHeraldAction
4 extends HeraldAction {
6 const DO_SIGNED = 'do.signed';
7 const DO_REQUIRED = 'do.required';
9 const ACTIONCONST = 'legalpad.require';
11 public function getActionGroupKey() {
12 return HeraldSupportActionGroup::ACTIONGROUPKEY;
15 public function supportsObject($object) {
16 // TODO: This could probably be more general. Note that we call
17 // getAuthorPHID() on the object explicitly below, and this also needs to
18 // be generalized.
19 return ($object instanceof DifferentialRevision);
22 protected function applyRequire(array $phids) {
23 $adapter = $this->getAdapter();
25 $edgetype_legal = LegalpadObjectNeedsSignatureEdgeType::EDGECONST;
26 $current = $adapter->loadEdgePHIDs($edgetype_legal);
28 $allowed_types = array(
29 PhabricatorLegalpadDocumentPHIDType::TYPECONST,
32 $targets = $this->loadStandardTargets($phids, $allowed_types, $current);
33 if (!$targets) {
34 return;
37 $phids = array_fuse(array_keys($targets));
39 $object = $adapter->getObject();
40 $author_phid = $object->getAuthorPHID();
42 $signatures = id(new LegalpadDocumentSignatureQuery())
43 ->setViewer(PhabricatorUser::getOmnipotentUser())
44 ->withDocumentPHIDs($phids)
45 ->withSignerPHIDs(array($author_phid))
46 ->execute();
47 $signatures = mpull($signatures, null, 'getDocumentPHID');
49 $signed = array();
50 foreach ($phids as $phid) {
51 if (isset($signatures[$phid])) {
52 $signed[] = $phid;
53 unset($phids[$phid]);
57 if ($signed) {
58 $this->logEffect(self::DO_SIGNED, $phids);
61 if (!$phids) {
62 return;
65 $xaction = $adapter->newTransaction()
66 ->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
67 ->setMetadataValue('edge:type', $edgetype_legal)
68 ->setNewValue(
69 array(
70 '+' => $phids,
71 ));
73 $adapter->queueTransaction($xaction);
75 $this->logEffect(self::DO_REQUIRED, $phids);
78 protected function getActionEffectMap() {
79 return array(
80 self::DO_SIGNED => array(
81 'icon' => 'fa-terminal',
82 'color' => 'green',
83 'name' => pht('Already Signed'),
85 self::DO_REQUIRED => array(
86 'icon' => 'fa-terminal',
87 'color' => 'green',
88 'name' => pht('Required Signature'),
93 protected function renderActionEffectDescription($type, $data) {
94 switch ($type) {
95 case self::DO_SIGNED:
96 return pht(
97 '%s document(s) are already signed: %s.',
98 phutil_count($data),
99 $this->renderHandleList($data));
100 case self::DO_REQUIRED:
101 return pht(
102 'Required %s signature(s): %s.',
103 phutil_count($data),
104 $this->renderHandleList($data));
108 public function getHeraldActionName() {
109 return pht('Require signatures');
112 public function supportsRuleType($rule_type) {
113 return ($rule_type != HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);
116 public function applyEffect($object, HeraldEffect $effect) {
117 return $this->applyRequire($effect->getTarget());
120 public function getHeraldActionStandardType() {
121 return self::STANDARD_PHID_LIST;
124 protected function getDatasource() {
125 return new LegalpadDocumentDatasource();
128 public function renderActionDescription($value) {
129 return pht(
130 'Require document signatures: %s.',
131 $this->renderHandleList($value));
134 public function isActionAvailable() {
135 return id(new PhabricatorLegalpadApplication())->isInstalled();