Path: blob/master/src/applications/maniphest/constants/ManiphestTaskPriority.php
12256 views
<?php12final class ManiphestTaskPriority extends ManiphestConstants {34const UNKNOWN_PRIORITY_KEYWORD = '!!unknown!!';56/**7* Get the priorities and their full descriptions.8*9* @return map Priorities to descriptions.10*/11public static function getTaskPriorityMap() {12$map = self::getConfig();13foreach ($map as $key => $spec) {14$map[$key] = idx($spec, 'name', $key);15}16return $map;17}181920/**21* Get the priorities and their command keywords.22*23* @return map Priorities to lists of command keywords.24*/25public static function getTaskPriorityKeywordsMap() {26$map = self::getConfig();27foreach ($map as $key => $spec) {28$words = idx($spec, 'keywords', array());29if (!is_array($words)) {30$words = array($words);31}3233foreach ($words as $word_key => $word) {34$words[$word_key] = phutil_utf8_strtolower($word);35}3637$words = array_unique($words);3839$map[$key] = $words;40}4142return $map;43}4445/**46* Get the canonical keyword for a given priority constant.47*48* @return string|null Keyword, or `null` if no keyword is configured.49*/50public static function getKeywordForTaskPriority($priority) {51$map = self::getConfig();5253$spec = idx($map, $priority);54if (!$spec) {55return null;56}5758$keywords = idx($spec, 'keywords');59if (!$keywords) {60return null;61}6263return head($keywords);64}656667/**68* Get a map of supported alternate names for each priority.69*70* Keys are aliases, like "wish" and "wishlist". Values are canonical71* priority keywords, like "wishlist".72*73* @return map<string, string> Map of aliases to canonical priority keywords.74*/75public static function getTaskPriorityAliasMap() {76$keyword_map = self::getTaskPriorityKeywordsMap();7778$result = array();79foreach ($keyword_map as $key => $keywords) {80$target = self::getKeywordForTaskPriority($key);81if ($target === null) {82continue;83}8485// NOTE: Include the raw priority value, like "25", in the list of86// aliases. This supports legacy sources like saved EditEngine forms.87$result[$key] = $target;8889foreach ($keywords as $keyword) {90$result[$keyword] = $target;91}92}9394return $result;95}969798/**99* Get the priorities and their related short (one-word) descriptions.100*101* @return map Priorities to short descriptions.102*/103public static function getShortNameMap() {104$map = self::getConfig();105foreach ($map as $key => $spec) {106$map[$key] = idx($spec, 'short', idx($spec, 'name', $key));107}108return $map;109}110111112/**113* Get a map from priority constants to their colors.114*115* @return map<int, string> Priorities to colors.116*/117public static function getColorMap() {118$map = self::getConfig();119foreach ($map as $key => $spec) {120$map[$key] = idx($spec, 'color', 'grey');121}122return $map;123}124125126/**127* Return the default priority for this instance of Phabricator.128*129* @return int The value of the default priority constant.130*/131public static function getDefaultPriority() {132return PhabricatorEnv::getEnvConfig('maniphest.default-priority');133}134135136/**137* Retrieve the full name of the priority level provided.138*139* @param int A priority level.140* @return string The priority name if the level is a valid one.141*/142public static function getTaskPriorityName($priority) {143return idx(self::getTaskPriorityMap(), $priority, $priority);144}145146/**147* Retrieve the color of the priority level given148*149* @param int A priority level.150* @return string The color of the priority if the level is valid,151* or black if it is not.152*/153public static function getTaskPriorityColor($priority) {154return idx(self::getColorMap(), $priority, 'black');155}156157public static function getTaskPriorityIcon($priority) {158return 'fa-arrow-right';159}160161public static function getTaskPriorityFromKeyword($keyword) {162$map = self::getTaskPriorityKeywordsMap();163164foreach ($map as $priority => $keywords) {165if (in_array($keyword, $keywords)) {166return $priority;167}168}169170return null;171}172173public static function isDisabledPriority($priority) {174$config = idx(self::getConfig(), $priority, array());175return idx($config, 'disabled', false);176}177178public static function getConfig() {179$config = PhabricatorEnv::getEnvConfig('maniphest.priorities');180krsort($config);181return $config;182}183184private static function isValidPriorityKeyword($keyword) {185if (!strlen($keyword) || strlen($keyword) > 64) {186return false;187}188189// Alphanumeric, but not exclusively numeric190if (!preg_match('/^(?![0-9]*$)[a-zA-Z0-9]+$/', $keyword)) {191return false;192}193return true;194}195196public static function validateConfiguration($config) {197if (!is_array($config)) {198throw new Exception(199pht(200'Configuration is not valid. Maniphest priority configurations '.201'must be dictionaries.'));202}203204$all_keywords = array();205foreach ($config as $key => $value) {206if (!ctype_digit((string)$key)) {207throw new Exception(208pht(209'Key "%s" is not a valid priority constant. Priority constants '.210'must be nonnegative integers.',211$key));212}213214if (!is_array($value)) {215throw new Exception(216pht(217'Value for key "%s" should be a dictionary.',218$key));219}220221PhutilTypeSpec::checkMap(222$value,223array(224'name' => 'string',225'keywords' => 'list<string>',226'short' => 'optional string',227'color' => 'optional string',228'disabled' => 'optional bool',229));230231$keywords = $value['keywords'];232foreach ($keywords as $keyword) {233if (!self::isValidPriorityKeyword($keyword)) {234throw new Exception(235pht(236'Key "%s" is not a valid priority keyword. Priority keywords '.237'must be 1-64 alphanumeric characters and cannot be '.238'exclusively digits. For example, "%s" or "%s" are '.239'reasonable choices.',240$keyword,241'low',242'critical'));243}244245if (isset($all_keywords[$keyword])) {246throw new Exception(247pht(248'Two different task priorities ("%s" and "%s") have the same '.249'keyword ("%s"). Keywords must uniquely identify priorities.',250$value['name'],251$all_keywords[$keyword],252$keyword));253}254255$all_keywords[$keyword] = $value['name'];256}257}258}259260}261262263