[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/releeph/field/specification/ -> ReleephLevelFieldSpecification.php (source)

   1  <?php
   2  
   3  /**
   4   * Provides a convenient field for storing a set of levels that you can use to
   5   * filter requests on.
   6   *
   7   * Levels are rendered with names and descriptions in the edit UI, and are
   8   * automatically documented via the "arc request" interface.
   9   *
  10   * See ReleephSeverityFieldSpecification for an example.
  11   */
  12  abstract class ReleephLevelFieldSpecification
  13    extends ReleephFieldSpecification {
  14  
  15    private $error;
  16  
  17    abstract public function getLevels();
  18    abstract public function getDefaultLevel();
  19    abstract public function getNameForLevel($level);
  20    abstract public function getDescriptionForLevel($level);
  21  
  22    public function getStorageKey() {
  23      throw new PhabricatorCustomFieldImplementationIncompleteException($this);
  24    }
  25  
  26    public function renderPropertyViewValue(array $handles) {
  27      return $this->getNameForLevel($this->getValue());
  28    }
  29  
  30    public function renderEditControl(array $handles) {
  31      $control_name = $this->getRequiredStorageKey();
  32      $all_levels = $this->getLevels();
  33  
  34      $level = $this->getValue();
  35      if (!$level) {
  36        $level = $this->getDefaultLevel();
  37      }
  38  
  39      $control = id(new AphrontFormRadioButtonControl())
  40        ->setLabel('Level')
  41        ->setName($control_name)
  42        ->setValue($level);
  43  
  44      if ($this->error) {
  45        $control->setError($this->error);
  46      } else if ($this->getDefaultLevel()) {
  47        $control->setError(true);
  48      }
  49  
  50      foreach ($all_levels as $level) {
  51        $name = $this->getNameForLevel($level);
  52        $description = $this->getDescriptionForLevel($level);
  53        $control->addButton($level, $name, $description);
  54      }
  55  
  56      return $control;
  57    }
  58  
  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 = '*';
  69        }
  70        $text .= "    {$default_marker} **{$name}**\n";
  71        $text .= phutil_console_wrap($description."\n", 8);
  72      }
  73      return $text;
  74    }
  75  
  76    public function validate($value) {
  77      if ($value === null) {
  78        $this->error = 'Required';
  79        $label = $this->getName();
  80        throw new ReleephFieldParseException(
  81          $this,
  82          "You must provide a {$label} level");
  83      }
  84  
  85      $levels = $this->getLevels();
  86      if (!in_array($value, $levels)) {
  87        $label = $this->getName();
  88        throw new ReleephFieldParseException(
  89          $this,
  90          "Level '{$value}' is not a valid {$label} level in this project.");
  91      }
  92    }
  93  
  94    public function setValueFromConduitAPIRequest(ConduitAPIRequest $request) {
  95      $key = $this->getRequiredStorageKey();
  96      $label = $this->getName();
  97      $name = idx($request->getValue('fields', array()), $key);
  98  
  99      if (!$name) {
 100        $level = $this->getDefaultLevel();
 101        if (!$level) {
 102          throw new ReleephFieldParseException(
 103            $this,
 104            "No value given for {$label}, ".
 105            "and no default is given for this level!");
 106        }
 107      } else {
 108        $level = $this->getLevelByName($name);
 109      }
 110  
 111      if (!$level) {
 112        throw new ReleephFieldParseException(
 113          $this,
 114          "Unknown {$label} level name '{$name}'");
 115      }
 116      $this->setValue($level);
 117    }
 118  
 119    private $nameMap = array();
 120  
 121    public function getLevelByName($name) {
 122      // Build this once
 123      if (!$this->nameMap) {
 124        foreach ($this->getLevels() as $level) {
 125          $level_name = $this->getNameForLevel($level);
 126          $this->nameMap[$level_name] = $level;
 127        }
 128      }
 129      return idx($this->nameMap, $name);
 130    }
 131  
 132  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1