API: defined components set's HTML fragment

All bulk edit tools will now use "bulk-edit-tools" HTML fragment,
defined in base component GridFieldBulkEditingTools. Previous
"GridFieldBulkEditingTools" is now GridFieldBulkEditingHelper.
This commit is contained in:
colymba 2012-08-07 23:51:54 +03:00
parent 65d1e7c5cb
commit a141aba650
5 changed files with 161 additions and 132 deletions

View File

@ -0,0 +1,130 @@
<?php
/**
* Generic helper class for the various bulk editing component
* contains common functions
*
* @author colymba
* @package GridFieldBulkEditingTools
*/
class GridFieldBulkEditingHelper {
//put your code here
public static function getModelCMSDataFields ( $config, $modelClass )
{
$cmsFields = singleton($modelClass)->getCMSFields();
$cmsDataFields = $cmsFields->dataFields();
$cmsDataFields = GridFieldBulkEditingHelper::filterNonEditableRecordsFields($config, $cmsDataFields);
return $cmsDataFields;
}
public static function filterNonEditableRecordsFields ( $config, $dataFields )
{
if ( isset($config['editableFields']) )
{
if ( $config['editableFields'] != null )
{
foreach ($dataFields as $name => $field)
{
if ( !in_array($name, $config['editableFields']) )
{
unset( $dataFields[$name] );
}
}
}
}
return $dataFields;
}
/**
* Filters out all unwanted fields from the config settings
*
* @param array $config
* @param array $dataFields
* @return array
*/
public static function getModelFilteredDataFields ( $config, $dataFields )
{
//remove the image field - for bulk image upload
if ( isset($config['imageFieldName']) )
{
if ( $config['imageFieldName'] != null )
{
//@TODO: this wont work if the fieldname has ID in it: i.e. TheIDImageID -> remove last 2 char only
unset( $dataFields[str_ireplace('ID', '', $config['imageFieldName']) ] );
}
}
//if class blacklist filter
if ( count($config['fieldsClassBlacklist']) > 0 )
{
foreach ($dataFields as $fieldName => $field)
{
//@TODO find PHP function that return the classname
if ( in_array($field->ClassName, $config['fieldsClassBlacklist']) )
{
array_push($config['fieldsNameBlacklist'], $fieldName);
}
}
}
//if name blacklist filter
if ( count($config['fieldsNameBlacklist']) > 0 )
{
foreach ( $config['fieldsNameBlacklist'] as $badFieldName )
{
unset( $dataFields[ $badFieldName ] );
}
}
return $dataFields;
}
public static function filterDatafieldsByClass ( $config, $dataFields )
{
//@todo
}
public static function filterDataFieldsByName ( $config, $dataFields )
{
//@todo
}
public static function dataFieldsToHTML ( $dataFields )
{
$fieldsHTML = array();
foreach ( $dataFields as $key => $field )
{
//@TODO: FieldHolder() does not seem to exist on UploadField
$fieldsHTML[$key] = $field->FieldHolder();
}
return $fieldsHTML;
}
public static function escapeFormFieldsHTML ( $formFieldsHTML, $unique )
{
$prefix = 'record_'.$unique.'_';
foreach ( $formFieldsHTML as $name => $html )
{
$formFieldsHTML[$name] = str_ireplace ( array('id="', 'for="', 'name="'),
array('id="'.$prefix, 'for="'.$prefix, 'name="'.$prefix),
$html);
}
return $formFieldsHTML;
}
public static function unescapeFormFieldsPOSTData ( $requestVars )
{
//@todo
}
}

View File

