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