GridFieldBulkEditingTools/bulkManager/code/BulkAction/Handler.php

149 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace Colymba\BulkManager\BulkAction;
use SilverStripe\Control\Controller;
use SilverStripe\Control\RequestHandler;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\ORM\DataList;
use SilverStripe\View\ArrayData;
/**
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()}.
*
* @author colymba
*/
class Handler 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
2015-12-15 13:08:57 +01:00
* @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.
*
2015-12-15 13:08:57 +01:00
* @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.
*
2015-12-15 13:08:57 +01:00
* @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.
*
2015-12-15 13:08:57 +01:00
* CMS-specific functionality: Passes through navigation breadcrumbs
* to the template, and includes the currently edited record (if any).
* see {@link LeftAndMain->Breadcrumbs()} for details.
*
2015-12-15 13:08:57 +01:00
* @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.
*
2015-12-15 13:08:57 +01:00
* @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.
*
2015-12-15 13:08:57 +01:00
* @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;
}
}
}