1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176:
<?php
namespace Nethgui\Widget\Xhtml;
class Selector extends \Nethgui\Widget\XhtmlWidget
{
protected function renderContent()
{
$name = $this->getAttribute('name');
$flags = $this->getAttribute('flags');
$value = $this->view[$name];
if ($value instanceof \Traversable) {
$value = iterator_to_array($value);
}
if (is_null($value)) {
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::SELECTOR_MULTIPLE) {
$value = array();
} else {
$value = '';
}
}
$choices = $this->getChoices($name, $dataSourceName);
$cssClass = 'Selector '
. ($flags & \Nethgui\Renderer\WidgetFactoryInterface::SELECTOR_MULTIPLE ? 'multiple ' : '')
. $this->getAttribute('class')
. ' '
. $this->getClientEventTarget()
. ' '
. $this->view->getClientEventTarget($dataSourceName);
;
$attributes = array('class' => $cssClass, 'id' => $this->view->getUniqueId($name));
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::SELECTOR_DROPDOWN) {
return $this->renderDropdown($value, $choices, $attributes);
} else {
return $this->renderWidgetList($value, $choices, $attributes);
}
}
private function renderDropdown($value, $choices, $attributes)
{
$name = $this->getAttribute('name');
$flags = $this->getAttribute('flags');
$label = $this->getAttribute('label', $this->getTranslateClosure($name . '_label'));
$flags = $this->applyDefaultLabelAlignment($flags, \Nethgui\Renderer\WidgetFactoryInterface::LABEL_ABOVE);
if (count($choices) == 0) {
$tagContent = '<option selected="selected" value=""/>';
} else {
$tagContent = $this->optGroups($value, $choices);
}
$content = $this->labeledControlTag($label, 'select', $name, $flags, '', $attributes, $tagContent);
return $content;
}
private function renderWidgetList($value, $choices, $attributes)
{
$name = $this->getAttribute('name');
$flags = $this->getAttribute('flags');
$hiddenWidget = new Hidden($this->view);
$hiddenWidget->setAttribute('flags', $flags)
->setAttribute('value', '')
->setAttribute('class', 'Hidden')
->setAttribute('name', $name);
$contentWidget = new Literal($this->view);
$contentWidget->setAttribute('data', $this->generateSelectorContentWidgetList($name, $value, $choices, $flags));
$panelWidget = new Panel($this->view);
$panelWidget
->setAttribute('class', $attributes['class'])
->setAttribute('name', $name)
->insert($hiddenWidget)
->insert($contentWidget);
if($this->getAttribute('flags') & \Nethgui\Renderer\WidgetFactoryInterface::LABEL_NONE) {
return $panelWidget->renderContent();
}
$fieldsetWidget = new Fieldset($this->view);
$fieldsetWidget->setAttribute('template', $this->getAttribute('label', $this->getTranslateClosure($name . '_label')))
->setAttribute('flags', $this->getAttribute('flags'));
if ($this->hasAttribute('icon-before')) {
$fieldsetWidget->setAttribute('icon-before', $this->getAttribute('icon-before'));
}
$fieldsetWidget->insert($panelWidget);
return $fieldsetWidget->renderContent();
}
private function generateSelectorContentWidgetList($name, $value, $choices, $flags)
{
$content = '';
if (count($choices) == 0) {
return '';
}
$content .= $this->openTag('ul');
foreach (array_values($choices) as $index => $choice) {
$content .= $this->openTag('li');
$choiceFlags = ($flags & ~(\Nethgui\Renderer\WidgetFactoryInterface::LABEL_LEFT | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_NONE)) | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_RIGHT;
if ($flags & \Nethgui\Renderer\WidgetFactoryInterface::SELECTOR_MULTIPLE) {
$choiceName = $name . '/' . $index;
if (in_array($choice[0], $value)) {
$choiceFlags |= \Nethgui\Renderer\WidgetFactoryInterface::STATE_CHECKED;
}
$attributes = array(
'type' => 'checkbox',
'value' => $choice[0],
);
} else {
$choiceName = $name;
if ($choice[0] == $value) {
$choiceFlags |= \Nethgui\Renderer\WidgetFactoryInterface::STATE_CHECKED;
}
$attributes = array(
'type' => 'radio',
'value' => $choice[0],
'id' => $this->view->getUniqueId($name . '/' . $index),
);
}
$choiceLabel = ( ! empty($choice[1]) ? $choice[1] : $choice[0]);
$content .= $this->labeledControlTag($choiceLabel, 'input', $choiceName, $choiceFlags, 'choice', $attributes);
$content .= $this->closeTag('li');
}
$content .= $this->closeTag('ul');
return $content;
}
}