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:
<?php
namespace Nethgui\Controller\Table;
class Read extends AbstractAction
{
private $columns;
public function __construct($identifier = NULL)
{
parent::__construct('read');
}
public function setColumns($columns)
{
foreach ($columns as $columnInfo) {
if ( ! is_array($columnInfo)) {
$columnName = $columnInfo;
$columnInfo = array('name' => strval($columnName), 'formatter' => 'default');
$methodName = 'prepareViewForColumn' . ucfirst($columnName);
if($columnName == 'Actions') {
$columnInfo['formatter'] = 'fmtButtonset';
} elseif (method_exists($this->getParent(), $methodName) || method_exists($this, $methodName)) {
$columnInfo['formatter'] = 'fmtRawHtml';
}
}
$this->columns[] = $columnInfo;
}
return $this;
}
public function prepareView(\Nethgui\View\ViewInterface $view)
{
parent::prepareView($view);
$view->setTemplate('Nethgui\Template\Table\Read');
$view['rows'] = $this->prepareRows($view);
if ($view->getTargetFormat() !== $view::TARGET_XHTML) {
return;
}
$view['columns'] = $this->columns;
$view['tableClass'] = sprintf('%s %s', count($view['rows']) > 10 ? 'large-dataTable' : 'small-dataTable', $view->getClientEventTarget('rows'));
$view['tableId'] = $view->getUniqueId();
$view['tableTitle'] = $view->getTranslator()->translate($this->getParent(), $this->getParent()->getAttributesProvider()->getTitle());
$view['TableActions'] = $view->spawnView($this->getParent());
$view['TableActions']->setTemplate(array($this, 'renderTableActions'));
}
public function renderTableActions(\Nethgui\Renderer\Xhtml $view)
{
$tableActions = $view->getModule()->getTableActions();
$buttonList = $view->elementList()
->setAttribute('class', 'Buttonlist')
->setAttribute('wrap', 'div/');
foreach ($tableActions as $tableAction) {
$actionId = $tableAction->getIdentifier();
if ($tableAction instanceof Help) {
$button = $view->button('Help', \Nethgui\Renderer\WidgetFactoryInterface::BUTTON_HELP);
} else {
$button = $view
->button($actionId, \Nethgui\Renderer\WidgetFactoryInterface::BUTTON_LINK)
->setAttribute('value', $view->getModuleUrl($actionId));
}
$buttonList->insert($button);
}
return $buttonList;
}
private function prepareRows(\Nethgui\View\ViewInterface $view)
{
$rows = new \ArrayObject();
if ( ! $this->hasAdapter()) {
throw new \UnexpectedValueException(sprintf('%s: the table adapter has not been set.', __CLASS__), 1326474696);
}
foreach ($this->getAdapter() as $key => $values) {
$row = new \ArrayObject();
$rowMetadata = new \ArrayObject(array('rowCssClass' => '', 'columns' => array()));
$row[] = $rowMetadata;
foreach ($this->columns as $columnIndex => $columnInfo) {
$row[] = $this->prepareColumn($view, $columnIndex, $columnInfo['name'], $key, $values, $rowMetadata);
}
$rows[] = $row;
}
return $rows;
}
private function prepareColumn(\Nethgui\View\ViewInterface $view, $columnIndex, $column, $key, $values, &$rowMetadata)
{
$methodName = 'prepareViewForColumn' . ucfirst($column);
if (method_exists($this->getParent(), $methodName)) {
$columnValue = $this->getParent()->$methodName($this, $view, $key, $values, $rowMetadata);
} elseif (method_exists($this, $methodName)) {
$columnValue = $this->$methodName($view, $key, $values, $rowMetadata);
} else {
$columnValue = isset($values[$column]) ? $values[$column] : NULL;
}
return $columnValue;
}
public function prepareViewForColumnKey(\Nethgui\View\ViewInterface $view, $key, $values, &$rowMetadata)
{
return htmlspecialchars($key);
}
public function prepareViewForColumnActions(\Nethgui\View\ViewInterface $view, $key, $values, &$rowMetadata)
{
$cellView = $view->spawnView($this->getParent());
$cellView->setTemplate(array($this, 'renderColumnActions'));
foreach ($this->getParent()->getRowActions() as $action) {
$actionId = $action->getIdentifier();
$actionInfo = array();
$actionInfo[] = $cellView->translate($actionId . '_label');
$actionInfo[] = $cellView->getModuleUrl(sprintf('%s/%s', $action->getIdentifier(), $key));
$cellView[$actionId] = $actionInfo;
}
return $cellView;
}
public function renderColumnActions(\Nethgui\Renderer\Xhtml $view)
{
$buttonList = $view->buttonList(\Nethgui\Renderer\WidgetFactoryInterface::BUTTONSET)
->setAttribute('maxElements', 1);
foreach ($view as $actionId => $actionInfo) {
$button = $view
->button($actionId, \Nethgui\Renderer\WidgetFactoryInterface::BUTTON_LINK)
->setAttribute('value', $actionInfo[1]);
$buttonList->insert($button);
}
return $buttonList;
}
public function nextPath()
{
return $this->getRequest()->hasArgument('deferred') ? FALSE : parent::nextPath();
}
}