Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/badges/storage/PhabricatorBadgesBadge.php
12256 views
1
<?php
2
3
final class PhabricatorBadgesBadge extends PhabricatorBadgesDAO
4
implements
5
PhabricatorPolicyInterface,
6
PhabricatorApplicationTransactionInterface,
7
PhabricatorSubscribableInterface,
8
PhabricatorFlaggableInterface,
9
PhabricatorDestructibleInterface,
10
PhabricatorConduitResultInterface,
11
PhabricatorNgramsInterface {
12
13
protected $name;
14
protected $flavor;
15
protected $description;
16
protected $icon;
17
protected $quality;
18
protected $mailKey;
19
protected $editPolicy;
20
protected $status;
21
protected $creatorPHID;
22
23
const STATUS_ACTIVE = 'open';
24
const STATUS_ARCHIVED = 'closed';
25
26
const DEFAULT_ICON = 'fa-star';
27
28
public static function getStatusNameMap() {
29
return array(
30
self::STATUS_ACTIVE => pht('Active'),
31
self::STATUS_ARCHIVED => pht('Archived'),
32
);
33
}
34
35
public static function initializeNewBadge(PhabricatorUser $actor) {
36
$app = id(new PhabricatorApplicationQuery())
37
->setViewer($actor)
38
->withClasses(array('PhabricatorBadgesApplication'))
39
->executeOne();
40
41
$view_policy = PhabricatorPolicies::getMostOpenPolicy();
42
43
$edit_policy =
44
$app->getPolicy(PhabricatorBadgesDefaultEditCapability::CAPABILITY);
45
46
return id(new PhabricatorBadgesBadge())
47
->setIcon(self::DEFAULT_ICON)
48
->setQuality(PhabricatorBadgesQuality::DEFAULT_QUALITY)
49
->setCreatorPHID($actor->getPHID())
50
->setEditPolicy($edit_policy)
51
->setFlavor('')
52
->setDescription('')
53
->setStatus(self::STATUS_ACTIVE);
54
}
55
56
protected function getConfiguration() {
57
return array(
58
self::CONFIG_AUX_PHID => true,
59
self::CONFIG_COLUMN_SCHEMA => array(
60
'name' => 'sort255',
61
'flavor' => 'text255',
62
'description' => 'text',
63
'icon' => 'text255',
64
'quality' => 'uint32',
65
'status' => 'text32',
66
'mailKey' => 'bytes20',
67
),
68
self::CONFIG_KEY_SCHEMA => array(
69
'key_creator' => array(
70
'columns' => array('creatorPHID', 'dateModified'),
71
),
72
),
73
) + parent::getConfiguration();
74
}
75
76
public function generatePHID() {
77
return
78
PhabricatorPHID::generateNewPHID(PhabricatorBadgesPHIDType::TYPECONST);
79
}
80
81
public function isArchived() {
82
return ($this->getStatus() == self::STATUS_ARCHIVED);
83
}
84
85
public function getViewURI() {
86
return '/badges/view/'.$this->getID().'/';
87
}
88
89
public function save() {
90
if (!$this->getMailKey()) {
91
$this->setMailKey(Filesystem::readRandomCharacters(20));
92
}
93
return parent::save();
94
}
95
96
97
/* -( PhabricatorPolicyInterface )----------------------------------------- */
98
99
100
public function getCapabilities() {
101
return array(
102
PhabricatorPolicyCapability::CAN_VIEW,
103
PhabricatorPolicyCapability::CAN_EDIT,
104
);
105
}
106
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();
113
}
114
}
115
116
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
117
return false;
118
}
119
120
121
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
122
123
124
public function getApplicationTransactionEditor() {
125
return new PhabricatorBadgesEditor();
126
}
127
128
public function getApplicationTransactionTemplate() {
129
return new PhabricatorBadgesTransaction();
130
}
131
132
133
/* -( PhabricatorSubscribableInterface )----------------------------------- */
134
135
136
public function isAutomaticallySubscribed($phid) {
137
return false;
138
}
139
140
141
142
/* -( PhabricatorDestructibleInterface )----------------------------------- */
143
144
145
public function destroyObjectPermanently(
146
PhabricatorDestructionEngine $engine) {
147
148
$awards = id(new PhabricatorBadgesAwardQuery())
149
->setViewer($engine->getViewer())
150
->withBadgePHIDs(array($this->getPHID()))
151
->execute();
152
153
foreach ($awards as $award) {
154
$engine->destroyObject($award);
155
}
156
157
$this->openTransaction();
158
$this->delete();
159
$this->saveTransaction();
160
}
161
162
/* -( PhabricatorConduitResultInterface )---------------------------------- */
163
164
165
public function getFieldSpecificationsForConduit() {
166
return array(
167
id(new PhabricatorConduitSearchFieldSpecification())
168
->setKey('name')
169
->setType('string')
170
->setDescription(pht('The name of the badge.')),
171
id(new PhabricatorConduitSearchFieldSpecification())
172
->setKey('creatorPHID')
173
->setType('phid')
174
->setDescription(pht('User PHID of the creator.')),
175
id(new PhabricatorConduitSearchFieldSpecification())
176
->setKey('status')
177
->setType('string')
178
->setDescription(pht('Active or archived status of the badge.')),
179
);
180
}
181
182
public function getFieldValuesForConduit() {
183
return array(
184
'name' => $this->getName(),
185
'creatorPHID' => $this->getCreatorPHID(),
186
'status' => $this->getStatus(),
187
);
188
}
189
190
public function getConduitSearchAttachments() {
191
return array();
192
}
193
194
/* -( PhabricatorNgramInterface )------------------------------------------ */
195
196
197
public function newNgrams() {
198
return array(
199
id(new PhabricatorBadgesBadgeNameNgrams())
200
->setValue($this->getName()),
201
);
202
}
203
204
}
205
206