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 {
|
|
|
|
|
2012-08-18 14:51:18 +02:00
|
|
|
/**
|
|
|
|
* Returns all or allowed Form Fields for editing
|
|
|
|
*
|
|
|
|
* @param type $config
|
|
|
|
* @param type $modelClass
|
|
|
|
* @param type $recordID
|
|
|
|
* @return type
|
|
|
|
*/
|
|
|
|
public static function getModelCMSDataFields ( $config, $modelClass )
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-18 14:43:31 +02:00
|
|
|
/**
|
2012-08-18 14:51:18 +02:00
|
|
|
* Populate the FomFields with a given record's value
|
|
|
|
*
|
2012-08-18 14:43:31 +02:00
|
|
|
* @TODO: UploadField get populated OK, however, file recovery and controllers URL are all wrong and should be updated manually
|
|
|
|
* UploadField url should point to GridFieldBulkManager_Request appropriate method
|
|
|
|
*
|
|
|
|
* @param type $cmsDataFields
|
|
|
|
* @param type $modelClass
|
|
|
|
* @param type $recordID
|
|
|
|
* @return type
|
|
|
|
*/
|
2012-08-18 00:05:19 +02:00
|
|
|
public static function populateCMSDataFields ( $cmsDataFields, $modelClass, $recordID )
|
|
|
|
{
|
|
|
|
$record = DataObject::get_by_id($modelClass, $recordID);
|
2012-08-18 14:43:31 +02:00
|
|
|
|
|
|
|
$recordComponents = array(
|
|
|
|
'one' => $record->has_one(),
|
|
|
|
'many' => $record->has_many(),
|
|
|
|
'manymany' => $record->many_many()
|
|
|
|
);
|
|
|
|
|
2012-08-18 00:05:19 +02:00
|
|
|
foreach ( $cmsDataFields as $name => $f )
|
2012-08-18 14:43:31 +02:00
|
|
|
{
|
|
|
|
if ( array_key_exists($name, $recordComponents['one']) )
|
|
|
|
{
|
|
|
|
$obj = $record->{$name}();
|
|
|
|
switch ( get_class($f) )
|
|
|
|
{
|
|
|
|
case 'UploadField':
|
|
|
|
$cmsDataFields[$name]->setRecord($record);
|
|
|
|
$cmsDataFields[$name]->setItems( DataList::create($obj->ClassName)->byID($obj->ID) );
|
|
|
|
print_r($cmsDataFields[$name]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$cmsDataFields[$name]->setValue( $obj->ID );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else if ( array_key_exists($name, $recordComponents['many']) || array_key_exists($name, $recordComponents['manymany']) )
|
|
|
|
{
|
|
|
|
$list = $record->{$name}();
|
|
|
|
switch ( get_class($f) )
|
|
|
|
{
|
|
|
|
case 'UploadField':
|
|
|
|
$cmsDataFields[$name]->setRecord($record);
|
|
|
|
$cmsDataFields[$name]->setItems($list);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'DropdownField':
|
|
|
|
case 'ListboxField':
|
|
|
|
$cmsDataFields[$name]->setValue( array_values($list->getIDList()) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
$cmsDataFields[$name]->setValue( $record->getField($name) );
|
|
|
|
}
|
2012-08-16 22:32:03 +02:00
|
|
|
}
|
2012-08-18 14:43:31 +02:00
|
|
|
|
2012-08-07 22:51:54 +02:00
|
|
|
return $cmsDataFields;
|
|
|
|
}
|
|
|
|
|
2012-08-18 14:51:18 +02:00
|
|
|
/**
|
|
|
|
* Remove all the fields that were not explicitly specified as editable via the $config
|
|
|
|
*
|
|
|
|
* @param type $config
|
|
|
|
* @param type $dataFields
|
|
|
|
* @return type
|
|
|
|
*/
|
2012-08-07 22:51:54 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2012-08-18 14:51:18 +02:00
|
|
|
/**
|
|
|
|
* Convert a list of DataFields into a list of their repective HTML
|
|
|
|
*
|
|
|
|
* @param type $dataFields
|
|
|
|
* @return type
|
|
|
|
*/
|
2012-08-07 22:51:54 +02:00
|
|
|
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-18 14:51:18 +02:00
|
|
|
/**
|
|
|
|
* Escape form fields name with a $unique token
|
|
|
|
* avoid having an ID URLParams sent through and cought as a pageID
|
|
|
|
*
|
|
|
|
* @param type $formFields
|
|
|
|
* @param type $unique
|
|
|
|
* @return type
|
|
|
|
*/
|
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-18 14:51:18 +02:00
|
|
|
/**
|
|
|
|
* Escape HTML form node names with a $unique token
|
|
|
|
* avoid having an ID URLParams sent through and cought as a pageID
|
|
|
|
*
|
|
|
|
* @param type $formFieldsHTML
|
|
|
|
* @param type $unique
|
|
|
|
* @return type
|
|
|
|
*/
|
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;
|
|
|
|
}
|
|
|
|
|
2012-09-05 19:17:19 +02:00
|
|
|
/**
|
|
|
|
* Simple function that replace the 'record_XX_' off of the ID field name
|
|
|
|
* prefix needed since it was taken for a pageID if sent as is as well as fixing other things
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-08-07 22:51:54 +02:00
|
|
|
public static function unescapeFormFieldsPOSTData ( $requestVars )
|
|
|
|
{
|
2012-09-05 19:17:19 +02:00
|
|
|
$return = array();
|
|
|
|
|
|
|
|
foreach( $requestVars as $key => $val)
|
|
|
|
{
|
|
|
|
$return[ preg_replace( '/record_(\d+)_(\w+)/i', '$2', $key) ] = $val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset($return['url']) ) unset($return['url']);
|
|
|
|
if ( isset($return['cacheBuster']) ) unset($return['cacheBuster']);
|
|
|
|
|
|
|
|
return $return;
|
2012-08-07 22:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|