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: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230:
<?php
namespace Nethgui\Widget\Xhtml;
class ObjectPicker extends \Nethgui\Widget\XhtmlWidget
{
private $metadata;
private $values = array();
private function initializeRendering()
{
$this->metadata = array(
'value' => $this->getAttribute('objectValue', 0),
'label' => $this->getAttribute('objectLabel', $this->getAttribute('objectValue', 0)),
'url' => $this->getAttribute('objectUrl', FALSE),
'listenToEvents' => array(),
'selector' => FALSE,
);
$name = $this->getAttribute('name', FALSE);
if ( ! empty($name)) {
$this->insert($this->view->checkBox($name, '', 0));
$this->metadata['selector'] = $name;
}
foreach ($this->getChildren() as $child) {
$childName = $child->getAttribute('name');
if ($this->view[$childName] instanceof \Traversable) {
$value = iterator_to_array($this->view[$childName]);
} elseif (is_array($this->view[$childName])) {
$value = $this->view[$childName];
} elseif (empty($this->view[$childName])) {
$value = array();
} else {
throw new \UnexpectedValueException(sprintf('%s: Invalid value type in view key `%s`: %s. Must be one of Traversable, array or an empty value.', get_class($this), $childName, var_export($this->view[$childName], TRUE)), 1322149475);
}
$this->values[$childName] = $value;
$this->metadata['listenToEvents'][] = $this->view->getClientEventTarget($childName);
}
}
public function insert(\Nethgui\Renderer\WidgetInterface $child)
{
if ( ! $child instanceof CheckBox) {
throw new \InvalidArgumentException(sprintf('%s: Unsupported widget class: %s', get_class($this), get_class($child)), 1322149476);
}
if ( ! $child->hasAttribute('uncheckedValue')) {
$child->setAttribute('uncheckedValue', FALSE);
}
$child->setAttribute('helpId', FALSE);
$childFlags = $child->getAttribute('flags', 0);
$childFlags &= ~ (\Nethgui\Renderer\WidgetFactoryInterface::LABEL_ABOVE | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_RIGHT | \Nethgui\Renderer\WidgetFactoryInterface::LABEL_LEFT);
$childFlags |= \Nethgui\Renderer\WidgetFactoryInterface::LABEL_RIGHT | \Nethgui\Renderer\WidgetFactoryInterface::STATE_DISABLED;
$child->setAttribute('flags', $childFlags);
return parent::insert($child);
}
protected function renderContent()
{
$this->initializeRendering();
$content = '';
$content .= $this->openTag('div', array('class' => 'ObjectPicker ' . implode(' ', $this->metadata['listenToEvents'])));
foreach ($this->getChildren() as $child) {
$content .= $this->selfClosingTag('input', array('type' => 'hidden', 'value' => '', 'name' => $this->getControlName($child->getAttribute('name'))));
}
$content .= $this->renderMeta();
$content .= $this->renderObjects();
$content .= $this->closeTag('div');
if ($this->hasAttribute('template')) {
$fieldsetWidget = new Fieldset($this->view);
$fieldsetWidget
->setAttribute('template', $this->getAttribute('template'))
->setAttribute('icon-before', $this->getAttribute('icon-before'))
->insert($this->view->literal($content))
;
return $fieldsetWidget;
}
return $content;
}
private function renderMeta()
{
$content = '';
$content .= $this->selfClosingTag('input', array('name' => $this->getControlName('meta'), 'type' => 'hidden', 'disabled' => 'disabled', 'value' => json_encode($this->metadata), 'class' => 'metadata'));
$content .= $this->openTag('div', array('class' => 'searchbox'));
$content .= $this->selfClosingTag('input', array('type' => 'text', 'class' => 'TextInput', 'disabled' => 'disabled', 'value' => '', 'placeholder' => $this->view->translate('Search...')));
$content .= ' ' . $this->openTag('button', array('type' => 'button', 'class' => 'Button custom', 'disabled' => 'disabled')) . htmlspecialchars($this->view->translate('Add')) . $this->closeTag('button');
$content .= $this->closeTag('div');
$content .= $this->openTag('div', array('class' => 'schema'));
$content .= $this->renderChildren();
$content .= $this->closeTag('div');
$flags = $this->getAttribute('flags', 0);
if ( ! ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_UNOBTRUSIVE)) {
$content = $this->escapeUnobstrusive($content);
}
return $content;
}
private function renderObjects()
{
$objects = $this->getAttribute('objects', array());
$flags = $this->getAttribute('flags', 0);
$attributes = array();
if (is_string($objects)) {
$attributes['class'] = 'Objects ' . $this->view->getClientEventTarget($objects);
$attributes['id'] = $this->view->getUniqueId($objects);
$objects = $this->view[$objects];
} else {
$attributes['class'] = 'Objects ' . $this->view->getClientEventTarget('Datasource');
$attributes['id'] = $this->view->getUniqueId('Datasource');
}
$content = '';
if ( ! ($flags & \Nethgui\Renderer\WidgetFactoryInterface::STATE_UNOBTRUSIVE)) {
if ((is_array($objects) || $objects instanceof \Countable) && count($objects) > 0) {
$content .= '<ul>';
foreach ($objects as $index => $object) {
$content .= '<li>';
$content .= $this->renderObjectWidget($index, $object);
$content .= '</li>';
}
$content .= '</ul>';
}
}
return $this->openTag('div', $attributes) . $content . $this->closeTag('div');
}
private function renderObjectWidget($index, $object)
{
$flags = $this->getAttribute('flags', 0);
$content = '';
$contentSelectionFragment = '';
$contentProperties = '';
if ($this->metadata['url'] && isset($object[$this->metadata['url']])) {
$content .= $this->openTag('a', array('class' => 'label', 'href' => $object[$this->metadata['url']])) . htmlspecialchars($object[$this->metadata['label']]) . $this->closeTag('a');
} else {
$content .= $this->openTag('span', array('class' => 'label')) . htmlspecialchars($object[$this->metadata['label']]) . $this->closeTag('span');
}
$content .= '<div class="checkboxset">';
foreach ($this->getChildren() as $child) {
$childClone = clone $child;
$childFlags = $child->getAttribute('flags', 0);
$childFlags &= ~\Nethgui\Renderer\WidgetFactoryInterface::STATE_DISABLED;
if (in_array($object[$this->metadata['value']], $this->values[$child->getAttribute('name')])) {
$childFlags |= \Nethgui\Renderer\WidgetFactoryInterface::STATE_CHECKED;
} else {
$childFlags &= ~\Nethgui\Renderer\WidgetFactoryInterface::STATE_CHECKED;
}
$childClone->setAttribute('flags', $childFlags);
$childClone->setAttribute('name', $child->getAttribute('name') . '/' . $index);
$childClone->setAttribute('label', $child->getAttribute('label', $this->getTranslateClosure($child->getAttribute('name') . '_label')));
$childClone->setAttribute('value', $object[$this->metadata['value']]);
$content .= $childClone->renderContent();
}
$content .= '</div>';
return $content;
}
}