@ -1,130 +1,19 @@
<?php <?php
/** /**
* Generic helper class for the various bulk editing component *
* contains common functions
* *
* @author colymba * @author colymba
* @package GridFieldBulkEditingTools * @package GridFieldBulkEditingTools
*/ */
class GridFieldBulkEditingTools { class GridFieldBulkEditingTools implements GridField_HTMLProvider
//put your code here
public static function getModelCMSDataFields ( $config, $modelClass )
{ {
$cmsFields = singleton($modelClass)->getCMSFields(); public function getHTMLFragments($gridField)
$cmsDataFields = $cmsFields->dataFields();
$cmsDataFields = GridFieldBulkEditingTools::filterNonEditableRecordsFields($config, $cmsDataFields);
return $cmsDataFields;
}
public static function filterNonEditableRecordsFields ( $config, $dataFields )
{ {
if ( isset($config['editableFields']) ) Requirements::css('GridFieldBulkImageUpload/css/GridFieldBulkEditingTools.css');
{
if ( $config['editableFields'] != null )
{
foreach ($dataFields as $name => $field)
{
if ( !in_array($name, $config['editableFields']) )
{
unset( $dataFields[$name] );
}
}
}
}
return $dataFields; return array(
} //"footer" => '<tr><td colspan="'.$columnsCount.'">\$DefineFragment(bulk-edit-tools)</td></tr>'
"after" => "<div id=\"bulkEditTools\">\$DefineFragment(bulk-edit-tools)</div>"
/** );
* Filters out all unwanted fields from the config settings
*
* @param array $config
* @param array $dataFields
* @return array
*/
public static function getModelFilteredDataFields ( $config, $dataFields )
{
//remove the image field - for bulk image upload
if ( isset($config['imageFieldName']) )
{
if ( $config['imageFieldName'] != null )
{
//@TODO: this wont work if the fieldname has ID in it: i.e. TheIDImageID -> remove last 2 char only
unset( $dataFields[str_ireplace('ID', '', $config['imageFieldName']) ] );
} }
} }
//if class blacklist filter
if ( count($config['fieldsClassBlacklist']) > 0 )
{
foreach ($dataFields as $fieldName => $field)
{
//@TODO find PHP function that return the classname
if ( in_array($field->ClassName, $config['fieldsClassBlacklist']) )
{
array_push($config['fieldsNameBlacklist'], $fieldName);
}
}
}
//if name blacklist filter
if ( count($config['fieldsNameBlacklist']) > 0 )
{
foreach ( $config['fieldsNameBlacklist'] as $badFieldName )
{
unset( $dataFields[ $badFieldName ] );
}
}
return $dataFields;
}
public static function filterDatafieldsByClass ( $config, $dataFields )
{
//@todo
}
public static function filterDataFieldsByName ( $config, $dataFields )
{
//@todo
}
public static function dataFieldsToHTML ( $dataFields )
{
$fieldsHTML = array();
foreach ( $dataFields as $key => $field )
{
//@TODO: FieldHolder() does not seem to exist on UploadField
$fieldsHTML[$key] = $field->FieldHolder();
}
return $fieldsHTML;
}
public static function escapeFormFieldsHTML ( $formFieldsHTML, $unique )
{
$prefix = 'record_'.$unique.'_';
foreach ( $formFieldsHTML as $name => $html )
{
$formFieldsHTML[$name] = str_ireplace ( array('id="', 'for="', 'name="'),
array('id="'.$prefix, 'for="'.$prefix, 'name="'.$prefix),
$html);
}
return $formFieldsHTML;
}
public static function unescapeFormFieldsPOSTData ( $requestVars )
{
//@todo
}
}

View File

@ -1,6 +1,9 @@
<?php <?php
/** /**
* GridField component for uploading images in bulk * GridField component for uploading images in bulk
*
* @author colymba
* @package GridFieldBulkEditingTools
*/ */
class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLHandler { class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLHandler {
@ -121,14 +124,13 @@ class GridFieldBulkImageUpload implements GridField_HTMLProvider, GridField_URLH
* @return array * @return array
*/ */
public function getHTMLFragments($gridField) { public function getHTMLFragments($gridField) {
$data = new ArrayData(array( $data = new ArrayData(array(
'NewLink' => $gridField->Link('bulkimageupload'), 'NewLink' => $gridField->Link('bulkimageupload'),
'ButtonName' => 'Bulk Upload' 'ButtonName' => 'Bulk Upload'
)); ));
return array( return array(
'toolbar-header-right' => $data->renderWith('GridFieldAddNewbutton') 'bulk-edit-tools' => $data->renderWith('GridFieldAddNewbutton')
); );
} }

View File

@ -6,6 +6,9 @@
* * Form creation * * Form creation
* * file upload * * file upload
* * editing and cancelling records * * editing and cancelling records
*
* @author colymba
* @package GridFieldBulkEditingTools
*/ */
class GridFieldBulkImageUpload_Request extends RequestHandler { class GridFieldBulkImageUpload_Request extends RequestHandler {
@ -168,19 +171,19 @@ class GridFieldBulkImageUpload_Request extends RequestHandler {
function getRecordHTMLFormFields( $recordID = 0 ) function getRecordHTMLFormFields( $recordID = 0 )
{ {
$config = $this->component->getConfig(); $config = $this->component->getConfig();
$recordCMSDataFields = GridFieldBulkEditingTools::getModelCMSDataFields( $config, $this->gridField->list->dataClass ); $recordCMSDataFields = GridFieldBulkEditingHelper::getModelCMSDataFields( $config, $this->gridField->list->dataClass );
//@TODO: if editableFields given use them with filterNonEditableRecordsFields() //@TODO: if editableFields given use them with filterNonEditableRecordsFields()
// otherwise go through getModelFilteredDataFields // otherwise go through getModelFilteredDataFields
$recordCMSDataFields = GridFieldBulkEditingTools::filterNonEditableRecordsFields($config, $recordCMSDataFields); $recordCMSDataFields = GridFieldBulkEditingHelper::filterNonEditableRecordsFields($config, $recordCMSDataFields);
if ( $config['imageFieldName'] == null ) $config['imageFieldName'] = $this->getDefaultRecordImageField(); if ( $config['imageFieldName'] == null ) $config['imageFieldName'] = $this->getDefaultRecordImageField();
$recordCMSDataFields = GridFieldBulkEditingTools::getModelFilteredDataFields($config, $recordCMSDataFields); $recordCMSDataFields = GridFieldBulkEditingHelper::getModelFilteredDataFields($config, $recordCMSDataFields);
$formFieldsHTML = GridFieldBulkEditingTools::dataFieldsToHTML($recordCMSDataFields); $formFieldsHTML = GridFieldBulkEditingHelper::dataFieldsToHTML($recordCMSDataFields);
$formFieldsHTML = GridFieldBulkEditingTools::escapeFormFieldsHTML($formFieldsHTML, $recordID); $formFieldsHTML = GridFieldBulkEditingHelper::escapeFormFieldsHTML($formFieldsHTML, $recordID);
return $formFieldsHTML; return $formFieldsHTML;
} }

View File

@ -0,0 +1,5 @@
div#bulkEditTools
{
margin: 0;
text-align: right;
}