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