From e4c2531c83de538c88a138acff97bbf0fd628820 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 26 Oct 2022 19:20:24 +1300 Subject: [PATCH] Add Validate and Import search kit actions to import search displays --- .../Event/Subscriber/ImportSubscriber.php | 3 +- ext/civiimport/Civi/BAO/Import.php | 13 +++++- ext/civiimport/civiimport.php | 42 +++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/ext/civiimport/Civi/Api4/Event/Subscriber/ImportSubscriber.php b/ext/civiimport/Civi/Api4/Event/Subscriber/ImportSubscriber.php index 3dafe36045..4a267ffc68 100644 --- a/ext/civiimport/Civi/Api4/Event/Subscriber/ImportSubscriber.php +++ b/ext/civiimport/Civi/Api4/Event/Subscriber/ImportSubscriber.php @@ -80,7 +80,8 @@ class ImportSubscriber extends \Civi\Core\Service\AutoService implements EventSu $exists = Entity::get(FALSE)->addWhere('name', '=', 'Import_' . $event->id)->selectRowCount()->execute()->count(); if (!$exists || $event->action === 'delete') { // Flush entities cache key so our new Import will load as an entity. - Civi::cache('metadata')->set('api4.entities.info', NULL); + Civi::cache('metadata')->delete('api4.entities.info'); + Civi::cache('metadata')->delete('civiimport_tables'); CRM_Core_DAO_AllCoreTables::flush(); Managed::reconcile(FALSE)->setModules(['civiimport'])->execute(); } diff --git a/ext/civiimport/Civi/BAO/Import.php b/ext/civiimport/Civi/BAO/Import.php index b8ab6edef4..715c672f20 100644 --- a/ext/civiimport/Civi/BAO/Import.php +++ b/ext/civiimport/Civi/BAO/Import.php @@ -42,12 +42,23 @@ class Import extends CRM_Core_DAO { public static $_primaryKey = ['_id']; /** + * Get the array of import tables in the database. + * + * Note that this can be required early (e.g EntityTypes, even + * before caching & class loading is fully available (the latter could be resolved now). + * Where that is the case `_civiimport_civicrm_get_import_tables` should be called directly. + * * @return array */ public static function getImportTables(): array { // This calls a function on the extension file as it is called from `entityTypes` // which can be called very early, before this class is available to that hook. - return _civiimport_civicrm_get_import_tables(); + if (!\Civi::cache('metadata')->has('civiimport_tables')) { + $tables = _civiimport_civicrm_get_import_tables(); + \Civi::cache('metadata')->set('civiimport_tables', $tables); + return $tables; + } + return \Civi::cache('metadata')->get('civiimport_tables'); } /** diff --git a/ext/civiimport/civiimport.php b/ext/civiimport/civiimport.php index be706d5b51..fe0aa5c517 100644 --- a/ext/civiimport/civiimport.php +++ b/ext/civiimport/civiimport.php @@ -14,6 +14,8 @@ use CRM_Civiimport_ExtensionUtil as E; * Implements hook_civicrm_config(). * * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/ + * + * @noinspection PhpUnused */ function civiimport_civicrm_config(&$config) { _civiimport_civix_civicrm_config($config); @@ -100,6 +102,9 @@ function civiimport_civicrm_entityTypes(array &$entityTypes): void { * Note this lives here as `entityTypes` hook calls it - which may not fully * have class loading set up by the time it runs. * + * Where the database is fully booted already it is better to call + * `Civi\BAO\Import::getImportTables()` which has caching. + * * @return array */ function _civiimport_civicrm_get_import_tables(): array { @@ -159,6 +164,43 @@ function civiimport_civicrm_alterTemplateFile($formName, $form, $type, &$templat } } +/** + * Implements search tasks hook to add the `validate` and `import` actions. + * + * @param array $tasks + * @param bool $checkPermissions + * @param int|null $userId + * + * @noinspection PhpUnused + */ +function civiimport_civicrm_searchKitTasks(array &$tasks, bool $checkPermissions, ?int $userId) { + foreach (_civiimport_civicrm_get_import_tables() as $import) { + $tasks['Import_' . $import['user_job_id']]['validate'] = [ + 'title' => E::ts('Validate'), + 'icon' => 'fa-check', + 'apiBatch' => [ + 'action' => 'validate', + 'params' => NULL, + 'runMsg' => E::ts('Validating %1 row/s...'), + 'successMsg' => E::ts('Ran validation on %1 row/s.'), + 'errorMsg' => E::ts('An error occurred while attempting to validate %1 row/s.'), + ], + ]; + $tasks['Import_' . $import['user_job_id']]['import'] = [ + 'title' => E::ts('Import'), + 'icon' => 'fa-arrow-right', + 'apiBatch' => [ + 'action' => 'import', + 'params' => NULL, + 'runMsg' => E::ts('Importing %1 row/s...'), + 'confirmMsg' => E::ts('Are you sure you want to import %1 row/s?'), + 'successMsg' => E::ts('Ran import on %1 row/s.'), + 'errorMsg' => E::ts('An error occurred while attempting to import %1 row/s.'), + ], + ]; + } +} + /** * Load the angular app for our form. * -- 2.25.1