|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Abstract class for Widgets |
| 4 | + */ |
| 5 | +namespace PHPForm\Widgets; |
| 6 | + |
| 7 | +use Fleshgrinder\Core\Formatter; |
| 8 | + |
| 9 | +use PHPForm\Utils\Attributes; |
| 10 | + |
| 11 | +abstract class ChoiceWidget extends Widget |
| 12 | +{ |
| 13 | + const TEMPLATE_OPTION = ''; |
| 14 | + |
| 15 | + protected $allow_multiple_selected = false; |
| 16 | + protected $input_type = null; |
| 17 | + protected $option_inherits_attrs = true; |
| 18 | + protected $choices; |
| 19 | + |
| 20 | + /** |
| 21 | + * The constructor. |
| 22 | + */ |
| 23 | + public function __construct(array $choices = array(), array $css_classes = null, array $attrs = null) |
| 24 | + { |
| 25 | + parent::__construct($css_classes, $attrs); |
| 26 | + |
| 27 | + $this->choices = $choices; |
| 28 | + } |
| 29 | + |
| 30 | + public function setChoices(array $value) |
| 31 | + { |
| 32 | + $this->choices = $choices; |
| 33 | + } |
| 34 | + |
| 35 | + public function getContext(string $name, $value, array $attrs = null) |
| 36 | + { |
| 37 | + $context = parent::getContext($name, $value, $attrs); |
| 38 | + $context["options"] = $this->renderOptions($context['name'], $context['value'], $attrs); |
| 39 | + |
| 40 | + return $context; |
| 41 | + } |
| 42 | + |
| 43 | + public function renderOptions(string $name, $value, array $attrs = null) |
| 44 | + { |
| 45 | + $options = ''; |
| 46 | + $has_selected = false; |
| 47 | + |
| 48 | + foreach ($this->choices as $choice_value => $choice_label) { |
| 49 | + $selected = false; |
| 50 | + |
| 51 | + if (!$has_selected || $this->allow_multiple_selected) { |
| 52 | + $selected = in_array($choice_value, $value); |
| 53 | + $has_selected = true; |
| 54 | + } |
| 55 | + |
| 56 | + $context = $this->getOptionContext($name, $choice_value, $choice_label, $selected, $attrs); |
| 57 | + |
| 58 | + $options .= Formatter::format(static::TEMPLATE_OPTION, $context); |
| 59 | + } |
| 60 | + |
| 61 | + return $options; |
| 62 | + } |
| 63 | + |
| 64 | + public function getOptionContext(string $name, $value, string $label, bool $is_selected, array $attrs = null) |
| 65 | + { |
| 66 | + if (!$this->option_inherits_attrs || is_null($attrs)) { |
| 67 | + $attrs = array(); |
| 68 | + } |
| 69 | + |
| 70 | + if ($is_selected) { |
| 71 | + $attrs["selected"] = "selected"; |
| 72 | + } |
| 73 | + |
| 74 | + return array( |
| 75 | + "type" => $this->input_type, |
| 76 | + "name" => $name, |
| 77 | + "value" => htmlentities($value), |
| 78 | + "label" => htmlentities($label), |
| 79 | + "attrs" => Attributes::flatatt($attrs) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + protected function formatValue($value) |
| 84 | + { |
| 85 | + if (is_array($value)) { |
| 86 | + $values = []; |
| 87 | + |
| 88 | + foreach ($value as $v) { |
| 89 | + $values[] = parent::formatValue($v); |
| 90 | + } |
| 91 | + } else { |
| 92 | + $values = [parent::formatValue($value)]; |
| 93 | + } |
| 94 | + |
| 95 | + return $values; |
| 96 | + } |
| 97 | +} |
0 commit comments