mirror of
https://github.com/colymba/GridFieldBulkEditingTools.git
synced 2024-10-22 11:05:57 +02:00
Cleaning stuff up ✨
Removing old code, unsed methods, deprecated classes, obscure stuff and all sorts of junk
This commit is contained in:
parent
de2d3234f4
commit
3d3690baec
@ -2,14 +2,13 @@
|
||||
|
||||
namespace Colymba\BulkUpload;
|
||||
|
||||
use SilverStripe\Assets\Image;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Control\RequestHandler;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Control\HTTPResponse;
|
||||
use SilverStripe\Core\Convert;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
//use SilverStripe\Core\Injector\Injector;
|
||||
//use SilverStripe\ORM\DataObject;
|
||||
|
||||
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
|
||||
|
||||
@ -47,7 +46,7 @@ class BulkUploadHandler extends RequestHandler
|
||||
* @var array
|
||||
*/
|
||||
private static $allowed_actions = array(
|
||||
'upload', 'attach', 'fileexists', 'select'
|
||||
'upload', 'attach'
|
||||
);
|
||||
|
||||
/**
|
||||
@ -74,19 +73,9 @@ class BulkUploadHandler extends RequestHandler
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the original component's UploadField.
|
||||
*
|
||||
* @return UploadField UploadField instance as defined in the component
|
||||
*/
|
||||
public function getUploadField()
|
||||
{
|
||||
return $this->component->bulkUploadField($this->gridField);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new DataObject
|
||||
* Add uploaded file to the Dataobject
|
||||
* Add file ID to the Dataobject
|
||||
* Add DataObject to Gridfield list
|
||||
* Publish DataObject if enabled
|
||||
*
|
||||
@ -116,51 +105,13 @@ class BulkUploadHandler extends RequestHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* Process upload through UploadField,
|
||||
* creates new record and link newly uploaded file
|
||||
* adds record to GrifField relation list
|
||||
* and return image/file data and record edit form.
|
||||
* Process upload through AssetAdmin::apiCreateFile,
|
||||
* uses result file ID to create the DataObject.
|
||||
*
|
||||
* @param HTTPRequest $request
|
||||
*
|
||||
* @return string json
|
||||
*/
|
||||
/*public function upload(HTTPRequest $request)
|
||||
{
|
||||
//create record
|
||||
$recordClass = $this->component->getRecordClassName($this->gridField);
|
||||
$record = Injector::inst()->create($recordClass);
|
||||
$record->write();
|
||||
|
||||
// passes the current gridfield-instance to a call-back method on the new object
|
||||
$record->extend('onBulkUpload', $this->gridField);
|
||||
|
||||
//get uploadField and process upload
|
||||
$uploadField = $this->getUploadField();
|
||||
$uploadField->setRecord($record);
|
||||
|
||||
$fileRelationName = $uploadField->getName();
|
||||
$uploadResponse = $uploadField->upload($request);
|
||||
|
||||
//get uploaded File response datas
|
||||
$uploadResponse = Convert::json2array($uploadResponse->getBody());
|
||||
$uploadResponse = array_shift($uploadResponse);
|
||||
|
||||
// Attach the file to record.
|
||||
$record->{"{$fileRelationName}ID"} = $uploadResponse['id'];
|
||||
|
||||
// attached record to gridField relation
|
||||
$this->gridField->list->add($record);
|
||||
|
||||
// JS Template Data
|
||||
$responseData = $this->newRecordJSTemplateData($record, $uploadResponse);
|
||||
|
||||
$response = new HTTPResponse(Convert::raw2json(array($responseData)));
|
||||
$this->contentTypeNegotiation($response);
|
||||
|
||||
return $response;
|
||||
}*/
|
||||
|
||||
public function upload(HTTPRequest $request)
|
||||
{
|
||||
$assetAdmin = AssetAdmin::singleton();
|
||||
@ -177,86 +128,6 @@ class BulkUploadHandler extends RequestHandler
|
||||
return $uploadResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the Upload/Attach response from the UploadField
|
||||
* with the new DataObject records for the JS template.
|
||||
*
|
||||
* @param DataObject $record Newly create DataObject record
|
||||
* @param array $uploadResponse Upload or Attach response from UploadField
|
||||
*
|
||||
* @return array Updated $uploadResponse with $record data
|
||||
*/
|
||||
protected function newRecordJSTemplateData(DataObject &$record, &$uploadResponse)
|
||||
{
|
||||
// fetch uploadedFile record and sort out previewURL
|
||||
// update $uploadResponse datas in case changes happened onAfterWrite()
|
||||
$uploadedFile = DataObject::get_by_id(
|
||||
$this->component->getFileRelationClassName($this->gridField),
|
||||
$uploadResponse['id']
|
||||
);
|
||||
|
||||
if ($uploadedFile) {
|
||||
$uploadResponse['name'] = $uploadedFile->Name;
|
||||
$uploadResponse['url'] = $uploadedFile->getURL();
|
||||
|
||||
if ($uploadedFile instanceof Image) {
|
||||
$uploadResponse['thumbnail_url'] = $uploadedFile->Fill(30, 30)->getURL();
|
||||
} else {
|
||||
$uploadResponse['thumbnail_url'] = $uploadedFile->IconTag();
|
||||
}
|
||||
|
||||
// check if our new record has a Title, if not create one automatically
|
||||
$title = $record->getTitle();
|
||||
if (!$title || $title === $record->ID) {
|
||||
if ($record->hasDatabaseField('Title')) {
|
||||
$record->Title = $uploadedFile->Title;
|
||||
$record->write();
|
||||
} elseif ($record->hasDatabaseField('Name')) {
|
||||
$record->Name = $uploadedFile->Title;
|
||||
$record->write();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Collect all data for JS template
|
||||
$return = array_merge($uploadResponse, array(
|
||||
'record' => array(
|
||||
'id' => $record->ID,
|
||||
),
|
||||
));
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass getRelationAutosetClass request to UploadField
|
||||
* Used by select dialog.
|
||||
*
|
||||
* @link UploadField->getRelationAutosetClass()
|
||||
* @param string $default
|
||||
* @return string
|
||||
*/
|
||||
public function getRelationAutosetClass($default = 'SilverStripe\\Assets\\File')
|
||||
{
|
||||
$uploadField = $this->getUploadField();
|
||||
|
||||
return $uploadField->getRelationAutosetClass($default);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass getAllowedMaxFileNumber request to UploadField
|
||||
* Used by select dialog.
|
||||
*
|
||||
* @link UploadField->getAllowedMaxFileNumber()
|
||||
* @return int|null
|
||||
*/
|
||||
public function getAllowedMaxFileNumber()
|
||||
{
|
||||
$uploadField = $this->getUploadField();
|
||||
|
||||
return $uploadField->getAllowedMaxFileNumber();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve File to be attached
|
||||
* and generated DataObjects for each one.
|
||||
@ -278,32 +149,6 @@ class BulkUploadHandler extends RequestHandler
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass select request to UploadField.
|
||||
*
|
||||
* @link UploadField->select()
|
||||
*/
|
||||
public function select(HTTPRequest $request)
|
||||
{
|
||||
|
||||
$uploadField = $this->getUploadField();
|
||||
$uploadField->setRequest($request);
|
||||
|
||||
return $uploadField->handleSelect($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass fileexists request to UploadField.
|
||||
*
|
||||
* @link UploadField->fileexists()
|
||||
*/
|
||||
public function fileexists(HTTPRequest $request)
|
||||
{
|
||||
$uploadField = $this->getUploadField();
|
||||
|
||||
return $uploadField->fileexists($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $action
|
||||
*
|
||||
@ -313,24 +158,4 @@ class BulkUploadHandler extends RequestHandler
|
||||
{
|
||||
return Controller::join_links($this->gridField->Link(), '/bulkupload/', $action);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets response 'Content-Type' depending on browser capabilities
|
||||
* e.g. IE needs text/plain for iframe transport
|
||||
* https://github.com/blueimp/jQuery-File-Upload/issues/1795.
|
||||
*
|
||||
* @param HTTPResponse $response HTTP Response to set content-type on
|
||||
*/
|
||||
protected function contentTypeNegotiation(&$response)
|
||||
{
|
||||
if (isset($_SERVER['HTTP_ACCEPT'])
|
||||
&& ((strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)
|
||||
|| $_SERVER['HTTP_ACCEPT'] === '*/*'
|
||||
)
|
||||
) {
|
||||
$response->addHeader('Content-Type', 'application/json');
|
||||
} else {
|
||||
$response->addHeader('Content-Type', 'text/plain');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -41,24 +41,6 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
*/
|
||||
protected $autoPublishDataObject = false;
|
||||
|
||||
/**
|
||||
* UploadField configuration.
|
||||
* These options are passed on directly to the UploadField
|
||||
* via {@link UploadField::setConfig()} api.
|
||||
*
|
||||
* Defaults are:
|
||||
* 'sequentialUploads' => false : process uploads 1 after the other rather than all at once
|
||||
* 'canAttachExisting' => true : displays "From files" button in the UploadField
|
||||
* 'canPreviewFolder' => true : displays the upload location in the UploadField
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $ufConfig = array(
|
||||
'sequentialUploads' => false,
|
||||
'canAttachExisting' => true,
|
||||
'canPreviewFolder' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* UploadField setup function calls.
|
||||
* List of setup functions to call on {@link UploadField} with the value to pass.
|
||||
@ -72,19 +54,6 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
'setFolderName' => 'bulkUpload',
|
||||
);
|
||||
|
||||
/**
|
||||
* UploadField Validator setup function calls.
|
||||
* List of setup functions to call on {@link Upload::validator} with the value to pass.
|
||||
*
|
||||
* e.g. array('setAllowedMaxFileSize' => 10) will result in:
|
||||
* $uploadField->getValidator()->setAllowedMaxFileSize(10)
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $ufValidatorSetup = array(
|
||||
'setAllowedMaxFileSize' => null,
|
||||
);
|
||||
|
||||
/**
|
||||
* Component constructor.
|
||||
*
|
||||
@ -144,19 +113,6 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
return $this->autoPublishDataObject;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an UploadField configuration parameter.
|
||||
*
|
||||
* @param string $reference
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setUfConfig($reference, $value)
|
||||
{
|
||||
$this->ufConfig[$reference] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an UploadField setup function call.
|
||||
*
|
||||
@ -170,19 +126,6 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set an UploadField Validator setup function call.
|
||||
*
|
||||
* @param string $function
|
||||
* @param mixed $param
|
||||
*/
|
||||
public function setUfValidatorSetup($function, $param)
|
||||
{
|
||||
$this->ufValidatorSetup[$function] = $param;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one $config reference or the full $config.
|
||||
*
|
||||
@ -199,22 +142,6 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one $ufConfig reference or the full config.
|
||||
*
|
||||
* @param string $reference $ufConfig parameter to return
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUfConfig($reference = false)
|
||||
{
|
||||
if ($reference) {
|
||||
return $this->ufConfig[$reference];
|
||||
} else {
|
||||
return $this->ufConfig;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one $ufSetup reference or the full config.
|
||||
*
|
||||
@ -231,22 +158,6 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns one $ufValidatorSetup reference or the full config.
|
||||
*
|
||||
* @param string $reference $ufValidatorSetup parameter to return
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getUfValidatorSetup($reference = false)
|
||||
{
|
||||
if ($reference) {
|
||||
return $this->ufValidatorSetup[$reference];
|
||||
} else {
|
||||
return $this->ufValidatorSetup;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the class name of container `DataObject` record.
|
||||
* Either as set in the component config or from the `Gridfield` `dataClass`.
|
||||
@ -333,43 +244,14 @@ class BulkUploader implements GridField_HTMLProvider, GridField_URLHandler
|
||||
$fieldName = $fileRelationName . '_' . $this->getRecordClassName($gridField) . '_BU';
|
||||
$uploadField = BulkUploadField::create($gridField, $fieldName, '')
|
||||
->setForm($gridField->getForm())
|
||||
|
||||
/*->setConfig('previewMaxWidth', 20)
|
||||
->setConfig('previewMaxHeight', 20)
|
||||
->setConfig('changeDetection', false)*/
|
||||
|
||||
->setRecord(DataObject::create()) // avoid UploadField to get auto-config from the Page (e.g fix allowedMaxFileNumber)
|
||||
|
||||
/*->setTemplate('BulkUploadField')
|
||||
->setDownloadTemplateName('colymba-bulkuploaddownloadtemplate')
|
||||
|
||||
->setConfig('url', $gridField->Link('bulkupload/upload'))
|
||||
->setConfig('urlSelectDialog', $gridField->Link('bulkupload/select'))
|
||||
->setConfig('urlAttach', $gridField->Link('bulkupload/attach'))
|
||||
->setConfig('urlFileExists', $gridField->Link('bulkupload/fileexists'))*/
|
||||
;
|
||||
|
||||
/*
|
||||
//set UploadField config
|
||||
foreach ($this->ufConfig as $key => $val) {
|
||||
$uploadField->setConfig($key, $val);
|
||||
}
|
||||
*/
|
||||
|
||||
//UploadField setup
|
||||
foreach ($this->ufSetup as $fn => $param) {
|
||||
$uploadField->{$fn}($param);
|
||||
}
|
||||
|
||||
/*
|
||||
//UploadField Validator setup
|
||||
foreach ($this->ufValidatorSetup as $fn => $param) {
|
||||
$uploadField->getValidator()->{$fn}($param);
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
$schema['data']['createFileEndpoint'] = [
|
||||
'url' => $gridField->Link('bulkupload/upload'),
|
||||
'method' => 'post',
|
||||
|
@ -1,33 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Colymba\BulkUpload;
|
||||
|
||||
use Colymba\BulkUpload\BulkUploader;
|
||||
use SilverStripe\Dev\Deprecation;
|
||||
|
||||
/**
|
||||
* Legacy GridFieldBulkImageUpload component.
|
||||
*
|
||||
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link BulkUploader} class instead.
|
||||
*
|
||||
* @author colymba
|
||||
*/
|
||||
class GridFieldBulkImageUpload extends BulkUploader
|
||||
{
|
||||
/**
|
||||
* Component constructor.
|
||||
*
|
||||
* @deprecated 2.0 "GridFieldBulkImageUpload" is deprecated, use {@link BulkUploader} class instead.
|
||||
*
|
||||
* @param string $fileRelationName
|
||||
*/
|
||||
public function __construct($fileRelationName = null)
|
||||
{
|
||||
Deprecation::notice(
|
||||
'2.0',
|
||||
'"GridFieldBulkImageUpload" is deprecated, use "BulkUploader" class instead.'
|
||||
);
|
||||
|
||||
return new BulkUploader($fileRelationName);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user