1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-14 01:26:13 +01:00
phorge-phorge/src/applications/badges/storage/PhabricatorBadgesBadge.php
epriestley 5e182180a9 Provide a "PHUIFormIconSetControl"
Summary:
Ref T9992. This is a step on the path to getting EditEngine working in Badges, Projects and Calendar.

This doesn't add a new `EditField` for icons yet, just standardizes the old stuff. New stuff is more general and I saved 150 lines of code.

I put the endpoint in Files because the similar "choose a profile picture" endpoint will definitely go there, and this endpoint might eventually feature, like, "draw your own icon~~" or something.

Test Plan:
  - Created events, projects and badges with custom icons.
  - Edited events, projects and badges, changing their icons.

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T9992

Differential Revision: https://secure.phabricator.com/D14799
2015-12-16 08:46:51 -08:00

209 lines
5.1 KiB
PHP

<?php
final class PhabricatorBadgesBadge extends PhabricatorBadgesDAO
implements
PhabricatorPolicyInterface,
PhabricatorApplicationTransactionInterface,
PhabricatorSubscribableInterface,
PhabricatorTokenReceiverInterface,
PhabricatorFlaggableInterface,
PhabricatorDestructibleInterface {
protected $name;
protected $flavor;
protected $description;
protected $icon;
protected $quality;
protected $mailKey;
protected $editPolicy;
protected $status;
protected $creatorPHID;
private $recipientPHIDs = self::ATTACHABLE;
const STATUS_ACTIVE = 'open';
const STATUS_ARCHIVED = 'closed';
const DEFAULT_ICON = 'fa-star';
const DEFAULT_QUALITY = 'green';
const POOR = 'grey';
const COMMON = 'white';
const UNCOMMON = 'green';
const RARE = 'blue';
const EPIC = 'indigo';
const LEGENDARY = 'orange';
const HEIRLOOM = 'yellow';
public static function getStatusNameMap() {
return array(
self::STATUS_ACTIVE => pht('Active'),
self::STATUS_ARCHIVED => pht('Archived'),
);
}
public static function getQualityNameMap() {
return array(
self::POOR => pht('Poor'),
self::COMMON => pht('Common'),
self::UNCOMMON => pht('Uncommon'),
self::RARE => pht('Rare'),
self::EPIC => pht('Epic'),
self::LEGENDARY => pht('Legendary'),
self::HEIRLOOM => pht('Heirloom'),
);
}
public static function initializeNewBadge(PhabricatorUser $actor) {
$app = id(new PhabricatorApplicationQuery())
->setViewer($actor)
->withClasses(array('PhabricatorBadgesApplication'))
->executeOne();
$view_policy = PhabricatorPolicies::getMostOpenPolicy();
$edit_policy =
$app->getPolicy(PhabricatorBadgesDefaultEditCapability::CAPABILITY);
return id(new PhabricatorBadgesBadge())
->setIcon(self::DEFAULT_ICON)
->setQuality(self::DEFAULT_QUALITY)
->setCreatorPHID($actor->getPHID())
->setEditPolicy($edit_policy)
->setStatus(self::STATUS_ACTIVE);
}
protected function getConfiguration() {
return array(
self::CONFIG_AUX_PHID => true,
self::CONFIG_COLUMN_SCHEMA => array(
'name' => 'text255',
'flavor' => 'text255',
'description' => 'text',
'icon' => 'text255',
'quality' => 'text255',
'status' => 'text32',
'mailKey' => 'bytes20',
),
self::CONFIG_KEY_SCHEMA => array(
'key_creator' => array(
'columns' => array('creatorPHID', 'dateModified'),
),
),
) + parent::getConfiguration();
}
public function generatePHID() {
return
PhabricatorPHID::generateNewPHID(PhabricatorBadgesPHIDType::TYPECONST);
}
public function isArchived() {
return ($this->getStatus() == self::STATUS_ARCHIVED);
}
public function attachRecipientPHIDs(array $phids) {
$this->recipientPHIDs = $phids;
return $this;
}
public function getRecipientPHIDs() {
return $this->assertAttached($this->recipientPHIDs);
}
public function save() {
if (!$this->getMailKey()) {
$this->setMailKey(Filesystem::readRandomCharacters(20));
}
return parent::save();
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
PhabricatorPolicyCapability::CAN_EDIT,
);
}
public function getPolicy($capability) {
switch ($capability) {
case PhabricatorPolicyCapability::CAN_VIEW:
return PhabricatorPolicies::getMostOpenPolicy();
case PhabricatorPolicyCapability::CAN_EDIT:
return $this->getEditPolicy();
}
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
return false;
}
public function describeAutomaticCapability($capability) {
return null;
}
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
public function getApplicationTransactionEditor() {
return new PhabricatorBadgesEditor();
}
public function getApplicationTransactionObject() {
return $this;
}
public function getApplicationTransactionTemplate() {
return new PhabricatorBadgesTransaction();
}
public function willRenderTimeline(
PhabricatorApplicationTransactionView $timeline,
AphrontRequest $request) {
return $timeline;
}
/* -( PhabricatorSubscribableInterface )----------------------------------- */
public function isAutomaticallySubscribed($phid) {
return ($this->creatorPHID == $phid);
}
public function shouldShowSubscribersProperty() {
return true;
}
public function shouldAllowSubscription($phid) {
return true;
}
/* -( PhabricatorTokenReceiverInterface )---------------------------------- */
public function getUsersToNotifyOfTokenGiven() {
return array($this->getCreatorPHID());
}
/* -( PhabricatorDestructibleInterface )----------------------------------- */
public function destroyObjectPermanently(
PhabricatorDestructionEngine $engine) {
$this->openTransaction();
$this->delete();
$this->saveTransaction();
}
}