Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / releeph / field / specification / ReleephLevelFieldSpecification.php
blob395636c983d1e1207c685dc5af8891d482db54cc
1 <?php
3 /**
4 * Provides a convenient field for storing a set of levels that you can use to
5 * filter requests on.
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 {
15 private $error;
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();
35 if (!$level) {
36 $level = $this->getDefaultLevel();
39 $control = id(new AphrontFormRadioButtonControl())
40 ->setLabel(pht('Level'))
41 ->setName($control_name)
42 ->setValue($level);
44 if ($this->error) {
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);
56 return $control;
59 public function renderHelpForArcanist() {
60 $text = '';
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);
73 return $text;
76 public function validate($value) {
77 if ($value === null) {
78 $this->error = pht('Required');
79 $label = $this->getName();
80 throw new ReleephFieldParseException(
81 $this,
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(
89 $this,
90 pht(
91 "Level '%s' is not a valid %s level in this project.",
92 $value,
93 $label));
97 public function setValueFromConduitAPIRequest(ConduitAPIRequest $request) {
98 $key = $this->getRequiredStorageKey();
99 $label = $this->getName();
100 $name = idx($request->getValue('fields', array()), $key);
102 if (!$name) {
103 $level = $this->getDefaultLevel();
104 if (!$level) {
105 throw new ReleephFieldParseException(
106 $this,
107 pht(
108 'No value given for %s, and no default is given for this level!',
109 $label));
111 } else {
112 $level = $this->getLevelByName($name);
115 if (!$level) {
116 throw new ReleephFieldParseException(
117 $this,
118 pht("Unknown %s level name '%s'", $label, $name));
120 $this->setValue($level);
121 return $this;
124 private $nameMap = array();
126 public function getLevelByName($name) {
127 // Build this once
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);