2012-08-07 22:51:54 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Generic helper class for the various bulk editing component
|
|
|
|
* contains common functions
|
|
|
|
*
|
2012-08-16 22:32:03 +02:00
|
|
|
* @todo clean up functions names: makes them consistent and more explicit
|
|
|
|
*
|
2012-08-07 22:51:54 +02:00
|
|
|
* @author colymba
|
|
|
|
* @package GridFieldBulkEditingTools
|
|
|
|
*/
|
|
|
|
class GridFieldBulkEditingHelper {
|
|
|
|
//put your code here
|
|
|
|
|
2012-08-16 22:32:03 +02:00
|
|
|
public static function getModelCMSDataFields ( $config, $modelClass, $recordID = null )
|
2012-08-07 22:51:54 +02:00
|
|
|
{
|
2012-08-18 00:05:19 +02:00
|
|
|
$cmsFields = singleton($modelClass)->getCMSFields();
|
2012-08-16 22:32:03 +02:00
|
|
|
|
2012-08-07 22:51:54 +02:00
|
|
|
$cmsDataFields = $cmsFields->dataFields();
|
|
|
|
$cmsDataFields = GridFieldBulkEditingHelper::filterNonEditableRecordsFields($config, $cmsDataFields);
|
|
|
|
|
2012-08-18 00:05:19 +02:00
|
|
|
return $cmsDataFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function populateCMSDataFields ( $cmsDataFields, $modelClass, $recordID )
|
|
|
|
{
|
2012-08-16 22:32:03 +02:00
|
|
|
// @TODO: can we handle has_one/has_many/many_many relations
|
2012-08-18 00:05:19 +02:00
|
|
|
$record = DataObject::get_by_id($modelClass, $recordID);
|
|
|
|
foreach ( $cmsDataFields as $name => $f )
|
2012-08-16 22:32:03 +02:00
|
|
|
{
|
2012-08-18 00:05:19 +02:00
|
|
|
$cmsDataFields[$name]->setValue( $record->getField($name) );
|
2012-08-16 22:32:03 +02:00
|
|
|
}
|
2012-08-07 22:51:54 +02:00
|
|
|
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 )
|
|
|
|
{
|
2012-08-09 19:28:52 +02:00
|
|
|
unset( $dataFields[ substr($config['imageFieldName'], 0, -2) ] );
|
2012-08-07 22:51:54 +02:00
|
|
|
}
|
|
|
|
}
|
2012-08-09 19:25:08 +02:00
|
|
|
|
2012-08-07 22:51:54 +02:00
|
|
|
//if class blacklist filter
|
|
|
|
if ( count($config['fieldsClassBlacklist']) > 0 )
|
|
|
|
{
|
|
|
|
foreach ($dataFields as $fieldName => $field)
|
|
|
|
{
|
2012-08-09 19:25:08 +02:00
|
|
|
if ( in_array(get_class($field), $config['fieldsClassBlacklist']) )
|
2012-08-07 22:51:54 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-16 22:32:03 +02:00
|
|
|
public static function escapeFormFieldsName ( $formFields, $unique )
|
|
|
|
{
|
|
|
|
$prefix = 'record_'.$unique.'_';
|
|
|
|
|
|
|
|
foreach ( $formFields as $name => $f )
|
|
|
|
{
|
|
|
|
$f->Name = $prefix . $f->Name;
|
|
|
|
$formFields[$name] = $f;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $formFields;
|
|
|
|
}
|
|
|
|
|
2012-08-07 22:51:54 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|