2011-12-06 01:56:24 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* This class is a snapshot of the current status of a {@link GridField}.
|
2013-05-20 12:18:07 +02:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* It's designed to be inserted into a Form as a HiddenField and passed through
|
2013-05-20 12:18:07 +02:00
|
|
|
* to actions such as the {@link GridField_FormAction}.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-12-06 01:56:24 +01:00
|
|
|
* @see GridField
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
class GridState extends HiddenField {
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
/**
|
|
|
|
* @var GridField
|
2013-03-04 22:29:27 +01:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
protected $grid;
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* @var GridState_Data
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
2013-03-04 22:29:27 +01:00
|
|
|
protected $data = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $name
|
|
|
|
* @param string $data - json encoded string
|
|
|
|
*/
|
|
|
|
public function __construct($grid, $value = null) {
|
|
|
|
$this->grid = $grid;
|
|
|
|
|
|
|
|
if ($value) $this->setValue($value);
|
|
|
|
|
2012-02-09 17:17:39 +01:00
|
|
|
parent::__construct($grid->getName() . '[GridState]');
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* @param mixed $d
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return object
|
2013-03-04 22:29:27 +01:00
|
|
|
*/
|
|
|
|
public static function array_to_object($d) {
|
|
|
|
if(is_array($d)) {
|
|
|
|
return (object) array_map(array('GridState', 'array_to_object'), $d);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
return $d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param mixed $value
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function setValue($value) {
|
|
|
|
if (is_string($value)) {
|
2013-03-04 22:29:27 +01:00
|
|
|
$this->data = new GridState_Data(json_decode($value, true));
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
parent::setValue($value);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
/**
|
2013-10-10 23:08:59 +02:00
|
|
|
* @return GridState_Data
|
2013-03-04 22:29:27 +01:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getData() {
|
2013-03-04 22:29:27 +01:00
|
|
|
if(!$this->data) {
|
|
|
|
$this->data = new GridState_Data();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->data;
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return DataList
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function getList() {
|
|
|
|
return $this->grid->getList();
|
|
|
|
}
|
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
/**
|
|
|
|
* Returns a json encoded string representation of this state.
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2013-03-04 22:29:27 +01:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
public function Value() {
|
2013-03-04 22:29:27 +01:00
|
|
|
if(!$this->data) {
|
2012-01-09 06:21:14 +01:00
|
|
|
return json_encode(array());
|
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
|
|
|
return json_encode($this->data->toArray());
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-03-04 22:29:27 +01:00
|
|
|
* Returns a json encoded string representation of this state.
|
2011-12-06 01:56:24 +01:00
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function dataValue() {
|
|
|
|
return $this->Value();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function attrValue() {
|
|
|
|
return Convert::raw2att($this->Value());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @return string
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
public function __toString() {
|
|
|
|
return $this->Value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-15 08:53:05 +02:00
|
|
|
* Simple set of data, similar to stdClass, but without the notice-level
|
2013-05-20 12:18:07 +02:00
|
|
|
* errors.
|
2013-03-04 22:29:27 +01:00
|
|
|
*
|
|
|
|
* @see GridState
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2011-12-06 01:56:24 +01:00
|
|
|
*/
|
|
|
|
class GridState_Data {
|
2013-03-04 22:29:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
protected $data;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($data = array()) {
|
2011-12-06 01:56:24 +01:00
|
|
|
$this->data = $data;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __get($name) {
|
2013-10-10 23:08:59 +02:00
|
|
|
return $this->getData($name, new GridState_Data());
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-10 23:08:59 +02:00
|
|
|
public function __call($name, $arguments) {
|
|
|
|
// Assume first parameter is default value
|
|
|
|
$default = empty($arguments) ? new GridState_Data() : $arguments[0];
|
|
|
|
return $this->getData($name, $default);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-10-10 23:08:59 +02:00
|
|
|
/**
|
|
|
|
* Retrieve the value for the given key
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-10-10 23:08:59 +02:00
|
|
|
* @param string $name The name of the value to retrieve
|
|
|
|
* @param mixed $default Default value to assign if not set
|
|
|
|
* @return mixed The value associated with this key, or the value specified by $default if not set
|
|
|
|
*/
|
|
|
|
public function getData($name, $default = null) {
|
2014-02-03 22:18:26 +01:00
|
|
|
if(!isset($this->data[$name])) {
|
2013-10-10 23:08:59 +02:00
|
|
|
$this->data[$name] = $default;
|
2013-03-04 22:29:27 +01:00
|
|
|
} else if(is_array($this->data[$name])) {
|
|
|
|
$this->data[$name] = new GridState_Data($this->data[$name]);
|
|
|
|
}
|
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
return $this->data[$name];
|
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __set($name, $value) {
|
2011-12-06 01:56:24 +01:00
|
|
|
$this->data[$name] = $value;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __isset($name) {
|
2011-12-06 01:56:24 +01:00
|
|
|
return isset($this->data[$name]);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-02-03 22:18:26 +01:00
|
|
|
public function __unset($name) {
|
|
|
|
unset($this->data[$name]);
|
|
|
|
}
|
2011-12-06 01:56:24 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __toString() {
|
2013-03-04 22:29:27 +01:00
|
|
|
if(!$this->data) {
|
|
|
|
return "";
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
return json_encode($this->toArray());
|
2011-12-06 01:56:24 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function toArray() {
|
2011-12-06 01:56:24 +01:00
|
|
|
$output = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
foreach($this->data as $k => $v) {
|
|
|
|
$output[$k] = (is_object($v) && method_exists($v, 'toArray')) ? $v->toArray() : $v;
|
|
|
|
}
|
2013-03-04 22:29:27 +01:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 22:29:27 +01:00
|
|
|
/**
|
|
|
|
* @see GridState
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-11-29 05:12:47 +01:00
|
|
|
* @package forms
|
2013-05-20 12:18:07 +02:00
|
|
|
* @subpackage fields-gridfield
|
2013-03-04 22:29:27 +01:00
|
|
|
*/
|
2011-12-06 01:56:24 +01:00
|
|
|
class GridState_Component implements GridField_HTMLProvider {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-12-06 01:56:24 +01:00
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
return array(
|
|
|
|
'before' => $gridField->getState(false)->Field()
|
|
|
|
);
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|