Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/badges/xaction/PhabricatorBadgesBadgeAwardTransaction.php
12256 views
1
<?php
2
3
final class PhabricatorBadgesBadgeAwardTransaction
4
extends PhabricatorBadgesBadgeTransactionType {
5
6
const TRANSACTIONTYPE = 'badge.award';
7
8
public function generateOldValue($object) {
9
return null;
10
}
11
12
public function applyExternalEffects($object, $value) {
13
foreach ($value as $phid) {
14
$award = PhabricatorBadgesAward::initializeNewBadgesAward(
15
$this->getActor(),
16
$object,
17
$phid);
18
$award->save();
19
}
20
return;
21
}
22
23
public function getTitle() {
24
$new = $this->getNewValue();
25
if (!is_array($new)) {
26
$new = array();
27
}
28
$handles = $this->renderHandleList($new);
29
return pht(
30
'%s awarded this badge to %s recipient(s): %s.',
31
$this->renderAuthor(),
32
new PhutilNumber(count($new)),
33
$handles);
34
}
35
36
public function getTitleForFeed() {
37
$new = $this->getNewValue();
38
if (!is_array($new)) {
39
$new = array();
40
}
41
$handles = $this->renderHandleList($new);
42
return pht(
43
'%s awarded %s to %s recipient(s): %s.',
44
$this->renderAuthor(),
45
$this->renderObject(),
46
new PhutilNumber(count($new)),
47
$handles);
48
}
49
50
public function getIcon() {
51
return 'fa-user-plus';
52
}
53
54
public function validateTransactions($object, array $xactions) {
55
$errors = array();
56
57
foreach ($xactions as $xaction) {
58
$user_phids = $xaction->getNewValue();
59
if (!$user_phids) {
60
$errors[] = $this->newRequiredError(
61
pht('Recipient is required.'));
62
continue;
63
}
64
65
foreach ($user_phids as $user_phid) {
66
// Check if a valid user
67
$user = id(new PhabricatorPeopleQuery())
68
->setViewer($this->getActor())
69
->withPHIDs(array($user_phid))
70
->executeOne();
71
if (!$user) {
72
$errors[] = $this->newInvalidError(
73
pht(
74
'Recipient PHID "%s" is not a valid user PHID.',
75
$user_phid));
76
continue;
77
}
78
79
// Check if already awarded
80
$award = id(new PhabricatorBadgesAwardQuery())
81
->setViewer($this->getActor())
82
->withRecipientPHIDs(array($user_phid))
83
->withBadgePHIDs(array($object->getPHID()))
84
->executeOne();
85
if ($award) {
86
$errors[] = $this->newInvalidError(
87
pht(
88
'%s has already been awarded this badge.',
89
$user->getUsername()));
90
}
91
}
92
}
93
94
return $errors;
95
}
96
97
}
98
99