Merge branch 4.5 into master
[civicrm-core.git] / CRM / Contact / Form / Task / AddToGroup.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class provides the functionality to group
38 * contacts. This class provides functionality for the actual
39 * addition of contacts to groups.
40 */
41 class CRM_Contact_Form_Task_AddToGroup extends CRM_Contact_Form_Task {
42
43 /**
44 * The context that we are working on
45 *
46 * @var string
47 */
48 protected $_context;
49
50 /**
51 * The groupId retrieved from the GET vars
52 *
53 * @var int
54 */
55 protected $_id;
56
57 /**
58 * The title of the group
59 *
60 * @var string
61 */
62 protected $_title;
63
64 /**
65 * Build all the data structures needed to build the form
66 *
67 * @return void
68 */
69 public function preProcess() {
70 /*
71 * initialize the task and row fields
72 */
73 parent::preProcess();
74
75 $this->_context = $this->get('context');
76 $this->_id = $this->get('amtgID');
77 }
78
79 /**
80 * Build the form object
81 *
82 *
83 * @return void
84 */
85 public function buildQuickForm() {
86
87 //create radio buttons to select existing group or add a new group
88 $options = array(ts('Add Contact To Existing Group'), ts('Create New Group'));
89
90 if (!$this->_id) {
91 $this->addRadio('group_option', ts('Group Options'), $options, array('onclick' => "return showElements();"));
92
93 $this->add('text', 'title', ts('Group Name:') . ' ',
94 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title')
95 );
96 $this->addRule('title', ts('Name already exists in Database.'),
97 'objectExists', array('CRM_Contact_DAO_Group', $this->_id, 'title')
98 );
99
100 $this->add('textarea', 'description', ts('Description:') . ' ',
101 CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')
102 );
103
104 $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
105 if (!CRM_Core_Permission::access('CiviMail')) {
106 $isWorkFlowEnabled = CRM_Mailing_Info::workflowEnabled();
107 if ($isWorkFlowEnabled &&
108 !CRM_Core_Permission::check('create mailings') &&
109 !CRM_Core_Permission::check('schedule mailings') &&
110 !CRM_Core_Permission::check('approve mailings')
111 ) {
112 unset($groupTypes['Mailing List']);
113 }
114 }
115
116 if (!empty($groupTypes)) {
117 $this->addCheckBox('group_type',
118 ts('Group Type'),
119 $groupTypes,
120 NULL, NULL, NULL, NULL, '&nbsp;&nbsp;&nbsp;'
121 );
122 }
123 }
124
125 // add select for groups
126 $group = array('' => ts('- select group -')) + CRM_Core_PseudoConstant::nestedGroup();
127
128 $groupElement = $this->add('select', 'group_id', ts('Select Group'), $group, FALSE, array('class' => 'crm-select2 huge'));
129
130 $this->_title = $group[$this->_id];
131
132 if ($this->_context === 'amtg') {
133 $groupElement->freeze();
134
135 // also set the group title
136 $groupValues = array('id' => $this->_id, 'title' => $this->_title);
137 $this->assign_by_ref('group', $groupValues);
138 }
139
140 // Set dynamic page title for 'Add Members Group (confirm)'
141 if ($this->_id) {
142 CRM_Utils_System::setTitle(ts('Add Contacts: %1', array(1 => $this->_title)));
143 }
144 else {
145 CRM_Utils_System::setTitle(ts('Add Contacts to A Group'));
146 }
147
148 $this->addDefaultButtons(ts('Add to Group'));
149 }
150
151 /**
152 * Set the default form values
153 *
154 *
155 * @return array
156 * the default array reference
157 */
158 public function setDefaultValues() {
159 $defaults = array();
160
161 if ($this->_context === 'amtg') {
162 $defaults['group_id'] = $this->_id;
163 }
164
165 $defaults['group_option'] = 0;
166 return $defaults;
167 }
168
169 /**
170 * Add local and global form rules
171 *
172 *
173 * @return void
174 */
175 public function addRules() {
176 $this->addFormRule(array('CRM_Contact_Form_task_AddToGroup', 'formRule'));
177 }
178
179 /**
180 * Global validation rules for the form
181 *
182 * @param array $params
183 *
184 * @return array
185 * list of errors to be posted back to the form
186 * @static
187 */
188 public static function formRule($params) {
189 $errors = array();
190
191 if (!empty($params['group_option']) && empty($params['title'])) {
192 $errors['title'] = "Group Name is a required field";
193 }
194 elseif (empty($params['group_option']) && empty($params['group_id'])) {
195 $errors['group_id'] = "Select Group is a required field.";
196 }
197
198 return empty($errors) ? TRUE : $errors;
199 }
200
201 /**
202 * Process the form after the input has been submitted and validated
203 *
204 *
205 * @return void
206 */
207 public function postProcess() {
208 $params = $this->controller->exportValues();
209 $groupOption = CRM_Utils_Array::value('group_option', $params, NULL);
210 if ($groupOption) {
211 $groupParams = array();
212 $groupParams['title'] = $params['title'];
213 $groupParams['description'] = $params['description'];
214 $groupParams['visibility'] = "User and User Admin Only";
215 if (array_key_exists('group_type', $params) && is_array($params['group_type'])) {
216 $groupParams['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR,
217 array_keys($params['group_type'])
218 ) . CRM_Core_DAO::VALUE_SEPARATOR;
219 }
220 else {
221 $groupParams['group_type'] = '';
222 }
223 $groupParams['is_active'] = 1;
224
225 $createdGroup = CRM_Contact_BAO_Group::create($groupParams);
226 $groupID = $createdGroup->id;
227 $groupName = $groupParams['title'];
228 }
229 else {
230 $groupID = $params['group_id'];
231 $group = CRM_Core_PseudoConstant::group();
232 $groupName = $group[$groupID];
233 }
234
235 list($total, $added, $notAdded) = CRM_Contact_BAO_GroupContact::addContactsToGroup($this->_contactIds, $groupID);
236
237 $status = array(ts('%count contact added to group', array('count' => $added, 'plural' => '%count contacts added to group')));
238 if ($notAdded) {
239 $status[] = ts('%count contact was already in group', array('count' => $notAdded, 'plural' => '%count contacts were already in group'));
240 }
241 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
242 CRM_Core_Session::setStatus($status, ts('Added Contact to %1', array(1 => $groupName, 'count' => $added, 'plural' => 'Added Contacts to %1')), 'success', array('expires' => 0));
243
244 if ($this->_context === 'amtg') {
245 CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url('civicrm/group/search', "reset=1&force=1&context=smog&gid=$groupID"));
246 }
247 }
248 }