[Import] Simplify and limit dataSource retrieval
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 17 Apr 2022 22:55:22 +0000 (10:55 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 18 Apr 2022 00:40:01 +0000 (12:40 +1200)
CRM/Contact/Import/Form/DataSource.php
CRM/Import/DataSource/CSV.php
CRM/Import/DataSource/SQL.php
CRM/Import/Forms.php

index b9eb4dfb4dcd405156f4e195c488e0329156d655..bc8972dd9aa48244fa138da7d8249693bd6f402b 100644 (file)
 /**
  * This class delegates to the chosen DataSource to grab the data to be imported.
  */
-class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
+class CRM_Contact_Import_Form_DataSource extends CRM_Import_Forms {
 
   private $_dataSource;
 
   private $_dataSourceIsValid = FALSE;
 
-  private $_dataSourceClassFile;
-
   private $_dataSourceClass;
 
   /**
@@ -97,12 +95,12 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
       $this->assign('showOnlyDataSourceFormPane', TRUE);
     }
 
-    $dataSources = $this->_getDataSources();
+    $dataSources = $this->getDataSources();
     if ($this->_dataSource && isset($dataSources[$this->_dataSource])) {
       $this->_dataSourceIsValid = TRUE;
       $this->assign('showDataSourceFormPane', TRUE);
       $dataSourcePath = explode('_', $this->_dataSource);
-      $templateFile = 'CRM/Contact/Import/Form/' . $dataSourcePath[3] . ".tpl";
+      $templateFile = 'CRM/Contact/Import/Form/' . $dataSourcePath[3] . '.tpl';
     }
     elseif ($this->_dataSource) {
       $this->invalidConfig('Invalid data source');
@@ -124,13 +122,10 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
       $this->_dataSourceClass->buildQuickForm($this);
     }
 
-    // Get list of data sources and display them as options
-    $dataSources = $this->_getDataSources();
-
     $this->assign('urlPath', "civicrm/import");
     $this->assign('urlPathVar', 'snippet=4');
 
-    $this->add('select', 'dataSource', ts('Data Source'), $dataSources, TRUE,
+    $this->add('select', 'dataSource', ts('Data Source'), $this->getDataSources(), TRUE,
       ['onchange' => 'buildDataSourceFormBlock(this.value);']
     );
 
@@ -221,48 +216,6 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Core_Form {
     return $defaults;
   }
 
-  /**
-   * @return array
-   * @throws Exception
-   */
-  private function _getDataSources() {
-    // Hmm... file-system scanners don't really belong in forms...
-    if (isset(Civi::$statics[__CLASS__]['datasources'])) {
-      return Civi::$statics[__CLASS__]['datasources'];
-    }
-
-    // Open the data source dir and scan it for class files
-    global $civicrm_root;
-    $dataSourceDir = $civicrm_root . DIRECTORY_SEPARATOR . 'CRM' . DIRECTORY_SEPARATOR . 'Import' . DIRECTORY_SEPARATOR . 'DataSource' . DIRECTORY_SEPARATOR;
-    $dataSources = [];
-    if (!is_dir($dataSourceDir)) {
-      $this->invalidConfig("Import DataSource directory $dataSourceDir does not exist");
-    }
-    if (!$dataSourceHandle = opendir($dataSourceDir)) {
-      $this->invalidConfig("Unable to access DataSource directory $dataSourceDir");
-    }
-
-    while (($dataSourceFile = readdir($dataSourceHandle)) !== FALSE) {
-      $fileType = filetype($dataSourceDir . $dataSourceFile);
-      $matches = [];
-      if (($fileType === 'file' || $fileType === 'link') &&
-        preg_match('/^(.+)\.php$/', $dataSourceFile, $matches)
-      ) {
-        $dataSourceClass = "CRM_Import_DataSource_" . $matches[1];
-        require_once $dataSourceDir . DIRECTORY_SEPARATOR . $dataSourceFile;
-        $object = new $dataSourceClass();
-        $info = $object->getInfo();
-        if ($object->checkPermission()) {
-          $dataSources[$dataSourceClass] = $info['title'];
-        }
-      }
-    }
-    closedir($dataSourceHandle);
-
-    Civi::$statics[__CLASS__]['datasources'] = $dataSources;
-    return $dataSources;
-  }
-
   /**
    * Call the DataSource's postProcess method.
    */
index 34ecf9c7bcaf8def3486e70bb0177c2726fa1531..61b1217afeb32b39d3b6baeb6ed6702caff0abee 100644 (file)
@@ -24,7 +24,7 @@ class CRM_Import_DataSource_CSV extends CRM_Import_DataSource {
    * @return array
    *   collection of info about this data source
    */
-  public function getInfo() {
+  public function getInfo(): array {
     return ['title' => ts('Comma-Separated Values (CSV)')];
   }
 
index c1cbb61e1e4db6033223584f7b7c45fa064552af..633b3dd05a1f11329c67e4da377396187d9b9b3e 100644 (file)
@@ -22,7 +22,7 @@ class CRM_Import_DataSource_SQL extends CRM_Import_DataSource {
    * @return array
    *   collection of info about this data source
    */
-  public function getInfo() {
+  public function getInfo(): array {
     return [
       'title' => ts('SQL Query'),
       'permissions' => ['import SQL datasource'],
index 69f683774d06763a4077028843d6ddf34ccb4f11..237d2d7d1d9a639d18c437c296f5b3e0dfebb04b 100644 (file)
@@ -42,4 +42,28 @@ class CRM_Import_Forms extends CRM_Core_Form {
 
   }
 
+  /**
+   * Get the available datasource.
+   *
+   * Permission dependent, this will look like
+   * [
+   *   'CRM_Import_DataSource_CSV' => 'Comma-Separated Values (CSV)',
+   *   'CRM_Import_DataSource_SQL' => 'SQL Query',
+   * ]
+   *
+   * The label is translated.
+   *
+   * @return array
+   */
+  protected function getDataSources(): array {
+    $dataSources = [];
+    foreach (['CRM_Import_DataSource_SQL', 'CRM_Import_DataSource_CSV'] as $dataSourceClass) {
+      $object = new $dataSourceClass();
+      if ($object->checkPermission()) {
+        $dataSources[$dataSourceClass] = $object->getInfo()['title'];
+      }
+    }
+    return $dataSources;
+  }
+
 }