Commit ab8e18eb authored by Avris's avatar Avris

v3.2 missing commit

parent 57394037
Pipeline #9612494 passed with stage
in 31 seconds
......@@ -9,7 +9,10 @@ use Avris\Micrus\Forms\FormObject;
class Choice extends Widget implements WhenNotSetInRequest
{
/** @var array */
protected $choices;
protected $choices = [];
/** @var array */
protected $objects = [];
public function __construct(Form $form, $name, array $options = [], $asserts = [])
{
......@@ -42,16 +45,29 @@ class Choice extends Widget implements WhenNotSetInRequest
protected function getChoices($options)
{
if (isset($options['objects'])) {
$choices = [];
foreach ($options['objects'] as $object) {
$id = FormObject::get($object, 'id');
$this->objects[$id] = $object;
$choices[$id] = (string) $object;
}
return $choices;
}
if (isset($options['model'])) {
$choices = [];
foreach ($this->fetchService('orm')->findAll($options['model']) as $object) {
$choices[FormObject::get($object, 'id')] = (string) $object;
}
return $choices;
}
if (isset($options['choiceTranslation'])) {
$prefix = $options['choiceTranslation'];
return array_map(function ($value) use ($prefix) {
return l($prefix . $value);
}, $options['choices']);
......@@ -69,12 +85,13 @@ class Choice extends Widget implements WhenNotSetInRequest
);
foreach ($choices as $key => $value) {
$selected = $multiple ? in_array($key, $widgetValue) : $key == $widgetValue;
$kv = $this->options->get('objects') ? $this->objects[$key] : $key;
$selected = $multiple ? in_array($kv, $widgetValue) : $kv == $widgetValue;
$out .= sprintf(
'<option value="%s" %s>%s</option>',
htmlentities($key),
$selected ? 'selected' : '',
$this->getNonExpandedValueTemplate($key, $value)
htmlentities($value)
);
}
$out .= '</select>';
......@@ -82,50 +99,31 @@ class Choice extends Widget implements WhenNotSetInRequest
return $out;
}
/**
* @param string $key
* @param string $value
* @return string
*/
protected function getNonExpandedValueTemplate($key, $value)
{
return htmlentities($value);
}
protected function getExpandedTemplate($choices, $multiple, $widgetValue)
{
$out = '';
foreach ($choices as $key => $value) {
$kv = $this->options->get('objects') ? $this->objects[$key] : $key;
$out .= $multiple
? sprintf(
'<div class="checkbox"><label>' .
'<input type="checkbox" name="{name}[]" value="%s" {attributes} %s><span>%s</span>' .
'</label></div>',
htmlentities($key),
in_array($key, $widgetValue) ? 'checked="checked"' : '',
$this->getExpandedValueTemplate($key, $value)
in_array($kv, $widgetValue) ? 'checked="checked"' : '',
htmlentities($value)
) : sprintf(
'<div class="radio"><label>' .
'<input type="radio" name="{name}" value="%s" {attributes} %s><span>%s</span>' .
'</label></div>',
htmlentities($key),
$key == $widgetValue ? 'checked="checked"' : '',
$this->getExpandedValueTemplate($key, $value)
$kv == $widgetValue ? 'checked="checked"' : '',
htmlentities($value)
);
}
return $out;
}
/**
* @param string $key
* @param string $value
* @return string
*/
protected function getExpandedValueTemplate($key, $value)
{
return htmlentities($value);
}
protected function getDefaultAssert()
{
return new Assert\Choice(
......@@ -140,7 +138,6 @@ class Choice extends Widget implements WhenNotSetInRequest
if (!$value) {
return $this->options->get('multiple') ? [] : null;
}
if ($this->options->get('multiple')) {
return $this->fetchService('orm')->findBy($model, 'id', $value);
}
......@@ -153,20 +150,18 @@ class Choice extends Widget implements WhenNotSetInRequest
return $this->options->get('multiple') ? [] : null;
}
$choices = $this->options->get('choices');
if ($this->options->get('multiple')) {
$objects = [];
foreach ($value as $item) {
if (isset($choices[$item])) {
$objects[$item] = $choices[$item];
if (isset($this->objects[$item])) {
$objects[$item] = $this->objects[$item];
}
}
return $objects;
}
return isset($choices[$value]) ? $choices[$value] : null;
return isset($this->objects[$value]) ? $this->objects[$value] : null;
}
return $value;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment