Consolidate error handling on Custom data import tpl
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 10 Aug 2022 06:03:07 +0000 (18:03 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 10 Aug 2022 06:27:17 +0000 (18:27 +1200)
CRM/Core/BAO/CustomGroup.php
CRM/Custom/Import/Form/DataSource.php
CRM/Import/Form/DataSource.php
templates/CRM/Custom/Import/Form/DataSource.tpl
templates/CRM/Import/Form/DataSource.tpl

index 3b6e9269cf801a4c1f94599bd010b6e1ba246427..b53f87b1bd06427375771317c61b0f8c6e82d789 100644 (file)
@@ -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;
index dd0d8d7539fa6f4f84a0ea69940c6de6e2b5f10c..394df3b2e63f6b409150c35ecf91da522778be4b 100644 (file)
@@ -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();
   }
 
index ca33c0bceb8bb2675642366670c9e70065560224..17018fd7e4393db557be220eea8835624f575076 100644 (file)
@@ -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.
    */
index a5fde9dd84b69a1562a8e493f3375c98448073de..6581685d79d5f6a0886ef1c2874f7becaa2127c2 100644 (file)
@@ -8,70 +8,71 @@
  +--------------------------------------------------------------------+
 *}
 
+{* Import Wizard - Step 1 (choose data source) *}
 {* Import Wizard - Step 1 (choose data source) *}
 <div class="crm-block crm-form-block crm-import-datasource-form-block">
 
   {* 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}
-  <div class="messages warning no-popup">
-    {ts}This import screen cannot be used because there are no Multi-value custom data groups.{/ts}
-  </div>
- {/if}
 {if $errorMessage}
+    <div class="messages warning no-popup">
+      {$errorMessage}
+    </div>
 {/if}
   <div class="help">
     {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'}
   </div>
- <div id="upload-file" class="form-item">
- <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
-   <table class="form-layout">
-    <tr class="crm-import-uploadfile-form-block-uploadFile">
 <div id="upload-file" class="form-item">
 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
+    <table class="form-layout">
+      <tr class="crm-import-uploadfile-form-block-uploadFile">
       <td class="label">{$form.uploadFile.label}</td>
       <td>{$form.uploadFile.html}<br />
       <span class="description">
         {ts}File format must be comma-separated-values (CSV).{/ts}
       </span>
-    </td>
-  </tr>
-  <tr>
-    <td>&nbsp;</td>
-      <td>{ts 1=$uploadSize}Maximum Upload File Size: %1 MB{/ts}</td>
-        </tr>
-  <tr class="crm-import-form-block-skipColumnHeader">
-            <td>&nbsp;</td>
-            <td>{$form.skipColumnHeader.html} {$form.skipColumnHeader.label}<br />
-                <span class="description">
-                    {ts}Check this box if the first row of your file consists of field names (Example: "Contact ID", "Participant Role").{/ts}
-                </span>
-            </td>
-  </tr>
-  <tr class="crm-import-uploadfile-form-block-multipleCustomData">
-              <td class="label">{$form.multipleCustomData.label}</td>
-              <td><span>{$form.multipleCustomData.html}</span> </td>
-  </tr>
-  <tr class="crm-import-uploadfile-from-block-contactType">
-              <td class="label">{$form.contactType.label}</td>
-             <td>{$form.contactType.html}</td>
-  </tr>
+      </td>
+    </tr>
+      <tr>
+        <td>&nbsp;</td>
+        <td>{ts 1=$uploadSize}Maximum Upload File Size: %1 MB{/ts}</td>
+      </tr>
+      <tr class="crm-import-form-block-skipColumnHeader">
+        <td>&nbsp;</td>
+        <td>{$form.skipColumnHeader.html} {$form.skipColumnHeader.label}<br />
+          <span class="description">
+            {ts}Check this box if the first row of your file consists of field names (Example: "Contact ID", "Participant Role").{/ts}
+          </span>
+        </td>
+      </tr>
+     <tr class="crm-import-uploadfile-form-block-multipleCustomData">
+       <td class="label">{$form.multipleCustomData.label}</td>
+       <td><span>{$form.multipleCustomData.html}</span> </td>
+     </tr>
+     <tr class="crm-import-uploadfile-from-block-contactType">
+       <td class="label">{$form.contactType.label}</td>
+       <td>{$form.contactType.html}</td>
+     </tr>
 
-   <tr class="crm-import-datasource-form-block-fieldSeparator">
-     <td class="label">{$form.fieldSeparator.label} {help id='id-fieldSeparator' file='CRM/Contact/Import/Form/DataSource'}</td>
-     <td>{$form.fieldSeparator.html}</td>
-   </tr>
-  <tr class="crm-import-uploadfile-form-block-date_format">
-            {include file="CRM/Core/Date.tpl"}
-  </tr>
-  {if $savedMapping}
-  <tr class="crm-import-uploadfile-form-block-savedMapping">
-              <td class="label">{$form.savedMapping.label}</td>
-              <td><span>{$form.savedMapping.html}</span> </td>
-  </tr>
-  <tr>
-            <td>&nbsp;</td>
-            <td class="description">{ts}Select Saved Mapping, or leave blank to create a new mapping.{/ts}</td>
-        {/if}
-        </tr>
-    </table>
-    <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
+     <tr class="crm-import-datasource-form-block-fieldSeparator">
+       <td class="label">{$form.fieldSeparator.label} {help id='id-fieldSeparator' file='CRM/Contact/Import/Form/DataSource'}</td>
+       <td>{$form.fieldSeparator.html}</td>
+     </tr>
+     <tr class="crm-import-uploadfile-form-block-date_format">
+       {include file="CRM/Core/Date.tpl"}
+     </tr>
+     {if $savedMapping}
+       <tr class="crm-import-uploadfile-form-block-savedMapping">
+         <td class="label">{$form.savedMapping.label}</td>
+         <td><span>{$form.savedMapping.html}</span> </td>
+       </tr>
+       <tr>
+         <td>&nbsp;</td>
+         <td class="description">{ts}Select Saved Mapping, or leave blank to create a new mapping.{/ts}</td>
+       </tr>
+     {/if}
+  </table>
+  <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
   </div>
- </div>
+</div>
index 6fee27113398d02c9ff14ed947b5116224a2bcc3..28ce84c23c9d94f73d60689212f84c11933fbe16 100644 (file)
 
   {* 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}
+    <div class="messages warning no-popup">
+      {$errorMessage}
+    </div>
+  {/if}
   <div class="help">
     {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'}