3 final class PhabricatorBadgesBadge
extends PhabricatorBadgesDAO
5 PhabricatorPolicyInterface
,
6 PhabricatorApplicationTransactionInterface
,
7 PhabricatorSubscribableInterface
,
8 PhabricatorFlaggableInterface
,
9 PhabricatorDestructibleInterface
,
10 PhabricatorConduitResultInterface
,
11 PhabricatorNgramsInterface
{
15 protected $description;
19 protected $editPolicy;
21 protected $creatorPHID;
23 const STATUS_ACTIVE
= 'open';
24 const STATUS_ARCHIVED
= 'closed';
26 const DEFAULT_ICON
= 'fa-star';
28 public static function getStatusNameMap() {
30 self
::STATUS_ACTIVE
=> pht('Active'),
31 self
::STATUS_ARCHIVED
=> pht('Archived'),
35 public static function initializeNewBadge(PhabricatorUser
$actor) {
36 $app = id(new PhabricatorApplicationQuery())
38 ->withClasses(array('PhabricatorBadgesApplication'))
41 $view_policy = PhabricatorPolicies
::getMostOpenPolicy();
44 $app->getPolicy(PhabricatorBadgesDefaultEditCapability
::CAPABILITY
);
46 return id(new PhabricatorBadgesBadge())
47 ->setIcon(self
::DEFAULT_ICON
)
48 ->setQuality(PhabricatorBadgesQuality
::DEFAULT_QUALITY
)
49 ->setCreatorPHID($actor->getPHID())
50 ->setEditPolicy($edit_policy)
53 ->setStatus(self
::STATUS_ACTIVE
);
56 protected function getConfiguration() {
58 self
::CONFIG_AUX_PHID
=> true,
59 self
::CONFIG_COLUMN_SCHEMA
=> array(
61 'flavor' => 'text255',
62 'description' => 'text',
64 'quality' => 'uint32',
66 'mailKey' => 'bytes20',
68 self
::CONFIG_KEY_SCHEMA
=> array(
69 'key_creator' => array(
70 'columns' => array('creatorPHID', 'dateModified'),
73 ) + parent
::getConfiguration();
76 public function generatePHID() {
78 PhabricatorPHID
::generateNewPHID(PhabricatorBadgesPHIDType
::TYPECONST
);
81 public function isArchived() {
82 return ($this->getStatus() == self
::STATUS_ARCHIVED
);
85 public function getViewURI() {
86 return '/badges/view/'.$this->getID().'/';
89 public function save() {
90 if (!$this->getMailKey()) {
91 $this->setMailKey(Filesystem
::readRandomCharacters(20));
93 return parent
::save();
97 /* -( PhabricatorPolicyInterface )----------------------------------------- */
100 public function getCapabilities() {
102 PhabricatorPolicyCapability
::CAN_VIEW
,
103 PhabricatorPolicyCapability
::CAN_EDIT
,
107 public function getPolicy($capability) {
108 switch ($capability) {
109 case PhabricatorPolicyCapability
::CAN_VIEW
:
110 return PhabricatorPolicies
::getMostOpenPolicy();
111 case PhabricatorPolicyCapability
::CAN_EDIT
:
112 return $this->getEditPolicy();
116 public function hasAutomaticCapability($capability, PhabricatorUser
$viewer) {
121 /* -( PhabricatorApplicationTransactionInterface )------------------------- */
124 public function getApplicationTransactionEditor() {
125 return new PhabricatorBadgesEditor();
128 public function getApplicationTransactionTemplate() {
129 return new PhabricatorBadgesTransaction();
133 /* -( PhabricatorSubscribableInterface )----------------------------------- */
136 public function isAutomaticallySubscribed($phid) {
142 /* -( PhabricatorDestructibleInterface )----------------------------------- */
145 public function destroyObjectPermanently(
146 PhabricatorDestructionEngine
$engine) {
148 $awards = id(new PhabricatorBadgesAwardQuery())
149 ->setViewer($engine->getViewer())
150 ->withBadgePHIDs(array($this->getPHID()))
153 foreach ($awards as $award) {
154 $engine->destroyObject($award);
157 $this->openTransaction();
159 $this->saveTransaction();
162 /* -( PhabricatorConduitResultInterface )---------------------------------- */
165 public function getFieldSpecificationsForConduit() {
167 id(new PhabricatorConduitSearchFieldSpecification())
170 ->setDescription(pht('The name of the badge.')),
171 id(new PhabricatorConduitSearchFieldSpecification())
172 ->setKey('creatorPHID')
174 ->setDescription(pht('User PHID of the creator.')),
175 id(new PhabricatorConduitSearchFieldSpecification())
178 ->setDescription(pht('Active or archived status of the badge.')),
182 public function getFieldValuesForConduit() {
184 'name' => $this->getName(),
185 'creatorPHID' => $this->getCreatorPHID(),
186 'status' => $this->getStatus(),
190 public function getConduitSearchAttachments() {
194 /* -( PhabricatorNgramInterface )------------------------------------------ */
197 public function newNgrams() {
199 id(new PhabricatorBadgesBadgeNameNgrams())
200 ->setValue($this->getName()),