2015-09-21 12:49:02 +02:00
|
|
|
<?php
|
2016-09-09 09:00:05 +02:00
|
|
|
|
2017-06-16 06:07:09 +02:00
|
|
|
namespace Symbiote\GridFieldExtensions;
|
2016-11-29 18:20:15 +01:00
|
|
|
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
2018-01-25 23:53:40 +01:00
|
|
|
use SilverStripe\Forms\GridField\GridField;
|
2016-09-09 09:00:05 +02:00
|
|
|
use SilverStripe\ORM\DataList;
|
|
|
|
use SilverStripe\ORM\PaginatedList;
|
2018-01-25 23:53:40 +01:00
|
|
|
use SilverStripe\ORM\Search\SearchContext;
|
2016-09-09 09:00:05 +02:00
|
|
|
|
2015-09-21 12:49:02 +02:00
|
|
|
/**
|
|
|
|
* Used by {@link GridFieldAddExistingSearchButton} to provide the searching
|
|
|
|
* functionality.
|
|
|
|
*/
|
2016-12-21 03:34:58 +01:00
|
|
|
class GridFieldAddExistingSearchHandler extends RequestHandler
|
|
|
|
{
|
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'index',
|
|
|
|
'add',
|
|
|
|
'SearchForm'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GridField
|
|
|
|
*/
|
|
|
|
protected $grid;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var GridFieldAddExistingSearchButton
|
|
|
|
*/
|
|
|
|
protected $button;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SearchContext
|
|
|
|
*/
|
|
|
|
protected $context;
|
|
|
|
|
|
|
|
public function __construct($grid, $button)
|
|
|
|
{
|
|
|
|
$this->grid = $grid;
|
|
|
|
$this->button = $button;
|
|
|
|
$this->context = singleton($grid->getModelClass())->getDefaultSearchContext();
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
{
|
2018-01-25 23:53:40 +01:00
|
|
|
return $this->renderWith(__CLASS__);
|
2016-12-21 03:34:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add($request)
|
|
|
|
{
|
|
|
|
if (!$id = $request->postVar('id')) {
|
|
|
|
$this->httpError(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$list = $this->grid->getList();
|
|
|
|
$item = DataList::create($list->dataClass())->byID($id);
|
|
|
|
|
|
|
|
if (!$item) {
|
|
|
|
$this->httpError(400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$list->add($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Form
|
|
|
|
*/
|
|
|
|
public function SearchForm()
|
|
|
|
{
|
2018-01-25 23:53:40 +01:00
|
|
|
$form = Form::create(
|
2016-12-21 03:34:58 +01:00
|
|
|
$this,
|
2017-05-03 19:29:44 +02:00
|
|
|
'SearchForm',
|
2016-12-21 03:34:58 +01:00
|
|
|
$this->context->getFields(),
|
2018-01-25 23:53:40 +01:00
|
|
|
FieldList::create(
|
2016-12-21 03:34:58 +01:00
|
|
|
FormAction::create('doSearch', _t('GridFieldExtensions.SEARCH', 'Search'))
|
|
|
|
->setUseButtonTag(true)
|
2018-01-25 23:53:40 +01:00
|
|
|
->addExtraClass('btn btn-primary font-icon-search')
|
2016-12-21 03:34:58 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2018-01-25 23:53:40 +01:00
|
|
|
$form->addExtraClass('stacked add-existing-search-form form--no-dividers');
|
2016-12-21 03:34:58 +01:00
|
|
|
$form->setFormMethod('GET');
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function doSearch($data, $form)
|
|
|
|
{
|
|
|
|
$list = $this->context->getQuery($data, false, false, $this->getSearchList());
|
|
|
|
$list = $list->subtract($this->grid->getList());
|
|
|
|
$list = new PaginatedList($list, $this->request);
|
|
|
|
|
|
|
|
$data = $this->customise(array(
|
|
|
|
'SearchForm' => $form,
|
|
|
|
'Items' => $list
|
|
|
|
));
|
|
|
|
return $data->index();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Items()
|
|
|
|
{
|
|
|
|
$list = $this->getSearchList();
|
|
|
|
$list = $list->subtract($this->grid->getList());
|
|
|
|
$list = new PaginatedList($list, $this->request);
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function Link($action = null)
|
|
|
|
{
|
|
|
|
return Controller::join_links($this->grid->Link(), 'add-existing-search', $action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DataList
|
|
|
|
*/
|
|
|
|
protected function getSearchList()
|
|
|
|
{
|
|
|
|
return $this->button->getSearchList() ?: DataList::create($this->grid->getList()->dataClass());
|
|
|
|
}
|
2015-09-21 12:49:02 +02:00
|
|
|
}
|