From: Tim Otten Date: Thu, 28 Jul 2016 19:58:46 +0000 (-0700) Subject: xml/templates/dao.tpl - Centralize export() / import() logic X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=84a0493ce59dc0d76a7b5239031a709f1e4dc5f3;p=civicrm-core.git xml/templates/dao.tpl - Centralize export() / import() logic There's repetitive bugginess in these functions. We can reduce SLOC and gain leverage by moving the guts to a central function. QA note: To test that these patches had no functional impact, I used some helper scripts: https://gist.github.com/totten/4473975264fc2a788391d3c5ef5bf18b Fun fact: This is net-reduction of ~6,000 SLOC. --- diff --git a/CRM/Core/DAO/AllCoreTables.php b/CRM/Core/DAO/AllCoreTables.php index d5eae75443..21fc67e847 100644 --- a/CRM/Core/DAO/AllCoreTables.php +++ b/CRM/Core/DAO/AllCoreTables.php @@ -173,4 +173,82 @@ class CRM_Core_DAO_AllCoreTables { self::init($fresh); } + /** + * (Quasi-Private) Do not call externally. For use by DAOs. + * + * @param string $dao + * Ex: 'CRM_Core_DAO_Address'. + * @param string $labelName + * Ex: 'address'. + * @param bool $prefix + * @param array $foreignDAOs + * @return array + */ + public static function getExports($dao, $labelName, $prefix, $foreignDAOs) { + // Bug-level compatibility -- or sane behavior? + $cacheKey = $dao . ':export'; + // $cacheKey = $dao . ':' . ($prefix ? 'export-prefix' : 'export'); + + if (!isset(Civi::$statics[__CLASS__][$cacheKey])) { + $exports = array(); + $fields = $dao::fields(); + + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('export', $field)) { + if ($prefix) { + $exports[$labelName] = & $fields[$name]; + } else { + $exports[$name] = & $fields[$name]; + } + } + } + + foreach ($foreignDAOs as $foreignDAO) { + $exports = array_merge($exports, $foreignDAO::export(TRUE)); + } + + Civi::$statics[__CLASS__][$cacheKey] = $exports; + } + return Civi::$statics[__CLASS__][$cacheKey]; + } + + /** + * (Quasi-Private) Do not call externally. For use by DAOs. + * + * @param string $dao + * Ex: 'CRM_Core_DAO_Address'. + * @param string $labelName + * Ex: 'address'. + * @param bool $prefix + * @param array $foreignDAOs + * @return array + */ + public static function getImports($dao, $labelName, $prefix, $foreignDAOs) { + // Bug-level compatibility -- or sane behavior? + $cacheKey = $dao . ':import'; + // $cacheKey = $dao . ':' . ($prefix ? 'import-prefix' : 'import'); + + if (!isset(Civi::$statics[__CLASS__][$cacheKey])) { + $imports = array(); + $fields = $dao::fields(); + + foreach($fields as $name => $field) { + if (CRM_Utils_Array::value('import', $field)) { + if ($prefix) { + $imports[$labelName] = & $fields[$name]; + } else { + $imports[$name] = & $fields[$name]; + } + } + } + + foreach ($foreignDAOs as $foreignDAO) { + $imports = array_merge($imports, $foreignDAO::import(TRUE)); + } + + Civi::$statics[__CLASS__][$cacheKey] = $imports; + } + return Civi::$statics[__CLASS__][$cacheKey]; + } + } diff --git a/xml/templates/dao.tpl b/xml/templates/dao.tpl index 96c1718627..0d432b382a 100644 --- a/xml/templates/dao.tpl +++ b/xml/templates/dao.tpl @@ -75,22 +75,6 @@ class {$table.className} extends CRM_Core_DAO {ldelim} */ static $_links = null; - /** - * static instance to hold the values that can - * be imported - * - * @var array - */ - static $_import = null; - - /** - * static instance to hold the values that can - * be exported - * - * @var array - */ - static $_export = null; - /** * static value to see if we should log any modifications to * this table in the civicrm_log table @@ -288,28 +272,12 @@ class {$table.className} extends CRM_Core_DAO {ldelim} * @return array */ static function &import( $prefix = false ) {ldelim} - if ( ! ( self::$_import ) ) {ldelim} - self::$_import = array ( ); - $fields = self::fields( ); - foreach ( $fields as $name => $field ) {ldelim} - if ( CRM_Utils_Array::value( 'import', $field ) ) {ldelim} - if ( $prefix ) {ldelim} - self::$_import['{$table.labelName}'] =& $fields[$name]; - {rdelim} else {ldelim} - self::$_import[$name] =& $fields[$name]; - {rdelim} - {rdelim} - {rdelim} - {if $table.foreignKey} - {foreach from=$table.foreignKey item=foreign} - {if $foreign.import} - self::$_import = array_merge( self::$_import, - {$foreign.className}::import( true ) ); - {/if} - {/foreach} - {/if} - {rdelim} - return self::$_import; + $r = CRM_Core_DAO_AllCoreTables::getImports(__CLASS__, '{$table.labelName}', $prefix, array( + {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign} + {if $foreign.import}'{$foreign.className}',{/if} + {/foreach}{/if} + )); + return $r; {rdelim} /** @@ -320,28 +288,12 @@ class {$table.className} extends CRM_Core_DAO {ldelim} * @return array */ static function &export( $prefix = false ) {ldelim} - if ( ! ( self::$_export ) ) {ldelim} - self::$_export = array ( ); - $fields = self::fields( ); - foreach ( $fields as $name => $field ) {ldelim} - if ( CRM_Utils_Array::value( 'export', $field ) ) {ldelim} - if ( $prefix ) {ldelim} - self::$_export['{$table.labelName}'] =& $fields[$name]; - {rdelim} else {ldelim} - self::$_export[$name] =& $fields[$name]; - {rdelim} - {rdelim} - {rdelim} - {if $table.foreignKey} - {foreach from=$table.foreignKey item=foreign} - {if $foreign.export} - self::$_export = array_merge( self::$_export, - {$foreign.className}::export( true ) ); - {/if} - {/foreach} - {/if} - {rdelim} - return self::$_export; + $r = CRM_Core_DAO_AllCoreTables::getExports(__CLASS__, '{$table.labelName}', $prefix, array( + {if $table.foreignKey}{foreach from=$table.foreignKey item=foreign} + {if $foreign.export}'{$foreign.className}',{/if} + {/foreach}{/if} + )); + return $r; {rdelim} {rdelim}