Path: blob/master/src/applications/badges/storage/PhabricatorBadgesBadge.php
12256 views
<?php12final class PhabricatorBadgesBadge extends PhabricatorBadgesDAO3implements4PhabricatorPolicyInterface,5PhabricatorApplicationTransactionInterface,6PhabricatorSubscribableInterface,7PhabricatorFlaggableInterface,8PhabricatorDestructibleInterface,9PhabricatorConduitResultInterface,10PhabricatorNgramsInterface {1112protected $name;13protected $flavor;14protected $description;15protected $icon;16protected $quality;17protected $mailKey;18protected $editPolicy;19protected $status;20protected $creatorPHID;2122const STATUS_ACTIVE = 'open';23const STATUS_ARCHIVED = 'closed';2425const DEFAULT_ICON = 'fa-star';2627public static function getStatusNameMap() {28return array(29self::STATUS_ACTIVE => pht('Active'),30self::STATUS_ARCHIVED => pht('Archived'),31);32}3334public static function initializeNewBadge(PhabricatorUser $actor) {35$app = id(new PhabricatorApplicationQuery())36->setViewer($actor)37->withClasses(array('PhabricatorBadgesApplication'))38->executeOne();3940$view_policy = PhabricatorPolicies::getMostOpenPolicy();4142$edit_policy =43$app->getPolicy(PhabricatorBadgesDefaultEditCapability::CAPABILITY);4445return id(new PhabricatorBadgesBadge())46->setIcon(self::DEFAULT_ICON)47->setQuality(PhabricatorBadgesQuality::DEFAULT_QUALITY)48->setCreatorPHID($actor->getPHID())49->setEditPolicy($edit_policy)50->setFlavor('')51->setDescription('')52->setStatus(self::STATUS_ACTIVE);53}5455protected function getConfiguration() {56return array(57self::CONFIG_AUX_PHID => true,58self::CONFIG_COLUMN_SCHEMA => array(59'name' => 'sort255',60'flavor' => 'text255',61'description' => 'text',62'icon' => 'text255',63'quality' => 'uint32',64'status' => 'text32',65'mailKey' => 'bytes20',66),67self::CONFIG_KEY_SCHEMA => array(68'key_creator' => array(69'columns' => array('creatorPHID', 'dateModified'),70),71),72) + parent::getConfiguration();73}7475public function generatePHID() {76return77PhabricatorPHID::generateNewPHID(PhabricatorBadgesPHIDType::TYPECONST);78}7980public function isArchived() {81return ($this->getStatus() == self::STATUS_ARCHIVED);82}8384public function getViewURI() {85return '/badges/view/'.$this->getID().'/';86}8788public function save() {89if (!$this->getMailKey()) {90$this->setMailKey(Filesystem::readRandomCharacters(20));91}92return parent::save();93}949596/* -( PhabricatorPolicyInterface )----------------------------------------- */979899public function getCapabilities() {100return array(101PhabricatorPolicyCapability::CAN_VIEW,102PhabricatorPolicyCapability::CAN_EDIT,103);104}105106public function getPolicy($capability) {107switch ($capability) {108case PhabricatorPolicyCapability::CAN_VIEW:109return PhabricatorPolicies::getMostOpenPolicy();110case PhabricatorPolicyCapability::CAN_EDIT:111return $this->getEditPolicy();112}113}114115public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {116return false;117}118119120/* -( PhabricatorApplicationTransactionInterface )------------------------- */121122123public function getApplicationTransactionEditor() {124return new PhabricatorBadgesEditor();125}126127public function getApplicationTransactionTemplate() {128return new PhabricatorBadgesTransaction();129}130131132/* -( PhabricatorSubscribableInterface )----------------------------------- */133134135public function isAutomaticallySubscribed($phid) {136return false;137}138139140141/* -( PhabricatorDestructibleInterface )----------------------------------- */142143144public function destroyObjectPermanently(145PhabricatorDestructionEngine $engine) {146147$awards = id(new PhabricatorBadgesAwardQuery())148->setViewer($engine->getViewer())149->withBadgePHIDs(array($this->getPHID()))150->execute();151152foreach ($awards as $award) {153$engine->destroyObject($award);154}155156$this->openTransaction();157$this->delete();158$this->saveTransaction();159}160161/* -( PhabricatorConduitResultInterface )---------------------------------- */162163164public function getFieldSpecificationsForConduit() {165return array(166id(new PhabricatorConduitSearchFieldSpecification())167->setKey('name')168->setType('string')169->setDescription(pht('The name of the badge.')),170id(new PhabricatorConduitSearchFieldSpecification())171->setKey('creatorPHID')172->setType('phid')173->setDescription(pht('User PHID of the creator.')),174id(new PhabricatorConduitSearchFieldSpecification())175->setKey('status')176->setType('string')177->setDescription(pht('Active or archived status of the badge.')),178);179}180181public function getFieldValuesForConduit() {182return array(183'name' => $this->getName(),184'creatorPHID' => $this->getCreatorPHID(),185'status' => $this->getStatus(),186);187}188189public function getConduitSearchAttachments() {190return array();191}192193/* -( PhabricatorNgramInterface )------------------------------------------ */194195196public function newNgrams() {197return array(198id(new PhabricatorBadgesBadgeNameNgrams())199->setValue($this->getName()),200);201}202203}204205206