Merge pull request #6493 from eileenmcnaughton/CRM-17023
[civicrm-core.git] / CRM / Contact / Form / GroupContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components for groupContact
38 *
39 */
40 class CRM_Contact_Form_GroupContact extends CRM_Core_Form {
41
42 /**
43 * The groupContact id, used when editing the groupContact
44 *
45 * @var int
46 */
47 protected $_groupContactId;
48
49 /**
50 * The contact id, used when add/edit groupContact
51 *
52 * @var int
53 */
54 protected $_contactId;
55
56 /**
57 * Explicitly declare the entity api name.
58 */
59 public function getDefaultEntity() {
60 return 'GroupContact';
61 }
62
63 /**
64 * Explicitly declare the form context.
65 */
66 public function getDefaultContext() {
67 return 'create';
68 }
69
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);
74 }
75
76 /**
77 * Build the form object.
78 *
79 * @return void
80 */
81 public function buildQuickForm() {
82 // get the list of all the groups
83 if ($this->_context == 'user') {
84 $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE);
85 $allGroups = CRM_Core_PseudoConstant::staticGroup($onlyPublicGroups);
86 }
87 else {
88 $allGroups = CRM_Core_PseudoConstant::group();
89 }
90
91 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
92 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, '&nbsp;&nbsp;', TRUE);
93
94 // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed").
95 $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId);
96
97 // Remove current groups from drowdown options ($groupSelect)
98 if (is_array($currentGroups)) {
99 // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups
100 $groupSelect = array_diff_key($groupHierarchy, $currentGroups);
101 }
102 else {
103 $groupSelect = $groupHierarchy;
104 }
105
106 $groupSelect = array('' => ts('- select group -')) + $groupSelect;
107
108 if (count($groupSelect) > 1) {
109 $session = CRM_Core_Session::singleton();
110 // user dashboard
111 if (strstr($session->readUserContext(), 'user')) {
112 $msg = ts('Join a Group');
113 }
114 else {
115 $msg = ts('Add to a group');
116 }
117
118 $this->addField('group_id', array('class' => 'crm-action-menu action-icon-plus', 'placeholder' => $msg, 'options' => $groupSelect));
119
120 $this->addButtons(array(
121 array(
122 'type' => 'next',
123 'name' => ts('Add'),
124 'isDefault' => TRUE,
125 ),
126 )
127 );
128 }
129 }
130
131 /**
132 *
133 * @return void
134 */
135 public function postProcess() {
136 $contactID = array($this->_contactId);
137 $groupId = $this->controller->exportValue('GroupContact', 'group_id');
138 $method = ($this->_context == 'user') ? 'Web' : 'Admin';
139
140 $session = CRM_Core_Session::singleton();
141 $userID = $session->get('userID');
142
143 if ($userID == $this->_contactId) {
144 $method = 'Web';
145 }
146 $groupContact = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactID, $groupId, $method);
147
148 if ($groupContact && $this->_context != 'user') {
149 $groups = CRM_Core_PseudoConstant::group();
150 CRM_Core_Session::setStatus(ts("Contact has been added to '%1'.", array(1 => $groups[$groupId])), ts('Added to Group'), 'success');
151 }
152 }
153
154 }