Skip to content

Commit

Permalink
Unshare shared function in order to clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jul 14, 2023
1 parent eb725aa commit 7a58f7f
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Task/AddToGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
49 changes: 48 additions & 1 deletion CRM/Custom/Form/CustomData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand All @@ -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
Expand All @@ -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;
Expand Down
60 changes: 59 additions & 1 deletion CRM/Custom/Form/CustomDataByType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
30 changes: 16 additions & 14 deletions tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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',
Expand All @@ -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()
Expand All @@ -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',
Expand All @@ -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();
Expand All @@ -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()
Expand Down

0 comments on commit 7a58f7f

Please sign in to comment.