3 final class ManiphestTaskPriority
extends ManiphestConstants
{
5 const UNKNOWN_PRIORITY_KEYWORD
= '!!unknown!!';
8 * Get the priorities and their full descriptions.
10 * @return map Priorities to descriptions.
12 public static function getTaskPriorityMap() {
13 $map = self
::getConfig();
14 foreach ($map as $key => $spec) {
15 $map[$key] = idx($spec, 'name', $key);
22 * Get the priorities and their command keywords.
24 * @return map Priorities to lists of command keywords.
26 public static function getTaskPriorityKeywordsMap() {
27 $map = self
::getConfig();
28 foreach ($map as $key => $spec) {
29 $words = idx($spec, 'keywords', array());
30 if (!is_array($words)) {
31 $words = array($words);
34 foreach ($words as $word_key => $word) {
35 $words[$word_key] = phutil_utf8_strtolower($word);
38 $words = array_unique($words);
47 * Get the canonical keyword for a given priority constant.
49 * @return string|null Keyword, or `null` if no keyword is configured.
51 public static function getKeywordForTaskPriority($priority) {
52 $map = self
::getConfig();
54 $spec = idx($map, $priority);
59 $keywords = idx($spec, 'keywords');
64 return head($keywords);
69 * Get a map of supported alternate names for each priority.
71 * Keys are aliases, like "wish" and "wishlist". Values are canonical
72 * priority keywords, like "wishlist".
74 * @return map<string, string> Map of aliases to canonical priority keywords.
76 public static function getTaskPriorityAliasMap() {
77 $keyword_map = self
::getTaskPriorityKeywordsMap();
80 foreach ($keyword_map as $key => $keywords) {
81 $target = self
::getKeywordForTaskPriority($key);
82 if ($target === null) {
86 // NOTE: Include the raw priority value, like "25", in the list of
87 // aliases. This supports legacy sources like saved EditEngine forms.
88 $result[$key] = $target;
90 foreach ($keywords as $keyword) {
91 $result[$keyword] = $target;
100 * Get the priorities and their related short (one-word) descriptions.
102 * @return map Priorities to short descriptions.
104 public static function getShortNameMap() {
105 $map = self
::getConfig();
106 foreach ($map as $key => $spec) {
107 $map[$key] = idx($spec, 'short', idx($spec, 'name', $key));
114 * Get a map from priority constants to their colors.
116 * @return map<int, string> Priorities to colors.
118 public static function getColorMap() {
119 $map = self
::getConfig();
120 foreach ($map as $key => $spec) {
121 $map[$key] = idx($spec, 'color', 'grey');
128 * Return the default priority for this instance of Phabricator.
130 * @return int The value of the default priority constant.
132 public static function getDefaultPriority() {
133 return PhabricatorEnv
::getEnvConfig('maniphest.default-priority');
138 * Retrieve the full name of the priority level provided.
140 * @param int A priority level.
141 * @return string The priority name if the level is a valid one.
143 public static function getTaskPriorityName($priority) {
144 return idx(self
::getTaskPriorityMap(), $priority, $priority);
148 * Retrieve the color of the priority level given
150 * @param int A priority level.
151 * @return string The color of the priority if the level is valid,
152 * or black if it is not.
154 public static function getTaskPriorityColor($priority) {
155 return idx(self
::getColorMap(), $priority, 'black');
158 public static function getTaskPriorityIcon($priority) {
159 return 'fa-arrow-right';
162 public static function getTaskPriorityFromKeyword($keyword) {
163 $map = self
::getTaskPriorityKeywordsMap();
165 foreach ($map as $priority => $keywords) {
166 if (in_array($keyword, $keywords)) {
174 public static function isDisabledPriority($priority) {
175 $config = idx(self
::getConfig(), $priority, array());
176 return idx($config, 'disabled', false);
179 public static function getConfig() {
180 $config = PhabricatorEnv
::getEnvConfig('maniphest.priorities');
185 private static function isValidPriorityKeyword($keyword) {
186 if (!strlen($keyword) ||
strlen($keyword) > 64) {
190 // Alphanumeric, but not exclusively numeric
191 if (!preg_match('/^(?![0-9]*$)[a-zA-Z0-9]+$/', $keyword)) {
197 public static function validateConfiguration($config) {
198 if (!is_array($config)) {
201 'Configuration is not valid. Maniphest priority configurations '.
202 'must be dictionaries.'));
205 $all_keywords = array();
206 foreach ($config as $key => $value) {
207 if (!ctype_digit((string)$key)) {
210 'Key "%s" is not a valid priority constant. Priority constants '.
211 'must be nonnegative integers.',
215 if (!is_array($value)) {
218 'Value for key "%s" should be a dictionary.',
222 PhutilTypeSpec
::checkMap(
226 'keywords' => 'list<string>',
227 'short' => 'optional string',
228 'color' => 'optional string',
229 'disabled' => 'optional bool',
232 $keywords = $value['keywords'];
233 foreach ($keywords as $keyword) {
234 if (!self
::isValidPriorityKeyword($keyword)) {
237 'Key "%s" is not a valid priority keyword. Priority keywords '.
238 'must be 1-64 alphanumeric characters and cannot be '.
239 'exclusively digits. For example, "%s" or "%s" are '.
240 'reasonable choices.',
246 if (isset($all_keywords[$keyword])) {
249 'Two different task priorities ("%s" and "%s") have the same '.
250 'keyword ("%s"). Keywords must uniquely identify priorities.',
252 $all_keywords[$keyword],
256 $all_keywords[$keyword] = $value['name'];