PageRenderTime 78ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/applications/badges/storage/PhabricatorBadgesBadge.php

http://github.com/facebook/phabricator
PHP | 204 lines | 152 code | 46 blank | 6 comment | 3 complexity | 6cd81780be8859d7b8ed57ba05fd06c1 MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause, LGPL-2.0, MIT, LGPL-2.1, LGPL-3.0
  1. <?php
  2. final class PhabricatorBadgesBadge extends PhabricatorBadgesDAO
  3. implements
  4. PhabricatorPolicyInterface,
  5. PhabricatorApplicationTransactionInterface,
  6. PhabricatorSubscribableInterface,
  7. PhabricatorFlaggableInterface,
  8. PhabricatorDestructibleInterface,
  9. PhabricatorConduitResultInterface,
  10. PhabricatorNgramsInterface {
  11. protected $name;
  12. protected $flavor;
  13. protected $description;
  14. protected $icon;
  15. protected $quality;
  16. protected $mailKey;
  17. protected $editPolicy;
  18. protected $status;
  19. protected $creatorPHID;
  20. const STATUS_ACTIVE = 'open';
  21. const STATUS_ARCHIVED = 'closed';
  22. const DEFAULT_ICON = 'fa-star';
  23. public static function getStatusNameMap() {
  24. return array(
  25. self::STATUS_ACTIVE => pht('Active'),
  26. self::STATUS_ARCHIVED => pht('Archived'),
  27. );
  28. }
  29. public static function initializeNewBadge(PhabricatorUser $actor) {
  30. $app = id(new PhabricatorApplicationQuery())
  31. ->setViewer($actor)
  32. ->withClasses(array('PhabricatorBadgesApplication'))
  33. ->executeOne();
  34. $view_policy = PhabricatorPolicies::getMostOpenPolicy();
  35. $edit_policy =
  36. $app->getPolicy(PhabricatorBadgesDefaultEditCapability::CAPABILITY);
  37. return id(new PhabricatorBadgesBadge())
  38. ->setIcon(self::DEFAULT_ICON)
  39. ->setQuality(PhabricatorBadgesQuality::DEFAULT_QUALITY)
  40. ->setCreatorPHID($actor->getPHID())
  41. ->setEditPolicy($edit_policy)
  42. ->setFlavor('')
  43. ->setDescription('')
  44. ->setStatus(self::STATUS_ACTIVE);
  45. }
  46. protected function getConfiguration() {
  47. return array(
  48. self::CONFIG_AUX_PHID => true,
  49. self::CONFIG_COLUMN_SCHEMA => array(
  50. 'name' => 'sort255',
  51. 'flavor' => 'text255',
  52. 'description' => 'text',
  53. 'icon' => 'text255',
  54. 'quality' => 'uint32',
  55. 'status' => 'text32',
  56. 'mailKey' => 'bytes20',
  57. ),
  58. self::CONFIG_KEY_SCHEMA => array(
  59. 'key_creator' => array(
  60. 'columns' => array('creatorPHID', 'dateModified'),
  61. ),
  62. ),
  63. ) + parent::getConfiguration();
  64. }
  65. public function generatePHID() {
  66. return
  67. PhabricatorPHID::generateNewPHID(PhabricatorBadgesPHIDType::TYPECONST);
  68. }
  69. public function isArchived() {
  70. return ($this->getStatus() == self::STATUS_ARCHIVED);
  71. }
  72. public function getViewURI() {
  73. return '/badges/view/'.$this->getID().'/';
  74. }
  75. public function save() {
  76. if (!$this->getMailKey()) {
  77. $this->setMailKey(Filesystem::readRandomCharacters(20));
  78. }
  79. return parent::save();
  80. }
  81. /* -( PhabricatorPolicyInterface )----------------------------------------- */
  82. public function getCapabilities() {
  83. return array(
  84. PhabricatorPolicyCapability::CAN_VIEW,
  85. PhabricatorPolicyCapability::CAN_EDIT,
  86. );
  87. }
  88. public function getPolicy($capability) {
  89. switch ($capability) {
  90. case PhabricatorPolicyCapability::CAN_VIEW:
  91. return PhabricatorPolicies::getMostOpenPolicy();
  92. case PhabricatorPolicyCapability::CAN_EDIT:
  93. return $this->getEditPolicy();
  94. }
  95. }
  96. public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
  97. return false;
  98. }
  99. /* -( PhabricatorApplicationTransactionInterface )------------------------- */
  100. public function getApplicationTransactionEditor() {
  101. return new PhabricatorBadgesEditor();
  102. }
  103. public function getApplicationTransactionTemplate() {
  104. return new PhabricatorBadgesTransaction();
  105. }
  106. /* -( PhabricatorSubscribableInterface )----------------------------------- */
  107. public function isAutomaticallySubscribed($phid) {
  108. return false;
  109. }
  110. /* -( PhabricatorDestructibleInterface )----------------------------------- */
  111. public function destroyObjectPermanently(
  112. PhabricatorDestructionEngine $engine) {
  113. $awards = id(new PhabricatorBadgesAwardQuery())
  114. ->setViewer($engine->getViewer())
  115. ->withBadgePHIDs(array($this->getPHID()))
  116. ->execute();
  117. foreach ($awards as $award) {
  118. $engine->destroyObject($award);
  119. }
  120. $this->openTransaction();
  121. $this->delete();
  122. $this->saveTransaction();
  123. }
  124. /* -( PhabricatorConduitResultInterface )---------------------------------- */
  125. public function getFieldSpecificationsForConduit() {
  126. return array(
  127. id(new PhabricatorConduitSearchFieldSpecification())
  128. ->setKey('name')
  129. ->setType('string')
  130. ->setDescription(pht('The name of the badge.')),
  131. id(new PhabricatorConduitSearchFieldSpecification())
  132. ->setKey('creatorPHID')
  133. ->setType('phid')
  134. ->setDescription(pht('User PHID of the creator.')),
  135. id(new PhabricatorConduitSearchFieldSpecification())
  136. ->setKey('status')
  137. ->setType('string')
  138. ->setDescription(pht('Active or archived status of the badge.')),
  139. );
  140. }
  141. public function getFieldValuesForConduit() {
  142. return array(
  143. 'name' => $this->getName(),
  144. 'creatorPHID' => $this->getCreatorPHID(),
  145. 'status' => $this->getStatus(),
  146. );
  147. }
  148. public function getConduitSearchAttachments() {
  149. return array();
  150. }
  151. /* -( PhabricatorNgramInterface )------------------------------------------ */
  152. public function newNgrams() {
  153. return array(
  154. id(new PhabricatorBadgesBadgeNameNgrams())
  155. ->setValue($this->getName()),
  156. );
  157. }
  158. }