Unshare shared function in order to clean-up
authorEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 14 Jul 2023 23:01:25 +0000 (11:01 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Fri, 14 Jul 2023 23:17:42 +0000 (11:17 +1200)
CRM/Contact/Form/Task/AddToGroup.php
CRM/Custom/Form/CustomData.php
CRM/Custom/Form/CustomDataByType.php
tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php

index 7a61aa1cf8ffabd895e32fc0c7cf9a684b3070a2..4d11df34bdbc7e5da82402b47293e1aeb8c8d76b 100644 (file)
@@ -181,7 +181,7 @@ class CRM_Contact_Form_Task_AddToGroup extends CRM_Contact_Form_Task {
       $groupParams = [];
       $groupParams['title'] = $params['title'];
       $groupParams['description'] = $params['description'];
-      $groupParams['visibility'] = "User and User Admin Only";
+      $groupParams['visibility'] = 'User and User Admin Only';
       $groupParams['group_type'] = array_keys($params['group_type'] ?? []);
       $groupParams['is_active'] = 1;
       $groupParams['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $this->_id, 'Group');
index 79c9d68c601c410d8ac044ba5704887041533e72..0d2812be204008f41baab6b4e7ff4ddae721a962 100644 (file)
@@ -147,7 +147,45 @@ class CRM_Custom_Form_CustomData {
       $subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($subType, CRM_Core_DAO::VALUE_SEPARATOR));
     }
 
-    self::setGroupTree($form, $subType, $gid, $onlySubType, $getCachedTree);
+    $singleRecord = NULL;
+    if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+      $singleRecord = $form->_groupCount;
+    }
+    $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
+    // when a new record is being added for multivalued custom fields.
+    if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
+      !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+      $singleRecord = 'new';
+    }
+
+    $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
+      NULL,
+      $form->_entityId,
+      $gid,
+      $subType,
+      $form->_subName,
+      $getCachedTree,
+      $onlySubType,
+      FALSE,
+      CRM_Core_Permission::EDIT,
+      $singleRecord
+    );
+
+    if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
+      $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
+    }
+    // we should use simplified formatted groupTree
+    $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
+
+    if (isset($form->_groupTree) && is_array($form->_groupTree)) {
+      $keys = array_keys($groupTree);
+      foreach ($keys as $key) {
+        $form->_groupTree[$key] = $groupTree[$key];
+      }
+    }
+    else {
+      $form->_groupTree = $groupTree;
+    }
   }
 
   /**
@@ -173,6 +211,14 @@ class CRM_Custom_Form_CustomData {
   /**
    * Add the group data as a formatted array to the form.
    *
+   * Note this is only called from this class in core but it is called
+   * from the gdpr extension so rather than clean it up we will deprecate in place
+   * and stop calling from core. (Calling functions like this from extensions)
+   * is not supported but since we are aware of it we can deprecate rather than
+   * remove it).
+   *
+   * @deprecated since 5.65 will be removed around 5.80.
+   *
    * @param CRM_Core_Form $form
    * @param string $subType
    * @param int $gid
@@ -183,6 +229,7 @@ class CRM_Custom_Form_CustomData {
    * @throws \CRM_Core_Exception
    */
   public static function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
+    CRM_Core_Error::deprecatedFunctionWarning('no alternative - maybe copy & paste to your extension');
     $singleRecord = NULL;
     if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
       $singleRecord = $form->_groupCount;
index d7d22e9104d998c0d808d7121255ff8632f32caa..b4cc13c266ce6171c9f2ca43b8e1f2cd47526ea9 100644 (file)
@@ -52,12 +52,70 @@ class CRM_Custom_Form_CustomDataByType extends CRM_Core_Form {
       CRM_Core_Error::deprecatedWarning('Using a CRM_Core_DAO::VALUE_SEPARATOR separated subType on civicrm/custom route is deprecated, use a comma-separated string instead.');
       $this->_subType = str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($this->_subType, CRM_Core_DAO::VALUE_SEPARATOR));
     }
-    CRM_Custom_Form_CustomData::setGroupTree($this, $this->_subType, $this->_groupID, $this->_onlySubtype);
+    $this->setGroupTree($this, $this->_subType, $this->_groupID, $this->_onlySubtype);
 
     $this->assign('suppressForm', TRUE);
     $this->controller->_generateQFKey = FALSE;
   }
 
