2012-01-09 17:17:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2012-02-27 18:26:11 +01:00
|
|
|
* Provides view and edit forms at GridField-specific URLs.
|
|
|
|
* These can be placed into pop-ups by an appropriate front-end.
|
2012-03-09 00:54:02 +01:00
|
|
|
* Usually added to a grid field alongside of {@link GridFieldEditButton}
|
2012-02-27 18:26:11 +01:00
|
|
|
* which takes care of linking the individual rows to their edit view.
|
2012-01-09 17:17:31 +01:00
|
|
|
*
|
|
|
|
* The URLs provided will be off the following form:
|
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>
|
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>/edit
|
|
|
|
*/
|
2012-03-09 00:54:02 +01:00
|
|
|
class GridFieldDetailForm implements GridField_URLHandler {
|
2012-01-09 17:17:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
2012-03-09 00:54:02 +01:00
|
|
|
protected $template = 'GridFieldDetailForm';
|
2012-01-25 05:31:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-02-27 17:41:01 +01:00
|
|
|
protected $name;
|
2012-01-25 05:31:27 +01:00
|
|
|
|
2012-03-05 17:05:40 +01:00
|
|
|
/**
|
|
|
|
* @var Validator The form validator used for both add and edit fields.
|
|
|
|
*/
|
|
|
|
protected $validator;
|
|
|
|
|
2012-11-08 01:26:38 +01:00
|
|
|
/**
|
|
|
|
* @var FieldList Falls back to {@link DataObject->getCMSFields()} if not defined.
|
|
|
|
*/
|
|
|
|
protected $fields;
|
|
|
|
|
2012-04-30 12:31:17 +02:00
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $itemRequestClass;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var function With two parameters: $form and $component
|
|
|
|
*/
|
|
|
|
protected $itemEditFormCallback;
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getURLHandlers($gridField) {
|
2012-01-09 17:17:31 +01:00
|
|
|
return array(
|
|
|
|
'item/$ID' => 'handleItem',
|
2012-01-25 05:31:27 +01:00
|
|
|
'autocomplete' => 'handleAutocomplete',
|
2012-01-09 17:17:31 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
* Create a popup component. The two arguments will specify how the popup form's HTML and
|
|
|
|
* behaviour is created. The given controller will be customised, putting the edit form into the
|
|
|
|
* template with the given name.
|
|
|
|
*
|
|
|
|
* The arguments are experimental API's to support partial content to be passed back to whatever
|
|
|
|
* controller who wants to display the getCMSFields
|
|
|
|
*
|
2012-02-27 17:41:01 +01:00
|
|
|
* @param string $name The name of the edit form to place into the pop-up form
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
2012-02-27 17:41:01 +01:00
|
|
|
public function __construct($name = 'DetailForm') {
|
|
|
|
$this->name = $name;
|
2012-01-25 05:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type $gridField
|
|
|
|
* @param type $request
|
2012-03-12 09:55:33 +01:00
|
|
|
* @return GridFieldDetailForm_ItemRequest
|
2012-01-25 05:31:27 +01:00
|
|
|
*/
|
|
|
|
public function handleItem($gridField, $request) {
|
2012-02-27 17:41:01 +01:00
|
|
|
$controller = $gridField->getForm()->Controller();
|
2012-02-27 17:43:32 +01:00
|
|
|
|
2012-02-20 02:50:53 +01:00
|
|
|
if(is_numeric($request->param('ID'))) {
|
|
|
|
$record = $gridField->getList()->byId($request->param("ID"));
|
|
|
|
} else {
|
|
|
|
$record = Object::create($gridField->getModelClass());
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:31:17 +02:00
|
|
|
$class = $this->getItemRequestClass();
|
2012-02-27 17:41:01 +01:00
|
|
|
|
|
|
|
$handler = Object::create($class, $gridField, $this, $record, $controller, $this->name);
|
2012-01-09 17:17:31 +01:00
|
|
|
$handler->setTemplate($this->template);
|
2012-02-20 02:50:53 +01:00
|
|
|
|
2012-03-06 01:20:53 +01:00
|
|
|
return $handler->handleRequest($request, DataModel::inst());
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
2012-09-19 12:07:39 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTemplate() {
|
2012-01-09 17:17:31 +01:00
|
|
|
return $this->template;
|
|
|
|
}
|
2012-02-27 17:41:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function setName($name) {
|
2012-02-27 17:41:01 +01:00
|
|
|
$this->name = $name;
|
2012-03-05 17:05:40 +01:00
|
|
|
return $this;
|
2012-02-27 17:41:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getName() {
|
2012-02-27 17:41:01 +01:00
|
|
|
return $this->name;
|
|
|
|
}
|
2012-03-05 17:05:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Validator $validator
|
|
|
|
*/
|
|
|
|
public function setValidator(Validator $validator) {
|
|
|
|
$this->validator = $validator;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Validator
|
|
|
|
*/
|
|
|
|
public function getValidator() {
|
|
|
|
return $this->validator;
|
|
|
|
}
|
2012-04-30 12:31:17 +02:00
|
|
|
|
2012-11-08 01:26:38 +01:00
|
|
|
/**
|
|
|
|
* @param FieldList $fields
|
|
|
|
*/
|
|
|
|
public function setFields(FieldList $fields) {
|
|
|
|
$this->fields = $fields;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return FieldList
|
|
|
|
*/
|
|
|
|
public function getFields() {
|
|
|
|
return $this->fields;
|
|
|
|
}
|
|
|
|
|
2012-04-30 12:31:17 +02:00
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
public function setItemRequestClass($class) {
|
|
|
|
$this->itemRequestClass = $class;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public function getItemRequestClass() {
|
|
|
|
if($this->itemRequestClass) {
|
|
|
|
return $this->itemRequestClass;
|
|
|
|
} else if(ClassInfo::exists(get_class($this) . "_ItemRequest")) {
|
|
|
|
return get_class($this) . "_ItemRequest";
|
|
|
|
} else {
|
|
|
|
return 'GridFieldItemRequest_ItemRequest';
|
|
|
|
}
|
|
|
|
}
|
2012-04-30 13:46:51 +02:00
|
|
|
|
|
|
|
/**
|
2012-04-30 14:20:21 +02:00
|
|
|
* @param Closure $cb Make changes on the edit form after constructing it.
|
2012-04-30 13:46:51 +02:00
|
|
|
*/
|
2012-04-30 14:20:21 +02:00
|
|
|
public function setItemEditFormCallback(Closure $cb) {
|
2012-04-30 13:46:51 +02:00
|
|
|
$this->itemEditFormCallback = $cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-04-30 14:20:21 +02:00
|
|
|
* @return Closure
|
2012-04-30 13:46:51 +02:00
|
|
|
*/
|
|
|
|
public function getItemEditFormCallback() {
|
|
|
|
return $this->itemEditFormCallback;
|
|
|
|
}
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
2012-03-12 09:55:33 +01:00
|
|
|
class GridFieldDetailForm_ItemRequest extends RequestHandler {
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var GridField
|
|
|
|
*/
|
2012-01-09 17:17:31 +01:00
|
|
|
protected $gridField;
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var GridField_URLHandler
|
|
|
|
*/
|
2012-01-09 17:17:31 +01:00
|
|
|
protected $component;
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
protected $popupController;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $popupFormName;
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $template = 'GridFieldItemEditView';
|
|
|
|
|
|
|
|
static $url_handlers = array(
|
|
|
|
'$Action!' => '$Action',
|
2012-01-25 05:31:27 +01:00
|
|
|
'' => 'edit',
|
2012-01-09 17:17:31 +01:00
|
|
|
);
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridFIeld $gridField
|
|
|
|
* @param GridField_URLHandler $component
|
|
|
|
* @param DataObject $record
|
|
|
|
* @param Controller $popupController
|
|
|
|
* @param string $popupFormName
|
|
|
|
*/
|
|
|
|
public function __construct($gridField, $component, $record, $popupController, $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;
|
2012-01-25 05:31:27 +01:00
|
|
|
$this->popupController = $popupController;
|
|
|
|
$this->popupFormName = $popupFormName;
|
2012-01-09 17:17:31 +01:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2012-01-25 05:31:27 +01: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
|
|
|
|
|
|
|
public function view($request) {
|
|
|
|
if(!$this->record->canView()) {
|
|
|
|
$this->httpError(403);
|
|
|
|
}
|
|
|
|
|
|
|
|
$controller = $this->getToplevelController();
|
|
|
|
|
|
|
|
$form = $this->ItemEditForm($this->gridField, $request);
|
|
|
|
$form->makeReadonly();
|
|
|
|
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'Backlink' => $controller->Link(),
|
|
|
|
'ItemEditForm' => $form
|
|
|
|
));
|
|
|
|
$return = $data->renderWith($this->template);
|
|
|
|
|
|
|
|
if($request->isAjax()) {
|
|
|
|
return $return;
|
|
|
|
} else {
|
|
|
|
return $controller->customise(array('Content' => $return));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function edit($request) {
|
2012-03-07 01:17:51 +01:00
|
|
|
$controller = $this->getToplevelController();
|
2012-02-27 17:43:32 +01:00
|
|
|
$form = $this->ItemEditForm($this->gridField, $request);
|
|
|
|
|
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,
|
|
|
|
))->renderWith($this->template);
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2012-04-05 14:44:42 +02:00
|
|
|
if($request->isAjax()) {
|
2012-01-09 17:17:31 +01:00
|
|
|
return $return;
|
|
|
|
} 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,
|
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.
|
|
|
|
*
|
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.
|
|
|
|
*
|
|
|
|
* @return Form
|
|
|
|
*/
|
2012-09-19 12:07:39 +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)) {
|
|
|
|
$controller = Controller::curr();
|
|
|
|
$noActionURL = $controller->removeAction($_REQUEST['url']);
|
|
|
|
$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();
|
|
|
|
|
|
|
|
if(!$canView) {
|
|
|
|
$controller = Controller::curr();
|
|
|
|
// TODO More friendly error
|
|
|
|
return $controller->httpError(403);
|
|
|
|
}
|
|
|
|
|
2012-03-07 06:09:21 +01:00
|
|
|
$actions = new FieldList();
|
|
|
|
if($this->record->ID !== 0) {
|
2012-12-17 00:46:51 +01:00
|
|
|
if($canEdit) {
|
|
|
|
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Save', 'Save'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->addExtraClass('ss-ui-action-constructive')
|
|
|
|
->setAttribute('data-icon', 'accept'));
|
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2012-12-17 00:46:51 +01:00
|
|
|
if($canDelete) {
|
|
|
|
$actions->push(FormAction::create('doDelete', _t('GridFieldDetailForm.Delete', 'Delete'))
|
|
|
|
->setUseButtonTag(true)
|
|
|
|
->addExtraClass('ss-ui-action-destructive'));
|
|
|
|
}
|
2012-09-26 23:34:00 +02:00
|
|
|
|
2012-03-07 06:09:21 +01:00
|
|
|
}else{ // adding new record
|
|
|
|
//Change the Save label to 'Create'
|
2012-05-10 05:46:54 +02:00
|
|
|
$actions->push(FormAction::create('doSave', _t('GridFieldDetailForm.Create', 'Create'))
|
2012-09-26 23:34:00 +02:00
|
|
|
->setUseButtonTag(true)
|
|
|
|
->addExtraClass('ss-ui-action-constructive')
|
|
|
|
->setAttribute('data-icon', 'add'));
|
2012-03-07 06:09:21 +01:00
|
|
|
|
|
|
|
// Add a Cancel link which is a button-like link and link back to one level up.
|
|
|
|
$curmbs = $this->Breadcrumbs();
|
2012-03-08 19:53:19 +01:00
|
|
|
if($curmbs && $curmbs->count()>=2){
|
2012-03-07 06:09:21 +01:00
|
|
|
$one_level_up = $curmbs->offsetGet($curmbs->count()-2);
|
2012-09-11 13:47:12 +02:00
|
|
|
$text = sprintf(
|
2012-09-26 23:34:00 +02:00
|
|
|
"<a class=\"%s\" href=\"%s\">%s</a>",
|
|
|
|
"crumb ss-ui-button ss-ui-action-destructive cms-panel-link ui-corner-all", // CSS classes
|
|
|
|
$one_level_up->Link, // url
|
|
|
|
_t('GridFieldDetailForm.CancelBtn', 'Cancel') // label
|
2012-09-11 13:47:12 +02:00
|
|
|
);
|
2012-03-07 06:09:21 +01:00
|
|
|
$actions->push(new LiteralField('cancelbutton', $text));
|
|
|
|
}
|
|
|
|
}
|
2012-11-08 01:26:38 +01:00
|
|
|
$fields = $this->component->getFields();
|
|
|
|
if(!$fields) $fields = $this->record->getCMSFields();
|
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
|
|
|
);
|
2012-11-08 01:26:38 +01: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
|
|
|
|
2012-12-17 00:46:51 +01:00
|
|
|
if($this->record->ID && !$canEdit) {
|
2013-01-24 07:56:02 +01:00
|
|
|
// Restrict editing of existing records
|
|
|
|
$form->makeReadonly();
|
2012-12-17 00:46:51 +01: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().
|
|
|
|
if($list instanceof ManyManyList) {
|
|
|
|
$extraData = $list->getExtraData('', $this->record->ID);
|
|
|
|
$form->loadDataFrom(array('ManyMany' => $extraData));
|
|
|
|
}
|
|
|
|
|
2012-03-05 16:59:24 +01:00
|
|
|
// TODO Coupling with CMS
|
2012-03-07 01:17:51 +01:00
|
|
|
$toplevelController = $this->getToplevelController();
|
|
|
|
if($toplevelController && $toplevelController instanceof LeftAndMain) {
|
2012-03-07 16:23:02 +01:00
|
|
|
// Always show with base template (full width, no other panels),
|
|
|
|
// regardless of overloaded CMS controller templates.
|
|
|
|
// TODO Allow customization, e.g. to display an edit form alongside a search form from the CMS controller
|
|
|
|
$form->setTemplate('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');
|
2012-08-27 04:18:54 +02:00
|
|
|
if($form->Fields()->hasTabset()) {
|
|
|
|
$form->Fields()->findOrMakeTab('Root')->setTemplate('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();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-03-07 01:17:51 +01:00
|
|
|
/**
|
2012-03-12 09:55:33 +01:00
|
|
|
* Traverse up nested requests until we reach the first that's not a GridFieldDetailForm_ItemRequest.
|
2012-03-07 01:17:51 +01:00
|
|
|
* The opposite of {@link Controller::curr()}, required because
|
|
|
|
* Controller::$controller_stack is not directly accessible.
|
|
|
|
*
|
|
|
|
* @return Controller
|
|
|
|
*/
|
|
|
|
protected function getToplevelController() {
|
|
|
|
$c = $this->popupController;
|
2012-03-12 09:55:33 +01:00
|
|
|
while($c && $c instanceof GridFieldDetailForm_ItemRequest) {
|
2012-03-07 01:17:51 +01:00
|
|
|
$c = $c->getController();
|
|
|
|
}
|
|
|
|
return $c;
|
|
|
|
}
|
2012-10-07 22:18:13 +02:00
|
|
|
|
|
|
|
protected function getBackLink(){
|
|
|
|
// TODO Coupling with CMS
|
|
|
|
$backlink = '';
|
|
|
|
$toplevelController = $this->getToplevelController();
|
|
|
|
if($toplevelController && $toplevelController instanceof LeftAndMain) {
|
|
|
|
if($toplevelController->hasMethod('Backlink')) {
|
|
|
|
$backlink = $toplevelController->Backlink();
|
|
|
|
} elseif($this->popupController->hasMethod('Breadcrumbs')) {
|
|
|
|
$parents = $this->popupController->Breadcrumbs(false)->items;
|
|
|
|
$backlink = array_pop($parents)->Link;
|
2012-11-09 23:05:06 +01:00
|
|
|
}
|
2012-10-07 22:18:13 +02:00
|
|
|
}
|
2012-11-09 23:05:06 +01:00
|
|
|
if(!$backlink) $backlink = $toplevelController->Link();
|
|
|
|
|
2012-10-07 22:18:13 +02:00
|
|
|
return $backlink;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-07 01:17:51 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function doSave($data, $form) {
|
2012-02-20 02:50:53 +01:00
|
|
|
$new_record = $this->record->ID == 0;
|
2012-09-02 16:57:50 +02:00
|
|
|
$controller = Controller::curr();
|
2012-11-08 01:26:38 +01:00
|
|
|
$list = $this->gridField->getList();
|
|
|
|
|
|
|
|
if($list instanceof ManyManyList) {
|
|
|
|
// Data is escaped in ManyManyList->add()
|
|
|
|
$extraData = (isset($data['ManyMany'])) ? $data['ManyMany'] : null;
|
|
|
|
} else {
|
|
|
|
$extraData = null;
|
|
|
|
}
|
2012-02-20 02:50:53 +01:00
|
|
|
|
2012-12-17 00:46:51 +01:00
|
|
|
if(!$this->record->canEdit()) {
|
|
|
|
return $controller->httpError(403);
|
|
|
|
}
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
try {
|
|
|
|
$form->saveInto($this->record);
|
|
|
|
$this->record->write();
|
2012-11-08 01:26:38 +01:00
|
|
|
$list->add($this->record, $extraData);
|
2012-01-09 17:17:31 +01:00
|
|
|
} catch(ValidationException $e) {
|
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad');
|
2012-08-24 01:18:16 +02:00
|
|
|
$responseNegotiator = new PjaxResponseNegotiator(array(
|
|
|
|
'CurrentForm' => function() use(&$form) {
|
|
|
|
return $form->forTemplate();
|
|
|
|
},
|
|
|
|
'default' => function() use(&$controller) {
|
|
|
|
return $controller->redirectBack();
|
|
|
|
}
|
|
|
|
));
|
|
|
|
if($controller->getRequest()->isAjax()){
|
|
|
|
$controller->getRequest()->addHeader('X-Pjax', 'CurrentForm');
|
|
|
|
}
|
|
|
|
return $responseNegotiator->respond($controller->getRequest());
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Save this item into the given relationship
|
|
|
|
|
2012-12-21 11:18:51 +01:00
|
|
|
$link = '<a href="' . $this->Link('edit') . '">"'
|
|
|
|
. htmlspecialchars($this->record->Title, ENT_QUOTES)
|
|
|
|
. '"</a>';
|
|
|
|
$message = _t(
|
|
|
|
'GridFieldDetailForm.Saved',
|
|
|
|
'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
|
|
|
);
|
|
|
|
|
|
|
|
$form->sessionMessage($message, 'good');
|
|
|
|
|
2012-08-20 15:08:30 +02:00
|
|
|
if($new_record) {
|
|
|
|
return Controller::curr()->redirect($this->Link());
|
2012-09-02 16:57:50 +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)
|
|
|
|
return $this->edit(Controller::curr()->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
|
|
|
|
$noActionURL = $controller->removeAction($data['url']);
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
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(
|
|
|
|
_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();
|
2012-02-27 05:27:03 +01:00
|
|
|
} catch(ValidationException $e) {
|
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad');
|
2012-03-07 01:17:51 +01:00
|
|
|
return Controller::curr()->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
|
|
|
);
|
2012-10-07 22:18:13 +02:00
|
|
|
|
|
|
|
$toplevelController = $this->getToplevelController();
|
|
|
|
if($toplevelController && $toplevelController instanceof LeftAndMain) {
|
|
|
|
$backForm = $toplevelController->getEditForm();
|
|
|
|
$backForm->sessionMessage($message, 'good');
|
|
|
|
} else {
|
|
|
|
$form->sessionMessage($message, 'good');
|
|
|
|
}
|
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
|
2012-02-27 05:27:03 +01:00
|
|
|
$controller = Controller::curr();
|
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
|
|
|
|
2012-10-07 22:18:13 +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
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
2012-09-19 12:07:39 +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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getTemplate() {
|
2012-01-09 17:17:31 +01:00
|
|
|
return $this->template;
|
|
|
|
}
|
2012-02-28 18:48:33 +01:00
|
|
|
|
2012-03-07 01:17:51 +01:00
|
|
|
/**
|
|
|
|
* @return Controller
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getController() {
|
2012-03-07 01:17:51 +01:00
|
|
|
return $this->popupController;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return GridField
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getGridField() {
|
2012-03-07 01:17:51 +01:00
|
|
|
return $this->gridField;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param boolean $unlinked
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function Breadcrumbs($unlinked = false) {
|
2012-03-08 19:53:19 +01:00
|
|
|
if(!$this->popupController->hasMethod('Breadcrumbs')) return;
|
2012-02-28 18:48:33 +01:00
|
|
|
|
|
|
|
$items = $this->popupController->Breadcrumbs($unlinked);
|
2012-03-05 18:21:08 +01:00
|
|
|
if($this->record && $this->record->ID) {
|
2012-02-28 18:48:33 +01:00
|
|
|
$items->push(new ArrayData(array(
|
|
|
|
'Title' => $this->record->Title,
|
2012-03-07 01:17:51 +01:00
|
|
|
'Link' => $this->Link()
|
2012-02-28 18:48:33 +01: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
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|