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