2012-07-16 22:39:01 +02:00
|
|
|
<?php
|
2012-07-18 14:23:51 +02:00
|
|
|
/**
|
|
|
|
* GridField component for uploading images in bulk
|
|
|
|
*/
|
|
|
|
class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLHandler {
|
2012-07-16 22:39:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Target record Image foreign key field name
|
2012-07-18 14:23:51 +02:00
|
|
|
*
|
|
|
|
* @var string
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
protected $recordImageFieldName;
|
|
|
|
|
|
|
|
/**
|
2012-07-18 14:23:51 +02:00
|
|
|
* Target record editable fields
|
|
|
|
*
|
|
|
|
* @var array
|
2012-07-16 22:39:01 +02:00
|
|
|
*/
|
|
|
|
protected $recordEditableFields;
|
|
|
|
|
2012-07-20 20:02:45 +02:00
|
|
|
/**
|
|
|
|
* component configuration
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $config = array(
|
|
|
|
'imageFieldName' => null,
|
|
|
|
'editableFields' => null,
|
|
|
|
'fieldsClassBlacklist' => array( 'GridField', 'UploadField' ),
|
|
|
|
'fieldsNameBlacklist' => array()
|
|
|
|
);
|
|
|
|
|
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-07-16 22:39:01 +02:00
|
|
|
{
|
2012-07-18 01:12:10 +02:00
|
|
|
$this->setRecordImageField($imageField);
|
|
|
|
|
|
|
|
if ( !is_array($editableFields) && $editableFields != null ) $editableFields = array($editableFields);
|
|
|
|
$this->setRecordEditableFields($editableFields);
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 20:02:45 +02:00
|
|
|
function setConfig ( $reference, $value )
|
|
|
|
{
|
|
|
|
if ( isset( $this->config[$reference] ) )
|
|
|
|
{
|
|
|
|
if ( ($reference == 'fieldsClassBlacklist' || $reference == 'fieldsClassBlacklist') && !is_array($value) )
|
|
|
|
{
|
|
|
|
$value = array($value);
|
|
|
|
}
|
|
|
|
$this->$config[$reference] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 )
|
|
|
|
{
|
|
|
|
if (key_exists($className, $this->config['fieldsNameBlacklist'])) {
|
|
|
|
return delete( $this->config['fieldsNameBlacklist'][$className] );
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @param string $field
|
2012-07-18 01:12:10 +02:00
|
|
|
*/
|
|
|
|
function setRecordImageField($field)
|
2012-07-16 22:39:01 +02:00
|
|
|
{
|
2012-07-18 01:12:10 +02:00
|
|
|
$this->recordImageFieldName = $field;
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @param array $fields
|
2012-07-18 01:12:10 +02:00
|
|
|
*/
|
|
|
|
function setRecordEditableFields($fields)
|
2012-07-16 22:39:01 +02:00
|
|
|
{
|
|
|
|
$this->recordEditableFields = $fields;
|
|
|
|
}
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return string
|
2012-07-18 01:12:10 +02:00
|
|
|
*/
|
2012-07-16 22:39:01 +02:00
|
|
|
public function getRecordImageField()
|
|
|
|
{
|
2012-07-18 01:12:10 +02:00
|
|
|
return $this->recordImageFieldName;
|
2012-07-16 22:39:01 +02:00
|
|
|
}
|
|
|
|
|
2012-07-18 01:12:10 +02:00
|
|
|
/**
|
|
|
|
*
|
2012-07-18 14:23:51 +02:00
|
|
|
* @return string
|
2012-07-18 01:12:10 +02:00
|
|
|
*/
|
2012-07-16 22:39:01 +02:00
|
|
|
public function getRecordEditableFields()
|
|
|
|
{
|
|
|
|
return $this->recordEditableFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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(
|
|
|
|
'toolbar-header-right' => $data->renderWith('GridFieldAddNewbutton')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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());
|
|
|
|
}
|
|
|
|
}
|