silverstripe-framework/forms/gridfield/GridState.php

210 lines
3.8 KiB
PHP
Raw Normal View History

<?php
/**
2014-08-15 08:53:05 +02:00
* This class is a snapshot of the current status of a {@link GridField}.
*
2014-08-15 08:53:05 +02:00
* It's designed to be inserted into a Form as a HiddenField and passed through
* to actions such as the {@link GridField_FormAction}.
2014-08-15 08:53:05 +02:00
*
* @see GridField
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-gridfield
*/
class GridState extends HiddenField {
2014-08-15 08:53:05 +02:00
/**
* @var GridField
*/
protected $grid;
/**
* @var GridState_Data
*/
protected $data = null;
2014-08-15 08:53:05 +02:00
/**
*
* @param GridField $name
* @param string $data - json encoded string
*/
public function __construct($grid, $value = null) {
$this->grid = $grid;
if ($value) $this->setValue($value);
parent::__construct($grid->getName() . '[GridState]');
}
/**
* @param mixed $d
2014-08-15 08:53:05 +02:00
* @return object
*/
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
return $d;
}
/**
2014-08-15 08:53:05 +02:00
* @param mixed $value
*/
public function setValue($value) {
if (is_string($value)) {
$this->data = new GridState_Data(json_decode($value, true));
}
parent::setValue($value);
}
2014-08-15 08:53:05 +02:00
/**
* @return GridState_Data
*/
public function getData() {
if(!$this->data) {
$this->data = new GridState_Data();
}
return $this->data;
}
/**
2014-08-15 08:53:05 +02:00
* @return DataList
*/
public function getList() {
return $this->grid->getList();
}
/**
* Returns a json encoded string representation of this state.
*
2014-08-15 08:53:05 +02:00
* @return string
*/
public function Value() {
if(!$this->data) {
return json_encode(array());
}
return json_encode($this->data->toArray());
}
/**
* Returns a json encoded string representation of this state.
*
2014-08-15 08:53:05 +02:00
* @return string
*/
public function dataValue() {
return $this->Value();
}
/**
*
2014-08-15 08:53:05 +02:00
* @return string
*/
public function attrValue() {
return Convert::raw2att($this->Value());
}
/**
*
2014-08-15 08:53:05 +02:00
* @return string
*/
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
* errors.
*
* @see GridState
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-gridfield
*/
class GridState_Data {
/**
* @var array
*/
protected $data;
2014-08-15 08:53:05 +02:00
public function __construct($data = array()) {
$this->data = $data;
}
2014-08-15 08:53:05 +02:00
public function __get($name) {
return $this->getData($name, new GridState_Data());
}
2014-08-15 08:53:05 +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
/**
* Retrieve the value for the given key
2014-08-15 08:53:05 +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) {
if(!isset($this->data[$name])) {
$this->data[$name] = $default;
} else if(is_array($this->data[$name])) {
$this->data[$name] = new GridState_Data($this->data[$name]);
}
return $this->data[$name];
}
public function __set($name, $value) {
$this->data[$name] = $value;
}
2014-08-15 08:53:05 +02:00
public function __isset($name) {
return isset($this->data[$name]);
}
2014-08-15 08:53:05 +02:00
public function __unset($name) {
unset($this->data[$name]);
}
public function __toString() {
if(!$this->data) {
return "";
}
2014-08-15 08:53:05 +02:00
return json_encode($this->toArray());
}
public function toArray() {
$output = array();
2014-08-15 08:53:05 +02:00
foreach($this->data as $k => $v) {
$output[$k] = (is_object($v) && method_exists($v, 'toArray')) ? $v->toArray() : $v;
}
return $output;
}
}
/**
* @see GridState
2014-08-15 08:53:05 +02:00
*
* @package forms
* @subpackage fields-gridfield
*/
class GridState_Component implements GridField_HTMLProvider {
2014-08-15 08:53:05 +02:00
public function getHTMLFragments($gridField) {
return array(
'before' => $gridField->getState(false)->Field()
);
}
}