2014-09-11 03:48:53 +00:00
|
|
|
<?php namespace Backend\Traits;
|
|
|
|
|
|
|
|
|
|
use Input;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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()
|
|
|
|
|
{
|
2014-10-10 22:07:30 +00:00
|
|
|
if ($this->selectedItemsCache !== false) {
|
2014-09-11 03:48:53 +00:00
|
|
|
return $this->selectedItemsCache;
|
2014-10-10 22:07:30 +00:00
|
|
|
}
|
2014-09-11 03:48:53 +00:00
|
|
|
|
|
|
|
|
$items = $this->getSession('selected', []);
|
2014-10-10 22:07:30 +00:00
|
|
|
if (!is_array($items)) {
|
2014-09-11 03:48:53 +00:00
|
|
|
return $this->selectedItemsCache = [];
|
2014-10-10 22:07:30 +00:00
|
|
|
}
|
2014-09-11 03:48:53 +00:00
|
|
|
|
|
|
|
|
return $this->selectedItemsCache = $items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function extendSelection()
|
|
|
|
|
{
|
2016-05-04 09:20:41 +00:00
|
|
|
$items = (array) Input::get($this->selectionInputName, []);
|
2014-09-11 03:48:53 +00:00
|
|
|
$currentSelection = $this->getSelectedItems();
|
|
|
|
|
|
2016-05-04 09:20:41 +00:00
|
|
|
$this->putSession('selected', $currentSelection + $items);
|
2014-09-11 03:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function resetSelection()
|
|
|
|
|
{
|
|
|
|
|
$this->putSession('selected', []);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-30 16:17:18 +00:00
|
|
|
protected function removeSelection($itemId)
|
|
|
|
|
{
|
|
|
|
|
$currentSelection = $this->getSelectedItems();
|
|
|
|
|
|
|
|
|
|
unset($currentSelection[$itemId]);
|
|
|
|
|
$this->putSession('selected', $currentSelection);
|
|
|
|
|
$this->selectedItemsCache = $currentSelection;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-11 03:48:53 +00:00
|
|
|
protected function isItemSelected($itemId)
|
|
|
|
|
{
|
|
|
|
|
$selectedItems = $this->getSelectedItems();
|
2014-10-10 22:07:30 +00:00
|
|
|
if (!is_array($selectedItems) || !isset($selectedItems[$itemId])) {
|
2014-09-11 03:48:53 +00:00
|
|
|
return false;
|
2014-10-10 22:07:30 +00:00
|
|
|
}
|
2014-09-11 03:48:53 +00:00
|
|
|
|
|
|
|
|
return $selectedItems[$itemId];
|
|
|
|
|
}
|
2014-10-10 22:07:30 +00:00
|
|
|
}
|