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: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258:
<?php
namespace Nethgui\Controller;
/*
* Copyright (C) 2011 Nethesis S.r.l.
*
* This script is part of NethServer.
*
* NethServer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NethServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NethServer. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A Controller for handling a generic table CRUD scenario, and any other
* action defined on a table.
*
* - Tracks the actions involving a row
* - Tracks the actions involving the whole table
*
* @see Table\Modify
* @see Table\Read
* @author Davide Principi <davide.principi@nethesis.it>
* @since 1.0
* @api
*/
class TableController extends \Nethgui\Controller\CompositeController implements \Nethgui\Adapter\AdapterAggregateInterface
{
/**
*
* @var Table\Read
*/
private $readAction = NULL;
/**
* @var array
*/
private $rowActions = array();
/**
* @var array
*/
private $tableActions = array();
/**
*
* @var \Nethgui\Adapter\AdapterInterface
*/
private $tableAdapter = NULL;
/**
* Set the adapter object that gives access to the database table
*
* @param \Nethgui\Adapter\AdapterInterface $tableAdapter
* @return TableController
*/
protected function setTableAdapter(\Nethgui\Adapter\AdapterInterface $tableAdapter)
{
$this->tableAdapter = $tableAdapter;
return $this;
}
public function getAdapter()
{
return $this->tableAdapter;
}
public function hasAdapter()
{
return $this->tableAdapter instanceof \Nethgui\Adapter\AdapterInterface;
}
/**
*
* @param array $columns
* @return \Nethgui\Controller\TableController
*/
protected function setColumns(array $columns)
{
$this->getReadAction()->setColumns($columns);
return $this;
}
/**
* @return \Nethgui\Controller\Table\AbstractAction
*/
protected function getReadAction()
{
if($this->readAction === NULL) {
$this->setReadAction(new Table\Read('read'));
}
return $this->readAction;
}
/**
*
* @param \Nethgui\Controller\Table\AbstractAction $action
* @return \Nethgui\Controller\TableController
* @throws \LogicException
*/
protected function setReadAction(Table\AbstractAction $action)
{
if($this->readAction !== NULL) {
throw new \LogicException(sprintf("%s: the read action has been already set. Call this method before any getReadAction() or setColumns()", __CLASS__), 1354266668);
}
$this->readAction = $action;
$this->addChild($this->readAction);
return $this;
}
public function initialize()
{
if ( ! $this->hasAdapter()) {
throw new \LogicException(sprintf('%s: you must call setTableAdapter() before %s::initialize()', get_class($this), __CLASS__), 1325610869);
}
// propagate the adapter to every that is missing it:
foreach ($this->getChildren() as $childAction) {
if ($childAction instanceof \Nethgui\Controller\Table\AbstractAction
&& ! $childAction->hasAdapter()) {
$childAction->setAdapter($this->getAdapter());
}
}
/**
* Calling the parent method at this point ensures that the table
* adapter has been set BEFORE the child initialization
*/
parent::initialize();
}
/**
* Add $childAction to the TableController, and propagate the table adapter,
* if it has not been done yet.
*
* @param \Nethgui\Module\ModuleInterface $childAction
*/
public function addChild(\Nethgui\Module\ModuleInterface $childAction)
{
if ($childAction instanceof \Nethgui\Controller\Table\AbstractAction) {
if ($childAction instanceof \Nethgui\Controller\Table\AbstractAction
&& ! $childAction->hasAdapter()) {
$childAction->setAdapter($this->getAdapter());
}
}
return parent::addChild($childAction);
}
/**
* A row action is executed in a row context (i.e. row updating, deletion...)
*
* @see getRowActions()
* @param \Nethgui\Controller\Table\AbstractAction $action
* @return \Nethgui\Controller\TableController
*/
public function addRowAction(\Nethgui\Controller\Table\AbstractAction $action)
{
$this->rowActions[] = $action;
$this->addChild($action);
return $this;
}
/**
* Enable $action to be expanded by instantiating all the classes
* under the given $path.
*
* @param \Nethgui\Controller\Table\AbstractAction $action
* @param string $path
* @return \Nethgui\Controller\TableController
*/
public function addRowActionPluggable(\Nethgui\Controller\Table\AbstractAction $action, $path = 'Plugin')
{
return $this->addRowAction(new \Nethgui\Controller\Table\PluggableAction($action, $path));
}
/**
* Actions for a single row of the table
* @return array
*/
public function getRowActions()
{
return $this->rowActions;
}
/**
* A table action involves the whole table (i.e. create a new row,
* print the table...)
* @see getTableActions()
* @return TableController
*/
public function addTableAction(\Nethgui\Module\ModuleInterface $a)
{
$this->tableActions[] = $a;
$this->addChild($a);
return $this;
}
/**
* Enable $action to be expanded by instantiating all the classes
* under the given $path.
*
* @see addRowActionPluggable()
* @see addTableAction()
* @param \Nethgui\Controller\Table\AbstractAction $action
* @param string $path
* @return \Nethgui\Controller\TableController
*/
public function addTableActionPluggable(\Nethgui\Controller\Table\AbstractAction $action, $path = 'Plugin')
{
return $this->addTableAction(new \Nethgui\Controller\Table\PluggableAction($action, $path));
}
/**
* Actions for the whole table
* @return array
*/
public function getTableActions()
{
return $this->tableActions;
}
public function renderIndex(\Nethgui\Renderer\Xhtml $renderer)
{
$this->sortChildren(function(\Nethgui\Module\ModuleInterface $a, \Nethgui\Module\ModuleInterface $b) {
if ($a->getIdentifier() === 'read') {
return -1;
} elseif ($b->getIdentifier() === 'read') {
return 1;
}
return 0;
});
return parent::renderIndex($renderer);
}
/**
*
* @param \Nethgui\Module\ModuleInterface $currentAction The current action
* @param array $changes changed parameters list
* @param array $parameters Parameters for $currentAction
*/
public function onParametersSaved(\Nethgui\Module\ModuleInterface $currentAction, $changes, $parameters)
{
// NOOP;
}
}