Skip to content

Commit

Permalink
Clean up the 2 forms that support Group-extending custom data for not…
Browse files Browse the repository at this point in the history
…ices, php8.2 compliance
  • Loading branch information
eileenmcnaughton committed Feb 4, 2024
1 parent 4642796 commit d613dbb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 71 deletions.
23 changes: 14 additions & 9 deletions CRM/Contact/Form/Task/AddToGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* addition of contacts to groups.
*/
class CRM_Contact_Form_Task_AddToGroup extends CRM_Contact_Form_Task {
use CRM_Custom_Form_CustomDataTrait;

/**
* The context that we are working on
Expand Down Expand Up @@ -52,9 +53,13 @@ public function preProcess() {
parent::preProcess();

$this->_context = $this->get('context');
$this->_id = $this->get('amtgID');

CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Group', $this->_id);
$this->assign('entityID', $this->getGroupID());
}

public function getGroupID(): ?int {
$this->_id = $this->get('amtgID');
return $this->_id ? (int) $this->_id : NULL;
}

/**
Expand Down Expand Up @@ -117,8 +122,9 @@ public function buildQuickForm() {
}
else {
$this->setTitle(ts('Add Contacts to A Group'));
//build custom data
CRM_Custom_Form_CustomData::buildQuickForm($this);
}
if ($this->isSubmitted()) {
$this->addCustomDataFieldsToForm('Group');
}

$this->addDefaultButtons(ts('Add to Group'));
Expand All @@ -139,7 +145,6 @@ public function setDefaultValues() {
}

$defaults['group_option'] = 0;
$defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
return $defaults;
}

Expand All @@ -155,7 +160,7 @@ public function addRules() {
*
* @param array $params
*
* @return array
* @return array|true
* list of errors to be posted back to the form
*/
public static function formRule($params) {
Expand All @@ -174,7 +179,7 @@ public static function formRule($params) {
/**
* Process the form after the input has been submitted and validated.
*/
public function postProcess() {
public function postProcess(): void {
$params = $this->controller->exportValues();
$groupOption = $params['group_option'] ?? NULL;
if ($groupOption) {
Expand All @@ -184,7 +189,7 @@ public function postProcess() {
$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');
$groupParams['custom'] = CRM_Core_BAO_CustomField::postProcess($this->getSubmittedValues(), $this->_id, 'Group');

$createdGroup = CRM_Contact_BAO_Group::create($groupParams);
$groupID = $createdGroup->id;
Expand All @@ -196,7 +201,7 @@ public function postProcess() {
$groupName = $group[$groupID];
}

list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID);
[$total, $added, $notAdded] = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID);

$status = [
ts('%count contact added to group', [
Expand Down
31 changes: 10 additions & 21 deletions CRM/Group/Form/Edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
class CRM_Group_Form_Edit extends CRM_Core_Form {

use CRM_Core_Form_EntityFormTrait;
use CRM_Custom_Form_CustomDataTrait;

/**
* The group object, if an id is present
Expand All @@ -43,13 +44,6 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
*/
protected $_groupValues;

/**
* What blocks should we show and hide.
*
* @var CRM_Core_ShowHideBlocks
*/
protected $_showHide;

/**
* The civicrm_group_organization table id
*
Expand All @@ -60,7 +54,7 @@ class CRM_Group_Form_Edit extends CRM_Core_Form {
/**
* Set entity fields to be assigned to the form.
*/
protected function setEntityFields() {
protected function setEntityFields(): void {
$this->entityFields = [
'frontend_title' => ['name' => 'frontend_title', 'required' => TRUE],
'frontend_description' => ['name' => 'frontend_description'],
Expand Down Expand Up @@ -159,9 +153,6 @@ public function preProcess() {
$session->pushUserContext(CRM_Utils_System::url('civicrm/group', 'reset=1'));
}
$this->addExpectedSmartyVariables(['freezeMailingList', 'hideMailingList']);

//build custom data
CRM_Custom_Form_CustomData::preProcess($this, NULL, NULL, 1, 'Group', $this->_id);
}

/**
Expand Down Expand Up @@ -201,22 +192,19 @@ public function setDefaultValues() {
}
}

$parentGroupIds = explode(',', $this->_groupValues['parents']);
$parentGroupIds = explode(',', ($this->_groupValues['parents'] ?? ''));
$defaults['parents'] = $parentGroupIds;
if (empty($defaults['parents'])) {
$defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
}

// custom data set defaults
$defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
return $defaults;
}

/**
* Build the form object.
*/
public function buildQuickForm() {
self::buildQuickEntityForm();
$this->buildQuickEntityForm();
if ($this->_action & CRM_Core_Action::DELETE) {
return;
}
Expand Down Expand Up @@ -253,8 +241,9 @@ public function buildQuickForm() {
}
$this->addElement('checkbox', 'is_active', ts('Is active?'));

//build custom data
CRM_Custom_Form_CustomData::buildQuickForm($this);
if ($this->isSubmitted()) {
$this->addCustomDataFieldsToForm('Group');
}

$options = [
'selfObj' => $this,
Expand All @@ -271,7 +260,7 @@ public function buildQuickForm() {
* @param array $fileParams
* @param array $options
*
* @return array
* @return array|true
* list of errors to be posted back to the form
*/
public static function formRule($fields, $fileParams, $options) {
Expand Down Expand Up @@ -333,7 +322,7 @@ public function postProcess() {

$params['is_reserved'] ??= FALSE;
$params['is_active'] ??= FALSE;
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
$params['custom'] = CRM_Core_BAO_CustomField::postProcess($this->getSubmittedValues(),
$this->_id,
'Group'
);
Expand Down Expand Up @@ -421,7 +410,7 @@ public static function buildParentGroups(&$form) {
*
* @param CRM_Core_Form $form
*/
public static function buildGroupOrganizations(&$form) {
public static function buildGroupOrganizations($form) {
if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) {
//group organization Element
$props = ['api' => ['params' => ['contact_type' => 'Organization']]];
Expand Down
3 changes: 3 additions & 0 deletions Civi/Test/FormWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,9 @@ private function setFormObject(string $class, array $formValues = [], array $url

case strpos($class, 'Search') !== FALSE:
$this->form->controller = new \CRM_Contact_Controller_Search();
if ($class === 'CRM_Contact_Form_Search_Basic') {
$this->form->setAction(\CRM_Core_Action::BASIC);
}
break;

case strpos($class, '_Form_') !== FALSE:
Expand Down
3 changes: 1 addition & 2 deletions templates/CRM/Contact/Form/Task/AddToGroup.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
</tr>
{/if}
<tr>
<td colspan=2>{include file="CRM/Custom/Form/CustomData.tpl"}</td>
<td colspan=2>{include file="CRM/common/customDataBlock.tpl" groupID='' customDataType='Group' customDataSubType=false cid=false}</td>
</tr>
</table>
</td>
Expand All @@ -52,7 +52,6 @@
</table>
<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
</div>
{include file="CRM/common/showHide.tpl"}

{if !$form.group_id.value}
{literal}
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/Group/Form/Edit.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@


<tr>
<td colspan=2>{include file="CRM/Custom/Form/CustomData.tpl"}</td>
<td colspan=2>{include file="CRM/common/customDataBlock.tpl" groupID='' customDataType='Group' customDataSubType=false cid=false}</td>
</tr>
</table>

Expand Down
63 changes: 25 additions & 38 deletions tests/phpunit/CRM/Contact/Form/Task/AddToGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,16 @@

use Civi\Api4\Group;
use Civi\Api4\GroupContact;
use Civi\Test\FormTrait;

/**
* @group headless
*/
class CRM_Contact_Form_Task_AddToGroupTest extends CiviUnitTestCase {
use FormTrait;

/**
* @param array $formValues
* @return CRM_Contact_Form_Task_AddToGroup
*/
protected function getSearchTaskFormObject(array $formValues): CRM_Contact_Form_Task_AddToGroup {
$_POST = $formValues;
$_SERVER['REQUEST_METHOD'] = 'GET';
$form = new CRM_Contact_Form_Task_AddToGroup();
$form->controller = new CRM_Contact_Controller_Search();
$form->controller->setStateMachine(new CRM_Core_StateMachine($form->controller));
$_SESSION['_' . $form->controller->_name . '_container']['values']['Advanced'] = $formValues;
return $form;
}

/**
* Test delete to trash.
* Test add to existing group.
*
* @throws \CRM_Core_Exception
*/
Expand All @@ -42,19 +30,17 @@ public function testAddToGroup(): void {
'first_name' => 'John',
'last_name' => 'Doe',
]);
$contactId = $contact['id'];
$existingGroupId = $this->groupCreate();
$form = $this->getSearchTaskFormObject(['cids' => $contactId, 'group_option' => 0, 'group_id' => $existingGroupId]);
$form->preProcess();
$form->_contactIds = [$contactId];
$form->set('_componentIds', [$contactId]);
$form->buildQuickForm();
$form->setDefaultValues();
$form->postProcess();

$form = $this->getTestForm('CRM_Contact_Form_Search_Basic', ['radio_ts' => 'ts_all'])
->addSubsequentForm('CRM_Contact_Form_Task_AddToGroup', [
'group_option' => 0,
'group_id' => $existingGroupId,
]);
$form->processForm();
$groupCount = GroupContact::get()
->addWhere('group_id', '=', $existingGroupId)
->addWhere('status', '=', 'Added')
->addWhere('contact_id', '=', $contact['id'])
->execute()
->count();
$this->assertEquals(1, $groupCount);
Expand All @@ -71,24 +57,24 @@ public function testAddToNewGroupWithCustomField(): void {
'first_name' => 'Pete',
'last_name' => 'Johnson',
]);
$contactId = $contact['id'];

$customGroup = $this->customGroupCreate(['extends' => 'Group']);
$customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
$customFieldId = $customField['id'];

$form = $this->getSearchTaskFormObject([
'cids' => $contactId,
'group_option' => 1,
'title' => 'Test Group With Custom Field',
'description' => '',
'custom_' . $customFieldId => 'Custom Value ABC',
]);
$form->preProcess();
$form->_contactIds = [$contactId];
$form->set('_componentIds', [$contactId]);
$form->buildQuickForm();
$form->setDefaultValues();
$form->postProcess();
$form = $this->getTestForm(
'CRM_Contact_Form_Search_Basic',
['radio_ts' => 'ts_all']
)->addSubsequentForm(
'CRM_Contact_Form_Task_AddToGroup',
[
'group_option' => 1,
'title' => 'Test Group With Custom Field',
'description' => '',
'custom_' . $customFieldId => 'Custom Value ABC',
]
);
$form->processForm();

$group = Group::get()
->addSelect('custom.*', 'id')
Expand All @@ -102,6 +88,7 @@ public function testAddToNewGroupWithCustomField(): void {
$groupCount = GroupContact::get()
->addWhere('group_id', '=', $group['id'])
->addWhere('status', '=', 'Added')
->addWhere('contact_id', '=', $contact['id'])
->execute()
->count();
$this->assertEquals(1, $groupCount);
Expand Down

0 comments on commit d613dbb

Please sign in to comment.