Remove all "FileHasObject" edge reads and writes
[phabricator.git] / src / applications / badges / xaction / PhabricatorBadgesBadgeAwardTransaction.php
blob01fad6e214e3c94892414d67e9e91f14d105620b
1 <?php
3 final class PhabricatorBadgesBadgeAwardTransaction
4 extends PhabricatorBadgesBadgeTransactionType {
6 const TRANSACTIONTYPE = 'badge.award';
8 public function generateOldValue($object) {
9 return null;
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();
20 return;
23 public function getTitle() {
24 $new = $this->getNewValue();
25 if (!is_array($new)) {
26 $new = array();
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);
36 public function getTitleForFeed() {
37 $new = $this->getNewValue();
38 if (!is_array($new)) {
39 $new = array();
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);
50 public function getIcon() {
51 return 'fa-user-plus';
54 public function validateTransactions($object, array $xactions) {
55 $errors = array();
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;
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;
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()));
94 return $errors;