From: Eileen McNaughton <emcnaughton@wikimedia.org> Date: Sun, 17 Apr 2022 22:55:22 +0000 (+1200) Subject: [Import] Simplify and limit dataSource retrieval X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=39dc35d45f93ef6eef4abed80a6c366bb8c514ee;p=civicrm-core.git [Import] Simplify and limit dataSource retrieval --- diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index b9eb4dfb4d..bc8972dd9a 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -18,14 +18,12 @@ /** * 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. */ diff --git a/CRM/Import/DataSource/CSV.php b/CRM/Import/DataSource/CSV.php index 34ecf9c7bc..61b1217afe 100644 --- a/CRM/Import/DataSource/CSV.php +++ b/CRM/Import/DataSource/CSV.php @@ -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)')]; } diff --git a/CRM/Import/DataSource/SQL.php b/CRM/Import/DataSource/SQL.php index c1cbb61e1e..633b3dd05a 100644 --- a/CRM/Import/DataSource/SQL.php +++ b/CRM/Import/DataSource/SQL.php @@ -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'], diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php index 69f683774d..237d2d7d1d 100644 --- a/CRM/Import/Forms.php +++ b/CRM/Import/Forms.php @@ -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; + } + }