4 * Provides a convenient field for storing a set of levels that you can use to
7 * Levels are rendered with names and descriptions in the edit UI, and are
8 * automatically documented via the "arc request" interface.
10 * See ReleephSeverityFieldSpecification for an example.
12 abstract class ReleephLevelFieldSpecification
13 extends ReleephFieldSpecification
{
17 abstract public function getLevels();
18 abstract public function getDefaultLevel();
19 abstract public function getNameForLevel($level);
20 abstract public function getDescriptionForLevel($level);
22 public function getStorageKey() {
23 throw new PhabricatorCustomFieldImplementationIncompleteException($this);
26 public function renderPropertyViewValue(array $handles) {
27 return $this->getNameForLevel($this->getValue());
30 public function renderEditControl(array $handles) {
31 $control_name = $this->getRequiredStorageKey();
32 $all_levels = $this->getLevels();
34 $level = $this->getValue();
36 $level = $this->getDefaultLevel();
39 $control = id(new AphrontFormRadioButtonControl())
40 ->setLabel(pht('Level'))
41 ->setName($control_name)
45 $control->setError($this->error
);
46 } else if ($this->getDefaultLevel()) {
47 $control->setError(true);
50 foreach ($all_levels as $level) {
51 $name = $this->getNameForLevel($level);
52 $description = $this->getDescriptionForLevel($level);
53 $control->addButton($level, $name, $description);
59 public function renderHelpForArcanist() {
61 $levels = $this->getLevels();
62 $default = $this->getDefaultLevel();
63 foreach ($levels as $level) {
64 $name = $this->getNameForLevel($level);
65 $description = $this->getDescriptionForLevel($level);
66 $default_marker = ' ';
67 if ($level === $default) {
68 $default_marker = '*';
70 $text .= " {$default_marker} **{$name}**\n";
71 $text .= phutil_console_wrap($description."\n", 8);
76 public function validate($value) {
77 if ($value === null) {
78 $this->error
= pht('Required');
79 $label = $this->getName();
80 throw new ReleephFieldParseException(
82 pht('You must provide a %s level.', $label));
85 $levels = $this->getLevels();
86 if (!in_array($value, $levels)) {
87 $label = $this->getName();
88 throw new ReleephFieldParseException(
91 "Level '%s' is not a valid %s level in this project.",
97 public function setValueFromConduitAPIRequest(ConduitAPIRequest
$request) {
98 $key = $this->getRequiredStorageKey();
99 $label = $this->getName();
100 $name = idx($request->getValue('fields', array()), $key);
103 $level = $this->getDefaultLevel();
105 throw new ReleephFieldParseException(
108 'No value given for %s, and no default is given for this level!',
112 $level = $this->getLevelByName($name);
116 throw new ReleephFieldParseException(
118 pht("Unknown %s level name '%s'", $label, $name));
120 $this->setValue($level);
124 private $nameMap = array();
126 public function getLevelByName($name) {
128 if (!$this->nameMap
) {
129 foreach ($this->getLevels() as $level) {
130 $level_name = $this->getNameForLevel($level);
131 $this->nameMap
[$level_name] = $level;
134 return idx($this->nameMap
, $name);