Remove product literal strings in "pht()", part 18
[phabricator.git] / src / applications / maniphest / constants / ManiphestTaskStatus.php
blobc040befeed8de5cb051c6ef736eb0c52e1956b3e
1 <?php
3 /**
4 * @task validate Configuration Validation
5 */
6 final class ManiphestTaskStatus extends ManiphestConstants {
8 const STATUS_OPEN = 'open';
9 const STATUS_CLOSED_RESOLVED = 'resolved';
10 const STATUS_CLOSED_WONTFIX = 'wontfix';
11 const STATUS_CLOSED_INVALID = 'invalid';
12 const STATUS_CLOSED_DUPLICATE = 'duplicate';
13 const STATUS_CLOSED_SPITE = 'spite';
15 const SPECIAL_DEFAULT = 'default';
16 const SPECIAL_CLOSED = 'closed';
17 const SPECIAL_DUPLICATE = 'duplicate';
19 const LOCKED_COMMENTS = 'comments';
20 const LOCKED_EDITS = 'edits';
22 private static function getStatusConfig() {
23 return PhabricatorEnv::getEnvConfig('maniphest.statuses');
26 private static function getEnabledStatusMap() {
27 $spec = self::getStatusConfig();
29 $is_serious = PhabricatorEnv::getEnvConfig('phabricator.serious-business');
30 foreach ($spec as $const => $status) {
31 if ($is_serious && !empty($status['silly'])) {
32 unset($spec[$const]);
33 continue;
37 return $spec;
40 public static function getTaskStatusMap() {
41 return ipull(self::getEnabledStatusMap(), 'name');
45 /**
46 * Get the statuses and their command keywords.
48 * @return map Statuses to lists of command keywords.
50 public static function getTaskStatusKeywordsMap() {
51 $map = self::getEnabledStatusMap();
52 foreach ($map as $key => $spec) {
53 $words = idx($spec, 'keywords', array());
54 if (!is_array($words)) {
55 $words = array($words);
58 // For statuses, we include the status name because it's usually
59 // at least somewhat meaningful.
60 $words[] = $key;
62 foreach ($words as $word_key => $word) {
63 $words[$word_key] = phutil_utf8_strtolower($word);
66 $words = array_unique($words);
68 $map[$key] = $words;
71 return $map;
75 public static function getTaskStatusName($status) {
76 return self::getStatusAttribute($status, 'name', pht('Unknown Status'));
79 public static function getTaskStatusFullName($status) {
80 $name = self::getStatusAttribute($status, 'name.full');
81 if ($name !== null) {
82 return $name;
85 return self::getStatusAttribute($status, 'name', pht('Unknown Status'));
88 public static function renderFullDescription($status, $priority) {
89 if (self::isOpenStatus($status)) {
90 $name = pht('%s, %s', self::getTaskStatusFullName($status), $priority);
91 $color = 'grey';
92 $icon = 'fa-square-o';
93 } else {
94 $name = self::getTaskStatusFullName($status);
95 $color = 'indigo';
96 $icon = 'fa-check-square-o';
99 $tag = id(new PHUITagView())
100 ->setName($name)
101 ->setIcon($icon)
102 ->setType(PHUITagView::TYPE_SHADE)
103 ->setColor($color);
105 return $tag;
108 private static function getSpecialStatus($special) {
109 foreach (self::getStatusConfig() as $const => $status) {
110 if (idx($status, 'special') == $special) {
111 return $const;
114 return null;
117 public static function getDefaultStatus() {
118 return self::getSpecialStatus(self::SPECIAL_DEFAULT);
121 public static function getDefaultClosedStatus() {
122 return self::getSpecialStatus(self::SPECIAL_CLOSED);
125 public static function getDuplicateStatus() {
126 return self::getSpecialStatus(self::SPECIAL_DUPLICATE);
129 public static function getOpenStatusConstants() {
130 $result = array();
131 foreach (self::getEnabledStatusMap() as $const => $status) {
132 if (empty($status['closed'])) {
133 $result[] = $const;
136 return $result;
139 public static function getClosedStatusConstants() {
140 $all = array_keys(self::getTaskStatusMap());
141 $open = self::getOpenStatusConstants();
142 return array_diff($all, $open);
145 public static function isOpenStatus($status) {
146 foreach (self::getOpenStatusConstants() as $constant) {
147 if ($status == $constant) {
148 return true;
151 return false;
154 public static function isClaimStatus($status) {
155 return self::getStatusAttribute($status, 'claim', true);
158 public static function isClosedStatus($status) {
159 return !self::isOpenStatus($status);
162 public static function areCommentsLockedInStatus($status) {
163 return (bool)self::getStatusAttribute($status, 'locked', false);
166 public static function areEditsLockedInStatus($status) {
167 $locked = self::getStatusAttribute($status, 'locked');
168 return ($locked === self::LOCKED_EDITS);
171 public static function isMFAStatus($status) {
172 return self::getStatusAttribute($status, 'mfa', false);
175 public static function getStatusActionName($status) {
176 return self::getStatusAttribute($status, 'name.action');
179 public static function getStatusColor($status) {
180 return self::getStatusAttribute($status, 'transaction.color');
183 public static function isDisabledStatus($status) {
184 return self::getStatusAttribute($status, 'disabled');
187 public static function getStatusIcon($status) {
188 $icon = self::getStatusAttribute($status, 'transaction.icon');
189 if ($icon) {
190 return $icon;
193 if (self::isOpenStatus($status)) {
194 return 'fa-exclamation-circle';
195 } else {
196 return 'fa-check-square-o';
200 public static function getStatusPrefixMap() {
201 $map = array();
202 foreach (self::getEnabledStatusMap() as $const => $status) {
203 foreach (idx($status, 'prefixes', array()) as $prefix) {
204 $map[$prefix] = $const;
208 $map += array(
209 'ref' => null,
210 'refs' => null,
211 'references' => null,
212 'cf.' => null,
215 return $map;
218 public static function getStatusSuffixMap() {
219 $map = array();
220 foreach (self::getEnabledStatusMap() as $const => $status) {
221 foreach (idx($status, 'suffixes', array()) as $prefix) {
222 $map[$prefix] = $const;
225 return $map;
228 private static function getStatusAttribute($status, $key, $default = null) {
229 $config = self::getStatusConfig();
231 $spec = idx($config, $status);
232 if ($spec) {
233 return idx($spec, $key, $default);
236 return $default;
240 /* -( Configuration Validation )------------------------------------------- */
244 * @task validate
246 public static function isValidStatusConstant($constant) {
247 if (!strlen($constant) || strlen($constant) > 64) {
248 return false;
251 // Alphanumeric, but not exclusively numeric
252 if (!preg_match('/^(?![0-9]*$)[a-zA-Z0-9]+$/', $constant)) {
253 return false;
255 return true;
259 * @task validate
261 public static function validateConfiguration(array $config) {
262 foreach ($config as $key => $value) {
263 if (!self::isValidStatusConstant($key)) {
264 throw new Exception(
265 pht(
266 'Key "%s" is not a valid status constant. Status constants '.
267 'must be 1-64 alphanumeric characters and cannot be exclusively '.
268 'digits. For example, "%s" or "%s" are reasonable choices.',
269 $key,
270 'open',
271 'closed'));
273 if (!is_array($value)) {
274 throw new Exception(
275 pht(
276 'Value for key "%s" should be a dictionary.',
277 $key));
280 PhutilTypeSpec::checkMap(
281 $value,
282 array(
283 'name' => 'string',
284 'name.full' => 'optional string',
285 'name.action' => 'optional string',
286 'closed' => 'optional bool',
287 'special' => 'optional string',
288 'transaction.icon' => 'optional string',
289 'transaction.color' => 'optional string',
290 'silly' => 'optional bool',
291 'prefixes' => 'optional list<string>',
292 'suffixes' => 'optional list<string>',
293 'keywords' => 'optional list<string>',
294 'disabled' => 'optional bool',
295 'claim' => 'optional bool',
296 'locked' => 'optional bool|string',
297 'mfa' => 'optional bool',
301 // Supported values are "comments" or "edits". For backward compatibility,
302 // "true" is an alias of "comments".
304 foreach ($config as $key => $value) {
305 $locked = idx($value, 'locked', false);
306 if ($locked === true || $locked === false) {
307 continue;
310 if ($locked === self::LOCKED_EDITS ||
311 $locked === self::LOCKED_COMMENTS) {
312 continue;
315 throw new Exception(
316 pht(
317 'Task status ("%s") has unrecognized value for "locked" '.
318 'configuration ("%s"). Supported values are: "%s", "%s".',
319 $key,
320 $locked,
321 self::LOCKED_COMMENTS,
322 self::LOCKED_EDITS));
325 $special_map = array();
326 foreach ($config as $key => $value) {
327 $special = idx($value, 'special');
328 if (!$special) {
329 continue;
332 if (isset($special_map[$special])) {
333 throw new Exception(
334 pht(
335 'Configuration has two statuses both marked with the special '.
336 'attribute "%s" ("%s" and "%s"). There should be only one.',
337 $special,
338 $special_map[$special],
339 $key));
342 switch ($special) {
343 case self::SPECIAL_DEFAULT:
344 if (!empty($value['closed'])) {
345 throw new Exception(
346 pht(
347 'Status "%s" is marked as default, but it is a closed '.
348 'status. The default status should be an open status.',
349 $key));
351 break;
352 case self::SPECIAL_CLOSED:
353 if (empty($value['closed'])) {
354 throw new Exception(
355 pht(
356 'Status "%s" is marked as the default status for closing '.
357 'tasks, but is not a closed status. It should be a closed '.
358 'status.',
359 $key));
361 break;
362 case self::SPECIAL_DUPLICATE:
363 if (empty($value['closed'])) {
364 throw new Exception(
365 pht(
366 'Status "%s" is marked as the status for closing tasks as '.
367 'duplicates, but it is not a closed status. It should '.
368 'be a closed status.',
369 $key));
371 break;
374 $special_map[$special] = $key;
377 // NOTE: We're not explicitly validating that we have at least one open
378 // and one closed status, because the DEFAULT and CLOSED specials imply
379 // that to be true. If those change in the future, that might become a
380 // reasonable thing to validate.
382 $required = array(
383 self::SPECIAL_DEFAULT,
384 self::SPECIAL_CLOSED,
385 self::SPECIAL_DUPLICATE,
388 foreach ($required as $required_special) {
389 if (!isset($special_map[$required_special])) {
390 throw new Exception(
391 pht(
392 'Configuration defines no task status with special attribute '.
393 '"%s", but you must specify a status which fills this special '.
394 'role.',
395 $required_special));