GridFieldBulkEditingTools/bulkUpload/code/GridFieldBulkUpload_Request.php

290 lines
7.1 KiB
PHP
Raw Normal View History

2014-04-05 18:54:50 +02:00
<?php
/**
2014-05-11 12:32:21 +02:00
* Handles request from the GridFieldBulkUpload component
2014-04-05 18:54:50 +02:00
*
* @author colymba
* @package GridFieldBulkEditingTools
2014-05-04 16:12:05 +02:00
* @subpackage BulkUpload
2014-04-05 18:54:50 +02:00
*/
2014-05-04 16:12:05 +02:00
class GridFieldBulkUpload_Request extends RequestHandler
{
2014-04-05 18:54:50 +02:00
/**
2014-05-11 12:32:21 +02:00
* Gridfield instance
2014-04-05 18:54:50 +02:00
* @var GridField
*/
protected $gridField;
2014-05-11 12:32:21 +02:00
2014-04-05 18:54:50 +02:00
/**
2014-05-11 12:32:21 +02:00
* Bulk upload component
* @var GridFieldBulkUpload
2014-04-05 18:54:50 +02:00
*/
protected $component;
2014-05-11 12:32:21 +02:00
2014-04-05 18:54:50 +02:00
/**
2014-05-11 12:32:21 +02:00
* Gridfield Form controller
2014-04-05 18:54:50 +02:00
* @var Controller
*/
protected $controller;
2014-05-11 12:32:21 +02:00
2014-04-05 18:54:50 +02:00
/**
2014-05-11 12:32:21 +02:00
* RequestHandler allowed actions
* @var array
2014-04-05 18:54:50 +02:00
*/
private static $allowed_actions = array(
2014-04-13 18:38:08 +02:00
'upload', 'select', 'attach', 'fileexists'
2014-04-05 18:54:50 +02:00
);
2014-05-11 12:32:21 +02:00
2014-04-05 18:54:50 +02:00
/**
2014-05-11 12:32:21 +02:00
* RequestHandler url => action map
* @var array
2014-04-05 18:54:50 +02:00
*/
private static $url_handlers = array(
'$Action!' => '$Action'
);
2014-05-11 12:32:21 +02:00
2014-04-05 18:54:50 +02:00
/**
2014-05-11 12:32:21 +02:00
* Handler's constructor
*
2014-04-05 18:54:50 +02:00
* @param GridFIeld $gridField
* @param GridField_URLHandler $component
* @param Controller $controller
*/
2014-05-04 16:12:05 +02:00
public function __construct($gridField, $component, $controller)
{
2014-04-05 18:54:50 +02:00
$this->gridField = $gridField;
$this->component = $component;
$this->controller = $controller;
parent::__construct();
}
/**
2014-04-13 18:38:08 +02:00
* Return the original component's UploadField
*
* @return UploadField UploadField instance as defined in the component
2014-04-05 18:54:50 +02:00
*/
public function getUploadField()
{
return $this->component->bulkUploadField($this->gridField);
}
/**
* 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
*
* @param SS_HTTPRequest $request
* @return string json
*/
public function upload(SS_HTTPRequest $request)
{
//create record
$recordClass = $this->gridField->list->dataClass;
$record = Object::create($recordClass);
$record->write();
// passes the current gridfield-instance to a call-back method on the new object
2014-05-11 21:23:03 +02:00
$record->extend("onBulkUpload", $this->gridField);
if ( $record->hasMethod('onBulkImageUpload') )
{
Deprecation::notice('2.0', '"onBulkImageUpload" callback is deprecated, use "onBulkUpload" instead.');
$record->extend("onBulkImageUpload", $this->gridField);
}
2014-04-05 18:54:50 +02:00
//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'];
$record->write();
// attached record to gridField relation
$this->gridField->list->add($record->ID);
2014-05-25 22:22:16 +02:00
// JS Template Data
$responseData = $this->newRecordJSTemplateData($record, $uploadResponse);
$response = new SS_HTTPResponse(Convert::raw2json(array($responseData)));
$response->addHeader('Content-Type', 'text/json');
return $response;
}
/**
* 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)
{
2014-04-07 18:46:27 +02:00
// fetch uploadedFile record and sort out previewURL
// update $uploadResponse datas in case changes happened onAfterWrite()
2014-04-05 18:54:50 +02:00
$uploadedFile = DataObject::get_by_id( $this->component->getFileRelationClassName($this->gridField), $uploadResponse['id'] );
2014-05-25 22:22:16 +02:00
2014-04-05 18:54:50 +02:00
if ( $uploadedFile )
{
$uploadResponse['name'] = $uploadedFile->Name;
$uploadResponse['url'] = $uploadedFile->getURL();
if ( $uploadedFile instanceof Image )
{
$uploadResponse['thumbnail_url'] = $uploadedFile->CroppedImage(30,30)->getURL();
}
else{
$uploadResponse['thumbnail_url'] = $uploadedFile->Icon();
}
2014-04-07 18:46:27 +02:00
// check if our new record has a Title, if not create one automatically
$title = $record->getTitle();
if ( !$title || $title === $record->ID )
{
$title = basename($uploadedFile->getFilename());
if ( $record->hasDatabaseField('Title') )
{
$record->Title = $title;
$record->write();
}
else if ($record->hasDatabaseField('Name')){
$record->Name = $title;
$record->write();
}
}
2014-04-05 18:54:50 +02:00
}
// Collect all data for JS template
$return = array_merge($uploadResponse, array(
'record' => array(
'id' => $record->ID
)
));
2014-05-25 22:22:16 +02:00
return $return;
2014-04-05 18:54:50 +02:00
}
2014-04-13 18:38:08 +02:00
/**
* Pass select request to UploadField
*
* @link UploadField->select()
*/
2014-04-05 18:54:50 +02:00
public function select(SS_HTTPRequest $request)
{
2014-05-25 22:22:16 +02:00
/*
2014-04-05 18:54:50 +02:00
$uploadField = $this->getUploadField();
return $uploadField->handleSelect($request);
2014-05-25 22:22:16 +02:00
*/
$uploadField = $this->getUploadField();
return UploadField_SelectHandler::create($this, $uploadField->getFolderName());
2014-04-05 18:54:50 +02:00
}
2014-04-13 18:38:08 +02:00
/**
2014-05-25 22:22:16 +02:00
* Pass getRelationAutosetClass request to UploadField
* Used by select dialog
2014-04-13 18:38:08 +02:00
*
2014-05-25 22:22:16 +02:00
* @link UploadField->getRelationAutosetClass()
2014-04-13 18:38:08 +02:00
*/
2014-05-25 22:22:16 +02:00
public function getRelationAutosetClass($default = 'File')
{
$uploadField = $this->getUploadField();
return $uploadField->getRelationAutosetClass($default);
}
/**
* Pass getAllowedMaxFileNumber request to UploadField
* Used by select dialog
*
* @link UploadField->getAllowedMaxFileNumber()
*/
public function getAllowedMaxFileNumber()
2014-04-05 18:54:50 +02:00
{
$uploadField = $this->getUploadField();
2014-05-25 22:22:16 +02:00
return $uploadField->getAllowedMaxFileNumber();
}
/**
* Retrieve Files to be attached
* and generated DataObjects for each one
*
* @param SS_HTTPRequest $request
* @return SS_HTTPResponse
*/
public function attach(SS_HTTPRequest $request)
{
$uploadField = $this->getUploadField();
$attachResponses = $uploadField->attach($request);
$attachResponses = json_decode($attachResponses->getBody(), true);
$fileRelationName = $uploadField->getName();
$recordClass = $this->gridField->list->dataClass;
$return = array();
foreach ($attachResponses as $attachResponse)
{
// create record
$record = Object::create($recordClass);
$record->write();
$record->extend("onBulkUpload", $this->gridField);
// attach file
$record->{"{$fileRelationName}ID"} = $attachResponse['id'];
$record->write();
// attached record to gridField relation
$this->gridField->list->add($record->ID);
// JS Template Data
$responseData = $this->newRecordJSTemplateData($record, $attachResponse);
// add to returned dataset
array_push($return, $responseData);
}
$response = new SS_HTTPResponse(Convert::raw2json($return));
$response->addHeader('Content-Type', 'text/json');
return $response;
2014-04-05 18:54:50 +02:00
}
2014-04-13 18:38:08 +02:00
/**
* Pass fileexists request to UploadField
*
* @link UploadField->fileexists()
*/
2014-04-05 18:54:50 +02:00
public function fileexists(SS_HTTPRequest $request)
{
$uploadField = $this->getUploadField();
return $uploadField->fileexists($request);
}
2014-05-25 22:22:16 +02:00
/**
* @param string $action
* @return string
*/
public function Link($action = null) {
return Controller::join_links($this->gridField->Link(), '/bulkupload/', $action);
}
2014-04-13 18:38:08 +02:00
}