Path: blob/master/src/applications/badges/xaction/PhabricatorBadgesBadgeAwardTransaction.php
12256 views
<?php12final class PhabricatorBadgesBadgeAwardTransaction3extends PhabricatorBadgesBadgeTransactionType {45const TRANSACTIONTYPE = 'badge.award';67public function generateOldValue($object) {8return null;9}1011public function applyExternalEffects($object, $value) {12foreach ($value as $phid) {13$award = PhabricatorBadgesAward::initializeNewBadgesAward(14$this->getActor(),15$object,16$phid);17$award->save();18}19return;20}2122public function getTitle() {23$new = $this->getNewValue();24if (!is_array($new)) {25$new = array();26}27$handles = $this->renderHandleList($new);28return pht(29'%s awarded this badge to %s recipient(s): %s.',30$this->renderAuthor(),31new PhutilNumber(count($new)),32$handles);33}3435public function getTitleForFeed() {36$new = $this->getNewValue();37if (!is_array($new)) {38$new = array();39}40$handles = $this->renderHandleList($new);41return pht(42'%s awarded %s to %s recipient(s): %s.',43$this->renderAuthor(),44$this->renderObject(),45new PhutilNumber(count($new)),46$handles);47}4849public function getIcon() {50return 'fa-user-plus';51}5253public function validateTransactions($object, array $xactions) {54$errors = array();5556foreach ($xactions as $xaction) {57$user_phids = $xaction->getNewValue();58if (!$user_phids) {59$errors[] = $this->newRequiredError(60pht('Recipient is required.'));61continue;62}6364foreach ($user_phids as $user_phid) {65// Check if a valid user66$user = id(new PhabricatorPeopleQuery())67->setViewer($this->getActor())68->withPHIDs(array($user_phid))69->executeOne();70if (!$user) {71$errors[] = $this->newInvalidError(72pht(73'Recipient PHID "%s" is not a valid user PHID.',74$user_phid));75continue;76}7778// Check if already awarded79$award = id(new PhabricatorBadgesAwardQuery())80->setViewer($this->getActor())81->withRecipientPHIDs(array($user_phid))82->withBadgePHIDs(array($object->getPHID()))83->executeOne();84if ($award) {85$errors[] = $this->newInvalidError(86pht(87'%s has already been awarded this badge.',88$user->getUsername()));89}90}91}9293return $errors;94}9596}979899