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:
<?php
namespace Nethgui\Widget\Xhtml;
class TextList extends \Nethgui\Widget\XhtmlWidget
{
protected function renderContent()
{
$name = $this->getAttribute('name');
$tags = explode('/', $this->getAttribute('tag', 'div/ul/li')) + array('', '', '');
$addClass = $this->getAttribute('class', $this->getTagClass($tags[0]));
$separator = $this->getAttribute('separator');
$values = $this->getAttribute('value', $this->view[$name]);
if ($values instanceof \Traversable) {
$values = iterator_to_array($values);
} elseif ( ! is_array($values)) {
$values = array();
}
$content = '';
$listTag = $this->getTagName($tags[1]);
$listClass = $this->getTagClass($tags[1]);
$elementTag = $this->getTagName($tags[2]);
$elementClass = $this->getTagClass($tags[2]);
$jsonOptions = json_encode(
array(
'separator' => $separator,
'wrap' => array(
'listTag' => $listTag,
'listClass' => $listClass ? $listClass : NULL,
'elementTag' => $elementTag,
'elementClass' => $elementClass ? $elementClass : NULL
)
)
);
if (count($values) > 0) {
$elements = array();
if ($elementTag) {
$elWrapBegin = $this->openTag($elementTag, array('class' => $elementClass));
$elWrapEnd = $this->closeTag($elementTag);
} else {
$elWrapBegin = '';
$elWrapEnd = '';
}
foreach ($values as $value) {
$elements[] = $elWrapBegin . htmlspecialchars($value) . $elWrapEnd;
}
$content = implode($separator ? $separator : '', $elements);
}
if ($content && $listTag) {
$content = $this->openTag($listTag, array('class' => $listClass)) .
$content .
$this->closeTag($listTag)
;
}
$widgetTag = $this->getTagName($tags[0]);
if ($widgetTag) {
$cssClass = implode(' ', array_filter(array('TextList', $addClass, $this->getClientEventTarget())));
$content = $this->openTag($widgetTag, array('class' => $cssClass, 'data-options' => $jsonOptions)) .
$content .
$this->closeTag($widgetTag)
;
}
return $content;
}
private function getTagName($tagDef)
{
$parts = explode('.', $tagDef);
if (isset($parts[0])) {
return $parts[0];
}
return FALSE;
}
private function getTagClass($tagDef, $defaultClass = FALSE)
{
$parts = explode('.', $tagDef);
if (isset($parts[1])) {
return $parts[1];
}
return $defaultClass;
}
}