API img field config now fileRelationName FIX 40

parameter is to be passed without 'ID' at the end. Implementation is now
more consistent.
This commit is contained in:
colymba 2013-06-26 19:46:46 +03:00
parent 35bfd865ec
commit 6a105b9a96
3 changed files with 64 additions and 69 deletions

View File

@ -31,10 +31,10 @@ Simplest usage, add the component to your GridField as below. The component will
### Usage 2 ### Usage 2
You can specify which Image field to use and which fields are editable from the managed Model You can specify which Image field to use and which fields are editable from the managed Model
$imageField (string): The name of the image field to use (should have 'ID' at the end: If your relation is set has 'MyImage' => 'Image', the parameter should be 'MyImageID') $fileRelationName (string): The name of the File/Image field to use (If your relation is set has 'MyImage' => 'Image', the parameter should be 'MyImage')
$editableFields (array): list of db fields name as string that will be editable like: array('myTextField', 'myVarcharField', 'myEnumField') $editableFields (array): list of db fields name as string that will be editable like: array('myTextField', 'myVarcharField', 'myEnumField')
$config->addComponent(new GridFieldBulkImageUpload( $imageField, $editableFields )); $config->addComponent(new GridFieldBulkImageUpload( $fileRelationName, $editableFields ));
### Configuration ### Configuration
The component's option can be configurated individually or in bulk through the 'config' functions like this: The component's option can be configurated individually or in bulk through the 'config' functions like this:
@ -43,7 +43,7 @@ The component's option can be configurated individually or in bulk through the '
#### $config overview #### $config overview
The available configuration options are: The available configuration options are:
* 'imageFieldName' : sets the name of the Image field of the managed Model (i.e. 'MyImageID') * 'fileRelationName' : sets the name of the File/Image field of the managed Model (i.e. 'MyImage')
* 'editableFields' : array of string referencing specific CMS fields available for editing * 'editableFields' : array of string referencing specific CMS fields available for editing
* 'fieldsClassBlacklist' : array of string referencing types (ClassName) of fields that wont be available for editing * 'fieldsClassBlacklist' : array of string referencing types (ClassName) of fields that wont be available for editing
* 'fieldsNameBlacklist' : array of string referencing the names of fields that wont be available for editing * 'fieldsNameBlacklist' : array of string referencing the names of fields that wont be available for editing

View File

