From 0e1b4b2ebd0e46ea544fdb51ad555334dd325d4e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 9 Dec 2022 15:30:27 +1300 Subject: [PATCH] Php layer consolidation on Import DataSource form --- CRM/Contact/Import/Form/DataSource.php | 33 -------------------------- CRM/Core/BAO/UserJob.php | 30 +++++++++++++++++++++++ CRM/Import/Form/DataSource.php | 8 ++++--- CRM/Import/Forms.php | 9 +++++-- 4 files changed, 42 insertions(+), 38 deletions(-) diff --git a/CRM/Contact/Import/Form/DataSource.php b/CRM/Contact/Import/Form/DataSource.php index ab86a34777..4a7768939e 100644 --- a/CRM/Contact/Import/Form/DataSource.php +++ b/CRM/Contact/Import/Form/DataSource.php @@ -42,39 +42,6 @@ class CRM_Contact_Import_Form_DataSource extends CRM_Import_Form_DataSource { return ['disableUSPS']; } - /** - * Set variables up before form is built. - * - * @throws \CRM_Core_Exception - */ - public function preProcess() { - $results = []; - $config = CRM_Core_Config::singleton(); - $handler = opendir($config->uploadDir); - $errorFiles = ['sqlImport.errors', 'sqlImport.conflicts', 'sqlImport.duplicates', 'sqlImport.mismatch']; - - // check for post max size avoid when called twice - $snippet = $_GET['snippet'] ?? 0; - if (empty($snippet)) { - CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); - } - - while ($file = readdir($handler)) { - if ($file !== '.' && $file !== '..' && - in_array($file, $errorFiles) && !is_writable($config->uploadDir . $file) - ) { - $results[] = $file; - } - } - closedir($handler); - if (!empty($results)) { - $this->invalidConfig(ts('%1 file(s) in %2 directory are not writable. Listed file(s) might be used during the import to log the errors occurred during Import process. Contact your site administrator for assistance.', [ - 1 => implode(', ', $results), - 2 => $config->uploadDir, - ])); - } - } - /** * Build the form object. * diff --git a/CRM/Core/BAO/UserJob.php b/CRM/Core/BAO/UserJob.php index 39acdb3dd5..036a4b4b9c 100644 --- a/CRM/Core/BAO/UserJob.php +++ b/CRM/Core/BAO/UserJob.php @@ -164,4 +164,34 @@ class CRM_Core_BAO_UserJob extends CRM_Core_DAO_UserJob implements \Civi\Core\Ho return array_values($types); } + /** + * Get user job type. + * + * @param string $type + * + * @return array + * @throws \CRM_Core_Exception + */ + public static function getType(string $type): array { + foreach (self::getTypes() as $importType) { + if ($importType['id'] === $type) { + return $importType; + } + } + throw new CRM_Core_Exception($type . 'not found'); + } + + /** + * Get the specified value for the import job type. + * + * @param string $type + * @param string $key + * + * @return mixed + * @throws \CRM_Core_Exception + */ + public static function getTypeValue(string $type, string $key) { + return self::getType($type)[$key]; + } + } diff --git a/CRM/Import/Form/DataSource.php b/CRM/Import/Form/DataSource.php index 7e04646bdc..8cd728b3aa 100644 --- a/CRM/Import/Form/DataSource.php +++ b/CRM/Import/Form/DataSource.php @@ -14,6 +14,8 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\Api4\Utils\CoreUtil; + /** * Base class for upload-only import forms (all but Contact import). */ @@ -22,7 +24,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { /** * Set variables up before form is built. */ - public function preProcess() { + public function preProcess(): void { // check for post max size CRM_Utils_Number::formatUnitSize(ini_get('post_max_size'), TRUE); $this->assign('importEntity', $this->getTranslatedEntity()); @@ -37,7 +39,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { * @return string */ protected function getTranslatedEntity(): string { - return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title'); + return (string) CoreUtil::getInfoItem($this->getBaseEntity(), 'title'); } /** @@ -48,7 +50,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { * @return string */ protected function getTranslatedEntities(): string { - return (string) Civi\Api4\Utils\CoreUtil::getInfoItem($this::IMPORT_ENTITY, 'title_plural'); + return (string) CoreUtil::getInfoItem($this->getBaseEntity(), 'title_plural'); } /** diff --git a/CRM/Import/Forms.php b/CRM/Import/Forms.php index 0e59a70507..ff4da683aa 100644 --- a/CRM/Import/Forms.php +++ b/CRM/Import/Forms.php @@ -761,10 +761,15 @@ class CRM_Import_Forms extends CRM_Core_Form { * Get the base entity for the import. * * @return string + * + * @throws \CRM_Core_Exception */ protected function getBaseEntity(): string { - $info = $this->getParser()->getUserJobInfo(); - return reset($info)['entity']; + if ($this->getUserJobID()) { + $info = $this->getParser()->getUserJobInfo(); + return reset($info)['entity']; + } + return CRM_Core_BAO_UserJob::getTypeValue($this->getUserJobType(), 'entity'); } /** -- 2.25.1