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 is for UF Group | |
38 | */ | |
39 | class CRM_UF_Form_Group extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * the form id saved to the session for an update | |
43 | * | |
44 | * @var int | |
45 | * @access protected | |
46 | */ | |
47 | protected $_id; | |
48 | ||
49 | /** | |
50 | * the title for group | |
51 | * | |
52 | * @var int | |
53 | * @access protected | |
54 | */ | |
55 | protected $_title; | |
56 | protected $_groupElement; | |
57 | protected $_group; | |
58 | protected $_allPanes; | |
59 | ||
60 | /** | |
61 | * Function to set variables up before form is built | |
62 | * | |
63 | * @return void | |
64 | * @access public | |
65 | */ | |
66 | public function preProcess() { | |
67 | // current form id | |
68 | $this->_id = $this->get('id'); | |
69 | if (!$this->_id) { | |
70 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this, FALSE, 0); | |
71 | } | |
72 | $this->assign('gid', $this->_id); | |
73 | $this->_group = CRM_Core_PseudoConstant::group(); | |
74 | ||
7df37017 RN |
75 | if ($this->_action & (CRM_Core_Action::UPDATE | CRM_Core_Action::DELETE)) { |
76 | $title = CRM_Core_BAO_UFGroup::getTitle($this->_id); | |
77 | $this->assign('profileTitle', $title); | |
78 | } | |
79 | ||
6a488035 TO |
80 | // setting title for html page |
81 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
6a488035 TO |
82 | CRM_Utils_System::setTitle(ts('Profile Settings') . " - $title"); |
83 | } | |
84 | elseif ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) { | |
85 | $ufGroup['module'] = implode(' , ', CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE)); | |
86 | $status = 0; | |
87 | $status = CRM_Core_BAO_UFGroup::usedByModule($this->_id); | |
88 | if ($this->_action & (CRM_Core_Action::DISABLE)) { | |
89 | if ($status) { | |
90 | $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you disable the profile - it will be removed from these forms and/or modules. Do you want to continue?'; | |
91 | } | |
92 | else { | |
93 | $message = 'Are you sure you want to disable this Profile?'; | |
94 | } | |
95 | } | |
96 | else { | |
97 | if ($status) { | |
98 | $message = 'This profile is currently used for ' . $ufGroup['module'] . '. If you delete the profile - it will be removed from these forms and/or modules. This action cannot be undone. Do you want to continue?'; | |
99 | } | |
100 | else { | |
101 | $message = 'Are you sure you want to delete this Profile? This action cannot be undone.'; | |
102 | } | |
103 | } | |
104 | $this->assign('message', $message); | |
105 | } | |
106 | else { | |
107 | CRM_Utils_System::setTitle(ts('New CiviCRM Profile')); | |
108 | } | |
109 | } | |
110 | ||
111 | /** | |
112 | * Function to actually build the form | |
113 | * | |
114 | * @return void | |
115 | * @access public | |
116 | */ | |
117 | public function buildQuickForm() { | |
118 | if ($this->_action & (CRM_Core_Action::DISABLE | CRM_Core_Action::DELETE)) { | |
119 | if ($this->_action & (CRM_Core_Action::DISABLE)) { | |
120 | $display = 'Disable Profile'; | |
121 | } | |
122 | else { | |
123 | $display = 'Delete Profile'; | |
124 | } | |
125 | $this->addButtons(array( | |
126 | array( | |
127 | 'type' => 'next', | |
128 | 'name' => $display, | |
129 | 'spacing' => ' ', | |
130 | 'isDefault' => TRUE, | |
131 | ), | |
132 | array( | |
133 | 'type' => 'cancel', | |
134 | 'name' => ts('Cancel'), | |
135 | ), | |
136 | ) | |
137 | ); | |
138 | return; | |
139 | } | |
140 | $this->applyFilter('__ALL__', 'trim'); | |
141 | ||
142 | // title | |
143 | $this->add('text', 'title', ts('Profile Name'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'title'), TRUE); | |
bcbde5ca | 144 | $this->add('textarea', 'description', ts('Description'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'description')); |
6a488035 TO |
145 | |
146 | ||
147 | //add checkboxes | |
148 | $uf_group_type = array(); | |
149 | $UFGroupType = CRM_Core_SelectValues::ufGroupTypes(); | |
150 | foreach ($UFGroupType as $key => $value) { | |
151 | $uf_group_type[] = $this->createElement('checkbox', $key, NULL, $value); | |
152 | } | |
153 | $this->addGroup($uf_group_type, 'uf_group_type', ts('Used For'), ' '); | |
154 | ||
155 | // help text | |
156 | $this->addWysiwyg('help_pre', ts('Pre-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post')); | |
157 | $this->addWysiwyg('help_post', ts('Post-form Help'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFGroup', 'help_post')); | |
158 | ||
159 | // weight | |
160 | $this->add('text', 'weight', ts('Order'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFJoin', 'weight'), TRUE); | |
161 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); | |
162 | ||
163 | // is this group active ? | |
164 | $this->addElement('checkbox', 'is_active', ts('Is this CiviCRM Profile active?')); | |
165 | ||
166 | $paneNames = array('Advanced Settings' => 'buildAdvanceSetting'); | |
167 | ||
168 | foreach ($paneNames as $name => $type) { | |
169 | if ($this->_id) { | |
170 | $dataURL = "&reset=1&action=update&id={$this->_id}&snippet=4&formType={$type}"; | |
171 | } | |
172 | else { | |
173 | $dataURL = "&reset=1&action=add&snippet=4&formType={$type}"; | |
174 | } | |
175 | ||
176 | $allPanes[$name] = array( | |
177 | 'url' => CRM_Utils_System::url('civicrm/admin/uf/group/setting', | |
178 | $dataURL | |
179 | ), | |
180 | 'open' => 'false', | |
181 | 'id' => $type, | |
182 | ); | |
183 | ||
0e6e8724 | 184 | CRM_UF_Form_AdvanceSetting::$type($this); |
6a488035 TO |
185 | } |
186 | ||
187 | $this->addButtons(array( | |
188 | array( | |
189 | 'type' => 'next', | |
190 | 'name' => ts('Save'), | |
191 | 'spacing' => ' ', | |
192 | 'isDefault' => TRUE, | |
193 | ), | |
194 | array( | |
195 | 'type' => 'cancel', | |
196 | 'name' => ts('Cancel'), | |
197 | ), | |
198 | ) | |
199 | ); | |
200 | ||
201 | // views are implemented as frozen form | |
202 | if ($this->_action & CRM_Core_Action::VIEW) { | |
203 | $this->freeze(); | |
204 | $this->addElement('button', 'done', ts('Done'), array('onclick' => "location.href='civicrm/admin/uf/group?reset=1&action=browse'")); | |
205 | } | |
206 | ||
207 | $this->addFormRule(array('CRM_UF_Form_Group', 'formRule'), $this); | |
208 | } | |
209 | ||
210 | /** | |
211 | * This function sets the default values for the form. Note that in edit/view mode | |
212 | * the default values are retrieved from the database | |
213 | * | |
214 | * @access public | |
215 | * | |
216 | * @return void | |
217 | */ | |
218 | function setDefaultValues() { | |
219 | $defaults = array(); | |
220 | $showHide = new CRM_Core_ShowHideBlocks(); | |
221 | ||
222 | if ($this->_action == CRM_Core_Action::ADD) { | |
223 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFJoin'); | |
224 | } | |
225 | ||
226 | //id fetched for Dojo Pane | |
227 | $pId = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
228 | if (isset($pId)) { | |
229 | $this->_id = $pId; | |
230 | } | |
231 | ||
232 | if ((isset($this->_id))) { | |
233 | ||
234 | $defaults['weight'] = CRM_Core_BAO_UFGroup::getWeight($this->_id); | |
235 | ||
236 | $params = array('id' => $this->_id); | |
237 | CRM_Core_BAO_UFGroup::retrieve($params, $defaults); | |
238 | $defaults['group'] = CRM_Utils_Array::value('limit_listings_group_id', $defaults); | |
239 | $defaults['add_contact_to_group'] = CRM_Utils_Array::value('add_to_group_id', $defaults); | |
240 | //get the uf join records for current uf group | |
241 | $ufJoinRecords = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id); | |
242 | foreach ($ufJoinRecords as $key => $value) { | |
243 | $checked[$value] = 1; | |
244 | } | |
245 | $defaults['uf_group_type'] = isset($checked) ? $checked : ""; | |
246 | ||
247 | //get the uf join records for current uf group other than default modules | |
248 | $otherModules = array(); | |
249 | $otherModules = CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_id, TRUE, TRUE); | |
250 | if (!empty($otherModules)) { | |
251 | $otherModuleString = NULL; | |
252 | foreach ($otherModules as $key) { | |
253 | $otherModuleString .= " [ x ] <label>" . $key . "</label>"; | |
254 | } | |
255 | $this->assign('otherModuleString', $otherModuleString); | |
256 | } | |
257 | ||
258 | $showAdvanced = 0; | |
259 | $advFields = array( | |
260 | 'group', 'post_URL', 'cancel_URL', | |
261 | 'add_captcha', 'is_map', 'is_uf_link', 'is_edit_link', | |
262 | 'is_update_dupe', 'is_cms_user', 'is_proximity_search', | |
263 | ); | |
264 | foreach ($advFields as $key) { | |
265 | if (!empty($defaults[$key])) { | |
266 | $showAdvanced = 1; | |
267 | $this->_allPanes['Advanced Settings']['open'] = 'true'; | |
268 | break; | |
269 | } | |
270 | } | |
271 | } | |
272 | else { | |
273 | $defaults['is_active'] = 1; | |
274 | $defaults['is_map'] = 0; | |
275 | $defaults['is_update_dupe'] = 0; | |
276 | $defaults['is_proximity_search'] = 0; | |
6a488035 TO |
277 | } |
278 | // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist) | |
279 | if (!($this->_action & CRM_Core_Action::DELETE) && !($this->_action & CRM_Core_Action::DISABLE)) { | |
280 | $showHide->addToTemplate(); | |
281 | } | |
282 | $this->assign('allPanes', $this->_allPanes); | |
283 | return $defaults; | |
284 | } | |
285 | ||
286 | /** | |
287 | * global form rule | |
288 | * | |
289 | * @param array $fields the input form values | |
290 | * @param array $files the uploaded files if any | |
291 | * @param array $self current form object. | |
292 | * | |
293 | * @return true if no errors, else array of errors | |
294 | * @access public | |
295 | * @static | |
296 | */ | |
297 | static function formRule($fields, $files, $self) { | |
298 | $errors = array(); | |
299 | ||
300 | //validate profile title as well as name. | |
301 | $title = $fields['title']; | |
302 | $name = CRM_Utils_String::munge($title, '_', 56); | |
303 | $name .= $self->_id ? '_' . $self->_id : ''; | |
304 | $query = 'select count(*) from civicrm_uf_group where ( name like %1 ) and id != %2'; | |
305 | $pCnt = CRM_Core_DAO::singleValueQuery($query, array(1 => array($name, 'String'), | |
306 | 2 => array((int)$self->_id, 'Integer'), | |
307 | )); | |
308 | if ($pCnt) { | |
309 | $errors['title'] = ts('Profile \'%1\' already exists in Database.', array(1 => $title)); | |
310 | } | |
311 | ||
312 | return empty($errors) ? TRUE : $errors; | |
313 | } | |
314 | ||
315 | /** | |
316 | * Process the form | |
317 | * | |
318 | * @return void | |
319 | * @access public | |
320 | */ | |
321 | public function postProcess() { | |
322 | if ($this->_action & CRM_Core_Action::DELETE) { | |
323 | $title = CRM_Core_BAO_UFGroup::getTitle($this->_id); | |
324 | CRM_Core_BAO_UFGroup::del($this->_id); | |
325 | CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been deleted.", array(1 => $title)), ts('Profile Deleted'), 'success'); | |
326 | } | |
327 | elseif ($this->_action & CRM_Core_Action::DISABLE) { | |
328 | $ufJoinParams = array('uf_group_id' => $this->_id); | |
329 | CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams); | |
330 | ||
331 | CRM_Core_BAO_UFGroup::setIsActive($this->_id, 0); | |
332 | } | |
333 | else { | |
334 | // get the submitted form values. | |
335 | $params = $ids = array(); | |
336 | $params = $this->controller->exportValues($this->_name); | |
337 | ||
338 | if (!array_key_exists('is_active', $params)) { | |
339 | $params['is_active'] = 0; | |
340 | } | |
341 | ||
342 | if ($this->_action & (CRM_Core_Action::UPDATE)) { | |
343 | $ids['ufgroup'] = $this->_id; | |
344 | // CRM-5284 | |
345 | // lets skip trying to mess around with profile weights and allow the user to do as needed. | |
346 | } | |
347 | elseif ($this->_action & CRM_Core_Action::ADD) { | |
348 | $session = CRM_Core_Session::singleton(); | |
349 | $params['created_id'] = $session->get('userID'); | |
350 | $params['created_date'] = date('YmdHis'); | |
351 | } | |
352 | ||
353 | // create uf group | |
354 | $ufGroup = CRM_Core_BAO_UFGroup::add($params, $ids); | |
355 | ||
a7488080 | 356 | if (!empty($params['is_active'])) { |
6a488035 TO |
357 | //make entry in uf join table |
358 | CRM_Core_BAO_UFGroup::createUFJoin($params, $ufGroup->id); | |
359 | } | |
360 | elseif ($this->_id) { | |
361 | // this profile has been set to inactive, delete all corresponding UF Join's | |
362 | $ufJoinParams = array('uf_group_id' => $this->_id); | |
363 | CRM_Core_BAO_UFGroup::delUFJoin($ufJoinParams); | |
364 | } | |
365 | ||
366 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
367 | $url = CRM_Utils_System::url('civicrm/admin/uf/group', 'reset=1&action=browse'); | |
368 | CRM_Core_Session::setStatus(ts("Your CiviCRM Profile '%1' has been saved.", array(1 => $ufGroup->title)), ts('Profile Saved'), 'success'); | |
369 | } | |
370 | else { | |
704f21c0 CW |
371 | // Jump directly to adding a field if popups are disabled |
372 | $action = CRM_Core_Resources::singleton()->ajaxPopupsEnabled ? '' : '/add'; | |
6f231148 | 373 | $url = CRM_Utils_System::url("civicrm/admin/uf/group/field$action", 'reset=1&new=1&gid=' . $ufGroup->id . '&action=' . ($action ? 'add' : 'browse')); |
6a488035 TO |
374 | CRM_Core_Session::setStatus(ts('Your CiviCRM Profile \'%1\' has been added. You can add fields to this profile now.', |
375 | array(1 => $ufGroup->title) | |
376 | ), ts('Profile Added'), 'success'); | |
377 | } | |
378 | $session = CRM_Core_Session::singleton(); | |
379 | $session->replaceUserContext($url); | |
380 | } | |
381 | ||
382 | // update cms integration with registration / my account | |
383 | CRM_Utils_System::updateCategories(); | |
384 | } | |
385 | } | |
386 |