Merge pull request #22380 from braders/core-483-show-customised-preferences-on-validation
[civicrm-core.git] / CRM / Core / CodeGen / DAO.php
index 1a47be7cc5dba895e150ea89f8f3ebd4aa4726b0..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(array(), 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(array(), 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.
    *
@@ -131,7 +143,7 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
    */
   protected function getTableChecksum() {
     if (!$this->tableChecksum) {
-      $flat = array();
+      $flat = [];
       CRM_Utils_Array::flatten($this->tables[$this->name], $flat);
       ksort($flat);
       $this->tableChecksum = md5(json_encode($flat));