+  /**
+   * Add the group data as a formatted array to the form.
+   *
+   * This was split off from a shared function.
+   *
+   * @param self $form
+   * @param string $subType
+   * @param int $gid
+   * @param bool $onlySubType
+   * @param bool $getCachedTree
+   *
+   * @return array
+   * @throws \CRM_Core_Exception
+   */
+  private function setGroupTree(&$form, $subType, $gid, $onlySubType = NULL, $getCachedTree = TRUE) {
+    $singleRecord = NULL;
+    if (!empty($form->_groupCount) && !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+      $singleRecord = $form->_groupCount;
+    }
+    $mode = CRM_Utils_Request::retrieve('mode', 'String', $form);
+    // when a new record is being added for multivalued custom fields.
+    if (isset($form->_groupCount) && $form->_groupCount == 0 && $mode == 'add' &&
+      !empty($form->_multiRecordDisplay) && $form->_multiRecordDisplay == 'single') {
+      $singleRecord = 'new';
+    }
+
+    $groupTree = CRM_Core_BAO_CustomGroup::getTree($form->_type,
+      NULL,
+      $form->_entityId,
+      $gid,
+      $subType,
+      $form->_subName,
+      $getCachedTree,
+      $onlySubType,
+      FALSE,
+      CRM_Core_Permission::EDIT,
+      $singleRecord
+    );
+
+    if (property_exists($form, '_customValueCount') && !empty($groupTree)) {
+      $form->_customValueCount = CRM_Core_BAO_CustomGroup::buildCustomDataView($form, $groupTree, TRUE, NULL, NULL, NULL, $form->_entityId);
+    }
+    // we should use simplified formatted groupTree
+    $groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, $form->_groupCount, $form);
+
+    if (isset($form->_groupTree) && is_array($form->_groupTree)) {
+      $keys = array_keys($groupTree);
+      foreach ($keys as $key) {
+        $form->_groupTree[$key] = $groupTree[$key];
+      }
+      return [$form, $groupTree];
+    }
+    else {
+      $form->_groupTree = $groupTree;
+      return [$form, $groupTree];
+    }
+  }
+
   /**
    * Set defaults.
    *
index 9d1a8edcadb0b9802cfdf53754236887e2269b00..dc8b16cc61c7b7f6ffba4ecb9334a1f0e231ead1 100644 (file)
@@ -9,23 +9,21 @@
  +--------------------------------------------------------------------+
  */
 
- /**
-  * @group headless
-  */
-class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
+use Civi\Api4\Group;
+use Civi\Api4\GroupContact;
 
-  protected function setUp(): void {
-    parent::setUp();
-  }
+/**
+ * @group headless
+ */
+class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
 
   /**
    * @param array $formValues
    * @return CRM_Contact_Form_Task_AddToGroup
    */
-  protected function getSearchTaskFormObject(array $formValues) {
+  protected function getSearchTaskFormObject(array $formValues): CRM_Contact_Form_Task_AddToGroup {
     $_POST = $formValues;
     $_SERVER['REQUEST_METHOD'] = 'GET';
-    /** @var CRM_Core_Form $form */
     $form = new CRM_Contact_Form_Task_AddToGroup();
     $form->controller = new CRM_Contact_Controller_Search();
     $form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
@@ -35,8 +33,10 @@ class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
 
   /**
    * Test delete to trash.
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testAddToGroup() {
+  public function testAddToGroup(): void {
     $contact = $this->callAPISuccess('Contact', 'create', [
       'contact_type' => 'Individual',
       'first_name' => 'John',
@@ -52,7 +52,7 @@ class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
     $form->setDefaultValues();
     $form->postProcess();
 
-    $groupCount = \Civi\Api4\GroupContact::get()
+    $groupCount = GroupContact::get()
       ->addWhere('group_id', '=', $existingGroupId)
       ->addWhere('status', '=', 'Added')
       ->execute()
@@ -62,8 +62,10 @@ class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
 
   /**
    * Test delete to trash.
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function testAddToNewGroupWithCustomField() {
+  public function testAddToNewGroupWithCustomField(): void {
     $contact = $this->callAPISuccess('Contact', 'create', [
       'contact_type' => 'Individual',
       'first_name' => 'Pete',
@@ -88,7 +90,7 @@ class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
     $form->setDefaultValues();
     $form->postProcess();
 
-    $group = \Civi\Api4\Group::get()
+    $group = Group::get()
       ->addSelect('custom.*', 'id')
       ->addWhere('title', '=', 'Test Group With Custom Field')
       ->execute();
@@ -97,7 +99,7 @@ class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
     $this->assertArrayKeyExists('new_custom_group.Custom_Field', $group);
     $this->assertEquals('Custom Value ABC', $group['new_custom_group.Custom_Field']);
 
-    $groupCount = \Civi\Api4\GroupContact::get()
+    $groupCount = GroupContact::get()
       ->addWhere('group_id', '=', $group['id'])
       ->addWhere('status', '=', 'Added')
       ->execute()