* * Serves as the base for Inputfields that provide selection of options (whether single or multi). * As a result, this class includes functionality for, and checks for both single-and-multi selection values. * Sublcasses will want to override the render method, but it's not necessary to override processInput(). * Subclasses that select multiple values should implement the InputfieldHasArrayValue interface. * * @todo add support for 'required' attribute? * */ class InputfieldSelect extends Inputfield { /** * Options specific to this Select * */ protected $options = array(); /** * Attributes for options specific to this select (if applicable) * */ protected $optionAttributes = array(); /** * Return information about this module * */ public static function getModuleInfo() { return array( 'title' => __('Select', __FILE__), // Module Title 'summary' => __('Selection of a single value from a select pulldown', __FILE__), // Module Summary 'version' => 102, 'permanent' => true, ); } public function __construct() { parent::__construct(); $this->set('defaultValue', ''); } /** * Add an option that may be selected * * If you want to add an optgroup, use the $value param as the label, and the label param as an array of options. * Note that optgroups may not be applicable to other Inputfields that descend from InputfieldSelect. * * @param string $value Value that the option submits * @param string $label Optional label associated with the value (if null, value will be used as the label) * @param array $attributes Optional attributes to be associated with this option (i.e. a 'selected' attribute for an "; foreach($options as $value => $label) { if(is_array($label)) { $out .= "\n\t" . $this->renderOptions($label, false) . "\n\t"; continue; } $selected = $this->isOptionSelected($value) ? " selected='selected'" : ''; $attrs = $this->getOptionAttributesString($value); $out .= "\n\t" . $this->entityEncode($label) . ""; } return $out; } /** * Check for default value and populate when appropriate * * This should be called at the beginning of render() and at the end of processInput() * */ protected function checkDefaultValue() { if(!$this->required || !$this->defaultValue || !$this->isEmpty()) return; // when a value is required and the value is empty and a default value is specified, we use it. if($this instanceof InputfieldHasArrayValue) { $value = explode("\n", $this->defaultValue); foreach($value as $k => $v) { $value[$k] = trim($v); // remove possible extra LF } } else { $value = $this->defaultValue; $pos = strpos($value, "\n"); if($pos) $value = substr($value, 0, $pos); $value = trim($value); } $this->attr('value', $value); } /** * Render and return the output for this Select * */ public function ___render() { $this->checkDefaultValue(); $attrs = $this->getAttributes(); unset($attrs['value']); $out = "\n"; return $out; } public function ___renderValue() { $out = ""; return $out; } /** * Get an attributes array intended for the