Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/legalpad/herald/LegalpadRequireSignatureHeraldAction.php
13486 views
1
<?php
2
3
final class LegalpadRequireSignatureHeraldAction
4
extends HeraldAction {
5
6
const DO_SIGNED = 'do.signed';
7
const DO_REQUIRED = 'do.required';
8
9
const ACTIONCONST = 'legalpad.require';
10
11
public function getActionGroupKey() {
12
return HeraldSupportActionGroup::ACTIONGROUPKEY;
13
}
14
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);
20
}
21
22
protected function applyRequire(array $phids) {
23
$adapter = $this->getAdapter();
24
25
$edgetype_legal = LegalpadObjectNeedsSignatureEdgeType::EDGECONST;
26
$current = $adapter->loadEdgePHIDs($edgetype_legal);
27
28
$allowed_types = array(
29
PhabricatorLegalpadDocumentPHIDType::TYPECONST,
30
);
31
32
$targets = $this->loadStandardTargets($phids, $allowed_types, $current);
33
if (!$targets) {
34
return;
35
}
36
37
$phids = array_fuse(array_keys($targets));
38
39
$object = $adapter->getObject();
40
$author_phid = $object->getAuthorPHID();
41
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');
48
49
$signed = array();
50
foreach ($phids as $phid) {
51
if (isset($signatures[$phid])) {
52
$signed[] = $phid;
53
unset($phids[$phid]);
54
}
55
}
56
57
if ($signed) {
58
$this->logEffect(self::DO_SIGNED, $phids);
59
}
60
61
if (!$phids) {
62
return;
63
}
64
65
$xaction = $adapter->newTransaction()
66
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
67
->setMetadataValue('edge:type', $edgetype_legal)
68
->setNewValue(
69
array(
70
'+' => $phids,
71
));
72
73
$adapter->queueTransaction($xaction);
74
75
$this->logEffect(self::DO_REQUIRED, $phids);
76
}
77
78
protected function getActionEffectMap() {
79
return array(
80
self::DO_SIGNED => array(
81
'icon' => 'fa-terminal',
82
'color' => 'green',
83
'name' => pht('Already Signed'),
84
),
85
self::DO_REQUIRED => array(
86
'icon' => 'fa-terminal',
87
'color' => 'green',
88
'name' => pht('Required Signature'),
89
),
90
);
91
}
92
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));
105
}
106
}
107
108
public function getHeraldActionName() {
109
return pht('Require signatures');
110
}
111
112
public function supportsRuleType($rule_type) {
113
return ($rule_type != HeraldRuleTypeConfig::RULE_TYPE_PERSONAL);
114
}
115
116
public function applyEffect($object, HeraldEffect $effect) {
117
return $this->applyRequire($effect->getTarget());
118
}
119
120
public function getHeraldActionStandardType() {
121
return self::STANDARD_PHID_LIST;
122
}
123
124
protected function getDatasource() {
125
return new LegalpadDocumentDatasource();
126
}
127
128
public function renderActionDescription($value) {
129
return pht(
130
'Require document signatures: %s.',
131
$this->renderHandleList($value));
132
}
133
134
public function isActionAvailable() {
135
return id(new PhabricatorLegalpadApplication())->isInstalled();
136
}
137
138
}
139
140