2012-07-16 22:39:01 +02:00
|
|
|
<?php
|
2012-07-18 14:23:51 +02:00
|
|
|
/**
|
2012-08-07 22:51:54 +02:00
|
|
|
* GridField component for uploading images in bulk
|
|
|
|
*
|
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
2012-07-18 14:23:51 +02:00
|
|
|
*/
|
|
|
|
class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLHandler {
|
2012-07-20 20:15:53 +02:00
|
|
|
|
2012-07-20 20:02:45 +02:00
|
|
|
/**
|
|
|
|
* component configuration
|
|
|
|
*
|
2012-07-20 20:15:53 +02:00
|
|
|
* 'imageFieldName' => field name of the $has_one Model Image relation
|
|
|
|
* 'editableFields' => fields editable on the Model
|
|
|
|
* 'fieldsClassBlacklist' => field types that will be removed from the automatic form generation
|
|
|
|
* 'fieldsNameBlacklist' => fields that will be removed from the automatic form generation
|
|
|
|
*
|
2012-07-20 20:02:45 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $config = array(
|
|
|
|
'imageFieldName' => null,
|
|
|
|
'editableFields' => null,
|
2012-08-09 19:24:33 +02:00
|
|
|
'fieldsClassBlacklist' => array(),
|
2012-08-07 21:40:12 +02:00
|
|
|
'fieldsNameBlacklist' => array(),
|
|
|
|
'folderName' => 'bulkUpload'
|
2012-07-20 20:02:45 +02:00
|
|
|
);
|
2012-08-09 19:24:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds any class that should not be used as they break the component
|
|
|
|
* These cannot be removed from the blacklist
|
|
|
|
*/
|
|
|
|
protected $forbiddenFieldsClasses = array( 'GridField', 'UploadField' );
|
2012-07-20 20:02:45 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
/**
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @param string $imageField
|
|
|
|
* @param string/array $editableFields
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
2012-07-18 01:12:10 +02:00
|
|
|
public function __construct($imageField = null, $editableFields = null)
|
2012-08-07 22:51:54 +02:00
|
|
|
{
|
2012-07-20 20:15:53 +02:00
|
|
|
if ( $imageField != null ) $this->setConfig ( 'imageFieldName', $imageField );
|
|
|
|
if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields );
|
2012-08-09 19:24:33 +02:00
|
|
|
|
|
|
|
//init classes blacklist with forbidden classes
|
|
|
|
$this->config['fieldsClassBlacklist'] = $this->forbiddenFieldsClasses;
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:15:53 +02:00
|
|
|
/**
|
|
|
|
* Set a component configuration parameter
|
|
|
|
*
|
|
|
|
* @param string $reference
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2012-07-20 20:02:45 +02:00
|
|
|
function setConfig ( $reference, $value )
|
|
|
|
{
|
|
|
|
if ( isset( $this->config[$reference] ) )
|
|
|
|
{
|
2012-07-20 20:15:53 +02:00
|
|
|
if ( ($reference == 'fieldsClassBlacklist' || $reference == 'fieldsClassBlacklist' || $reference == 'editableFields') && !is_array($value) )
|
2012-07-20 20:02:45 +02:00
|
|
|
{
|
|
|
|
$value = array($value);
|
|
|
|
}
|
2012-08-09 19:24:33 +02:00
|
|
|
|
|
|
|
//makes sure $forbiddenFieldsClasses are in no matter what
|
|
|
|
if ( $reference == 'fieldsClassBlacklist' )
|
|
|
|
{
|
|
|
|
$value = array_unique( array_merge($value, $this->forbiddenFieldsClasses) );
|
|
|
|
}
|
|
|
|
|
2012-07-20 20:15:53 +02:00
|
|
|
$this->config[$reference] = $value;
|
2012-07-20 20:02:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns one $config parameter of the full $config
|
|
|
|
*
|
|
|
|
* @param string $reference $congif parameter to return
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function getConfig ( $reference = false )
|
|
|
|
{
|
|
|
|
if ( $reference ) return $this->config[$reference];
|
|
|
|
else return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a field to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function addFieldNameToBlacklist ( $fieldName )
|
|
|
|
{
|
|
|
|
return array_push( $this->config['fieldsNameBlacklist'], $fieldName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a class to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function addClassToBlacklist ( $className )
|
|
|
|
{
|
|
|
|
return array_push( $this->config['fieldsClassBlacklist'], $className);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a field to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $fieldName
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function removeFieldNameFromBlacklist ( $fieldName )
|
|
|
|
{
|
|
|
|
if (key_exists($fieldName, $this->config['fieldsNameBlacklist'])) {
|
|
|
|
return delete( $this->config['fieldsNameBlacklist'][$fieldName] );
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a class to the editable fields blacklist
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function removeClassFromBlacklist ( $className )
|
|
|
|
{
|
2012-08-09 19:24:33 +02:00
|
|
|
if (key_exists($className, $this->config['fieldsNameBlacklist']) && !in_array($className, $this->forbiddenFieldsClasses)) {
|
2012-07-20 20:02:45 +02:00
|
|
|
return delete( $this->config['fieldsNameBlacklist'][$className] );
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2012-07-20 20:15:53 +02:00
|
|
|
|
2012-07-16 22:39:01 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return array
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function getHTMLFragments($gridField) {
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'NewLink' => $gridField->Link('bulkimageupload'),
|
|
|
|
'ButtonName' => 'Bulk Upload'
|
|
|
|
));
|
|
|
|
|
|
|
|
return array(
|
2012-08-07 22:51:54 +02:00
|
|
|
'bulk-edit-tools' => $data->renderWith('GridFieldAddNewbutton')
|
2012-07-16 22:39:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return array
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function getURLHandlers($gridField) {
|
|
|
|
return array(
|
|
|
|
'bulkimageupload' => 'handleBulkUpload'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-18 14:23:51 +02:00
|
|
|
* Pass control over to the RequestHandler
|
|
|
|
*
|
|
|
|
* @param GridField $gridField
|
|
|
|
* @param SS_HTTPRequest $request
|
|
|
|
* @return mixed
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
public function handleBulkUpload($gridField, $request)
|
|
|
|
{
|
|
|
|
$controller = $gridField->getForm()->Controller();
|
|
|
|
$handler = new GridFieldBulkImageUpload_Request($gridField, $this, $controller);
|
|
|
|
|
|
|
|
return $handler->handleRequest($request, DataModel::inst());
|
|
|
|
}
|
|
|
|
}
|