Implement conversion class so the app's plugin can provide extra conversion functions

This commit is contained in:
Nathan Gray 2010-10-06 22:33:52 +00:00
parent 44dbdb1864
commit eb5fac04e9
2 changed files with 39 additions and 6 deletions

View File

@ -30,6 +30,14 @@ class importexport_helper_functions {
'class.news_admin_import.inc.php', 'class.news_admin_import.inc.php',
), ),
); );
/**
* Class used to provide extra conversion functions
*
* Passed in as a param to conversion()
*/
protected static $cclass = null;
/** /**
* nothing to construct here, only static functions! * nothing to construct here, only static functions!
*/ */
@ -182,12 +190,14 @@ class importexport_helper_functions {
* *
* @param array _record reference with record to do the conversion with * @param array _record reference with record to do the conversion with
* @param array _conversion array with conversion description * @param array _conversion array with conversion description
* @param object &$cclass calling class to process the '@ evals' (not impelmeted yet) * @param object &$cclass calling class to process the '@ evals'
* @return bool * @return bool
*/ */
public static function conversion( $_record, $_conversion, &$_cclass = null ) { public static function conversion( &$_record, $_conversion, &$_cclass = null ) {
if (empty( $_conversion ) ) return $_record; if (empty( $_conversion ) ) return $_record;
self::$cclass =& $_cclass;
$PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in conversion $PSep = '||'; // Pattern-Separator, separats the pattern-replacement-pairs in conversion
$ASep = '|>'; // Assignment-Separator, separats pattern and replacesment $ASep = '|>'; // Assignment-Separator, separats pattern and replacesment
$CPre = '|['; $CPos = ']'; // |[_record-idx] is expanded to the corespondig value $CPre = '|['; $CPos = ']'; // |[_record-idx] is expanded to the corespondig value
@ -212,6 +222,16 @@ class importexport_helper_functions {
// conversion list may be longer than $_record aka (no_csv) // conversion list may be longer than $_record aka (no_csv)
$val = array_key_exists( $idx, $_record ) ? $_record[$idx] : ''; $val = array_key_exists( $idx, $_record ) ? $_record[$idx] : '';
$c_functions = array('cat', 'account', 'strtotime');
if($_cclass) {
// Add in additional methods
$reflection = new ReflectionClass(get_class($_cclass));
$methods = $reflection->getMethods(ReflectionMethod::IS_STATIC);
foreach($methods as $method) {
$c_functions[] = $method->name;
}
}
$c_functions = implode('|', $c_functions);
foreach ( $rvalues as $pattern => $replace ) { foreach ( $rvalues as $pattern => $replace ) {
if( ereg( (string)$pattern, $val) ) { if( ereg( (string)$pattern, $val) ) {
@ -226,8 +246,7 @@ class importexport_helper_functions {
$val $val
); );
} }
$val = preg_replace_callback( "/($c_functions)\(([^)]*)\)/i", array( self, 'c2_dispatcher') , $val );
$val = preg_replace_callback( "/(cat|account|strtotime)\(([^)]*)\)/i", array( self, 'c2_dispatcher') , $val );
} }
} }
// clean each field // clean each field
@ -254,8 +273,17 @@ class importexport_helper_functions {
list( $string, $format ) = explode( ',', $data ); list( $string, $format ) = explode( ',', $data );
return self::custom_strtotime( trim( $string ), trim( $format ) ); return self::custom_strtotime( trim( $string ), trim( $format ) );
default : default :
if(self::$cclass && method_exists(self::$cclass, $action)) {
$class = get_class(self::$cclass);
return call_user_func("$class::$action", $data);
}
$method = (string)$action. ( is_int( $data ) ? '_id2name' : '_name2id' ); $method = (string)$action. ( is_int( $data ) ? '_id2name' : '_name2id' );
return self::$method( $data ); if(self::$cclass && method_exists(self::$cclass, $method)) {
$class = get_class(self::$cclass);
return call_user_func("$class::$action", $data);
} else {
return self::$method( $data );
}
} }
} }

View File

@ -33,6 +33,11 @@ class importexport_import_csv implements importexport_iface_import_record { //,
*/ */
public $conversion = array(); public $conversion = array();
/**
* @var class with extra conversion functions
*/
public $conversion_class = null;
/** /**
* @var array holding the current record * @var array holding the current record
*/ */
@ -232,7 +237,7 @@ class importexport_import_csv implements importexport_iface_import_record { //,
* @return bool * @return bool
*/ */
protected function do_conversions( ) { protected function do_conversions( ) {
if ( $record = importexport_helper_functions::conversion( $this->record, $this->conversion )) { if ( $record = importexport_helper_functions::conversion( $this->record, $this->conversion, $this->conversion_class )) {
$this->record = $record; $this->record = $record;
return; return;
} }