Copy locationBuildForm function back to only class that calls it
authorEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 30 Aug 2023 20:59:57 +0000 (08:59 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Wed, 30 Aug 2023 21:44:44 +0000 (09:44 +1200)
CRM/Contact/Form/Contact.php
CRM/Contact/Form/Location.php

index 856877a178c6bbe0ae8aa63421289bece99f7c69..397236dc83c7bf669d10c2aa87d7e6ef968ced6c 100644 (file)
@@ -819,7 +819,7 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
 
     // build location blocks.
     CRM_Contact_Form_Edit_Lock::buildQuickForm($this);
-    CRM_Contact_Form_Location::buildQuickForm($this);
+    $this->buildLocationForm();
 
     // add attachment
     $this->addField('image_URL', ['maxlength' => '255', 'label' => ts('Browse/Upload Image'), 'accept' => 'image/png, image/jpeg, image/gif']);
@@ -883,6 +883,67 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
     $this->addButtons($buttons);
   }
 
+  private function buildLocationForm(): void {
+    // required for subsequent AJAX requests.
+    $ajaxRequestBlocks = [];
+    $generateAjaxRequest = 0;
+
+    //build 1 instance of all blocks, without using ajax ...
+    foreach ($this->_blocks as $blockName => $label) {
+      $name = strtolower($blockName);
+
+      $instances = [1];
+      if (!empty($_POST[$name]) && is_array($_POST[$name])) {
+        $instances = array_keys($_POST[$name]);
+      }
+      elseif (!empty($this->_values[$name]) && is_array($this->_values[$name])) {
+        $instances = array_keys($this->_values[$name]);
+      }
+
+      foreach ($instances as $instance) {
+        if ($instance == 1) {
+          $this->assign('addBlock', FALSE);
+          $this->assign('blockId', $instance);
+        }
+        else {
+          //we are going to build other block instances w/ AJAX
+          $generateAjaxRequest++;
+          $ajaxRequestBlocks[$blockName][$instance] = TRUE;
+        }
+        switch ($blockName) {
+          case 'Email':
+            // setDefaults uses this to tell which instance
+            $this->set('Email_Block_Count', $instance);
+            CRM_Contact_Form_Edit_Email::buildQuickForm($this, $instance);
+            // Only display the signature fields if this contact has a CMS account
+            // because they can only send email if they have access to the CRM
+            $ufID = $this->_contactId && CRM_Core_BAO_UFMatch::getUFId($this->_contactId);
+            $this->assign('isAddSignatureFields', (bool) $ufID);
+            if ($ufID) {
+              $this->add('textarea', "email[$instance][signature_text]", ts('Signature (Text)'),
+                ['rows' => 2, 'cols' => 40]
+              );
+              $this->add('wysiwyg', "email[$instance][signature_html]", ts('Signature (HTML)'),
+                ['rows' => 2, 'cols' => 40]
+              );
+            }
+            break;
+
+          default:
+            // @todo This pattern actually adds complexity compared to filling out a switch statement
+            // for the limited number of blocks - as we also have to receive the block count
+            $this->set($blockName . '_Block_Count', $instance);
+            $formName = 'CRM_Contact_Form_Edit_' . $blockName;
+            $formName::buildQuickForm($this);
+        }
+      }
+    }
+
+    //assign to generate AJAX request for building extra blocks.
+    $this->assign('generateAjaxRequest', $generateAjaxRequest);
+    $this->assign('ajaxRequestBlocks', $ajaxRequestBlocks);
+  }
+
   /**
    * Form submission of new/edit contact is processed.
    */
index 8040a045f503d530cc4f52abe90f76803836dc85..206e2e5abcddb432f5528cc48bd38bf1c4854e59 100644 (file)
  *
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
+ *
+ * @deprecated in CiviCRM 5.66, will be removed around CiviCRM 5.76.
  */
 class CRM_Contact_Form_Location {
 
   /**
    * Build the form object.
    *
+   * @deprecated in CiviCRM 5.66, will be removed around CiviCRM 5.76.
    * @param CRM_Core_Form $form
    */
   public static function buildQuickForm(&$form) {