xml/templates/dao.tpl - Centralize export() / import() logic
authorTim Otten <totten@civicrm.org>
Thu, 28 Jul 2016 19:58:46 +0000 (12:58 -0700)
committerTim Otten <totten@civicrm.org>
Thu, 28 Jul 2016 21:20:23 +0000 (14:20 -0700)
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.

CRM/Core/DAO/AllCoreTables.php
xml/templates/dao.tpl

index d5eae75443b37ff45a3951ea2709e198cf9198ef..21fc67e8470b0a6a5af488d9278d0c4712a61ed6 100644 (file)
@@ -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];
+  }
+
 }
index 96c1718627fec7e0cb915d5491e192804bf34784..0d432b382a678911c96dfa4b26d44a6f1681e532 100644 (file)
@@ -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}