GridFieldBulkEditingTools/bulkManager/code/GridFieldBulkActionHandler.php

139 lines
3.5 KiB
PHP
Raw Normal View History

<?php
/**
2014-05-04 16:12:05 +02:00
* Base class to extend for all custom bulk action handlers
* Gives access to the GridField, Component and Controller
2015-12-15 13:08:57 +01:00
* and implements useful functions like {@link getRecordIDList()} and {@link getRecords()}.
2014-05-04 16:12:05 +02:00
*
* @author colymba
*/
class GridFieldBulkActionHandler extends RequestHandler
{
2015-12-15 13:08:57 +01:00
/**
* Related GridField instance.
*
* @var GridField
*/
protected $gridField;
2015-12-15 13:08:57 +01:00
/**
* GridFieldBulkManager instance.
*
* @var GridFieldBulkManager
*/
protected $component;
2015-12-15 13:08:57 +01:00
/**
* Current controller instance.
*
* @var Controller
*/
protected $controller;
2015-12-15 13:08:57 +01:00
/**
* @param GridFIeld $gridField
* @param GridField_URLHandler $component
* @param Controller $controller
*/
public function __construct($gridField, $component, $controller)
{
$this->gridField = $gridField;
$this->component = $component;
$this->controller = $controller;
parent::__construct();
}
2015-12-15 13:08:57 +01:00
/**
* Returns the URL for this RequestHandler.
*
* @author SilverStripe
*
* @see GridFieldDetailForm_ItemRequest
*
* @param string $action
*
* @return string
*/
public function Link($action = null)
{
return Controller::join_links($this->gridField->Link(), 'bulkAction', $action);
}
2015-12-15 13:08:57 +01:00
/**
* Traverse up nested requests until we reach the first that's not a GridFieldDetailForm or GridFieldDetailForm_ItemRequest.
* The opposite of {@link Controller::curr()}, required because
* Controller::$controller_stack is not directly accessible.
*
* @return Controller
*/
protected function getToplevelController()
{
$c = $this->controller;
while ($c && ($c instanceof GridFieldDetailForm_ItemRequest || $c instanceof GridFieldDetailForm)) {
$c = $c->getController();
}
2015-12-15 13:08:57 +01:00
return $c;
}
2015-12-15 13:08:57 +01:00
/**
* Edited version of the GridFieldEditForm function
* adds the 'Bulk Upload' at the end of the crums.
*
* CMS-specific functionality: Passes through navigation breadcrumbs
* to the template, and includes the currently edited record (if any).
* see {@link LeftAndMain->Breadcrumbs()} for details.
*
* @author SilverStripe original Breadcrumbs() method
*
* @see GridFieldDetailForm_ItemRequest
*
* @param bool $unlinked
*
* @return ArrayData
*/
public function Breadcrumbs($unlinked = false)
{
if (!$this->controller->hasMethod('Breadcrumbs')) {
return;
}
2015-12-15 13:08:57 +01:00
$items = $this->controller->Breadcrumbs($unlinked);
$items->push(new ArrayData(array(
'Title' => 'Bulk Editing',
'Link' => false,
)));
2015-12-15 13:08:57 +01:00
return $items;
}
2015-12-15 13:08:57 +01:00
/**
* Returns the list of record IDs selected in the front-end.
*
* @return array List of IDs
*/
public function getRecordIDList()
{
$vars = $this->request->requestVars();
2015-12-15 13:08:57 +01:00
return $vars['records'];
}
2015-12-15 13:08:57 +01:00
/**
* Returns a DataList of the records selected in the front-end.
*
* @return DataList List of records
*/
public function getRecords()
{
$ids = $this->getRecordIDList();
2015-12-15 13:08:57 +01:00
if ($ids) {
$class = $this->gridField->list->dataClass;
return DataList::create($class)->byIDs($ids);
} else {
return false;
}
}
}