mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
70 lines
1.6 KiB
PHP
70 lines
1.6 KiB
PHP
<?php namespace Backend\Traits;
|
|
|
|
use Input;
|
|
|
|
/**
|
|
* Selectable Widget Trait
|
|
* Adds item selection features to back-end widgets
|
|
*
|
|
* @package winter\wn-backend-module
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
*/
|
|
|
|
trait SelectableWidget
|
|
{
|
|
protected $selectedItemsCache = false;
|
|
|
|
protected $selectionInputName = 'object';
|
|
|
|
public function onSelect()
|
|
{
|
|
$this->extendSelection();
|
|
}
|
|
|
|
protected function getSelectedItems()
|
|
{
|
|
if ($this->selectedItemsCache !== false) {
|
|
return $this->selectedItemsCache;
|
|
}
|
|
|
|
$items = $this->getSession('selected', []);
|
|
if (!is_array($items)) {
|
|
return $this->selectedItemsCache = [];
|
|
}
|
|
|
|
return $this->selectedItemsCache = $items;
|
|
}
|
|
|
|
protected function extendSelection()
|
|
{
|
|
$items = (array) Input::get($this->selectionInputName, []);
|
|
$currentSelection = $this->getSelectedItems();
|
|
|
|
$this->putSession('selected', $currentSelection + $items);
|
|
}
|
|
|
|
protected function resetSelection()
|
|
{
|
|
$this->putSession('selected', []);
|
|
}
|
|
|
|
protected function removeSelection($itemId)
|
|
{
|
|
$currentSelection = $this->getSelectedItems();
|
|
|
|
unset($currentSelection[$itemId]);
|
|
$this->putSession('selected', $currentSelection);
|
|
$this->selectedItemsCache = $currentSelection;
|
|
}
|
|
|
|
protected function isItemSelected($itemId)
|
|
{
|
|
$selectedItems = $this->getSelectedItems();
|
|
if (!is_array($selectedItems) || !isset($selectedItems[$itemId])) {
|
|
return false;
|
|
}
|
|
|
|
return $selectedItems[$itemId];
|
|
}
|
|
}
|