mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
62 lines
1.4 KiB
PHP
62 lines
1.4 KiB
PHP
<?php namespace Backend\Traits;
|
|
|
|
use Str;
|
|
use File;
|
|
use Lang;
|
|
use Input;
|
|
use Block;
|
|
use System\Classes\SystemException;
|
|
|
|
/**
|
|
* Selectable Widget Trait
|
|
* Adds item selection features to back-end widgets
|
|
*
|
|
* @package october\backend
|
|
* @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 = Input::get($this->selectionInputName, []);
|
|
$currentSelection = $this->getSelectedItems();
|
|
|
|
$this->putSession('selected', array_merge($currentSelection, $items));
|
|
}
|
|
|
|
protected function resetSelection()
|
|
{
|
|
$this->putSession('selected', []);
|
|
}
|
|
|
|
protected function isItemSelected($itemId)
|
|
{
|
|
$selectedItems = $this->getSelectedItems();
|
|
if (!is_array($selectedItems) || !isset($selectedItems[$itemId]))
|
|
return false;
|
|
|
|
return $selectedItems[$itemId];
|
|
}
|
|
} |