diff --git a/CRM/Contact/Form/Task/AddToGroup.php b/CRM/Contact/Form/Task/AddToGroup.php index 7a61aa1cf8ff..4d11df34bdbc 100644 --- a/CRM/Contact/Form/Task/AddToGroup.php +++ b/CRM/Contact/Form/Task/AddToGroup.php @@ -181,7 +181,7 @@ public function postProcess() { $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'); diff --git a/CRM/Custom/Form/CustomData.php b/CRM/Custom/Form/CustomData.php index 79c9d68c601c..0d2812be2040 100644 --- a/CRM/Custom/Form/CustomData.php +++ b/CRM/Custom/Form/CustomData.php @@ -147,7 +147,45 @@ public static function preProcess( $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 @@ public static function buildQuickForm(&$form) { /** * 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 @@ public static function buildQuickForm(&$form) { * @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; diff --git a/CRM/Custom/Form/CustomDataByType.php b/CRM/Custom/Form/CustomDataByType.php index d7d22e9104d9..b4cc13c266ce 100644 --- a/CRM/Custom/Form/CustomDataByType.php +++ b/CRM/Custom/Form/CustomDataByType.php @@ -52,12 +52,70 @@ public function preProcess() { 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. * diff --git a/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php b/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php index 9d1a8edcadb0..dc8b16cc61c7 100644 --- a/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php +++ b/tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php @@ -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 @@ protected function getSearchTaskFormObject(array $formValues) { /** * 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 @@ public function testAddToGroup() { $form->setDefaultValues(); $form->postProcess(); - $groupCount = \Civi\Api4\GroupContact::get() + $groupCount = GroupContact::get() ->addWhere('group_id', '=', $existingGroupId) ->addWhere('status', '=', 'Added') ->execute() @@ -62,8 +62,10 @@ public function testAddToGroup() { /** * 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 @@ public function testAddToNewGroupWithCustomField() { $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 @@ public function testAddToNewGroupWithCustomField() { $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()