Merge pull request #52 from eileenmcnaughton/newSettingStuff
[civicrm-core.git] / CRM / Contact / Form / GroupContact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 function preProcess() {
57 $this->_contactId = $this->get('contactId');
58 $this->_groupContactId = $this->get('groupContactId');
59 $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this);
60 }
61
62 /**
63 * This function sets the default values for the form. GroupContact that in edit/view mode
64 * the default values are retrieved from the database
65 *
66 * @access public
67 *
68 * @return None
69 */
70 function setDefaultValues() {
71 $defaults = array();
72 $params = array();
73
74 return $defaults;
75 }
76
77 /**
78 * This function is used to add the rules for form.
79 *
80 * @return None
81 * @access public
82 */
83 function addRules() {}
84
85 /**
86 * Function to build the form
87 *
88 * @return None
89 * @access public
90 */
91 public function buildQuickForm() {
92 // get the list of all the groups
93 if ($this->_context == 'user') {
94 $onlyPublicGroups = CRM_Utils_Request::retrieve('onlyPublicGroups', 'Boolean', $this, FALSE);
95 $allGroups = CRM_Core_PseudoConstant::staticGroup($onlyPublicGroups);
96 }
97 else {
98 $allGroups = CRM_Core_PseudoConstant::group();
99 }
100
101 // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title)
102 $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($allGroups, NULL, '&nbsp;&nbsp;', TRUE);
103
104 // get the list of groups contact is currently in ("Added") or unsubscribed ("Removed").
105 $currentGroups = CRM_Contact_BAO_GroupContact::getGroupList($this->_contactId);
106
107 // Remove current groups from drowdown options ($groupSelect)
108 if (is_array($currentGroups)) {
109 // Compare array keys, since the array values (group title) in $groupList may have extra spaces for indenting child groups
110 $groupSelect = array_diff_key($groupHierarchy, $currentGroups);
111 }
112 else {
113 $groupSelect = $groupHierarchy;
114 }
115
116 $groupSelect = array( '' => ts('- select group -')) + $groupSelect;
117
118 if (count($groupSelect) > 1) {
119 $session = CRM_Core_Session::singleton();
120 // user dashboard
121 if (strstr($session->readUserContext(), 'user')) {
122 $msg = ts('Join a Group');
123 }
124 else {
125 $msg = ts('Add to a group');
126 }
127
128 $this->add('select', 'group_id', $msg, $groupSelect, TRUE);
129
130 $this->addButtons(array(
131 array(
132 'type' => 'next',
133 'name' => ts('Add'),
134 'isDefault' => TRUE,
135 ),
136 )
137 );
138 }
139 }
140
141 /**
142 *
143 * @access public
144 *
145 * @return None
146 */
147 public function postProcess() {
148 $contactID = array($this->_contactId);
149 $groupId = $this->controller->exportValue('GroupContact', 'group_id');
150 $method = 'Admin';
151 $method = ($this->_context == 'user') ? 'Web' : 'Admin';
152
153 $session = CRM_Core_Session::singleton();
154 $userID = $session->get('userID');
155
156 if ($userID == $this->_contactId) {
157 $method = 'Web';
158 }
159 $groupContact = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactID, $groupId, $method);
160
161 if ($groupContact && $this->_context != 'user') {
162 $groups = CRM_Core_PseudoConstant::group();
163 CRM_Core_Session::setStatus(ts("Contact has been added to '%1'.", array(1 => $groups[$groupId])), ts('Added to Group'), 'success');
164 }
165 }
166 //end of function
167 }
168