3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * This class generates form components for groupContact.
37 class CRM_Contact_Form_GroupContact
extends CRM_Core_Form
{
40 * The groupContact id, used when editing the groupContact
44 protected $_groupContactId;
47 * The contact id, used when add/edit groupContact
51 protected $_contactId;
54 * Explicitly declare the entity api name.
56 public function getDefaultEntity() {
57 return 'GroupContact';
61 * Explicitly declare the form context.
63 public function getDefaultContext() {
70 public function preProcess() {
71 $this->_contactId
= $this->get('contactId');
72 $this->_groupContactId
= $this->get('groupContactId');
73 $this->_context
= CRM_Utils_Request
::retrieve('context', 'String', $this);
77 * Build the form object.
79 public function buildQuickForm() {
80 // get the list of all the groups
81 if ($this->_context
== 'user') {
82 $onlyPublicGroups = CRM_Utils_Request
::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE);
83 $allGroups = CRM_Core_PseudoConstant
::staticGroup($onlyPublicGroups);
86 $allGroups = CRM_Core_PseudoConstant
::group();
89 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
90 $groupHierarchy = CRM_Contact_BAO_Group
::getGroupsHierarchy($allGroups, NULL, ' ', TRUE);
92 // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed").
93 $currentGroups = CRM_Contact_BAO_GroupContact
::getGroupList($this->_contactId
);
95 // Remove current groups from drowdown options ($groupSelect)
96 if (is_array($currentGroups)) {
97 // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups
98 $groupSelect = array_diff_key($groupHierarchy, $currentGroups);
101 $groupSelect = $groupHierarchy;
104 $groupSelect = array('' => ts('- select group -')) +
$groupSelect;
106 if (count($groupSelect) > 1) {
107 $session = CRM_Core_Session
::singleton();
109 if (strstr($session->readUserContext(), 'user')) {
110 $msg = ts('Join a Group');
113 $msg = ts('Add to a group');
116 $this->addField('group_id', array('class' => 'crm-action-menu action-icon-plus', 'placeholder' => $msg, 'options' => $groupSelect));
118 $this->addButtons(array(
132 public function postProcess() {
133 $contactID = array($this->_contactId
);
134 $groupId = $this->controller
->exportValue('GroupContact', 'group_id');
135 $method = ($this->_context
== 'user') ?
'Web' : 'Admin';
137 $session = CRM_Core_Session
::singleton();
138 $userID = $session->get('userID');
140 if ($userID == $this->_contactId
) {
143 $groupContact = CRM_Contact_BAO_GroupContact
::addContactsToGroup($contactID, $groupId, $method);
145 if ($groupContact && $this->_context
!= 'user') {
146 $groups = CRM_Core_PseudoConstant
::group();
147 CRM_Core_Session
::setStatus(ts("Contact has been added to '%1'.", array(1 => $groups[$groupId])), ts('Added to Group'), 'success');