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:
<?php
namespace Nethgui\Widget\Xhtml;
class ObjectsCollection extends \Nethgui\Widget\XhtmlWidget
{
protected function getJsWidgetTypes()
{
return array('Nethgui:objectscollection');
}
private function normalizePlaceholders($placeholders)
{
$sanitized = array();
foreach ($placeholders as $p => $f) {
if (is_numeric($p)) {
$p = $f;
}
$sanitized[$p] = $f;
}
return $sanitized;
}
protected function renderContent()
{
$name = $this->getAttribute('name');
$tag = $this->getAttribute('tag', 'div');
$cssClass = trim('ObjectsCollection ' . $this->getAttribute('class', ''));
$key = $this->getAttribute('key', FALSE);
$template = $this->getAttribute('template', FALSE);
$ifEmpty = $this->getAttribute('ifEmpty', FALSE);
$placeholders = $this->normalizePlaceholders($this->getAttribute('placeholders', array()));
$renderer = new ElementRenderer($this->view, $name, '${key}', $template);
$emptyRenderer = new ElementRenderer($this->view, $name, '${key}', $ifEmpty);
$content = '';
$values = $this->view[$name];
if (empty($values)) {
$content = $emptyRenderer->render();
} else {
foreach ($values as $defaultKey => $data) {
$placeHolderValues = array();
foreach ($placeholders as $p => $f) {
$placeHolderValues[sprintf('${%s}', $p)] = $data[$f];
}
$vR = new ElementRenderer($this->view, $name, $key ? $data[$key] : $defaultKey, $template);
$content .= strtr((String) $vR->copyFrom($data)->render(), $placeHolderValues);
}
}
return $this->openTag($tag, array(
'class' => $cssClass . ' ' . $this->getClientEventTarget(),
'id' => $this->view->getUniqueId($name),
'data-state' => json_encode(array('rendered' => ! empty($values), 'key' => $key, 'template' => $renderer->render(), 'ifEmpty' => $emptyRenderer->render(), 'placeholders' => $placeholders)),
)) . $content . $this->closeTag($tag);
}
}
class ElementRenderer extends \Nethgui\Renderer\Xhtml
{
public function copyFrom($data)
{
$this->view->copyFrom($data);
return $this;
}
public function __construct(\Nethgui\Renderer\Xhtml $renderer, $name, $key, $template)
{
parent::__construct($renderer->view, $renderer->getTemplateResolver(), $renderer->getDefaultFlags());
$module = $this->createModule($name, $key, $renderer->getModule()->getAttributesProvider());
$this->httpResponse = $renderer->httpResponse;
$this->staticFiles = $renderer->staticFiles;
$this->view = $renderer->view->spawnView($module)->setTemplate($template);
}
public function getClientEventTarget($name)
{
return $name;
}
private function createModule($name, $id, \Nethgui\Module\ModuleAttributesInterface $ap)
{
$n = new ElementModule($name, $ap);
$m = new ElementModule($id, $ap);
$n->setParent($this->view->getModule());
return $m->setParent($n);
}
}
class ElementModule implements \Nethgui\Module\ModuleInterface
{
private $identifier, $parent;
public function __construct($id, \Nethgui\Module\ModuleAttributesInterface $attributesProvider)
{
$this->identifier = $id;
$this->attributesProvider = $attributesProvider;
}
public function getAttributesProvider()
{
return $this->attributesProvider;
}
public function getIdentifier()
{
return $this->identifier;
}
public function getParent()
{
return $this->parent;
}
public function initialize()
{
}
public function isInitialized()
{
return TRUE;
}
public function setParent(\Nethgui\Module\ModuleInterface $parentModule)
{
$this->parent = $parentModule;
return $this;
}
}