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:
<?php
namespace Nethgui\Controller\Table;
/*
* Copyright (C) 2012 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/>.
*/
/**
* Table action that works on a database record identified by a key
*
* Parameters are decleared in bind(). Clients
* must provide a parameter schema, before bind() is called. See setSchema().
*
* @api
* @author Davide Principi <davide.principi@nethesis.it>
* @since 1.0
*/
abstract class RowAbstractAction extends \Nethgui\Controller\Table\AbstractAction
{
const KEY = 1325671618;
const FIELD = 1325671619;
/**
*
* @var array
*/
private $parameterSchema = array();
/**
* The name of the key parameter to identify the table adapter record
* @var string
*/
private $key;
/**
* Values passed into the view in GET/create
* @var array
*/
private $defaultValues = array();
/**
*
* @var \Nethgui\Adapter\AdapterInterface
*/
private $originalAdapter;
/**
*
* @api
* @return string The key parameter name
*/
public function getKey()
{
return $this->key;
}
/**
* Receive the table adapter and convert it into a RecordAdapter
*
* Invoked by the parent module (i.e. TableController)
*
* @see \Nethgui\Controller\TableController
* @api
* @param \Nethgui\Adapter\AdapterInterface $adapter
* @return \Nethgui\Controller\Table\RowAbstractAction
*/
public function setAdapter(\Nethgui\Adapter\AdapterInterface $adapter)
{
if ( ! $adapter instanceof \Nethgui\Adapter\RecordAdapter) {
$this->originalAdapter = $adapter;
$adapter = new \Nethgui\Adapter\RecordAdapter($this->originalAdapter);
}
parent::setAdapter($adapter);
return $this;
}
public function bind(\Nethgui\Controller\RequestInterface $request)
{
if ( ! $this->hasAdapter()) {
throw new \LogicException(sprintf('%s: in %s you must invoke setAdapter() before bind().', __CLASS__, get_class($this)), 1325673694);
}
if (is_null($this->getKey())) {
throw new \LogicException(sprintf('%s: unknown key field name.', get_class($this)), 1325673717);
}
foreach ($this->getSchema() as $parameterDeclaration) {
$parameterName = array_shift($parameterDeclaration);
$validator = array_shift($parameterDeclaration);
$valueProvider = array_shift($parameterDeclaration);
if ($valueProvider === self::KEY) {
$valueProvider = new \Nethgui\Adapter\RecordKeyAdapter($this->getAdapter());
} elseif ($valueProvider === self::FIELD) {
$prop = array_shift($parameterDeclaration);
$separator = array_shift($parameterDeclaration);
// Null prop name falls back into parameterName:
if (is_null($prop)) {
$prop = $parameterName;
}
$valueProvider = array($this->getAdapter(), $prop, NULL, $separator);
}
$this->declareParameter($parameterName, $validator, $valueProvider);
}
parent::bind($request);
if ( ! $request->isMutation()
&& ! $this->getAdapter()->getKeyValue()) {
// initialize default parameter values
foreach ($this->defaultValues as $paramName => $paramValue) {
$this->parameters[$paramName] = $paramValue;
}
}
}
/**
* Set the mapping between the view parameters and the underlying datasource
*
* The array is a list of array of arguments to declareParameter(). A
* special processing is added to the third parameter, $valueProvider:
*
* - the KEY binds the parameter to the record key
*
* - the FIELD const binds the parameter with the corresponding prop. If
* the parameter name differs from the prop name add a 4th, corresponding
* to the prop name.
*
* @api
* @param array $parameterSchema
* @return \Nethgui\Controller\Table\RowAbstractAction The object itself
* @throws \LogicException
*/
public function setSchema($parameterSchema)
{
$this->parameterSchema = array();
foreach ($parameterSchema as $parameterDeclaration) {
if (isset($parameterDeclaration[0], $parameterDeclaration[2]) && $parameterDeclaration[2] === self::KEY) {
$this->key = $parameterDeclaration[0];
$this->parameterSchema = $parameterSchema;
return $this;
}
}
throw new \LogicException(sprintf('%s: invalid schema. You must declare a KEY field.', __CLASS__), 1325671156);
}
/**
* Get the declared parameter schema
*
* @api
* @see setSchema()
* @return array
*/
public function getSchema()
{
return $this->parameterSchema;
}
/**
* Set a parameter default value
*
* The given value is assigned to the parameter if the request is a QUERY
* and the record adapter is missing the identifier (key) value
*
* @param string $parameterName
* @param string $value
* @return \Nethgui\Controller\Table\RowAbstractAction
*/
public function setDefaultValue($parameterName, $value)
{
$this->defaultValues[$parameterName] = $value;
return $this;
}
protected function saveParameters()
{
$save1 = parent::saveParameters();
if(isset($this->originalAdapter)) {
$save2 = $this->originalAdapter->save();
} else {
$save2 = FALSE;
}
return $save1 || $save2;
}
}