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.
|
|
|
|
* Usually added to a grid field alongside of {@link GridFieldAction_Edit}
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
class GridFieldPopupForms implements GridField_URLHandler {
|
|
|
|
|
2012-02-27 17:41:01 +01:00
|
|
|
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
/**
|
|
|
|
* @var String
|
|
|
|
*/
|
2012-02-27 17:43:32 +01:00
|
|
|
protected $template = 'GridFieldPopupForms';
|
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-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
|
|
|
|
*
|
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
|
|
|
|
* @return GridFieldPopupForm_ItemRequest
|
|
|
|
*/
|
|
|
|
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-02-27 17:41:01 +01:00
|
|
|
if(!$class = ClassInfo::exists(get_class($this) . "_ItemRequest")) {
|
|
|
|
$class = 'GridFieldPopupForm_ItemRequest';
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
function setTemplate($template) {
|
|
|
|
$this->template = $template;
|
2012-03-05 17:05:40 +01:00
|
|
|
return $this;
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getTemplate() {
|
|
|
|
return $this->template;
|
|
|
|
}
|
2012-02-27 17:41:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
function setName($name) {
|
|
|
|
$this->name = $name;
|
2012-03-05 17:05:40 +01:00
|
|
|
return $this;
|
2012-02-27 17:41:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getName() {
|
|
|
|
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-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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-02-20 02:50:53 +01: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
|
|
|
}
|
|
|
|
|
|
|
|
function edit($request) {
|
2012-01-25 05:31:27 +01:00
|
|
|
$controller = $this->popupController;
|
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-02-27 17:41:01 +01:00
|
|
|
'Backlink' => $controller->Link(),
|
|
|
|
'ItemEditForm' => $form,
|
2012-01-09 17:17:31 +01:00
|
|
|
))->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-02-27 17:43:32 +01:00
|
|
|
// TODO Allow customization
|
|
|
|
'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-01-09 17:17:31 +01:00
|
|
|
function ItemEditForm() {
|
|
|
|
$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(
|
2012-02-28 15:56:47 +01:00
|
|
|
FormAction::create('doSave', _t('GridFieldDetailsForm.Save', 'Save'))
|
2012-03-06 16:58:13 +01:00
|
|
|
->addExtraClass('ss-ui-action-constructive')->setAttribute('data-icon', 'accept'),
|
2012-02-28 15:56:47 +01:00
|
|
|
FormAction::create('doDelete', _t('GridFieldDetailsForm.Delete', 'Delete'))
|
2012-03-06 16:58:13 +01:00
|
|
|
->addExtraClass('ss-ui-action-destructive')
|
2012-03-05 17:05:40 +01:00
|
|
|
),
|
|
|
|
$this->component->getValidator()
|
2012-01-09 17:17:31 +01:00
|
|
|
);
|
|
|
|
$form->loadDataFrom($this->record);
|
2012-03-05 16:59:24 +01:00
|
|
|
|
|
|
|
// TODO Coupling with CMS
|
|
|
|
if($this->popupController instanceof LeftAndMain) {
|
|
|
|
$form->addExtraClass('cms-edit-form');
|
|
|
|
$form->setTemplate($this->popupController->getTemplatesWithSuffix('_EditForm'));
|
|
|
|
$form->addExtraClass('cms-content center ss-tabset ' . $this->popupController->BaseCSSClasses());
|
|
|
|
if($form->Fields()->hasTabset()) $form->Fields()->findOrMakeTab('Root')->setTemplate('CMSTabSet');
|
|
|
|
// TODO Link back to controller action (and edited root record) rather than index,
|
|
|
|
// which requires more URL knowledge than the current link to this field gives us.
|
|
|
|
// The current root record is held in session only,
|
|
|
|
// e.g. page/edit/show/6/ vs. page/edit/EditForm/field/MyGridField/....
|
|
|
|
$form->Backlink = $this->popupController->Link();
|
|
|
|
}
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doSave($data, $form) {
|
2012-02-20 02:50:53 +01:00
|
|
|
$new_record = $this->record->ID == 0;
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
try {
|
|
|
|
$form->saveInto($this->record);
|
|
|
|
$this->record->write();
|
2012-02-20 02:50:53 +01:00
|
|
|
if($new_record)
|
|
|
|
$this->gridField->getList()->add($this->record);
|
2012-01-09 17:17:31 +01:00
|
|
|
} catch(ValidationException $e) {
|
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad');
|
2012-02-27 05:27:03 +01:00
|
|
|
return Controller::curr()->redirectBack();
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2012-02-27 05:27:03 +01:00
|
|
|
function doDelete($data, $form) {
|
|
|
|
try {
|
|
|
|
$toDelete = $this->record;
|
|
|
|
if (!$toDelete->canDelete()) {
|
|
|
|
throw new ValidationException(_t('GridFieldDetailsForm.DeletePermissionsFailure',"No delete permissions"),0);
|
|
|
|
}
|
|
|
|
|
|
|
|
$toDelete->delete();
|
|
|
|
} catch(ValidationException $e) {
|
|
|
|
$form->sessionMessage($e->getResult()->message(), 'bad');
|
|
|
|
return Director::redirectBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = sprintf(
|
|
|
|
_t('ComplexTableField.SUCCESSEDIT2', 'Deleted %s %s'),
|
|
|
|
$this->record->singular_name(),
|
|
|
|
'<a href="' . $this->Link('edit') . '">"' . htmlspecialchars($this->record->Title, ENT_QUOTES) . '"</a>'
|
|
|
|
);
|
|
|
|
|
|
|
|
$form->sessionMessage($message, 'good');
|
|
|
|
|
|
|
|
//when an item is deleted, redirect to the revelant admin section without the action parameter
|
|
|
|
$controller = Controller::curr();
|
|
|
|
$noActionURL = $controller->removeAction($data['url']);
|
|
|
|
|
|
|
|
return Director::redirect($noActionURL, 302); //redirect back to admin section
|
|
|
|
}
|
|
|
|
|
2012-01-09 17:17:31 +01:00
|
|
|
/**
|
|
|
|
* @param String
|
|
|
|
*/
|
|
|
|
function setTemplate($template) {
|
|
|
|
$this->template = $template;
|
2012-03-05 17:05:40 +01:00
|
|
|
return $this;
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
function getTemplate() {
|
|
|
|
return $this->template;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
function Breadcrumbs($unlinked = false) {
|
|
|
|
if(!($this->popupController instanceof LeftAndMain)) return false;
|
|
|
|
|
|
|
|
$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,
|
|
|
|
'Link' => false
|
|
|
|
)));
|
2012-03-05 18:21:08 +01:00
|
|
|
} else {
|
|
|
|
$items->push(new ArrayData(array(
|
|
|
|
'Title' => sprintf(_t('GridField.NewRecord', 'New %s'), $this->record->singular_name()),
|
|
|
|
'Link' => false
|
|
|
|
)));
|
2012-02-28 18:48:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Remove once ViewableData->First()/Last() is fixed
|
|
|
|
foreach($items as $i => $item) $item->iteratorProperties($i, $items->Count());
|
|
|
|
|
|
|
|
return $items;
|
|
|
|
}
|
2012-01-09 17:17:31 +01:00
|
|
|
}
|