3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for groupContact.
21 class CRM_Contact_Form_GroupContact
extends CRM_Core_Form
{
24 * The groupContact id, used when editing the groupContact
28 protected $_groupContactId;
31 * The contact id, used when add/edit groupContact
35 protected $_contactId;
38 * Explicitly declare the entity api name.
40 public function getDefaultEntity() {
41 return 'GroupContact';
45 * Explicitly declare the form context.
47 public function getDefaultContext() {
54 public function preProcess() {
55 $this->_contactId
= $this->get('contactId');
56 $this->_groupContactId
= $this->get('groupContactId');
57 $this->_context
= CRM_Utils_Request
::retrieve('context', 'Alphanumeric', $this);
61 * Build the form object.
63 public function buildQuickForm() {
64 // get the list of all the groups
65 if ($this->_context
== 'user') {
66 $onlyPublicGroups = CRM_Utils_Request
::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE);
67 $ids = CRM_Core_PseudoConstant
::allGroup();
68 $heirGroups = CRM_Contact_BAO_Group
::getGroupsHierarchy($ids);
71 foreach ($heirGroups as $id => $group) {
72 // make sure that this group has public visibility
73 if ($onlyPublicGroups && $group['visibility'] == 'User and User Admin Only') {
76 $allGroups[$id] = $group;
80 $allGroups = CRM_Core_PseudoConstant
::group();
83 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
84 $groupHierarchy = CRM_Contact_BAO_Group
::getGroupsHierarchy($allGroups, NULL, ' ', TRUE);
86 // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed").
87 $currentGroups = CRM_Contact_BAO_GroupContact
::getGroupList($this->_contactId
);
89 // Remove current groups from drowdown options ($groupSelect)
90 if (is_array($currentGroups)) {
91 // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups
92 $groupSelect = array_diff_key($groupHierarchy, $currentGroups);
95 $groupSelect = $groupHierarchy;
98 $groupSelect = ['' => ts('- select group -')] +
$groupSelect;
100 if (count($groupSelect) > 1) {
101 $session = CRM_Core_Session
::singleton();
103 if (strstr($session->readUserContext(), 'user')) {
104 $msg = ts('Join a Group');
107 $msg = ts('Add to a group');
110 $this->addField('group_id', ['class' => 'crm-action-menu fa-plus', 'placeholder' => $msg, 'options' => $groupSelect]);
125 public function postProcess() {
126 $contactID = [$this->_contactId
];
127 $groupId = $this->controller
->exportValue('GroupContact', 'group_id');
128 $method = ($this->_context
== 'user') ?
'Web' : 'Admin';
130 $session = CRM_Core_Session
::singleton();
131 $userID = $session->get('userID');
133 if ($userID == $this->_contactId
) {
136 $groupContact = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactID, $groupId, $method);
138 if ($groupContact && $this->_context
!= 'user') {
139 $groups = CRM_Core_PseudoConstant
::group();
140 CRM_Core_Session
::setStatus(ts("Contact has been added to '%1'.", [1 => $groups[$groupId]]), ts('Added to Group'), 'success');