From ff2b281e84572320f0a202c0980afa7f6a57abc9 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 10 Aug 2022 18:03:07 +1200 Subject: [PATCH] Consolidate error handling on Custom data import tpl --- CRM/Core/BAO/CustomGroup.php | 1 + CRM/Custom/Import/Form/DataSource.php | 56 ++++++++-- CRM/Import/Form/DataSource.php | 10 ++ .../CRM/Custom/Import/Form/DataSource.tpl | 103 +++++++++--------- templates/CRM/Import/Form/DataSource.tpl | 5 + 5 files changed, 114 insertions(+), 61 deletions(-) diff --git a/CRM/Core/BAO/CustomGroup.php b/CRM/Core/BAO/CustomGroup.php index 3b6e9269cf..b53f87b1bd 100644 --- a/CRM/Core/BAO/CustomGroup.php +++ b/CRM/Core/BAO/CustomGroup.php @@ -2251,6 +2251,7 @@ SELECT civicrm_custom_group.id as groupID, civicrm_custom_group.title as groupT * @return array */ public static function getMultipleFieldGroup() { + CRM_Core_Error::deprecatedFunctionWarning('api'); $multipleGroup = []; $dao = new CRM_Core_DAO_CustomGroup(); $dao->is_multiple = 1; diff --git a/CRM/Custom/Import/Form/DataSource.php b/CRM/Custom/Import/Form/DataSource.php index dd0d8d7539..394df3b2e6 100644 --- a/CRM/Custom/Import/Form/DataSource.php +++ b/CRM/Custom/Import/Form/DataSource.php @@ -15,6 +15,8 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\Api4\CustomGroup; + /** * This class gets the name of the file to upload */ @@ -33,6 +35,43 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource { return 'custom_field_import'; } + /** + * Multiple field custom groups. + * + * @var array + */ + protected $customFieldGroups; + + /** + * Get multi-field custom groups. + * + * @return array + * @throws \CRM_Core_Exception + */ + protected function getCustomGroups(): array { + if (isset($this->customFieldGroups)) { + return $this->customFieldGroups; + } + $this->customFieldGroups = []; + // If we make the permission TRUE is it too restrictive? + $fields = CustomGroup::get(FALSE)->addSelect('id', 'title') + ->addWhere('is_multiple', '=', TRUE) + ->addWhere('is_active', '=', TRUE)->execute(); + foreach ($fields as $field) { + $this->customFieldGroups[$field['id']] = $field['title']; + } + return $this->customFieldGroups; + } + + /** + * Get an error message to assign to the template. + * + * @return string + */ + protected function getErrorMessage(): string { + return empty($this->getCustomGroups()) ? ts('This import screen cannot be used because there are no Multi-value custom data groups.') : ''; + } + /** * Get the import entity (translated). * @@ -57,13 +96,15 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource { /** * @return array + * @throws \CRM_Core_Exception */ - public function setDefaultValues() { + public function setDefaultValues(): array { $config = CRM_Core_Config::singleton(); $defaults = [ 'contactType' => CRM_Import_Parser::CONTACT_INDIVIDUAL, 'fieldSeparator' => $config->fieldSeparator, - 'multipleCustomData' => $this->_id, + // Perhaps never used, but permits url passing of the group. + 'multipleCustomData' => CRM_Utils_Request::retrieve('id', 'Positive', $this), ]; $loadedMapping = $this->get('loadedMapping'); @@ -77,16 +118,11 @@ class CRM_Custom_Import_Form_DataSource extends CRM_Import_Form_DataSource { /** * Build the form object. * - * @return void + * @throws \CRM_Core_Exception */ - public function buildQuickForm() { + public function buildQuickForm(): void { parent::buildQuickForm(); - // Perhaps never used, but permits url passing of the group. - $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE); - $multipleCustomData = CRM_Core_BAO_CustomGroup::getMultipleFieldGroup(); - $this->assign('fieldGroups', $multipleCustomData); - $this->add('select', 'multipleCustomData', ts('Multi-value Custom Data'), ['' => ts('- select -')] + $multipleCustomData, TRUE); - + $this->add('select', 'multipleCustomData', ts('Multi-value Custom Data'), ['' => ts('- select -')] + $this->getCustomGroups(), TRUE); $this->addContactTypeSelector(); } diff --git a/CRM/Import/Form/DataSource.php b/CRM/Import/Form/DataSource.php index ca33c0bceb..17018fd7e4 100644 --- a/CRM/Import/Form/DataSource.php +++ b/CRM/Import/Form/DataSource.php @@ -55,6 +55,7 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { * Common form elements. */ public function buildQuickForm() { + $this->assign('errorMessage', $this->getErrorMessage()); $config = CRM_Core_Config::singleton(); // When we switch to using the DataSource.tpl used by Contact we can remove this in // favour of the one used by Contact - I was trying to consolidate @@ -109,6 +110,15 @@ abstract class CRM_Import_Form_DataSource extends CRM_Import_Forms { ]); } + /** + * Get an error message to assign to the template. + * + * @return string + */ + protected function getErrorMessage(): string { + return ''; + } + /** * A long-winded way to add one radio element to the form. */ diff --git a/templates/CRM/Custom/Import/Form/DataSource.tpl b/templates/CRM/Custom/Import/Form/DataSource.tpl index a5fde9dd84..6581685d79 100644 --- a/templates/CRM/Custom/Import/Form/DataSource.tpl +++ b/templates/CRM/Custom/Import/Form/DataSource.tpl @@ -8,70 +8,71 @@ +--------------------------------------------------------------------+ *} +{* Import Wizard - Step 1 (choose data source) *} {* Import Wizard - Step 1 (choose data source) *}
{* WizardHeader.tpl provides visual display of steps thru the wizard as well as title for current step *} {include file="CRM/common/WizardHeader.tpl"} - {if !$fieldGroups} -
- {ts}This import screen cannot be used because there are no Multi-value custom data groups.{/ts} -
- {/if} + {if $errorMessage} +
+ {$errorMessage} +
+ {/if}
{ts 1=$importEntity 2= $importEntities}The %1 Import Wizard allows you to easily upload %2 from other applications into CiviCRM.{/ts} {ts}Files to be imported must be in the 'comma-separated-values' format (CSV) and must contain data needed to match an existing contact in your CiviCRM database.{/ts} {help id='upload'}
-
-
{include file="CRM/common/formButtons.tpl" location="top"}
- - +
+
{include file="CRM/common/formButtons.tpl" location="top"}
+
+ - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - {include file="CRM/Core/Date.tpl"} - - {if $savedMapping} - - - - - - - - {/if} - -
{$form.uploadFile.label} {$form.uploadFile.html}
{ts}File format must be comma-separated-values (CSV).{/ts} -
 {ts 1=$uploadSize}Maximum Upload File Size: %1 MB{/ts}
 {$form.skipColumnHeader.html} {$form.skipColumnHeader.label}
- - {ts}Check this box if the first row of your file consists of field names (Example: "Contact ID", "Participant Role").{/ts} - -
{$form.multipleCustomData.label}{$form.multipleCustomData.html}
{$form.contactType.label}{$form.contactType.html}
 {ts 1=$uploadSize}Maximum Upload File Size: %1 MB{/ts}
 {$form.skipColumnHeader.html} {$form.skipColumnHeader.label}
+ + {ts}Check this box if the first row of your file consists of field names (Example: "Contact ID", "Participant Role").{/ts} + +
{$form.multipleCustomData.label}{$form.multipleCustomData.html}
{$form.contactType.label}{$form.contactType.html}
{$form.fieldSeparator.label} {help id='id-fieldSeparator' file='CRM/Contact/Import/Form/DataSource'}{$form.fieldSeparator.html}
{$form.savedMapping.label}{$form.savedMapping.html}
 {ts}Select Saved Mapping, or leave blank to create a new mapping.{/ts}
-
{include file="CRM/common/formButtons.tpl" location="bottom"}
+ + {$form.fieldSeparator.label} {help id='id-fieldSeparator' file='CRM/Contact/Import/Form/DataSource'} + {$form.fieldSeparator.html} + + + {include file="CRM/Core/Date.tpl"} + + {if $savedMapping} + + {$form.savedMapping.label} + {$form.savedMapping.html} + + +   + {ts}Select Saved Mapping, or leave blank to create a new mapping.{/ts} + + {/if} + +
{include file="CRM/common/formButtons.tpl" location="bottom"}
-
+ diff --git a/templates/CRM/Import/Form/DataSource.tpl b/templates/CRM/Import/Form/DataSource.tpl index 6fee271133..28ce84c23c 100644 --- a/templates/CRM/Import/Form/DataSource.tpl +++ b/templates/CRM/Import/Form/DataSource.tpl @@ -13,6 +13,11 @@ {* WizardHeader.tpl provides visual display of steps thru the wizard as well as title for current step *} {include file="CRM/common/WizardHeader.tpl"} + {if $errorMessage} +
+ {$errorMessage} +
+ {/if}
{ts 1=$importEntity 2= $importEntities}The %1 Import Wizard allows you to easily upload %2 from other applications into CiviCRM.{/ts} {ts}Files to be imported must be in the 'comma-separated-values' format (CSV) and must contain data needed to match an existing contact in your CiviCRM database.{/ts} {help id='upload'} -- 2.25.1