@ -10,7 +10,7 @@ class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLH
/** /**
* component configuration * component configuration
* *
* 'imageFieldName' => field name of the $has_one Model Image relation * 'fileRelationName' => field name of the $has_one File/Image relation
* 'editableFields' => fields editable on the Model * 'editableFields' => fields editable on the Model
* 'fieldsClassBlacklist' => field types that will be removed from the automatic form generation * 'fieldsClassBlacklist' => field types that will be removed from the automatic form generation
* 'fieldsNameBlacklist' => fields that will be removed from the automatic form generation * 'fieldsNameBlacklist' => fields that will be removed from the automatic form generation
@ -18,7 +18,7 @@ class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLH
* @var array * @var array
*/ */
protected $config = array( protected $config = array(
'imageFieldName' => null, 'fileRelationName' => null,
'editableFields' => null, 'editableFields' => null,
'fieldsClassBlacklist' => array(), 'fieldsClassBlacklist' => array(),
'fieldsNameBlacklist' => array(), 'fieldsNameBlacklist' => array(),
@ -35,12 +35,12 @@ class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLH
/** /**
* *
* @param string $imageField * @param string $fileRelationName
* @param string/array $editableFields * @param string/array $editableFields
*/ */
public function __construct($imageField = null, $editableFields = null) public function __construct($fileRelationName = null, $editableFields = null)
{ {
if ( $imageField != null ) $this->setConfig ( 'imageFieldName', $imageField ); if ( $fileRelationName != null ) $this->setConfig ( 'fileRelationName', $fileRelationName );
if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields ); if ( $editableFields != null ) $this->setConfig ( 'editableFields', $editableFields );
//init classes blacklist with forbidden classes //init classes blacklist with forbidden classes

View File

@ -76,19 +76,64 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
return Controller::join_links($this->gridField->Link(), 'bulkimageupload', $action); return Controller::join_links($this->gridField->Link(), 'bulkimageupload', $action);
} }
/**
* Get the first has_one Image relation from the GridField managed DataObject
* i.e. 'MyImage' => 'Image' will return 'MyImage'
*
* @return string Name of the $has_one relation
*/
function getDefaultFileRelationName()
{
$recordClass = $this->gridField->list->dataClass;
$recordHasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
$imageField = null;
foreach( $recordHasOneFields as $field => $type )
{
if($type == 'Image' || is_subclass_of($type, 'Image')) {
$imageField = $field;// . 'ID';
break;
}
}
return $imageField;
}
/** /**
* Returns the name of the Image field name from the managed record * Returns the name of the Image field name from the managed record
* Either as set in the component config or the default one * Either as set in the component config or the default one
* *
* @return string * @return string
*/ */
function getRecordImageField() function getFileRelationName()
{ {
$fieldName = $this->component->getConfig('imageFieldName'); $fieldName = $this->component->getConfig('imageFieldName');
if ( $fieldName == null ) $fieldName = $this->getDefaultRecordImageField(); if ( $fieldName == null ) $fieldName = $this->getDefaultFileRelationName();
return $fieldName; return $fieldName;
} }
/**
* Return the ClassName of the fileRelation
* i.e. 'MyImage' => 'Image' will return 'Image'
* i.e. 'MyImage' => 'File' will return 'File'
*
* @return string file relation className
*/
private function getFileRelationClassName()
{
$recordClass = $this->gridField->list->dataClass;
$recordHasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
$fieldName = $this->getFileRelationName();
if($fieldName != null)
{
return $recordHasOneFields[$fieldName];
}
else{
return 'File';
}
}
/** /**
* Returns the list of editable fields from the managed record * Returns the list of editable fields from the managed record
@ -103,56 +148,6 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
return $fields; return $fields;
} }
/**
* Get the first has_one Image realtion from the GridField managed DataObject
*
* @return string
*/
function getDefaultRecordImageField()
{
$recordClass = $this->gridField->list->dataClass;
$recordHasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
$imageField = null;
foreach( $recordHasOneFields as $field => $type )
{
if($type == 'Image' || is_subclass_of($type, 'Image')) {
$imageField = $field . 'ID';
break;
}
}
return $imageField;
}
/**
* Returns the classname of the first has_one image-relation of the managed DataObject or the
* classname of the given fieldname
*
* @return string
*/
private function getRecordImageClass()
{
$recordClass = $this->gridField->list->dataClass;
$recordHasOneFields = Config::inst()->get($recordClass, 'has_one', Config::INHERITED);
$fieldName = $this->component->getConfig('imageFieldName');
if($fieldName != null)
{
// filter out ID at the end:
$fieldName = substr($fieldName, 0, -2);
return $recordHasOneFields[$fieldName];
}
foreach($recordHasOneFields as $field => $type)
{
if($type == 'Image' || is_subclass_of($type, 'Image'))
{
return $type;
}
}
return null;
}
/** /**
* *
@ -170,7 +165,7 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
$recordCMSDataFields = GridFieldBulkEditingHelper::filterNonEditableRecordsFields($config, $recordCMSDataFields); $recordCMSDataFields = GridFieldBulkEditingHelper::filterNonEditableRecordsFields($config, $recordCMSDataFields);
if ( $config['imageFieldName'] == null ) $config['imageFieldName'] = $this->getDefaultRecordImageField(); if ( $config['imageFieldName'] == null ) $config['imageFieldName'] = $this->getDefaultFileRelationName();
$recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields); $recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
$recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields($recordCMSDataFields, $this->gridField->list->dataClass, $recordID); $recordCMSDataFields = GridFieldBulkEditingHelper::populateCMSDataFields($recordCMSDataFields, $this->gridField->list->dataClass, $recordID);
@ -230,8 +225,8 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
/* * /* *
* UploadField * UploadField
*/ */
$imageRealtionName = $this->getRecordImageClass(); $fileRelationName = $this->getFileRelationName();
$uploadField = UploadField::create($imageRealtionName, ''); $uploadField = UploadField::create($fileRelationName, '');
$uploadField->setConfig('previewMaxWidth', 40); $uploadField->setConfig('previewMaxWidth', 40);
$uploadField->setConfig('previewMaxHeight', 30); $uploadField->setConfig('previewMaxHeight', 30);
$uploadField->addExtraClass('ss-assetuploadfield'); $uploadField->addExtraClass('ss-assetuploadfield');
@ -357,18 +352,18 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
$record->extend("onBulkImageUpload", $this->gridField); $record->extend("onBulkImageUpload", $this->gridField);
//get uploadField and process upload //get uploadField and process upload
$imageRelationName = $this->getRecordImageClass(); $fileRelationName = $this->getFileRelationName();
$uploadField = $this->uploadForm()->Fields()->fieldByName($imageRelationName); $uploadField = $this->uploadForm()->Fields()->fieldByName($fileRelationName);
$uploadField->setRecord($record); $uploadField->setRecord($record);
$uploadResponse = $uploadField->upload( $request ); $uploadResponse = $uploadField->upload( $request );
//get uploaded File //get uploaded File
$uploadResponse = Convert::json2array( $uploadResponse->getBody() ); $uploadResponse = Convert::json2array( $uploadResponse->getBody() );
$uploadResponse = array_shift( $uploadResponse ); $uploadResponse = array_shift( $uploadResponse );
$uploadedFile = DataObject::get_by_id( $imageRelationName, $uploadResponse['id'] ); $uploadedFile = DataObject::get_by_id( $this->getFileRelationClassName(), $uploadResponse['id'] );
// Attach the file to record. // Attach the file to record.
$record->{"{$imageRelationName}ID"} = $uploadedFile->ID; $record->{"{$fileRelationName}ID"} = $uploadedFile->ID;
$record->write(); $record->write();
// attached record to gridField relation // attached record to gridField relation
@ -431,8 +426,8 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
$recordClass = $this->gridField->list->dataClass; $recordClass = $this->gridField->list->dataClass;
$record = DataObject::get_by_id($recordClass, $data['ID']); $record = DataObject::get_by_id($recordClass, $data['ID']);
$imageField = $this->getRecordImageField(); $imageField = $this->getFileRelationName();
$imageID = $record->$imageField; $imageID = $record->$imageField.'ID';
$image = DataObject::get_by_id('Image', $imageID); $image = DataObject::get_by_id('Image', $imageID);
$return[$data['ID']]['imageID'] = $imageID; $return[$data['ID']]['imageID'] = $imageID;