NEW Initial SessionGridFieldStateManager implementation, for managing

GridField states in session.
This commit is contained in:
Niklas Forsdahl 2024-06-14 11:50:59 +03:00
parent dcace43183
commit 4714a83333
5 changed files with 142 additions and 5 deletions

View File

@ -454,6 +454,13 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
$gridState = $this->gridField->getState(false); $gridState = $this->gridField->getState(false);
$actions->push(HiddenField::create($manager->getStateKey($this->gridField), null, $gridState)); $actions->push(HiddenField::create($manager->getStateKey($this->gridField), null, $gridState));
if ($manager instanceof GridFieldStateStoreInterface) {
$stateRequestVar = $manager->getStateRequestVar();
$stateValue = $this->getRequest()->requestVar($stateRequestVar);
if ($stateValue) {
$actions->push(HiddenField::create($stateRequestVar, '', $stateValue));
}
}
$actions->push($this->getRightGroupField()); $actions->push($this->getRightGroupField());
} else { // adding new record } else { // adding new record

View File

@ -0,0 +1,10 @@
<?php
namespace SilverStripe\Forms\GridField;
interface GridFieldStateStoreInterface
{
public function storeState(GridField $gridField, $value = null);
public function getStateRequestVar(): string;
}

View File

@ -85,7 +85,7 @@ class GridState extends HiddenField
public function getData() public function getData()
{ {
if (!$this->data) { if (!$this->data) {
$this->data = new GridState_Data(); $this->data = new GridState_Data([], $this);
} }
return $this->data; return $this->data;
@ -99,6 +99,11 @@ class GridState extends HiddenField
return $this->grid->getList(); return $this->grid->getList();
} }
public function getGridField(): GridField
{
return $this->grid;
}
/** /**
* Returns a json encoded string representation of this state. * Returns a json encoded string representation of this state.
* *

View File

@ -10,29 +10,33 @@ namespace SilverStripe\Forms\GridField;
*/ */
class GridState_Data class GridState_Data
{ {
use GridFieldStateAware;
/** /**
* @var array * @var array
*/ */
protected $data; protected $data;
protected GridState $state;
protected $defaults = []; protected $defaults = [];
public function __construct($data = []) public function __construct($data = [], GridState $state = null)
{ {
$this->data = $data; $this->data = $data;
$this->state = $state;
} }
public function __get($name) public function __get($name)
{ {
return $this->getData($name, new GridState_Data()); return $this->getData($name, new GridState_Data([], $this->state));
} }
public function __call($name, $arguments) public function __call($name, $arguments)
{ {
// Assume first parameter is default value // Assume first parameter is default value
if (empty($arguments)) { if (empty($arguments)) {
$default = new GridState_Data(); $default = new GridState_Data([], $this->state);
} else { } else {
$default = $arguments[0]; $default = $arguments[0];
} }
@ -72,16 +76,25 @@ class GridState_Data
$this->data[$name] = $default; $this->data[$name] = $default;
} else { } else {
if (is_array($this->data[$name])) { if (is_array($this->data[$name])) {
$this->data[$name] = new GridState_Data($this->data[$name]); $this->data[$name] = new GridState_Data($this->data[$name], $this->state);
} }
} }
return $this->data[$name]; return $this->data[$name];
} }
public function storeData()
{
$stateManager = $this->getStateManager();
if ($stateManager instanceof GridFieldStateStoreInterface) {
$stateManager->storeState($this->state->getGridField(), $this->state->Value());
}
}
public function __set($name, $value) public function __set($name, $value)
{ {
$this->data[$name] = $value; $this->data[$name] = $value;
$this->storeData();
} }
public function __isset($name) public function __isset($name)
@ -92,6 +105,7 @@ class GridState_Data
public function __unset($name) public function __unset($name)
{ {
unset($this->data[$name]); unset($this->data[$name]);
$this->storeData();
} }
public function __toString() public function __toString()

View File

@ -0,0 +1,101 @@
<?php
namespace SilverStripe\Forms\GridField;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
/**
* Creates a unique key for managing GridField states in user Session, for both storage and retrieval.
* Only stores states and generates a session key if a state is requested to be stored
* (i.e. the state is changed from the default).
* If a session state key is present in the request, it will always be used instead of generating a new one.
*/
class SessionGridFieldStateManager implements GridFieldStateManagerInterface, GridFieldStateStoreInterface
{
protected static $state_ids = [];
protected function getStateID(GridField $gridField, $create = false): ?string
{
$requestVar = $this->getStateRequestVar();
$sessionStateID = $gridField->getForm()->getRequestHandler()->getRequest()->requestVar($requestVar);
if (!$sessionStateID) {
$sessionStateID = Controller::curr()->getRequest()->requestVar($requestVar);
}
if ($sessionStateID) {
return $sessionStateID;
}
$stateKey = $this->getStateKey($gridField);
if (isset(self::$state_ids[$stateKey])) {
$sessionStateID = self::$state_ids[$stateKey];
} elseif ($create) {
$sessionStateID = substr(md5(time()), 0, 8);
// we don't want session state id to be strictly numeric, since this is used as a session key,
// and session keys in php has to be usable as variable names
if (is_numeric($sessionStateID)) {
$sessionStateID .= 'a';
}
self::$state_ids[$stateKey] = $sessionStateID;
}
return $sessionStateID;
}
public function storeState(GridField $gridField, $value = null)
{
$sessionStateID = $this->getStateID($gridField, true);
$sessionState = Controller::curr()->getRequest()->getSession()->get($sessionStateID);
if (!$sessionState) {
$sessionState = [];
}
$stateKey = $this->getStateKey($gridField);
$sessionState[$stateKey] = $value ?? $gridField->getState(false)->Value();
Controller::curr()->getRequest()->getSession()->set($sessionStateID, $sessionState);
}
public function getStateRequestVar(): string
{
return 'gridSessionState';
}
/**
* @param GridField $gridField
* @return string
*/
public function getStateKey(GridField $gridField): string
{
$record = $gridField->getForm()->getRecord();
return $gridField->getName() . '-' . ($record ? $record->ID : 0);
}
/**
* @param GridField $gridField
* @param string $url
* @return string
*/
public function addStateToURL(GridField $gridField, string $url): string
{
$sessionStateID = $this->getStateID($gridField);
if ($sessionStateID) {
return Controller::join_links($url, '?' . $this->getStateRequestVar() . '=' . $sessionStateID);
}
return $url;
}
/**
* @param GridField $gridField
* @param HTTPRequest $request
* @return string|null
*/
public function getStateFromRequest(GridField $gridField, HTTPRequest $request): ?string
{
$gridSessionStateID = $request->requestVar($this->getStateRequestVar());
if ($gridSessionStateID) {
$sessionState = $request->getSession()->get($gridSessionStateID);
$stateKey = $this->getStateKey($gridField);
if ($sessionState && isset($sessionState[$stateKey])) {
return $sessionState[$stateKey];
}
}
return null;
}
}