mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
upload is now done by UploadField
UploadField does all the uploading and Image/File object creation. Record creation and link to gridField relation is done by upload method
This commit is contained in:
parent
290a2460cd
commit
3fa751d301
@ -1,25 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
class GFBIUploadField extends UploadField
|
|
||||||
{
|
|
||||||
public function setConfig($key, $val) {
|
|
||||||
$this->ufConfig[$key] = $val;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractFileData($postvars)
|
|
||||||
{
|
|
||||||
return $this->extractUploadedFileData($postvars);
|
|
||||||
}
|
|
||||||
|
|
||||||
function saveTempFile($tmpFile, &$error = null)
|
|
||||||
{
|
|
||||||
return $this->saveTemporaryFile($tmpFile, $error);
|
|
||||||
}
|
|
||||||
|
|
||||||
function encodeFileAttr(File $file)
|
|
||||||
{
|
|
||||||
return $this->encodeFileAttributes($file);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -206,7 +206,8 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
|
|||||||
/* *
|
/* *
|
||||||
* UploadField
|
* UploadField
|
||||||
*/
|
*/
|
||||||
$uploadField = GFBIUploadField::create('BulkUploadField', '');
|
$imageRealtionName = $this->getRecordImageClass();
|
||||||
|
$uploadField = UploadField::create($imageRealtionName, '');
|
||||||
$uploadField->setConfig('previewMaxWidth', 40);
|
$uploadField->setConfig('previewMaxWidth', 40);
|
||||||
$uploadField->setConfig('previewMaxHeight', 30);
|
$uploadField->setConfig('previewMaxHeight', 30);
|
||||||
$uploadField->addExtraClass('ss-assetuploadfield');
|
$uploadField->addExtraClass('ss-assetuploadfield');
|
||||||
@ -315,89 +316,48 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process image upload and Object creation
|
* Process upload through UploadField,
|
||||||
* Create new DataObject and add image relation
|
* creates new record and link newly uploaded file
|
||||||
* returns Image data and editable Fields forms
|
* adds record to GrifField relation list
|
||||||
*
|
* and return image/file data and record edit form
|
||||||
* 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()
|
|
||||||
* @param SS_HTTPRequest $request
|
* @param SS_HTTPRequest $request
|
||||||
* @return string json
|
* @return string json
|
||||||
*/
|
*/
|
||||||
public function upload(SS_HTTPRequest $request)
|
public function upload(SS_HTTPRequest $request)
|
||||||
{
|
{
|
||||||
// Protect against CSRF on destructive action
|
//create record
|
||||||
$token = $this->uploadForm()->getSecurityToken();
|
|
||||||
if(!$token->checkRequest($request)) return $this->httpError(400);
|
|
||||||
|
|
||||||
//create DataObject
|
|
||||||
$recordClass = $this->gridField->list->dataClass;
|
$recordClass = $this->gridField->list->dataClass;
|
||||||
$record = Object::create($recordClass);
|
$record = Object::create($recordClass);
|
||||||
|
$record->write();
|
||||||
|
|
||||||
// passes the current gridfield-instance to a call-back method on the new object
|
// passes the current gridfield-instance to a call-back method on the new object
|
||||||
$record->extend("onBulkImageUpload", $this->gridField);
|
$record->extend("onBulkImageUpload", $this->gridField);
|
||||||
|
|
||||||
//Write + add DO to gridField relation list
|
//get uploadField and process upload
|
||||||
|
$imageRelationName = $this->getRecordImageClass();
|
||||||
|
$uploadField = $this->uploadForm()->Fields()->fieldByName($imageRelationName);
|
||||||
|
$uploadField->setRecord($record);
|
||||||
|
$uploadResponse = $uploadField->upload( $request );
|
||||||
|
|
||||||
|
//get uploaded File
|
||||||
|
$uploadResponse = Convert::json2array( $uploadResponse->getBody() );
|
||||||
|
$uploadResponse = array_shift( $uploadResponse );
|
||||||
|
$uploadedFile = DataObject::get_by_id( $imageRelationName, $uploadResponse['id'] );
|
||||||
|
|
||||||
|
// Attach the file to record.
|
||||||
|
$record->{"{$imageRelationName}ID"} = $uploadedFile->ID;
|
||||||
$record->write();
|
$record->write();
|
||||||
|
|
||||||
|
// attached record to gridField relation
|
||||||
$this->gridField->list->add($record->ID);
|
$this->gridField->list->add($record->ID);
|
||||||
|
|
||||||
//process upload and file attachement
|
|
||||||
$error = null;
|
|
||||||
$return = array();
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
$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();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach the file to the related record.
|
|
||||||
$record->setField($this->getRecordImageField(), $file->ID);
|
|
||||||
$record->write();
|
|
||||||
|
|
||||||
//get record's CMS Fields
|
//get record's CMS Fields
|
||||||
$recordEditableFormFields = $this->getRecordHTMLFormFields( $record->ID );
|
$recordEditableFormFields = $this->getRecordHTMLFormFields( $record->ID );
|
||||||
|
|
||||||
// Collect all output data.
|
// Collect all output data.
|
||||||
$return = array_merge($return, array(
|
$return = array_merge($uploadResponse, array(
|
||||||
'id' => $file->ID,
|
'preview_url' => $uploadedFile->setHeight(55)->Link(),
|
||||||
'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(
|
'record' => array(
|
||||||
'ID' => $record->ID,
|
'ID' => $record->ID,
|
||||||
'fields' => $recordEditableFormFields
|
'fields' => $recordEditableFormFields
|
||||||
|
Loading…
Reference in New Issue
Block a user