2012-01-09 17:17:31 +01:00
|
|
|
<?php
|
2016-06-03 10:51:02 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Forms\GridField;
|
|
|
|
|
|
|
|
use SilverStripe\Admin\LeftAndMain;
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\PjaxResponseNegotiator;
|
|
|
|
use SilverStripe\Control\RequestHandler;
|
|
|
|
use SilverStripe\Control\SS_HTTPRequest;
|
|
|
|
use SilverStripe\Control\SS_HTTPResponse;
|
|
|
|
use SilverStripe\Control\SS_HTTPResponse_Exception;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormAction;
|
|
|
|
use SilverStripe\Forms\LiteralField;
|
|
|
|
use SilverStripe\ORM\ArrayList;
|
2016-08-11 01:40:23 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBHTMLText;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\HasManyList;
|
|
|
|
use SilverStripe\ORM\ManyManyList;
|
2016-08-11 01:40:23 +02:00
|
|
|
use SilverStripe\ORM\SS_List;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\View\ArrayData;
|
|
|
|
use SilverStripe\View\SSViewer;
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
class GridFieldDetailForm_ItemRequest extends RequestHandler
|
|
|
|
{
|
2013-06-20 11:40:55 +02:00
|
|
|
|
|
|
|
private static $allowed_actions = array(
|
|
|
|
'edit',
|
|
|
|
'view',
|
|
|
|
'ItemEditForm'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-08-15 08:53:05 +02:00
|
|
|
* @var GridField
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2012-01-09 17:17:31 +01:00
|
|
|
protected $gridField;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
2016-08-23 04:32:26 +02:00
|
|
|
* @var GridFieldDetailForm
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2012-01-09 17:17:31 +01:00
|
|
|
protected $component;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var DataObject
|
|
|
|
*/
|
2012-01-09 17:17:31 +01:00
|
|
|
protected $record;
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
2014-06-12 05:31:03 +02:00
|
|
|
* This represents the current parent RequestHandler (which does not necessarily need to be a Controller).
|
|
|
|
* It allows us to traverse the RequestHandler chain upwards to reach the Controller stack.
|
2012-01-25 05:31:27 +01:00
|
|
|
*
|
2014-06-12 05:31:03 +02:00
|
|
|
* @var RequestHandler
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
protected $popupController;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $popupFormName;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
2016-08-23 04:32:26 +02:00
|
|
|
protected $template = null;
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $url_handlers = array(
|
2012-01-09 17:17:31 +01:00
|
|
|
'$Action!' => '$Action',
|
2012-01-25 05:31:27 +01:00
|
|
|
'' => 'edit',
|
2012-01-09 17:17:31 +01:00
|
|
|
);
|
2014-06-12 05:31:03 +02:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param GridField $gridField
|
2016-08-23 04:32:26 +02:00
|
|
|
* @param GridFieldDetailForm $component
|
2012-01-25 05:31:27 +01:00
|
|
|
* @param DataObject $record
|
2014-06-12 05:31:03 +02:00
|
|
|
* @param RequestHandler $requestHandler
|
|
|
|
* @param string $popupFormName
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function __construct($gridField, $component, $record, $requestHandler, $popupFormName)
|
|
|
|
{
|
2012-01-09 17:17:31 +01:00
|
|
|
$this->gridField = $gridField;
|
2012-03-05 16:59:24 +01:00
|
|
|
$this->component = $component;
|
2012-01-09 17:17:31 +01:00
|
|
|
$this->record = $record;
|
2014-06-12 05:31:03 +02:00
|
|
|
$this->popupController = $requestHandler;
|
2012-01-25 05:31:27 +01:00
|
|
|
$this->popupFormName = $popupFormName;
|
2012-01-09 17:17:31 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
public function Link($action = null)
|
|
|
|
{
|
2012-09-26 23:34:00 +02:00
|
|
|
return Controller::join_links($this->gridField->Link('item'),
|
|
|
|
$this->record->ID ? $this->record->ID : 'new', $action);
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
2012-05-11 09:44:39 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function view($request)
|
|
|
|
{
|
|
|
|
if (!$this->record->canView()) {
|
2012-05-11 09:44:39 +02:00
|
|
|
$this->httpError(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
$controller = $this->getToplevelController();
|
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
$form = $this->ItemEditForm();
|
2012-05-11 09:44:39 +02:00
|
|
|
$form->makeReadonly();
|
|
|
|
|
|
|
|
$data = new ArrayData(array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'Backlink' => $controller->Link(),
|
2012-05-11 09:44:39 +02:00
|
|
|
'ItemEditForm' => $form
|
|
|
|
));
|
2016-08-23 04:32:26 +02:00
|
|
|
$return = $data->renderWith($this->getTemplates());
|
2012-05-11 09:44:39 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($request->isAjax()) {
|
2012-05-11 09:44:39 +02:00
|
|
|
return $return;
|
|
|
|
} else {
|
|
|
|
return $controller->customise(array('Content' => $return));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function edit($request)
|
|
|
|
{
|
2012-03-07 01:17:51 +01:00
|
|
|
$controller = $this->getToplevelController();
|
2016-08-23 04:32:26 +02:00
|
|
|
$form = $this->ItemEditForm();
|
2012-02-27 17:43:32 +01:00
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
$return = $this->customise(array(
|
2012-05-08 10:24:14 +02:00
|
|
|
'Backlink' => $controller->hasMethod('Backlink') ? $controller->Backlink() : $controller->Link(),
|
2012-03-07 01:17:51 +01:00
|
|
|
'ItemEditForm' => $form,
|
2016-08-23 04:32:26 +02:00
|
|
|
))->renderWith($this->getTemplates());
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($request->isAjax()) {
|
2014-08-15 08:53:05 +02:00
|
|
|
return $return;
|
2012-01-09 17:17:31 +01:00
|
|
|
} else {
|
|
|
|
// If not requested by ajax, we need to render it within the controller context+template
|
|
|
|
return $controller->customise(array(
|
2012-03-07 01:17:51 +01:00
|
|
|
// TODO CMS coupling
|
2012-02-27 17:43:32 +01:00
|
|
|
'Content' => $return,
|
2014-08-15 08:53:05 +02:00
|
|
|
));
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
* Builds an item edit form. The arguments to getCMSFields() are the popupController and
|
|
|
|
* popupFormName, however this is an experimental API and may change.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-02-27 18:26:11 +01:00
|
|
|
* @todo In the future, we will probably need to come up with a tigher object representing a partially
|
2012-01-25 05:31:27 +01:00
|
|
|
* complete controller with gaps for extra functionality. This, for example, would be a better way
|
|
|
|
* of letting Security/login put its log-in form inside a UI specified elsewhere.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @return Form
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function ItemEditForm()
|
|
|
|
{
|
2012-11-08 01:26:38 +01:00
|
|
|
$list = $this->gridField->getList();
|
|
|
|
|
2012-03-07 05:17:32 +01:00
|
|
|
if (empty($this->record)) {
|
2014-06-12 05:31:03 +02:00
|
|
|
$controller = $this->getToplevelController();
|
2015-09-29 00:29:05 +02:00
|
|
|
$url = $controller->getRequest()->getURL();
|
|
|
|
$noActionURL = $controller->removeAction($url);
|
2012-03-07 05:17:32 +01:00
|
|
|
$controller->getResponse()->removeHeader('Location'); //clear the existing redirect
|
2012-05-23 11:50:02 +02:00
|
|
|
return $controller->redirect($noActionURL, 302);
|
2012-03-07 05:17:32 +01:00
|
|
|
}
|
2012-03-07 04:40:59 +01:00
|
|
|
|
2012-12-17 00:46:51 +01:00
|
|
|
$canView = $this->record->canView();
|
|
|
|
$canEdit = $this->record->canEdit();
|
|
|
|
$canDelete = $this->record->canDelete();
|
|
|
|
$canCreate = $this->record->canCreate();
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!$canView) {
|
2014-06-12 05:31:03 +02:00
|
|
|
$controller = $this->getToplevelController();
|
2012-12-17 00:46:51 +01:00
|
|
|
// TODO More friendly error
|
|
|
|
return $controller->httpError(403);
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Build actions
|
|
|
|
$actions = $this->getFormActions();
|
2013-11-13 06:21:18 +01:00
|
|
|
|
2016-03-06 22:17:15 +01:00
|
|
|
// If we are creating a new record in a has-many list, then
|
|
|
|
// pre-populate the record's foreign key.
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($list instanceof HasManyList && !$this->record->isInDB()) {
|
2016-03-06 22:17:15 +01:00
|
|
|
$key = $list->getForeignKey();
|
|
|
|
$id = $list->getForeignID();
|
|
|
|
$this->record->$key = $id;
|
|
|
|
}
|
|
|
|
|
2012-11-08 01:26:38 +01:00
|
|
|
$fields = $this->component->getFields();
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!$fields) {
|
|
|
|
$fields = $this->record->getCMSFields();
|
|
|
|
}
|
2013-11-13 06:21:18 +01:00
|
|
|
|
|
|
|
// If we are creating a new record in a has-many list, then
|
2016-02-28 08:56:10 +01:00
|
|
|
// Disable the form field as it has no effect.
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($list instanceof HasManyList) {
|
2013-11-13 06:21:18 +01:00
|
|
|
$key = $list->getForeignKey();
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($field = $fields->dataFieldByName($key)) {
|
2013-11-13 06:21:18 +01:00
|
|
|
$fields->makeFieldReadonly($field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2014-06-12 05:31:03 +02:00
|
|
|
// Caution: API violation. Form expects a Controller, but we are giving it a RequestHandler instead.
|
|
|
|
// Thanks to this however, we are able to nest GridFields, and also access the initial Controller by
|
|
|
|
// dereferencing GridFieldDetailForm_ItemRequest->getController() multiple times. See getToplevelController
|
|
|
|
// below.
|
2012-01-09 17:17:31 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'ItemEditForm',
|
2012-11-08 01:26:38 +01:00
|
|
|
$fields,
|
2012-03-07 06:09:21 +01:00
|
|
|
$actions,
|
2012-03-05 17:05:40 +01:00
|
|
|
$this->component->getValidator()
|
2012-01-09 17:17:31 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-11-15 23:48:31 +01:00
|
|
|
$form->loadDataFrom($this->record, $this->record->ID == 0 ? Form::MERGE_IGNORE_FALSEISH : Form::MERGE_DEFAULT);
|
2012-03-05 16:59:24 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($this->record->ID && !$canEdit) {
|
2013-01-24 07:56:02 +01:00
|
|
|
// Restrict editing of existing records
|
|
|
|
$form->makeReadonly();
|
2013-06-03 13:31:40 +02:00
|
|
|
// Hack to re-enable delete button if user can delete
|
|
|
|
if ($canDelete) {
|
|
|
|
$form->Actions()->fieldByName('action_doDelete')->setReadonly(false);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif (!$this->record->ID && !$canCreate) {
|
2013-01-24 07:56:02 +01:00
|
|
|
// Restrict creation of new records
|
|
|
|
$form->makeReadonly();
|
2012-12-17 00:46:51 +01:00
|
|
|
}
|
|
|
|
|
2012-11-08 01:26:38 +01:00
|
|
|
// Load many_many extraData for record.
|
|
|
|
// Fields with the correct 'ManyMany' namespace need to be added manually through getCMSFields().
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($list instanceof ManyManyList) {
|
2012-11-08 01:26:38 +01:00
|
|
|
$extraData = $list->getExtraData('', $this->record->ID);
|
|
|
|
$form->loadDataFrom(array('ManyMany' => $extraData));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-03-05 16:59:24 +01:00
|
|
|
// TODO Coupling with CMS
|
2012-03-07 01:17:51 +01:00
|
|
|
$toplevelController = $this->getToplevelController();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// Always show with base template (full width, no other panels),
|
2012-03-07 16:23:02 +01:00
|
|
|
// regardless of overloaded CMS controller templates.
|
|
|
|
// TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller
|
2016-08-11 01:40:23 +02:00
|
|
|
$form->setTemplate([
|
|
|
|
'type' => 'Includes',
|
|
|
|
'SilverStripe\\Admin\\LeftAndMain_EditForm',
|
|
|
|
]);
|
2012-08-27 04:18:54 +02:00
|
|
|
$form->addExtraClass('cms-content cms-edit-form center');
|
2012-04-18 22:11:40 +02:00
|
|
|
$form->setAttribute('data-pjax-fragment', 'CurrentForm Content');
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($form->Fields()->hasTabSet()) {
|
|
|
|
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
|
2012-10-31 15:57:48 +01:00
|
|
|
$form->addExtraClass('cms-tabset');
|
2012-07-05 21:53:40 +02:00
|
|
|
}
|
2012-10-07 22:18:13 +02:00
|
|
|
|
|
|
|
$form->Backlink = $this->getBackLink();
|
2012-03-05 16:59:24 +01:00
|
|
|
}
|
2012-04-30 13:46:51 +02:00
|
|
|
|
|
|
|
$cb = $this->component->getItemEditFormCallback();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($cb) {
|
|
|
|
$cb($form, $this);
|
|
|
|
}
|
2012-12-27 07:22:54 +01:00
|
|
|
$this->extend("updateItemEditForm", $form);
|
2012-01-09 17:17:31 +01:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
/**
|
|
|
|
* Build the set of form field actions for this DataObject
|
|
|
|
*
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function getFormActions()
|
|
|
|
{
|
2016-01-26 04:56:07 +01:00
|
|
|
$canEdit = $this->record->canEdit();
|
|
|
|
$canDelete = $this->record->canDelete();
|
|
|
|
$actions = new FieldList();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($this->record->ID !== 0) {
|
|
|
|
if ($canEdit) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Save', 'Save'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->addExtraClass('ss-ui-action-constructive')
|
|
|
|
->setAttribute('data-icon', 'accept'));
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($canDelete) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$actions->push(FormAction::create('doDelete', _t('GridFieldDetailForm.Delete', 'Delete'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->addExtraClass('ss-ui-action-destructive action-delete'));
|
|
|
|
}
|
|
|
|
|
|
|
|
} else { // adding new record
|
|
|
|
//Change the Save label to 'Create'
|
|
|
|
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Create', 'Create'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->addExtraClass('ss-ui-action-constructive')
|
|
|
|
->setAttribute('data-icon', 'add'));
|
|
|
|
|
|
|
|
// Add a Cancel link which is a button-like link and link back to one level up.
|
|
|
|
$crumbs = $this->Breadcrumbs();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($crumbs && $crumbs->count() >= 2) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$oneLevelUp = $crumbs->offsetGet($crumbs->count() - 2);
|
|
|
|
$text = sprintf(
|
|
|
|
"<a class=\"%s\" href=\"%s\">%s</a>",
|
|
|
|
"crumb ss-ui-button ss-ui-action-destructive cms-panel-link ui-corner-all", // CSS classes
|
|
|
|
$oneLevelUp->Link, // url
|
|
|
|
_t('GridFieldDetailForm.CancelBtn', 'Cancel') // label
|
|
|
|
);
|
|
|
|
$actions->push(new LiteralField('cancelbutton', $text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->extend('updateFormActions', $actions);
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2012-03-07 01:17:51 +01:00
|
|
|
/**
|
2014-06-12 05:31:03 +02:00
|
|
|
* Traverse the nested RequestHandlers until we reach something that's not GridFieldDetailForm_ItemRequest.
|
|
|
|
* This allows us to access the Controller responsible for invoking the top-level GridField.
|
|
|
|
* This should be equivalent to getting the controller off the top of the controller stack via Controller::curr(),
|
|
|
|
* but allows us to avoid accessing the global state.
|
|
|
|
*
|
|
|
|
* GridFieldDetailForm_ItemRequests are RequestHandlers, and as such they are not part of the controller stack.
|
|
|
|
*
|
2012-03-07 01:17:51 +01:00
|
|
|
* @return Controller
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function getToplevelController()
|
|
|
|
{
|
2012-03-07 01:17:51 +01:00
|
|
|
$c = $this->popupController;
|
2016-08-19 00:51:35 +02:00
|
|
|
while ($c && $c instanceof GridFieldDetailForm_ItemRequest) {
|
2012-03-07 01:17:51 +01:00
|
|
|
$c = $c->getController();
|
|
|
|
}
|
|
|
|
return $c;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function getBackLink()
|
|
|
|
{
|
2012-10-07 22:18:13 +02:00
|
|
|
// TODO Coupling with CMS
|
|
|
|
$backlink = '';
|
|
|
|
$toplevelController = $this->getToplevelController();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) {
|
|
|
|
if ($toplevelController->hasMethod('Backlink')) {
|
2012-10-07 22:18:13 +02:00
|
|
|
$backlink = $toplevelController->Backlink();
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif ($this->popupController->hasMethod('Breadcrumbs')) {
|
2012-10-07 22:18:13 +02:00
|
|
|
$parents = $this->popupController->Breadcrumbs(false)->items;
|
|
|
|
$backlink = array_pop($parents)->Link;
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-10-07 22:18:13 +02:00
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!$backlink) {
|
|
|
|
$backlink = $toplevelController->Link();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-07 22:18:13 +02:00
|
|
|
return $backlink;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-08 05:44:27 +02:00
|
|
|
/**
|
|
|
|
* Get the list of extra data from the $record as saved into it by
|
|
|
|
* {@see Form::saveInto()}
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-06-08 05:44:27 +02:00
|
|
|
* Handles detection of falsey values explicitly saved into the
|
|
|
|
* DataObject by formfields
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-06-08 05:44:27 +02:00
|
|
|
* @param DataObject $record
|
|
|
|
* @param SS_List $list
|
|
|
|
* @return array List of data to write to the relation
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function getExtraSavedData($record, $list)
|
|
|
|
{
|
2015-06-08 05:44:27 +02:00
|
|
|
// Skip extra data if not ManyManyList
|
2016-08-19 00:51:35 +02:00
|
|
|
if (!($list instanceof ManyManyList)) {
|
2015-06-08 05:44:27 +02:00
|
|
|
return null;
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-08 05:44:27 +02:00
|
|
|
$data = array();
|
2016-08-19 00:51:35 +02:00
|
|
|
foreach ($list->getExtraFields() as $field => $dbSpec) {
|
2015-06-08 05:44:27 +02:00
|
|
|
$savedField = "ManyMany[{$field}]";
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($record->hasField($savedField)) {
|
2015-06-08 05:44:27 +02:00
|
|
|
$data[$field] = $record->getField($savedField);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2012-03-07 01:17:51 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
public function doSave($data, $form)
|
|
|
|
{
|
2016-01-26 04:56:07 +01:00
|
|
|
$isNewRecord = $this->record->ID == 0;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Check permission
|
|
|
|
if (!$this->record->canEdit()) {
|
|
|
|
return $this->httpError(403);
|
2013-04-02 04:09:52 +02:00
|
|
|
}
|
2012-12-17 00:46:51 +01:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Save from form data
|
2012-01-09 17:17:31 +01:00
|
|
|
try {
|
2016-01-26 04:56:07 +01:00
|
|
|
$this->saveFormIntoRecord($data, $form);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
return $this->generateValidationResponse($form, $e);
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
2016-01-06 00:34:58 +01:00
|
|
|
$link = '<a href="' . $this->Link('edit') . '">"'
|
|
|
|
. htmlspecialchars($this->record->Title, ENT_QUOTES)
|
2014-01-30 22:38:34 +01:00
|
|
|
. '"</a>';
|
2012-12-21 11:18:51 +01:00
|
|
|
$message = _t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'GridFieldDetailForm.Saved',
|
2012-12-21 11:18:51 +01:00
|
|
|
'Saved {name} {link}',
|
|
|
|
array(
|
2013-01-11 15:50:09 +01:00
|
|
|
'name' => $this->record->i18n_singular_name(),
|
2012-12-21 11:18:51 +01:00
|
|
|
'link' => $link
|
|
|
|
)
|
2012-01-09 17:17:31 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-01-30 22:38:34 +01:00
|
|
|
$form->sessionMessage($message, 'good', false);
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2016-01-26 04:56:07 +01:00
|
|
|
// Redirect after save
|
|
|
|
return $this->redirectAfterSave($isNewRecord);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Response object for this request after a successful save
|
|
|
|
*
|
|
|
|
* @param bool $isNewRecord True if this record was just created
|
2016-06-03 10:51:02 +02:00
|
|
|
* @return SS_HTTPResponse|DBHTMLText
|
2016-01-26 04:56:07 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function redirectAfterSave($isNewRecord)
|
|
|
|
{
|
2016-01-26 04:56:07 +01:00
|
|
|
$controller = $this->getToplevelController();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($isNewRecord) {
|
2014-06-12 05:31:03 +02:00
|
|
|
return $controller->redirect($this->Link());
|
2016-08-19 00:51:35 +02:00
|
|
|
} elseif ($this->gridField->getList()->byID($this->record->ID)) {
|
2012-08-20 15:08:30 +02:00
|
|
|
// Return new view, as we can't do a "virtual redirect" via the CMS Ajax
|
|
|
|
// to the same URL (it assumes that its content is already current, and doesn't reload)
|
2014-06-12 05:31:03 +02:00
|
|
|
return $this->edit($controller->getRequest());
|
2012-09-02 16:57:50 +02:00
|
|
|
} else {
|
|
|
|
// Changes to the record properties might've excluded the record from
|
|
|
|
// a filtered list, so return back to the main view if it can't be found
|
2015-09-29 00:29:05 +02:00
|
|
|
$url = $controller->getRequest()->getURL();
|
|
|
|
$noActionURL = $controller->removeAction($url);
|
2014-08-15 08:53:05 +02:00
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content');
|
|
|
|
return $controller->redirect($noActionURL, 302);
|
2012-08-20 15:08:30 +02:00
|
|
|
}
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
public function httpError($errorCode, $errorMessage = null)
|
|
|
|
{
|
2016-01-26 04:56:07 +01:00
|
|
|
$controller = $this->getToplevelController();
|
|
|
|
return $controller->httpError($errorCode, $errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the given form data into the underlying dataobject and relation
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @throws ValidationException On error
|
|
|
|
* @return DataObject Saved record
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function saveFormIntoRecord($data, $form)
|
|
|
|
{
|
2016-01-26 04:56:07 +01:00
|
|
|
$list = $this->gridField->getList();
|
|
|
|
|
|
|
|
// Check object matches the correct classname
|
|
|
|
if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) {
|
|
|
|
$newClassName = $data['ClassName'];
|
|
|
|
// The records originally saved attribute was overwritten by $form->saveInto($record) before.
|
|
|
|
// This is necessary for newClassInstance() to work as expected, and trigger change detection
|
|
|
|
// on the ClassName attribute
|
|
|
|
$this->record->setClassName($this->record->ClassName);
|
|
|
|
// Replace $record with a new instance
|
|
|
|
$this->record = $this->record->newClassInstance($newClassName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save form and any extra saved data into this dataobject
|
|
|
|
$form->saveInto($this->record);
|
|
|
|
$this->record->write();
|
|
|
|
$extraData = $this->getExtraSavedData($this->record, $list);
|
|
|
|
$list->add($this->record, $extraData);
|
|
|
|
|
|
|
|
return $this->record;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a response object for a form validation error
|
|
|
|
*
|
|
|
|
* @param Form $form The source form
|
|
|
|
* @param ValidationException $e The validation error message
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
* @throws SS_HTTPResponse_Exception
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
protected function generateValidationResponse($form, $e)
|
|
|
|
{
|
2016-01-26 04:56:07 +01:00
|
|
|
$controller = $this->getToplevelController();
|
|
|
|
|
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad', false);
|
|
|
|
$responseNegotiator = new PjaxResponseNegotiator(array(
|
2016-08-19 00:51:35 +02:00
|
|
|
'CurrentForm' => function () use (&$form) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return $form->forTemplate();
|
|
|
|
},
|
2016-08-19 00:51:35 +02:00
|
|
|
'default' => function () use (&$controller) {
|
2016-01-26 04:56:07 +01:00
|
|
|
return $controller->redirectBack();
|
|
|
|
}
|
|
|
|
));
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($controller->getRequest()->isAjax()) {
|
2016-01-26 04:56:07 +01:00
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'CurrentForm');
|
|
|
|
}
|
|
|
|
return $responseNegotiator->respond($controller->getRequest());
|
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
* @param Form $form
|
|
|
|
* @return SS_HTTPResponse
|
|
|
|
*/
|
|
|
|
public function doDelete($data, $form)
|
|
|
|
{
|
2012-10-07 22:18:13 +02:00
|
|
|
$title = $this->record->Title;
|
2012-02-27 05:27:03 +01:00
|
|
|
try {
|
2012-10-07 22:18:13 +02:00
|
|
|
if (!$this->record->canDelete()) {
|
2012-09-26 23:34:00 +02:00
|
|
|
throw new ValidationException(
|
2016-08-19 00:51:35 +02:00
|
|
|
_t('GridFieldDetailForm.DeletePermissionsFailure', "No delete permissions"), 0);
|
2012-02-27 05:27:03 +01:00
|
|
|
}
|
|
|
|
|
2012-10-07 22:18:13 +02:00
|
|
|
$this->record->delete();
|
2016-08-19 00:51:35 +02:00
|
|
|
} catch (ValidationException $e) {
|
2014-01-30 22:38:34 +01:00
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad', false);
|
2014-06-12 05:31:03 +02:00
|
|
|
return $this->getToplevelController()->redirectBack();
|
2012-02-27 05:27:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$message = sprintf(
|
2012-05-10 05:46:54 +02:00
|
|
|
_t('GridFieldDetailForm.Deleted', 'Deleted %s %s'),
|
2013-01-11 15:50:09 +01:00
|
|
|
$this->record->i18n_singular_name(),
|
2012-10-07 22:18:13 +02:00
|
|
|
htmlspecialchars($title, ENT_QUOTES)
|
2012-02-27 05:27:03 +01:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-07 22:18:13 +02:00
|
|
|
$toplevelController = $this->getToplevelController();
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($toplevelController && $toplevelController instanceof LeftAndMain) {
|
2012-10-07 22:18:13 +02:00
|
|
|
$backForm = $toplevelController->getEditForm();
|
2014-01-30 22:38:34 +01:00
|
|
|
$backForm->sessionMessage($message, 'good', false);
|
2012-10-07 22:18:13 +02:00
|
|
|
} else {
|
2014-01-30 22:38:34 +01:00
|
|
|
$form->sessionMessage($message, 'good', false);
|
2012-10-07 22:18:13 +02:00
|
|
|
}
|
2012-02-27 05:27:03 +01:00
|
|
|
|
2012-10-07 22:18:13 +02:00
|
|
|
//when an item is deleted, redirect to the parent controller
|
2014-06-12 05:31:03 +02:00
|
|
|
$controller = $this->getToplevelController();
|
2012-05-29 11:34:47 +02:00
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'Content'); // Force a content refresh
|
2012-02-27 05:27:03 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
return $controller->redirect($this->getBackLink(), 302); //redirect back to admin section
|
2012-02-27 05:27:03 +01:00
|
|
|
}
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
/**
|
2016-08-11 01:40:23 +02:00
|
|
|
* @param string $template
|
|
|
|
* @return $this
|
2012-01-09 17:17:31 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function setTemplate($template)
|
|
|
|
{
|
2012-01-09 17:17:31 +01:00
|
|
|
$this->template = $template;
|
2012-03-05 17:05:40 +01:00
|
|
|
return $this;
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return string
|
2012-01-09 17:17:31 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function getTemplate()
|
|
|
|
{
|
2012-01-09 17:17:31 +01:00
|
|
|
return $this->template;
|
|
|
|
}
|
2012-02-28 18:48:33 +01:00
|
|
|
|
2016-08-23 04:32:26 +02:00
|
|
|
/**
|
|
|
|
* Get list of templates to use
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getTemplates()
|
|
|
|
{
|
|
|
|
$templates = SSViewer::get_templates_by_class($this, '', __CLASS__);
|
|
|
|
// Prefer any custom template
|
|
|
|
if($this->getTemplate()) {
|
|
|
|
array_unshift($templates, $this->getTemplate());
|
|
|
|
}
|
|
|
|
return $templates;
|
|
|
|
}
|
|
|
|
|
2012-03-07 01:17:51 +01:00
|
|
|
/**
|
|
|
|
* @return Controller
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function getController()
|
|
|
|
{
|
2012-03-07 01:17:51 +01:00
|
|
|
return $this->popupController;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return GridField
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function getGridField()
|
|
|
|
{
|
2012-03-07 01:17:51 +01:00
|
|
|
return $this->gridField;
|
|
|
|
}
|
|
|
|
|
2014-05-07 13:35:52 +02:00
|
|
|
/**
|
|
|
|
* @return DataObject
|
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function getRecord()
|
|
|
|
{
|
2014-05-07 13:35:52 +02:00
|
|
|
return $this->record;
|
|
|
|
}
|
|
|
|
|
2012-02-28 18:48:33 +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.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
|
|
|
* @param boolean $unlinked
|
2016-08-19 00:51:35 +02:00
|
|
|
* @return ArrayList
|
2012-02-28 18:48:33 +01:00
|
|
|
*/
|
2016-08-19 00:51:35 +02:00
|
|
|
public function Breadcrumbs($unlinked = false)
|
|
|
|
{
|
|
|
|
if (!$this->popupController->hasMethod('Breadcrumbs')) {
|
|
|
|
return null;
|
|
|
|
}
|
2012-02-28 18:48:33 +01:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/** @var ArrayList $items */
|
2012-02-28 18:48:33 +01:00
|
|
|
$items = $this->popupController->Breadcrumbs($unlinked);
|
2016-08-19 00:51:35 +02:00
|
|
|
if ($this->record && $this->record->ID) {
|
2016-03-30 07:46:18 +02:00
|
|
|
$title = ($this->record->Title) ? $this->record->Title : "#{$this->record->ID}";
|
2012-02-28 18:48:33 +01:00
|
|
|
$items->push(new ArrayData(array(
|
2016-03-30 07:46:18 +02:00
|
|
|
'Title' => $title,
|
2012-03-07 01:17:51 +01:00
|
|
|
'Link' => $this->Link()
|
2014-08-15 08:53:05 +02:00
|
|
|
)));
|
2012-03-05 18:21:08 +01:00
|
|
|
} else {
|
|
|
|
$items->push(new ArrayData(array(
|
2012-08-09 23:46:40 +02:00
|
|
|
'Title' => sprintf(_t('GridField.NewRecord', 'New %s'), $this->record->i18n_singular_name()),
|
2012-02-28 18:48:33 +01:00
|
|
|
'Link' => false
|
2014-08-15 08:53:05 +02:00
|
|
|
)));
|
2012-02-28 18:48:33 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-02-28 18:48:33 +01:00
|
|
|
return $items;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|