ENHANCEMENT GridFieldItemEditView (and specific view and behaviour for usage in CMS)

This commit is contained in:
Ingo Schommer 2012-01-09 17:17:31 +01:00
parent c82bae1def
commit a06300f0b4
6 changed files with 243 additions and 2 deletions

View File

@ -263,6 +263,13 @@
}
});
$('.cms-edit-form .ss-gridfield .action-edit').entwine({
onclick: function(e) {
$('.cms-container').loadPanel(this.attr('href'), '', {selector: '.cms-edit-form'});
e.preventDefault();
}
});
});
}(jQuery));

View File

@ -0,0 +1,4 @@
<div class="cms-content-fields center">
<a class="backlink ss-ui-button" href="$Backlink">Back</a>
$ItemEditForm
</div>

View File

@ -405,8 +405,8 @@ class Folder extends File {
$config->addComponent(new GridFieldPaginator(2));
$config->addComponent(new GridFieldAction_Delete());
$config->addComponent(new GridFieldAction_Edit());
$config->addComponent($gridFieldForm = new GridFieldItemEditView());
$gridFieldForm->setTemplate('CMSGridFieldItemEditView');
$config->addComponent($gridFieldForm = new GridFieldPopupForms());
$gridFieldForm->setTemplate('CMSGridFieldPopupForms');
$files = DataList::create('File')->filter('ParentID', $this->ID)->exclude('ClassName', 'Folder');
$gridField = new GridField('File','Files', $files, $config);
$gridField->setDisplayFields(array(

View File

@ -1,4 +1,90 @@
<?php
/**
* This class is an GridField Component that add Delete action for Objects in the GridField
*
*/
class GridFieldAction_Edit implements GridField_ColumnProvider {
/**
* Add a column 'Delete'
*
* @param type $gridField
* @param array $columns
*/
public function augmentColumns($gridField, &$columns) {
$columns[] = 'EditAction';
}
/**
* Return any special attributes that will be used for FormField::createTag()
*
* @param GridField $gridField
* @param DataObject $record
* @param string $columnName
* @return array
*/
public function getColumnAttributes($gridField, $record, $columnName) {
return array();
}
/**
* Add the title
*
* @param GridField $gridField
* @param string $columnName
* @return array
*/
public function getColumnMetadata($gridField, $columnName) {
if($columnName == 'EditAction') {
return array('title' => '');
}
}
/**
* Which columns are handled by this component
*
* @param type $gridField
* @return type
*/
public function getColumnsHandled($gridField) {
return array('EditAction');
}
/**
* Which GridField actions are this component handling
*
* @param GridField $gridField
* @return array
*/
public function getActions($gridField) {
return array('deleterecord');
}
/**
*
* @param GridField $gridField
* @param DataObject $record
* @param string $columnName
* @return string - the HTML for the column
*/
public function getColumnContent($gridField, $record, $columnName) {
return sprintf('<a class="action-edit" href="%s">%s</a>', Controller::join_links($gridField->Link('item'), $record->ID, 'edit'), _t('GridAction.Edit', 'edit'));
}
/**
* Handle the actions and apply any changes to the GridField
*
* @param GridField $gridField
* @param string $actionName
* @param mixed $arguments
* @param array $data - form data
* @return void
*/
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
}
}
/**
* This class is an GridField Component that add Delete action for Objects in the GridField
*

View File

@ -0,0 +1,143 @@
<?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';
function getURLHandlers($gridField) {
return array(
'item/$ID' => 'handleItem',
);
}
function handleItem($gridField, $request) {
$record = $gridField->getList()->byId($request->param("ID"));
$handler = new GridFieldPopupForm_ItemRequest($gridField, $this, $record);
$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 {
protected $gridField;
protected $component;
protected $record;
/**
* @var String
*/
protected $template = 'GridFieldItemEditView';
static $url_handlers = array(
'$Action!' => '$Action',
'' => 'index',
);
function __construct($gridField, $component, $record) {
$this->gridField = $gridField;
$this->component = $gridField;
$this->record = $record;
parent::__construct();
}
function Link($action = null) {
return Controller::join_links($this->gridField->Link('item'), $this->record->ID, $action);
}
function edit($request) {
$controller = $this->gridField->getForm()->Controller();
$return = $this->customise(array(
'Backlink' => $controller->Link(),
'ItemEditForm' => $this->ItemEditForm($this->gridField, $request),
))->renderWith($this->template);
if($controller->isAjax()) {
return $return;
} else {
// If not requested by ajax, we need to render it within the controller context+template
return $controller->customise(array(
$this->gridField->getForm()->Name() => $return,
));
}
}
function ItemEditForm() {
$request = $this->gridField->getForm()->Controller()->getRequest();
$form = new Form(
$this,
'ItemEditForm',
$this->record->getCMSFields(),
new FieldList(
$saveAction = new FormAction('doSave', _t('GridFieldDetailsForm.Save', 'Save'))
)
);
$saveAction->addExtraClass('ss-ui-action-constructive');
$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');
return $this->gridField->getForm()->Controller()->redirectBack();
}
/**
* @param String
*/
function setTemplate($template) {
$this->template = $template;
}
/**
* @return String
*/
function getTemplate() {
return $this->template;
}
}

View File

@ -0,0 +1 @@
$ItemEditForm