Merge pull request #18143 from mattwire/membertabbuttons
[civicrm-core.git] / CRM / Core / CodeGen / DAO.php
index 69cc8d0e479ac4d59b5479f9e01fb027ca1f8111..9865d9e5f8be8dee6d1c11a6b67cfde800a8988c 100644 (file)
@@ -26,6 +26,10 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
    */
   private $tsFunctionName;
 
+  private $useHelper = '';
+
+  private $ext = "'civicrm'";
+
   /**
    * CRM_Core_CodeGen_DAO constructor.
    *
@@ -37,6 +41,12 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
     parent::__construct($config);
     $this->name = $name;
     $this->tsFunctionName = $tsFunctionName;
+    // If this DAO belongs to an extension, add `use` statement and define EXT constant.
+    if (strpos($tsFunctionName, '::ts')) {
+      $this->tsFunctionName = 'E::ts';
+      $this->useHelper = 'use \\' . explode('::', $tsFunctionName)[0] . ' as E;';
+      $this->ext = 'E::LONG_NAME';
+    }
   }
 
   /**
@@ -68,16 +78,8 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
       return;
     }
 
-    $template = new CRM_Core_CodeGen_Util_Template('php');
-    $template->assign('table', $this->tables[$this->name]);
-    if (empty($this->tables[$this->name]['index'])) {
-      $template->assign('indicesPhp', var_export([], 1));
-    }
-    else {
-      $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
-    }
+    $template = $this->getTemplate();
     $template->assign('genCodeChecksum', $this->getTableChecksum());
-    $template->assign('tsFunctionName', $this->tsFunctionName);
     $template->run('dao.tpl', $this->getAbsFileName());
   }
 
@@ -88,21 +90,31 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
    */
   public function getRaw() {
     if (!$this->raw) {
-      $template = new CRM_Core_CodeGen_Util_Template('php');
-      $template->assign('table', $this->tables[$this->name]);
-      if (empty($this->tables[$this->name]['index'])) {
-        $template->assign('indicesPhp', var_export([], 1));
-      }
-      else {
-        $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
-      }
+      $template = $this->getTemplate();
       $template->assign('genCodeChecksum', 'NEW');
-      $template->assign('tsFunctionName', $this->tsFunctionName);
       $this->raw = $template->fetch('dao.tpl');
     }
     return $this->raw;
   }
 
+  /**
+   * @return CRM_Core_CodeGen_Util_Template
+   */
+  private function getTemplate() {
+    $template = new CRM_Core_CodeGen_Util_Template('php');
+    $template->assign('table', $this->tables[$this->name]);
+    if (empty($this->tables[$this->name]['index'])) {
+      $template->assign('indicesPhp', var_export([], 1));
+    }
+    else {
+      $template->assign('indicesPhp', var_export($this->tables[$this->name]['index'], 1));
+    }
+    $template->assign('tsFunctionName', $this->tsFunctionName);
+    $template->assign('ext', $this->ext);
+    $template->assign('useHelper', $this->useHelper);
+    return $template;
+  }
+
   /**
    * Get relative file name.
    *