_contactId = $this->get('contactId'); $this->_groupContactId = $this->get('groupContactId'); $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this); } /** * Function to build the form * * @return None * @access public */ public function buildQuickForm() { // get the list of all the groups if ($this->_context == 'user') { $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE); $allGroups = CRM_Core_PseudoConstant::staticGroup($onlyPublicGroups); } else { $allGroups = CRM_Core_PseudoConstant::group(); } // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title) $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, '  ', TRUE); // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed"). $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId); // Remove current groups from drowdown options ($groupSelect) if (is_array($currentGroups)) { // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups $groupSelect = array_diff_key($groupHierarchy, $currentGroups); } else { $groupSelect = $groupHierarchy; } $groupSelect = array( '' => ts('- select group -')) + $groupSelect; if (count($groupSelect) > 1) { $session = CRM_Core_Session::singleton(); // user dashboard if (strstr($session->readUserContext(), 'user')) { $msg = ts('Join a Group'); } else { $msg = ts('Add to a group'); } $this->add('select', 'group_id', $msg, $groupSelect, TRUE); $this->addButtons(array( array( 'type' => 'next', 'name' => ts('Add'), 'isDefault' => TRUE, ), ) ); } } /** * * @access public * * @return None */ public function postProcess() { $contactID = array($this->_contactId); $groupId = $this->controller->exportValue('GroupContact', 'group_id'); $method = 'Admin'; $method = ($this->_context == 'user') ? 'Web' : 'Admin'; $session = CRM_Core_Session::singleton(); $userID = $session->get('userID'); if ($userID == $this->_contactId) { $method = 'Web'; } $groupContact = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactID, $groupId, $method); if ($groupContact && $this->_context != 'user') { $groups = CRM_Core_PseudoConstant::group(); CRM_Core_Session::setStatus(ts("Contact has been added to '%1'.", array(1 => $groups[$groupId])), ts('Added to Group'), 'success'); } } //end of function }