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