2012-07-16 22:39:01 +02:00
|
|
|
<?php
|
2012-07-18 14:23:51 +02:00
|
|
|
/**
|
|
|
|
* Handles request from the GridFieldBulkImageUpload component
|
|
|
|
*
|
|
|
|
* Handles:
|
|
|
|
* * Form creation
|
|
|
|
* * file upload
|
|
|
|
* * editing and cancelling records
|
2012-08-07 22:51:54 +02:00
|
|
|
*
|
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
2012-07-18 14:23:51 +02:00
|
|
|
*/
|
2012-07-16 22:39:01 +02:00
|
|
|
class GridFieldBulkImageUpload_Request extends RequestHandler {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var GridField
|
|
|
|
*/
|
|
|
|
protected $gridField;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var GridField_URLHandler
|
|
|
|
*/
|
|
|
|
protected $component;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @var Controller
|
|
|
|
*/
|
|
|
|
protected $controller;
|
2012-07-18 01:12:10 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
/**
|
2012-07-18 14:23:51 +02:00
|
|
|
* Cache the records FieldList from getCMSfields()
|
|
|
|
*
|
2012-07-18 01:12:10 +02:00
|
|
|
* @var FieldList
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
2012-07-18 01:12:10 +02:00
|
|
|
protected $recordCMSFieldList;
|
2012-07-16 22:39:01 +02:00
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2013-03-28 18:24:22 +01:00
|
|
|
private static $url_handlers = array(
|
2012-08-07 23:02:13 +02:00
|
|
|
'$Action!' => '$Action'
|
2012-07-16 22:39:01 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridFIeld $gridField
|
|
|
|
* @param GridField_URLHandler $component
|
|
|
|
* @param Controller $controller
|
|
|
|
*/
|
|
|
|
public function __construct($gridField, $component, $controller) {
|
|
|
|
$this->gridField = $gridField;
|
|
|
|
$this->component = $component;
|
2012-07-18 01:12:10 +02:00
|
|
|
$this->controller = $controller;
|
2012-07-16 22:39:01 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL for this RequestHandler
|
2012-07-18 01:12:10 +02:00
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @author SilverStripe
|
|
|
|
* @see GridFieldDetailForm_ItemRequest
|
|
|
|
* @param string $action
|
|
|
|
* @return string
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function Link($action = null) {
|
|
|
|
return Controller::join_links($this->gridField->Link(), 'bulkimageupload', $action);
|
|
|
|
}
|
2012-07-18 01:12:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the Image field name from the managed record
|
|
|
|
* Either as set in the component config or the default one
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getRecordImageField()
|
|
|
|
{
|
2012-07-28 13:17:19 +02:00
|
|
|
$fieldName = $this->component->getConfig('imageFieldName');
|
2012-07-18 01:12:10 +02:00
|
|
|
if ( $fieldName == null ) $fieldName = $this->getDefaultRecordImageField();
|
|
|
|
|
|
|
|
return $fieldName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the list of editable fields from the managed record
|
|
|
|
* Either as set in the component config or the default ones
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function getRecordEditableFields()
|
|
|
|
{
|
2012-07-28 13:17:19 +02:00
|
|
|
$fields = $this->component->getConfig('editableFields');
|
2012-07-18 01:12:10 +02:00
|
|
|
if ( $fields == null ) $fields = $this->getDefaultRecordEditableFields();
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the first has_one Image realtion from the GridField managed DataObject
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getDefaultRecordImageField()
|
|
|
|
{
|
|
|
|
$recordClass = $this->gridField->list->dataClass;
|
2013-01-09 22:28:55 +01:00
|
|
|
$recordHasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
2012-07-18 01:12:10 +02:00
|
|
|
|
|
|
|
$imageField = null;
|
|
|
|
foreach( $recordHasOneFields as $field => $type )
|
|
|
|
{
|
2013-02-02 13:42:54 +01:00
|
|
|
if($type == 'Image' || is_subclass_of($type, 'Image')) {
|
2012-07-18 01:12:10 +02:00
|
|
|
$imageField = $field . 'ID';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $imageField;
|
|
|
|
}
|
2013-02-02 13:42:54 +01:00
|
|
|
|
|
|
|
/**
|
2013-02-02 18:26:48 +01:00
|
|
|
* Returns the classname of the first has_one image-relation of the managed DataObject or the
|
|
|
|
* classname of the given fieldname
|
2013-02-02 13:42:54 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-02 18:26:48 +01:00
|
|
|
private function getRecordImageClass()
|
2013-02-02 13:42:54 +01:00
|
|
|
{
|
|
|
|
$recordClass = $this->gridField->list->dataClass;
|
|
|
|
$recordHasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
|
|
|
|
|
2013-02-02 18:26:48 +01:00
|
|
|
$fieldName = $this->component->getConfig('imageFieldName');
|
|
|
|
if($fieldName != null)
|
|
|
|
{
|
|
|
|
// filter out ID at the end:
|
|
|
|
$fieldName = substr($fieldName, 0, -2);
|
|
|
|
return $recordHasOneFields[$fieldName];
|
|
|
|
}
|
2013-02-02 13:42:54 +01:00
|
|
|
foreach($recordHasOneFields as $field => $type)
|
|
|
|
{
|
|
|
|
if($type == 'Image' || is_subclass_of($type, 'Image'))
|
|
|
|
{
|
|
|
|
return $type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
2012-08-07 23:02:13 +02:00
|
|
|
*
|
|
|
|
* @param type $recordID
|
|
|
|
* @return type
|
2012-07-18 01:12:10 +02:00
|
|
|
*/
|
2012-07-28 13:17:19 +02:00
|
|
|
function getRecordHTMLFormFields( $recordID = 0 )
|
|
|
|
{
|
|
|
|
$config = $this->component->getConfig();
|
2012-08-07 22:51:54 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelCMSDataFields( $config, $this->gridField->list->dataClass );
|
2012-07-28 13:17:19 +02:00
|
|
|
|
|
|
|
//@TODO: if editableFields given use them with filterNonEditableRecordsFields()
|
|
|
|
// otherwise go through getModelFilteredDataFields
|
|
|
|
|
|
|
|
|
2012-08-07 22:51:54 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::filterNonEditableRecordsFields($config, $recordCMSDataFields);
|
2012-07-28 13:17:19 +02:00
|
|
|
|
|
|
|
if ( $config['imageFieldName'] == null ) $config['imageFieldName'] = $this->getDefaultRecordImageField();
|
|
|
|
|
2012-08-07 22:51:54 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
|
2013-05-07 16:19:17 +02:00
|
|
|
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields($recordCMSDataFields, $this->gridField->list->dataClass, $recordID);
|
2012-08-07 22:51:54 +02:00
|
|
|
$formFieldsHTML = GridFieldBulkEditingHelper::dataFieldsToHTML($recordCMSDataFields);
|
|
|
|
$formFieldsHTML = GridFieldBulkEditingHelper::escapeFormFieldsHTML($formFieldsHTML, $recordID);
|
2012-07-28 13:17:19 +02:00
|
|
|
|
|
|
|
return $formFieldsHTML;
|
|
|
|
}
|
2013-06-02 20:07:00 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
/**
|
2013-06-02 20:07:00 +02:00
|
|
|
* Creates and return the bulk upload form
|
|
|
|
*
|
|
|
|
* @return Form
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
2013-06-02 20:07:00 +02:00
|
|
|
public function uploadForm($id = null, $fields = null)
|
|
|
|
{
|
2012-07-16 22:39:01 +02:00
|
|
|
$actions = new FieldList();
|
|
|
|
|
2012-08-09 23:30:50 +02:00
|
|
|
$actions->push(
|
|
|
|
FormAction::create('SaveAll', 'Save All')
|
|
|
|
->setAttribute('id', 'bulkImageUploadUpdateBtn')
|
2013-06-02 20:07:00 +02:00
|
|
|
->addExtraClass('ss-ui-action-constructive')
|
2012-08-09 23:30:50 +02:00
|
|
|
->setAttribute('data-icon', 'accept')
|
|
|
|
->setAttribute('data-url', $this->Link('update'))
|
|
|
|
->setUseButtonTag(true)
|
2013-06-02 20:07:00 +02:00
|
|
|
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
|
2012-08-09 23:30:50 +02:00
|
|
|
);
|
2013-03-04 09:56:03 +01:00
|
|
|
|
2012-08-09 23:30:50 +02:00
|
|
|
$actions->push(
|
|
|
|
FormAction::create('Cancel', 'Cancel & Delete All')
|
|
|
|
->setAttribute('id', 'bulkImageUploadUpdateCancelBtn')
|
2013-06-02 20:07:00 +02:00
|
|
|
->addExtraClass('ss-ui-action-destructive')
|
2012-08-09 23:30:50 +02:00
|
|
|
->setAttribute('data-icon', 'decline')
|
|
|
|
->setAttribute('data-url', $this->Link('cancel'))
|
|
|
|
->setUseButtonTag(true)
|
2013-06-02 20:07:00 +02:00
|
|
|
->setAttribute('src', '')//changes type to image so isn't hooked by default actions handlers
|
2012-08-09 23:30:50 +02:00
|
|
|
);
|
2013-06-02 20:07:00 +02:00
|
|
|
|
2012-08-09 23:30:50 +02:00
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
/* *
|
|
|
|
* UploadField
|
|
|
|
*/
|
|
|
|
$uploadField = GFBIUploadField::create('BulkUploadField', '');
|
2012-07-16 22:39:01 +02:00
|
|
|
$uploadField->setConfig('previewMaxWidth', 40);
|
2013-06-02 20:07:00 +02:00
|
|
|
$uploadField->setConfig('previewMaxHeight', 30);
|
2012-07-16 22:39:01 +02:00
|
|
|
$uploadField->addExtraClass('ss-assetuploadfield');
|
2013-06-02 20:07:00 +02:00
|
|
|
$uploadField->removeExtraClass('ss-uploadfield');
|
2012-07-16 22:39:01 +02:00
|
|
|
$uploadField->setTemplate('AssetUploadField');
|
2013-06-02 20:07:00 +02:00
|
|
|
|
|
|
|
$uploadField->setDownloadTemplateName('colymba-gfbiu-uploadfield-downloadtemplate');
|
|
|
|
|
|
|
|
//always overwrite
|
|
|
|
$uploadField->setOverwriteWarning(false);
|
|
|
|
|
|
|
|
/* *
|
|
|
|
* UploadField configs
|
|
|
|
*/
|
|
|
|
//custom upload url
|
|
|
|
$uploadField->setConfig('url', $this->Link('upload'));
|
|
|
|
|
|
|
|
//max file size
|
2013-03-26 09:48:12 +01:00
|
|
|
$maxFileSize = $this->component->getConfig('maxFileSize');
|
|
|
|
if ( $maxFileSize !== null )
|
|
|
|
{
|
|
|
|
$uploadField->getValidator()->setAllowedMaxFileSize( $maxFileSize );
|
|
|
|
}
|
2013-06-02 20:07:00 +02:00
|
|
|
|
|
|
|
//upload dir
|
|
|
|
$uploadDir = $this->component->getConfig('folderName');
|
|
|
|
if ( $uploadDir !== null )
|
|
|
|
{
|
|
|
|
$uploadField->setFolderName($uploadDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
//sequential upload
|
|
|
|
$uploadField->setConfig('sequentialUploads', $this->component->getConfig('sequentialUploads'));
|
2012-07-16 22:39:01 +02:00
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
/* *
|
|
|
|
* Create form
|
|
|
|
*/
|
2012-07-16 22:39:01 +02:00
|
|
|
$form = new Form(
|
|
|
|
$this,
|
2013-06-02 20:07:00 +02:00
|
|
|
'uploadForm',
|
2012-07-16 22:39:01 +02:00
|
|
|
new FieldList(
|
|
|
|
$uploadField
|
|
|
|
),
|
|
|
|
$actions
|
|
|
|
);
|
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
|
|
|
|
$crumbs = $this->Breadcrumbs();
|
|
|
|
if($crumbs && $crumbs->count()>=2)
|
|
|
|
{
|
|
|
|
$one_level_up = $crumbs->offsetGet($crumbs->count()-2);
|
2012-07-16 22:39:01 +02:00
|
|
|
$form->Backlink = $one_level_up->Link;
|
|
|
|
}
|
2013-06-02 20:07:00 +02:00
|
|
|
|
|
|
|
$form->setTemplate('LeftAndMain_EditForm');
|
|
|
|
$form->addExtraClass('cms-content center'); //not using cms-edit-form to avoid btn being hooked with default handlers
|
|
|
|
$form->setAttribute('data-pjax-fragment', 'Content');
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default and main action that returns the upload form etc...
|
|
|
|
*
|
|
|
|
* @return string Form's HTML
|
|
|
|
*/
|
|
|
|
public function index($request)
|
|
|
|
{
|
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/AssetUploadField.js');
|
|
|
|
Requirements::css(FRAMEWORK_DIR . '/css/AssetUploadField.css');
|
|
|
|
|
2012-08-07 22:56:59 +02:00
|
|
|
Requirements::javascript(BULK_EDIT_TOOLS_PATH . '/javascript/GridFieldBulkImageUpload.js');
|
|
|
|
Requirements::css(BULK_EDIT_TOOLS_PATH . '/css/GridFieldBulkImageUpload.css');
|
2013-06-02 20:07:00 +02:00
|
|
|
Requirements::javascript(BULK_EDIT_TOOLS_PATH . '/javascript/GridFieldBulkImageUpload_downloadtemplate.js');
|
|
|
|
|
|
|
|
$form = $this->uploadForm();
|
|
|
|
|
|
|
|
if($request->isAjax())
|
|
|
|
{
|
|
|
|
$response = new SS_HTTPResponse($form->forAjaxTemplate()->getValue());
|
|
|
|
$response->addHeader('X-Pjax', 'Content');
|
|
|
|
$response->addHeader('X-Title', 'SilverStripe - Bulk '.$this->gridField->list->dataClass.' Image Upload');
|
2013-05-07 18:11:57 +02:00
|
|
|
return $response;
|
2013-06-02 20:07:00 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-05-07 18:11:57 +02:00
|
|
|
$controller = $this->getToplevelController();
|
|
|
|
return $controller->customise(array(
|
2013-06-02 20:07:00 +02:00
|
|
|
'Content' => $form
|
|
|
|
));
|
2013-05-07 18:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse up nested requests until we reach the first that's not a GridFieldDetailForm or GridFieldDetailForm_ItemRequest.
|
|
|
|
* The opposite of {@link Controller::curr()}, required because
|
|
|
|
* Controller::$controller_stack is not directly accessible.
|
|
|
|
*
|
|
|
|
* @return Controller
|
|
|
|
*/
|
|
|
|
protected function getToplevelController() {
|
|
|
|
$c = $this->controller;
|
|
|
|
while($c && ($c instanceof GridFieldDetailForm_ItemRequest || $c instanceof GridFieldDetailForm)) {
|
|
|
|
$c = $c->getController();
|
|
|
|
}
|
|
|
|
return $c;
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
|
|
|
* Process image upload and Object creation
|
|
|
|
* Create new DataObject and add image relation
|
|
|
|
* returns Image data and editable Fields forms
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* Overides UploadField's upload method by Zauberfisch
|
|
|
|
* Kept original file upload/processing but removed unessesary processing
|
|
|
|
* and adds DataObject creation and editableFields processing
|
|
|
|
*
|
|
|
|
* @author Zauberfisch original upload() method
|
|
|
|
* @see UploadField->upload()
|
2012-07-18 01:12:10 +02:00
|
|
|
* @param SS_HTTPRequest $request
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return string json
|
2012-07-18 01:12:10 +02:00
|
|
|
*/
|
2012-07-16 22:39:01 +02:00
|
|
|
public function upload(SS_HTTPRequest $request)
|
|
|
|
{
|
2013-06-02 20:07:00 +02:00
|
|
|
// Protect against CSRF on destructive action
|
|
|
|
$token = $this->uploadForm()->getSecurityToken();
|
|
|
|
if(!$token->checkRequest($request)) return $this->httpError(400);
|
|
|
|
|
|
|
|
//create DataObject
|
2013-03-04 10:44:53 +01:00
|
|
|
$recordClass = $this->gridField->list->dataClass;
|
2012-07-16 22:39:01 +02:00
|
|
|
$record = Object::create($recordClass);
|
2013-03-04 10:44:53 +01:00
|
|
|
|
2013-02-02 13:45:52 +01:00
|
|
|
// passes the current gridfield-instance to a call-back method on the new object
|
|
|
|
$record->extend("onBulkImageUpload", $this->gridField);
|
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
//Write + add DO to gridField relation list
|
2012-07-16 22:39:01 +02:00
|
|
|
$record->write();
|
2013-03-04 10:44:53 +01:00
|
|
|
$this->gridField->list->add($record->ID);
|
2012-07-16 22:39:01 +02:00
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
//process upload and file attachement
|
|
|
|
$error = null;
|
|
|
|
$return = array();
|
2012-07-16 22:39:01 +02:00
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
// Get field details
|
|
|
|
$uploadField = $this->uploadForm()->Fields()->fieldByName('BulkUploadField');
|
|
|
|
//$fileRecordName = $this->getRecordImageField();
|
|
|
|
//$fileRecordName = substr( $fileRecordName, 0, strlen($fileRecordName)-2 );
|
|
|
|
//$uploadField->setName($fileRecordName); //File/Image obj where to save the upload
|
|
|
|
//$uploadField->setRecord($record); //record containing the File/Image obj
|
2012-07-16 22:39:01 +02:00
|
|
|
|
2013-06-02 20:07:00 +02:00
|
|
|
$postVars = $request->postVar('BulkUploadField');
|
|
|
|
|
|
|
|
//$uploadedFiles = $uploadField->extractUploadedFileData($postVars);
|
|
|
|
$uploadedFiles = $uploadField->extractFileData($postVars);
|
|
|
|
$tmpFile = reset($uploadedFiles);
|
|
|
|
//$file = $uploadField->saveTemporaryFile($tmpFile, $error);
|
|
|
|
//$file = $uploadField->saveTempFile($tmpFile, $error);
|
|
|
|
/*
|
|
|
|
if(empty($file)) {
|
|
|
|
$return = array('error' => $error);
|
|
|
|
} else {
|
|
|
|
//$return = $uploadField->encodeFileAttributes($file);
|
|
|
|
$return = $uploadField->encodeFileAttr($file);
|
|
|
|
}*/
|
|
|
|
|
|
|
|
$file = Object::create($this->getRecordImageClass());
|
|
|
|
|
|
|
|
// Get the uploaded file into a new file object.
|
|
|
|
try {
|
|
|
|
$upload = new Upload();
|
|
|
|
$upload->loadIntoFile($tmpFile, $file, $this->component->getConfig('folderName'));
|
|
|
|
} catch (Exception $e) {
|
|
|
|
// we shouldn't get an error here, but just in case
|
|
|
|
$return['error'] = $e->getMessage();
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
2013-06-02 20:07:00 +02:00
|
|
|
|
|
|
|
// Attach the file to the related record.
|
|
|
|
$record->setField($this->getRecordImageField(), $file->ID);
|
|
|
|
$record->write();
|
|
|
|
|
|
|
|
//get record's CMS Fields
|
|
|
|
$recordEditableFormFields = $this->getRecordHTMLFormFields( $record->ID );
|
|
|
|
|
|
|
|
// Collect all output data.
|
|
|
|
$return = array_merge($return, array(
|
|
|
|
'id' => $file->ID,
|
|
|
|
'name' => $file->getTitle() . '.' . $file->getExtension(),
|
|
|
|
'url' => $file->getURL(),
|
|
|
|
'preview_url' => $file->setHeight(55)->Link(),
|
|
|
|
'thumbnail_url' => $file->SetRatioSize(40,30)->getURL(),
|
|
|
|
'size' => $file->getAbsoluteSize(),
|
|
|
|
//'buttons' => $file->UploadFieldFileButtons,
|
|
|
|
'record' => array(
|
|
|
|
'ID' => $record->ID,
|
|
|
|
'fields' => $recordEditableFormFields
|
|
|
|
)
|
|
|
|
));
|
2012-07-18 01:12:10 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
$response = new SS_HTTPResponse(Convert::raw2json(array($return)));
|
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a record with the newly edited fields
|
|
|
|
*
|
|
|
|
* @param SS_HTTPRequest $request
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return string
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function update(SS_HTTPRequest $request)
|
2012-09-05 19:07:55 +02:00
|
|
|
{
|
2012-09-05 19:17:19 +02:00
|
|
|
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($request->requestVars());
|
2012-09-05 19:07:55 +02:00
|
|
|
$record = DataObject::get_by_id($this->gridField->list->dataClass, $data['ID']);
|
2012-07-16 22:39:01 +02:00
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
foreach($data as $field => $value)
|
2012-09-05 19:07:55 +02:00
|
|
|
{
|
|
|
|
if ( $record->hasMethod($field) ) {
|
|
|
|
$list = $record->$field();
|
|
|
|
$list->setByIDList( $value );
|
|
|
|
}else{
|
|
|
|
$record->setCastedField($field, $value);
|
|
|
|
}
|
|
|
|
}
|
2012-07-16 22:39:01 +02:00
|
|
|
$record->write();
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
return '{done:1,recordID:'.$data['ID'].'}';
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-18 01:12:10 +02:00
|
|
|
* Delete the Image Object and File as well as the DataObject
|
|
|
|
* according to the ID sent from the form
|
|
|
|
*
|
2012-07-16 22:39:01 +02:00
|
|
|
* @param SS_HTTPRequest $request
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return string json
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function cancel(SS_HTTPRequest $request)
|
|
|
|
{
|
2012-09-05 19:17:19 +02:00
|
|
|
$data = GridFieldBulkEditingHelper::unescapeFormFieldsPOSTData($request->requestVars());
|
2012-07-16 22:39:01 +02:00
|
|
|
$return = array();
|
|
|
|
|
|
|
|
$recordClass = $this->gridField->list->dataClass;
|
|
|
|
$record = DataObject::get_by_id($recordClass, $data['ID']);
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
$imageField = $this->getRecordImageField();
|
2012-07-16 22:39:01 +02:00
|
|
|
$imageID = $record->$imageField;
|
|
|
|
$image = DataObject::get_by_id('Image', $imageID);
|
|
|
|
|
|
|
|
$return[$data['ID']]['imageID'] = $imageID;
|
|
|
|
$return[$data['ID']]['deletedDataObject'] = DataObject::delete_by_id($recordClass, $data['ID']);
|
|
|
|
|
|
|
|
$return[$data['ID']]['deletedFormattedImages'] = $image->deleteFormattedImages();
|
|
|
|
$return[$data['ID']]['deletedImageFile'] = unlink( Director::getAbsFile($image->getRelativePath()) );
|
|
|
|
|
|
|
|
|
|
|
|
$response = new SS_HTTPResponse(Convert::raw2json($return));
|
|
|
|
$response->addHeader('Content-Type', 'text/plain');
|
|
|
|
return $response;
|
|
|
|
}
|
2012-07-18 01:12:10 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
/**
|
2012-07-18 14:23:51 +02:00
|
|
|
* Edited version of the GridFieldEditForm function
|
|
|
|
* adds the 'Bulk Upload' at the end of the crums
|
|
|
|
*
|
2012-07-16 22:39:01 +02: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.
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @author SilverStripe original Breadcrumbs() method
|
|
|
|
* @see GridFieldDetailForm_ItemRequest
|
2012-07-16 22:39:01 +02:00
|
|
|
* @param boolean $unlinked
|
|
|
|
* @return ArrayData
|
|
|
|
*/
|
|
|
|
function Breadcrumbs($unlinked = false) {
|
|
|
|
if(!$this->controller->hasMethod('Breadcrumbs')) return;
|
|
|
|
|
|
|
|
$items = $this->controller->Breadcrumbs($unlinked);
|
|
|
|
$items->push(new ArrayData(array(
|
|
|
|
'Title' => 'Bulk Upload',
|
|
|
|
'Link' => false
|
|
|
|
)));
|
|
|
|
return $items;
|
|
|
|
}
|
2013-03-28 18:24:22 +01:00
|
|
|
}
|