2012-01-09 17:17:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides view and edit forms at GridField-specific URLs. These can be placed into pop-ups by an appropriate front-end.
|
|
|
|
*
|
|
|
|
* The URLs provided will be off the following form:
|
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>
|
|
|
|
* - <FormURL>/field/<GridFieldName>/item/<RecordID>/edit
|
|
|
|
*/
|
|
|
|
class GridFieldPopupForms implements GridField_URLHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
|
|
|
protected $template = 'GridFieldItemEditView';
|
2012-01-25 05:31:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
protected $popupController;
|
2012-01-09 17:17:31 +01:00
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $popupFormName;
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
function getURLHandlers($gridField) {
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param Controller $popupController The controller object that will be used to render the pop-up forms
|
|
|
|
* @param string $popupFormName The name of the edit form to place into the pop-up form
|
|
|
|
*/
|
|
|
|
public function __construct($popupController, $popupFormName) {
|
|
|
|
$this->popupController = $popupController;
|
|
|
|
$this->popupFormName = $popupFormName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param type $gridField
|
|
|
|
* @param type $request
|
|
|
|
* @return GridFieldPopupForm_ItemRequest
|
|
|
|
*/
|
|
|
|
public function handleItem($gridField, $request) {
|
2012-01-09 17:17:31 +01:00
|
|
|
$record = $gridField->getList()->byId($request->param("ID"));
|
2012-01-25 05:31:27 +01:00
|
|
|
$handler = new GridFieldPopupForm_ItemRequest($gridField, $this, $record, $this->popupController, $this->popupFormName);
|
2012-01-09 17:17:31 +01:00
|
|
|
$handler->setTemplate($this->template);
|
|
|
|
return $handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
function setTemplate($template) {
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getTemplate() {
|
|
|
|
return $this->template;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class GridFieldPopupForm_ItemRequest extends RequestHandler {
|
|
|
|
|
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;
|
|
|
|
$this->component = $gridField;
|
|
|
|
$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-01-09 17:17:31 +01:00
|
|
|
return Controller::join_links($this->gridField->Link('item'), $this->record->ID, $action);
|
|
|
|
}
|
|
|
|
|
|
|
|
function edit($request) {
|
2012-01-25 05:31:27 +01:00
|
|
|
$controller = $this->popupController;
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
$return = $this->customise(array(
|
2012-01-25 05:31:27 +01:00
|
|
|
'Backlink' => $this->gridField->getForm()->Controller()->Link(),
|
2012-01-09 17:17:31 +01:00
|
|
|
'ItemEditForm' => $this->ItemEditForm($this->gridField, $request),
|
|
|
|
))->renderWith($this->template);
|
|
|
|
|
|
|
|
if($controller->isAjax()) {
|
|
|
|
return $return;
|
|
|
|
} else {
|
2012-01-25 05:31:27 +01:00
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
// If not requested by ajax, we need to render it within the controller context+template
|
|
|
|
return $controller->customise(array(
|
2012-01-25 05:31:27 +01:00
|
|
|
$this->popupFormName => $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.
|
|
|
|
*
|
|
|
|
* In the future, we will probably need to come up with a tigher object representing a partially
|
|
|
|
* 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-01-09 17:17:31 +01:00
|
|
|
function ItemEditForm() {
|
2012-01-25 05:31:27 +01:00
|
|
|
$request = $this->popupController->getRequest();
|
2012-01-09 17:17:31 +01:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
|
|
|
'ItemEditForm',
|
2012-01-25 05:31:27 +01:00
|
|
|
// WARNING: The arguments passed here are a little arbitrary. This API will need cleanup
|
|
|
|
$this->record->getCMSFields($this->popupController, $this->popupFormName),
|
2012-01-09 17:17:31 +01:00
|
|
|
new FieldList(
|
|
|
|
$saveAction = new FormAction('doSave', _t('GridFieldDetailsForm.Save', 'Save'))
|
|
|
|
)
|
|
|
|
);
|
2012-02-17 00:35:10 +01:00
|
|
|
$saveAction->addExtraClass('ss-ui-action-constructive icon-accept');
|
2012-01-09 17:17:31 +01:00
|
|
|
$form->loadDataFrom($this->record);
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSave($data, $form) {
|
|
|
|
try {
|
|
|
|
$form->saveInto($this->record);
|
|
|
|
$this->record->write();
|
|
|
|
} catch(ValidationException $e) {
|
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad');
|
|
|
|
return Director::redirectBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Save this item into the given relationship
|
|
|
|
|
|
|
|
$message = sprintf(
|
|
|
|
_t('ComplexTableField.SUCCESSEDIT2', 'Saved %s %s'),
|
|
|
|
$this->record->singular_name(),
|
|
|
|
'<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'
|
|
|
|
);
|
|
|
|
|
|
|
|
$form->sessionMessage($message, 'good');
|
|
|
|
|
2012-01-25 05:31:27 +01:00
|
|
|
return $this->popupController->redirectBack();
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
function setTemplate($template) {
|
|
|
|
$this->template = $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getTemplate() {
|
|
|
|
return $this->template;
|
|
|
|
}
|
|
|
|
}
|