Merge pull request #156 from chillu/gridfield-assetadmin

Gridfield assetadmin
This commit is contained in:
Sam Minnée 2012-01-09 13:08:29 -08:00
commit 5ab38ae13b
12 changed files with 326 additions and 43 deletions

View File

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

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

@ -63,6 +63,21 @@
</li>
</ul>
<% end_if %>
<% if Code == 'AssetAdmin' %>
<ul>
<li class="last <% if Top.class == 'AssetAdmin' %>current<% end_if %>" id="Menu-AssetAdmin">
<a href="admin/assets/">
<span class="text">Edit &amp; organize</span>
</a>
</li>
<li class="first <% if Top.class == 'CMSFileAddController' %>current<% end_if %>" id="Menu-CMSFileAddController">
<a href="admin/assets/add">
<span class="text">Add files</span>
</a>
</li>
</ul>
<% end_if %>
</li>
<% end_control %>
</ul>

View File

@ -2,9 +2,8 @@
.cms table.ss-gridfield { width: 100%; padding: 0; margin: 20px 0 0 0; border-collapse: separate; display: table; border-bottom: 0 none; }
.cms table.ss-gridfield thead { color: #1d2224; background: transparent; }
.cms table.ss-gridfield tbody { background: #FFF; }
.cms table.ss-gridfield tbody td { /* Rounded buttons */ }
.cms table.ss-gridfield tbody td button { border: #CC0033 solid 1px; background: #CC0033; color: #FFF; -moz-border-radius: 15px; -webkit-border-radius: 15px; -o-border-radius: 15px; -ms-border-radius: 15px; -khtml-border-radius: 15px; border-radius: 15px; margin: 0 0 0 2px; padding: 0; width: auto; min-width: 30px; height: 30px; text-shadow: none; }
.cms table.ss-gridfield tbody td button:hover { color: #222; }
.cms table.ss-gridfield tbody td { /* Emulate a link by default */ }
.cms table.ss-gridfield tbody td button { border: none; background: none; margin: 0 0 0 2px; padding: 0; width: auto; text-shadow: none; }
.cms table.ss-gridfield tfoot { color: #1d2224; }
.cms table.ss-gridfield tfoot tr td { background: #95a5ab; padding: .7em; }
.cms table.ss-gridfield tr.sortable-header th { background: #7f9198; }

View File

@ -254,6 +254,28 @@ class File extends DataObject {
return $this->canEdit($member);
}
function getCMSFields() {
$urlLink = "<div class='field readonly'>";
$urlLink .= "<label class='left'>"._t('AssetTableField.URL','URL')."</label>";
$urlLink .= "<span class='readonly'><a href='{$this->Link()}'>{$this->RelativeLink()}</a></span>";
$urlLink .= "</div>";
return new FieldList(
new TabSet('Root',
new Tab('Main',
new TextField("Title", _t('AssetTableField.TITLE','Title')),
new TextField("Name", _t('AssetTableField.FILENAME','Filename')),
new LiteralField("AbsoluteURL", $urlLink),
new ReadonlyField("FileType", _t('AssetTableField.TYPE','Type')),
new ReadonlyField("Size", _t('AssetTableField.SIZE','Size'), $this->getSize()),
new DropdownField("OwnerID", _t('AssetTableField.OWNER','Owner'), Member::mapInCMSGroups()),
new DateField_Disabled("Created", _t('AssetTableField.CREATED','First uploaded')),
new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed'))
)
)
);
}
/**
* Returns a category based on the file extension.

View File

@ -404,6 +404,9 @@ class Folder extends File {
$config->addComponent(new GridFieldSortableHeader());
$config->addComponent(new GridFieldPaginator(2));
$config->addComponent(new GridFieldAction_Delete());
$config->addComponent(new GridFieldAction_Edit());
$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(
@ -416,30 +419,19 @@ class Folder extends File {
$titleField = ($this->ID && $this->ID != "root") ? new TextField("Title", _t('Folder.TITLE')) : new HiddenField("Title");
$fields = new FieldList(
new HiddenField("Name"),
new TabSet("Root",
new Tab("Files", _t('Folder.FILESTAB', "Files"),
new TabSet('Root',
new Tab('Main',
$titleField,
$gridField,
new HiddenField("ID"),
new HiddenField("Name"),
new HiddenField("DestFolderID")
),
new Tab("Details", _t('Folder.DETAILSTAB', "Details"),
new ReadonlyField("URL", _t('Folder.URL', 'URL')),
new ReadonlyField("ClassName", _t('Folder.TYPE','Type')),
new ReadonlyField("Created", _t('Folder.CREATED','First Uploaded')),
new ReadonlyField("LastEdited", _t('Folder.LASTEDITED','Last Updated'))
),
new Tab("Upload", _t('Folder.UPLOADTAB', "Upload"),
new LiteralField("UploadIframe",
$this->getUploadIframe()
)
)
),
new HiddenField("ID")
)
);
if(!$this->canEdit()) {
$fields->removeFieldFromTab("Root", "Upload");
$fields->removeByName("Upload");
}
$this->extend('updateCMSFields', $fields);
@ -447,16 +439,6 @@ class Folder extends File {
return $fields;
}
/**
* Display the upload form. Returns an iframe tag that will show admin/assets/uploadiframe.
*/
function getUploadIframe() {
return <<<HTML
<iframe name="AssetAdmin_upload" src="admin/assets/uploadiframe/{$this->ID}" id="AssetAdmin_upload" border="0" style="border-style none !important; width: 97%; min-height: 300px; height: 100%; height: expression(document.body.clientHeight) !important;">
</iframe>
HTML;
}
/**
* Get the children of this folder that are also folders.
*/

View File

@ -157,7 +157,7 @@ class FormScaffolder extends Object {
$ctf = new $fieldClass(
$this,
$relationship,
null,
$this->obj->$relationship(),
$relationshipFields,
"getCMSFields"
);

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
*
@ -36,7 +122,7 @@ class GridFieldAction_Delete implements GridField_ColumnProvider, GridField_Acti
*/
public function getColumnMetadata($gridField, $columnName) {
if($columnName == 'DeleteAction') {
return array('title' => 'Delete');
return array('title' => '');
}
}
@ -68,7 +154,13 @@ class GridFieldAction_Delete implements GridField_ColumnProvider, GridField_Acti
* @return string - the HTML for the column
*/
public function getColumnContent($gridField, $record, $columnName) {
$field = new GridField_Action($gridField, 'DeleteRecord'.$record->ID, "x", "deleterecord", array('RecordID' => $record->ID));
$field = new GridField_Action(
$gridField,
'DeleteRecord'.$record->ID,
_t('GridAction.Delete', "delete"),
"deleterecord",
array('RecordID' => $record->ID)
);
$output = $field->Field();
return $output;
}

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

@ -73,6 +73,31 @@ class Image extends File {
parent::defineMethods();
}
function getCMSFields() {
$fields = parent::getCMSFields();
$urlLink = "<div class='field readonly'>";
$urlLink .= "<label class='left'>"._t('AssetTableField.URL','URL')."</label>";
$urlLink .= "<span class='readonly'><a href='{$this->Link()}'>{$this->RelativeLink()}</a></span>";
$urlLink .= "</div>";
$big = $this->URL;
$formattedImage = $this->getFormattedImage('AssetLibraryPreview');
$thumbnail = $formattedImage ? $formattedImage->URL : '';
// Hmm this required the translated string to be appended to BottomRoot to add this to the Main tab
$fields->addFieldToTab('Root.Main',
new ReadonlyField("Dimensions", _t('AssetTableField.DIM','Dimensions'))
);
$fields->addFieldToTab('Root.Main',
new LiteralField("ImageFull",
"<img id='thumbnailImage' src='{$thumbnail}?r=" . rand(1,100000) . "' alt='{$this->Name}' />"
)
);
return $fields;
}
/**
* An image exists if it has a filename.
* Does not do any filesystem checks.

View File

@ -61,21 +61,14 @@ $gf_border_radius: 7px;
tbody {
background: #FFF;
td {
/* Rounded buttons */
/* Emulate a link by default */
button {
border: #CC0033 solid 1px;
background: #CC0033;
color: #FFF;
@include border-radius(15px);
border: none;
background: none;
margin: 0 0 0 2px;
padding: 0;
width: auto;
min-width: 30px;
height: 30px;
text-shadow: none;
&:hover {
color: #222;
}
}
}
}

View File

@ -0,0 +1 @@
$ItemEditForm