Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
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 | * form to process actions on the field aspect of Custom | |
38 | */ | |
39 | class CRM_UF_Form_Field extends CRM_Core_Form { | |
40 | ||
41 | /** | |
42 | * the uf group id saved to the session for an update | |
43 | * | |
44 | * @var int | |
45 | * @access protected | |
46 | */ | |
47 | protected $_gid; | |
48 | ||
49 | /** | |
50 | * The field id, used when editing the field | |
51 | * | |
52 | * @var int | |
53 | * @access protected | |
54 | */ | |
55 | protected $_id; | |
56 | ||
57 | /** | |
58 | * The set of fields that we can view/edit in the user field framework | |
59 | * | |
60 | * @var array | |
61 | * @access protected | |
62 | */ | |
63 | protected $_fields; | |
64 | ||
65 | /** | |
66 | * the title for field | |
67 | * | |
68 | * @var int | |
69 | * @access protected | |
70 | */ | |
71 | protected $_title; | |
72 | ||
73 | /** | |
74 | * The set of fields sent to the select element | |
75 | * | |
76 | * @var array | |
77 | * @access protected | |
78 | */ | |
79 | protected $_selectFields; | |
80 | ||
81 | /** | |
82 | * to store fields with if locationtype exits status | |
83 | * | |
84 | * @var array | |
85 | * @access protected | |
86 | */ | |
87 | protected $_hasLocationTypes; | |
88 | ||
89 | /** | |
90 | * is this profile has searchable field | |
91 | * or is any field having in selector true. | |
92 | * | |
93 | * @var boolean. | |
94 | * @access protected | |
95 | */ | |
96 | protected $_hasSearchableORInSelector; | |
97 | ||
98 | /** | |
99 | * Function to set variables up before form is built | |
100 | * | |
101 | * @return void | |
102 | * @access public | |
103 | */ | |
104 | public function preProcess() { | |
105 | $this->_gid = CRM_Utils_Request::retrieve('gid', 'Positive', $this); | |
106 | $this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this); | |
107 | if ($this->_gid) { | |
108 | $this->_title = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid, 'title'); | |
109 | CRM_Utils_System::setTitle($this->_title . ' - ' . ts('CiviCRM Profile Fields')); | |
110 | ||
111 | $url = CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
112 | "reset=1&action=browse&gid={$this->_gid}" | |
113 | ); | |
114 | ||
115 | $session = CRM_Core_Session::singleton(); | |
116 | $session->pushUserContext($url); | |
117 | $breadCrumb = array(array('title' => ts('CiviCRM Profile Fields'), | |
118 | 'url' => $url, | |
119 | )); | |
120 | CRM_Utils_System::appendBreadCrumb($breadCrumb); | |
121 | } | |
122 | ||
123 | $showBestResult = CRM_Utils_Request::retrieve('sbr', 'Positive', CRM_Core_DAO::$_nullArray); | |
124 | if ($showBestResult) { | |
125 | $this->assign('showBestResult', $showBestResult); | |
126 | } | |
127 | ||
128 | $this->_fields = CRM_Contact_BAO_Contact::importableFields('All', TRUE, TRUE, TRUE, TRUE, TRUE); | |
129 | $this->_fields = array_merge(CRM_Activity_BAO_Activity::exportableFields('Activity'), $this->_fields); | |
130 | ||
131 | //unset campaign related fields. | |
132 | if (isset($this->_fields['activity_campaign_id'])) { | |
133 | $this->_fields['activity_campaign_id']['title'] = ts('Campaign'); | |
134 | if (isset($this->_fields['activity_campaign'])) { | |
135 | unset($this->_fields['activity_campaign']); | |
136 | } | |
137 | } | |
138 | ||
139 | if (CRM_Core_Permission::access('CiviContribute')) { | |
140 | $this->_fields = array_merge(CRM_Contribute_BAO_Contribution::getContributionFields(FALSE), $this->_fields); | |
141 | $this->_fields = array_merge(CRM_Core_BAO_UFField::getContribBatchEntryFields(), $this->_fields); | |
142 | } | |
143 | ||
144 | if (CRM_Core_Permission::access('CiviMember')) { | |
145 | $this->_fields = array_merge(CRM_Member_BAO_Membership::getMembershipFields(), $this->_fields); | |
146 | } | |
147 | ||
148 | if (CRM_Core_Permission::access('CiviEvent')) { | |
149 | $this->_fields = array_merge(CRM_Event_BAO_Query::getParticipantFields(), $this->_fields); | |
150 | } | |
151 | ||
61750d67 DS |
152 | $this->_fields = array_merge($this->_fields, CRM_Contact_BAO_Query_Hook::singleton()->getFields()); |
153 | ||
6a488035 TO |
154 | $this->_selectFields = array(); |
155 | foreach ($this->_fields as $name => $field) { | |
156 | // lets skip note for now since we dont support it | |
157 | if ($name == 'note') { | |
158 | continue; | |
159 | } | |
160 | $this->_selectFields[$name] = $field['title']; | |
161 | $this->_hasLocationTypes[$name] = CRM_Utils_Array::value('hasLocationType', $field); | |
162 | } | |
163 | ||
164 | // lets add group, tag and current_employer to this list | |
165 | $this->_selectFields['group'] = ts('Group(s)'); | |
166 | $this->_selectFields['tag'] = ts('Tag(s)'); | |
167 | $this->_selectFields['current_employer'] = ts('Current Employer'); | |
168 | $this->_selectFields['phone_and_ext'] = ts('Phone and Extension'); | |
169 | ||
170 | //CRM-4363 check for in selector or searchable fields. | |
171 | $this->_hasSearchableORInSelector = CRM_Core_BAO_UFField::checkSearchableORInSelector($this->_gid); | |
172 | ||
173 | $this->assign('fieldId', $this->_id); | |
174 | if ($this->_id) { | |
175 | $fieldTitle = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFField', $this->_id, 'label'); | |
176 | $this->assign('fieldTitle', $fieldTitle); | |
177 | } | |
178 | } | |
179 | ||
180 | /** | |
181 | * Function to actually build the form | |
182 | * | |
183 | * @return void | |
184 | * @access public | |
185 | */ | |
186 | public function buildQuickForm() { | |
187 | if ($this->_action & CRM_Core_Action::DELETE) { | |
188 | $this->addButtons(array( | |
189 | array( | |
190 | 'type' => 'next', | |
191 | 'name' => ts('Delete Profile Field'), | |
192 | 'spacing' => ' ', | |
193 | 'isDefault' => TRUE, | |
194 | ), | |
195 | array( | |
196 | 'type' => 'cancel', | |
197 | 'name' => ts('Cancel'), | |
198 | ), | |
199 | ) | |
200 | ); | |
201 | return; | |
202 | } | |
203 | ||
204 | if (isset($this->_id)) { | |
205 | $params = array('id' => $this->_id); | |
206 | CRM_Core_BAO_UFField::retrieve($params, $defaults); | |
207 | ||
208 | // set it to null if so (avoids crappy E_NOTICE errors below | |
209 | $defaults['location_type_id'] = CRM_Utils_Array::value('location_type_id', $defaults); | |
210 | ||
211 | $specialFields = CRM_Core_BAO_UFGroup::getLocationFields(); | |
212 | ||
213 | if (!$defaults['location_type_id'] && | |
214 | $defaults["field_type"] != "Formatting" && | |
215 | in_array($defaults['field_name'], $specialFields) | |
216 | ) { | |
217 | $defaults['location_type_id'] = 0; | |
218 | } | |
219 | ||
220 | $defaults['field_name'] = array( | |
221 | $defaults['field_type'], | |
222 | ($defaults['field_type'] == "Formatting" ? "" : $defaults['field_name']), | |
223 | $defaults['location_type_id'], | |
224 | CRM_Utils_Array::value('phone_type_id', $defaults), | |
225 | ); | |
226 | $this->_gid = $defaults['uf_group_id']; | |
227 | } | |
228 | else { | |
229 | $defaults['is_active'] = 1; | |
230 | } | |
231 | ||
232 | $otherModules = array_values(CRM_Core_BAO_UFGroup::getUFJoinRecord($this->_gid)); | |
233 | $this->assign('otherModules', $otherModules); | |
234 | ||
235 | if ($this->_action & CRM_Core_Action::ADD) { | |
236 | $fieldValues = array('uf_group_id' => $this->_gid); | |
237 | $defaults['weight'] = CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_UFField', $fieldValues); | |
238 | } | |
239 | ||
240 | // lets trim all the whitespace | |
241 | $this->applyFilter('__ALL__', 'trim'); | |
242 | ||
243 | //hidden field to catch the group id in profile | |
244 | $this->add('hidden', 'group_id', $this->_gid); | |
245 | ||
246 | //hidden field to catch the field id in profile | |
247 | $this->add('hidden', 'field_id', $this->_id); | |
248 | ||
249 | $fields = CRM_Core_BAO_UFField::getAvailableFields($this->_gid, $defaults); | |
250 | ||
251 | $noSearchable = array(); | |
252 | $addressCustomFields = array_keys(CRM_Core_BAO_CustomField::getFieldsForImport('Address')); | |
253 | ||
254 | foreach ($fields as $key => $value) { | |
255 | foreach ($value as $key1 => $value1) { | |
256 | //CRM-2676, replacing the conflict for same custom field name from different custom group. | |
257 | if ($customFieldId = CRM_Core_BAO_CustomField::getKeyID($key1)) { | |
258 | $customGroupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id'); | |
259 | $customGroupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title'); | |
260 | $this->_mapperFields[$key][$key1] = $value1['title'] . ' :: ' . $customGroupName; | |
261 | if (in_array($key1, $addressCustomFields)) { | |
262 | $noSearchable[] = $value1['title'] . ' :: ' . $customGroupName; | |
263 | } | |
264 | } | |
265 | else { | |
266 | $this->_mapperFields[$key][$key1] = $value1['title']; | |
267 | } | |
268 | $hasLocationTypes[$key][$key1] = CRM_Utils_Array::value('hasLocationType', $value1); | |
269 | ||
270 | // hide the 'is searchable' field for 'File' custom data | |
271 | if (isset($value1['data_type']) && | |
272 | isset($value1['html_type']) && | |
273 | (($value1['data_type'] == 'File' && $value1['html_type'] == 'File') | |
274 | || ($value1['data_type'] == 'Link' && $value1['html_type'] == 'Link') | |
275 | ) | |
276 | ) { | |
277 | if (!in_array($value1['title'], $noSearchable)) { | |
278 | $noSearchable[] = $value1['title']; | |
279 | } | |
280 | } | |
281 | } | |
282 | } | |
283 | $this->assign('noSearchable', $noSearchable); | |
284 | ||
b2b0530a | 285 | $this->_location_types = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); |
6a488035 TO |
286 | $defaultLocationType = CRM_Core_BAO_LocationType::getDefault(); |
287 | ||
288 | /** | |
289 | * FIXME: dirty hack to make the default option show up first. This | |
290 | * avoids a mozilla browser bug with defaults on dynamically constructed | |
291 | * selector widgets. | |
292 | */ | |
293 | if ($defaultLocationType) { | |
294 | $defaultLocation = $this->_location_types[$defaultLocationType->id]; | |
295 | unset($this->_location_types[$defaultLocationType->id]); | |
296 | $this->_location_types = array( | |
297 | $defaultLocationType->id => $defaultLocation) + $this->_location_types; | |
298 | } | |
299 | ||
300 | $this->_location_types = array('Primary') + $this->_location_types; | |
301 | ||
302 | // since we need a hierarchical list to display contact types & subtypes, | |
303 | // this is what we going to display in first selector | |
304 | $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, FALSE); | |
305 | unset($contactTypes['']); | |
306 | ||
307 | $contactTypes = !empty($contactTypes) ? array('Contact' => 'Contacts') + $contactTypes : array(); | |
308 | $sel1 = array('' => '- select -') + $contactTypes; | |
309 | ||
310 | if (!empty($fields['Activity'])) { | |
311 | $sel1['Activity'] = 'Activity'; | |
312 | } | |
313 | ||
314 | if (CRM_Core_Permission::access('CiviEvent')) { | |
315 | $sel1['Participant'] = 'Participants'; | |
316 | } | |
317 | ||
318 | if (!empty($fields['Contribution'])) { | |
319 | $sel1['Contribution'] = 'Contributions'; | |
320 | } | |
321 | ||
322 | if (!empty($fields['Membership'])) { | |
323 | $sel1['Membership'] = 'Membership'; | |
324 | } | |
325 | ||
326 | if (!empty($fields['Formatting'])) { | |
327 | $sel1['Formatting'] = 'Formatting'; | |
328 | } | |
329 | ||
330 | foreach ($sel1 as $key => $sel) { | |
331 | if ($key) { | |
332 | $sel2[$key] = $this->_mapperFields[$key]; | |
333 | } | |
334 | } | |
335 | $sel3[''] = NULL; | |
b4f964d9 | 336 | $phoneTypes = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); |
6a488035 TO |
337 | ksort($phoneTypes); |
338 | ||
339 | foreach ($sel1 as $k => $sel) { | |
340 | if ($k) { | |
341 | foreach ($this->_location_types as $key => $value) { | |
342 | $sel4[$k]['phone'][$key] = &$phoneTypes; | |
343 | $sel4[$k]['phone_and_ext'][$key] = &$phoneTypes; | |
344 | } | |
345 | } | |
346 | } | |
347 | ||
348 | foreach ($sel1 as $k => $sel) { | |
349 | if ($k) { | |
350 | if (is_array($this->_mapperFields[$k])) { | |
351 | foreach ($this->_mapperFields[$k] as $key => $value) { | |
352 | if ($hasLocationTypes[$k][$key]) { | |
353 | $sel3[$k][$key] = $this->_location_types; | |
354 | } | |
355 | else { | |
356 | $sel3[$key] = NULL; | |
357 | } | |
358 | } | |
359 | } | |
360 | } | |
361 | } | |
362 | ||
363 | $this->_defaults = array(); | |
364 | $js = "<script type='text/javascript'>\n"; | |
365 | $formName = "document.{$this->_name}"; | |
366 | ||
367 | $alreadyMixProfile = FALSE; | |
368 | if (CRM_Core_BAO_UFField::checkProfileType($this->_gid)) { | |
369 | $alreadyMixProfile = TRUE; | |
370 | } | |
371 | $this->assign('alreadyMixProfile', $alreadyMixProfile); | |
372 | ||
373 | $sel = &$this->addElement('hierselect', 'field_name', ts('Field Name')); | |
374 | ||
375 | $formValues = $this->exportValues(); | |
376 | ||
377 | if (empty($formValues)) { | |
378 | for ($k = 1; $k < 4; $k++) { | |
379 | if (!$defaults['field_name'][$k]) { | |
380 | $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n"; | |
381 | } | |
382 | } | |
383 | } | |
384 | else { | |
385 | if (!empty($formValues['field_name'])) { | |
386 | foreach ($formValues['field_name'] as $value) { | |
387 | for ($k = 1; $k < 4; $k++) { | |
388 | if (!isset($formValues['field_name'][$k]) || !$formValues['field_name'][$k]) { | |
389 | $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n"; | |
390 | } | |
391 | else { | |
392 | $js .= "{$formName}['field_name[$k]'].style.display = '';\n"; | |
393 | } | |
394 | } | |
395 | } | |
396 | } | |
397 | else { | |
398 | for ($k = 1; $k < 4; $k++) { | |
399 | if (!isset($defaults['field_name'][$k])) { | |
400 | $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n"; | |
401 | } | |
402 | } | |
403 | } | |
404 | } | |
405 | ||
406 | foreach ($sel2 as $k => $v) { | |
407 | if (is_array($sel2[$k])) { | |
408 | asort($sel2[$k]); | |
409 | } | |
410 | } | |
411 | ||
0c145cc0 | 412 | // CRM_Core_Error::debug(array($sel1, $sel2, $sel3, $sel4)); |
6a488035 TO |
413 | $sel->setOptions(array($sel1, $sel2, $sel3, $sel4)); |
414 | ||
415 | // proper interpretation of spec in CRM-8732 | |
416 | if (!isset($this->_id) && in_array('Search Profile', $otherModules)) { | |
417 | $defaults['visibility'] = 'Public Pages and Listings'; | |
418 | } | |
419 | ||
420 | $js .= "</script>\n"; | |
421 | $this->assign('initHideBoxes', $js); | |
422 | ||
423 | $this->add('select', | |
424 | 'visibility', | |
425 | ts('Visibility'), | |
426 | CRM_Core_SelectValues::ufVisibility(), | |
427 | TRUE, | |
428 | array('onChange' => "showHideSeletorSearch(this.value);") | |
429 | ); | |
430 | ||
431 | //CRM-4363 | |
432 | $js = array('onChange' => "mixProfile();"); | |
433 | // should the field appear in selectors (as a column)? | |
434 | $this->add('checkbox', 'in_selector', ts('Results Column?'), NULL, NULL, $js); | |
435 | $this->add('checkbox', 'is_searchable', ts('Searchable?'), NULL, NULL, $js); | |
436 | ||
437 | $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_UFField'); | |
438 | ||
439 | // weight | |
440 | $this->add('text', 'weight', ts('Order'), $attributes['weight'], TRUE); | |
441 | $this->addRule('weight', ts('is a numeric field'), 'numeric'); | |
442 | ||
443 | $this->add('textarea', 'help_pre', ts('Field Pre Help'), $attributes['help_pre']); | |
444 | $this->add('textarea', 'help_post', ts('Field Post Help'), $attributes['help_post']); | |
445 | ||
446 | $this->add('checkbox', 'is_required', ts('Required?')); | |
447 | ||
448 | $this->add('checkbox', 'is_multi_summary', ts('Include in multi-record listing?')); | |
449 | $this->add('checkbox', 'is_active', ts('Active?')); | |
450 | $this->add('checkbox', 'is_view', ts('View Only?')); | |
451 | ||
452 | // $this->add( 'checkbox', 'is_registration', ts( 'Display in Registration Form?' ) ); | |
453 | //$this->add( 'checkbox', 'is_match' , ts( 'Key to Match Contacts?' ) ); | |
454 | ||
455 | $this->add('text', 'label', ts('Field Label'), $attributes['label']); | |
456 | ||
457 | $js = NULL; | |
458 | if ($this->_hasSearchableORInSelector) { | |
459 | $js = array('onclick' => "return verify( );"); | |
460 | } | |
461 | ||
462 | // add buttons | |
463 | $this->addButtons(array( | |
464 | array( | |
465 | 'type' => 'next', | |
466 | 'name' => ts('Save'), | |
467 | 'isDefault' => TRUE, | |
468 | 'js' => $js, | |
469 | ), | |
470 | array( | |
471 | 'type' => 'next', | |
472 | 'name' => ts('Save and New'), | |
473 | 'subName' => 'new', | |
474 | 'js' => $js, | |
475 | ), | |
476 | array( | |
477 | 'type' => 'cancel', | |
478 | 'name' => ts('Cancel'), | |
479 | ), | |
480 | ) | |
481 | ); | |
482 | ||
483 | $this->addFormRule(array('CRM_UF_Form_Field', 'formRule'), $this); | |
484 | ||
485 | // if view mode pls freeze it with the done button. | |
486 | if ($this->_action & CRM_Core_Action::VIEW) { | |
487 | $this->freeze(); | |
488 | $this->addElement('button', 'done', ts('Done'), | |
489 | array('onclick' => "location.href='civicrm/admin/uf/group/field?reset=1&action=browse&gid=" . $this->_gid . "'") | |
490 | ); | |
491 | } | |
492 | ||
493 | if (isset($defaults['field_name']) && CRM_Utils_Array::value(1, $defaults['field_name']) == 'url-1') { | |
494 | $defaults['field_name'][1] = 'url'; | |
495 | } | |
496 | ||
497 | $this->setDefaults($defaults); | |
498 | } | |
499 | ||
500 | /** | |
501 | * Process the form | |
502 | * | |
503 | * @return void | |
504 | * @access public | |
505 | */ | |
506 | public function postProcess() { | |
507 | $ids = array('uf_group' => $this->_gid); | |
508 | if ($this->_action & CRM_Core_Action::DELETE) { | |
509 | $fieldValues = array('uf_group_id' => $this->_gid); | |
510 | CRM_Utils_Weight::delWeight('CRM_Core_DAO_UFField', $this->_id, $fieldValues); | |
511 | $deleted = CRM_Core_BAO_UFField::del($this->_id); | |
512 | ||
513 | //update group_type every time. CRM-3608 | |
514 | if ($this->_gid && $deleted) { | |
515 | //get the profile type. | |
516 | $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($this->_gid, TRUE); | |
517 | CRM_Core_BAO_UFGroup::updateGroupTypes($this->_gid, $fieldsType); | |
518 | } | |
519 | ||
520 | CRM_Core_Session::setStatus(ts('Selected Profile Field has been deleted.'), ts('Profile Field Deleted'), 'success'); | |
521 | return; | |
522 | } | |
523 | ||
524 | // store the submitted values in an array | |
525 | $params = $this->controller->exportValues('Field'); | |
526 | if ($params['visibility'] == 'User and User Admin Only') { | |
527 | $params['is_searchable'] = $params['in_selector'] = 0; | |
528 | } | |
529 | ||
530 | if ($this->_action & CRM_Core_Action::UPDATE) { | |
531 | $ids['uf_field'] = $this->_id; | |
532 | } | |
533 | ||
534 | ||
535 | $name = NULL; | |
f06c4974 | 536 | if (isset($params['field_name'][1]) && isset($this->_selectFields[$params['field_name'][1]])) { |
6a488035 TO |
537 | // we dont get a name for a html formatting element |
538 | $name = $this->_selectFields[$params['field_name'][1]]; | |
539 | } | |
540 | ||
541 | //Hack for Formatting Field Name | |
542 | if ($params['field_name'][0] == 'Formatting') { | |
543 | $params['field_name'][1] = 'formatting_' . rand(1000, 9999); | |
544 | } | |
545 | ||
546 | // temporary hack to for website | |
547 | if ($params['field_name'][1] == 'url') { | |
548 | $params['field_name'][1] = 'url-1'; | |
549 | } | |
550 | ||
551 | //check for duplicate fields | |
552 | if ($params["field_name"][0] != "Formatting" && CRM_Core_BAO_UFField::duplicateField($params, $ids)) { | |
553 | CRM_Core_Session::setStatus(ts('The selected field already exists in this profile.'), ts('Field Not Added'), 'error'); | |
554 | return; | |
555 | } | |
556 | else { | |
557 | $params['weight'] = CRM_Core_BAO_UFField::autoWeight($params); | |
558 | $ufField = CRM_Core_BAO_UFField::add($params, $ids); | |
559 | ||
560 | //reset other field is searchable and in selector settings, CRM-4363 | |
561 | if ($this->_hasSearchableORInSelector && | |
562 | in_array($ufField->field_type, array('Participant', 'Contribution', 'Membership', 'Activity')) | |
563 | ) { | |
564 | CRM_Core_BAO_UFField::resetInSelectorANDSearchable($this->_gid); | |
565 | } | |
566 | ||
567 | $config = CRM_Core_Config::singleton(); | |
568 | $showBestResult = FALSE; | |
569 | if (in_array($ufField->field_name, array( | |
570 | 'country', 'state_province')) && count($config->countryLimit) > 1) { | |
571 | // get state or country field weight if exists | |
572 | $field = 'state_province'; | |
573 | if ($ufField->field_name == 'state_province') { | |
574 | $field = 'country'; | |
575 | } | |
576 | $ufFieldDAO = new CRM_Core_DAO_UFField(); | |
577 | $ufFieldDAO->field_name = $field; | |
578 | $ufFieldDAO->location_type_id = $ufField->location_type_id; | |
579 | $ufFieldDAO->uf_group_id = $ufField->uf_group_id; | |
580 | ||
581 | if ($ufFieldDAO->find(TRUE)) { | |
582 | if ($field == 'country' && $ufFieldDAO->weight > $ufField->weight) { | |
583 | $showBestResult = TRUE; | |
584 | } | |
585 | elseif ($field == 'state_province' && $ufFieldDAO->weight < $ufField->weight) { | |
586 | $showBestResult = TRUE; | |
587 | } | |
588 | } | |
589 | } | |
590 | ||
591 | //update group_type every time. CRM-3608 | |
592 | if ($this->_gid && is_a($ufField, 'CRM_Core_DAO_UFField')) { | |
593 | // get the profile type. | |
594 | $fieldsType = CRM_Core_BAO_UFGroup::calculateGroupType($this->_gid, TRUE); | |
595 | CRM_Core_BAO_UFGroup::updateGroupTypes($this->_gid, $fieldsType); | |
596 | } | |
597 | CRM_Core_Session::setStatus(ts('Your CiviCRM Profile Field \'%1\' has been saved to \'%2\'.', | |
598 | array(1 => $name, 2 => $this->_title) | |
599 | ), ts('Profile Field Saved'), 'success'); | |
600 | } | |
601 | $buttonName = $this->controller->getButtonName(); | |
602 | ||
603 | $session = CRM_Core_Session::singleton(); | |
604 | if ($buttonName == $this->getButtonName('next', 'new')) { | |
605 | CRM_Core_Session::setStatus(ts(' You can add another profile field.'), '', 'info'); | |
606 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field/add', | |
607 | "reset=1&action=add&gid={$this->_gid}&sbr={$showBestResult}" | |
608 | )); | |
609 | } | |
610 | else { | |
611 | $session->replaceUserContext(CRM_Utils_System::url('civicrm/admin/uf/group/field', | |
612 | "reset=1&action=browse&gid={$this->_gid}" | |
613 | )); | |
614 | $session->set('showBestResult', $showBestResult); | |
615 | } | |
616 | } | |
617 | ||
618 | /** | |
619 | * validation rule for subtype. | |
620 | * | |
621 | * @param array $groupType contains all groupTypes. | |
622 | * | |
623 | * @param string $fieldType type of field. | |
624 | * | |
625 | * @param array $errors | |
626 | * | |
627 | * @return array list of errors to be posted back to the form | |
628 | * @static | |
629 | * @access public | |
630 | */ | |
631 | static function formRuleSubType($fieldType, $groupType, $errors) { | |
632 | if (in_array($fieldType, array( | |
633 | 'Participant', 'Contribution', 'Membership', 'Activity'))) { | |
634 | $individualSubTypes = CRM_Contact_BAO_ContactType::subTypes('Individual'); | |
635 | foreach ($groupType as $value) { | |
636 | if (!in_array($value, $individualSubTypes) && | |
637 | !in_array($value, array( | |
638 | 'Participant', 'Contribution', 'Membership', | |
639 | 'Individual', 'Contact', 'Activity', | |
640 | )) | |
641 | ) { | |
642 | $errors['field_name'] = ts('Cannot add or update profile field "%1" with combination of Household or Organization or any subtypes of Household or Organisation.', array(1 => $fieldType)); | |
643 | break; | |
644 | } | |
645 | } | |
646 | } | |
647 | else { | |
648 | $basicType = CRM_Contact_BAO_ContactType::getBasicType($groupType); | |
649 | if ($basicType) { | |
650 | if (!is_array($basicType)) { | |
651 | $basicType = array($basicType); | |
652 | } | |
653 | if (!in_array($fieldType, $basicType)) { | |
654 | $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of subtype other than "%1".', | |
655 | array(1 => $fieldType) | |
656 | ); | |
657 | } | |
658 | } | |
659 | } | |
660 | } | |
661 | ||
662 | /** | |
663 | * validation rule for custom data extends entity column values. | |
664 | * | |
665 | * @param Object $customField Custom field | |
666 | * @param Integer $gid Group Id. | |
667 | * @param String $fieldType Group type of the field | |
668 | * @param Array $errors Collect errors | |
669 | * | |
670 | * @return Array list of errors to be posted back to the form | |
671 | * @static | |
672 | * @access public | |
673 | */ | |
674 | static function formRuleCustomDataExtentColumnValue($customField, $gid, $fieldType, &$errors) { | |
675 | // fix me : check object $customField | |
676 | if (in_array($fieldType, array( | |
677 | 'Participant', 'Contribution', 'Membership', 'Activity'))) { | |
678 | $params = array('id' => $customField->custom_group_id); | |
679 | $customGroup = array(); | |
680 | CRM_Core_BAO_CustomGroup::retrieve($params, $customGroup); | |
681 | if (($fieldType != CRM_Utils_Array::value('extends', $customGroup)) || | |
682 | !CRM_Utils_Array::value('extends_entity_column_value', $customGroup) | |
683 | ) { | |
684 | return $errors; | |
685 | } | |
686 | ||
687 | $extendsColumnValues = array(); | |
688 | foreach (explode(CRM_Core_DAO::VALUE_SEPARATOR, $customGroup['extends_entity_column_value']) as $val) { | |
689 | if ($val) { | |
690 | $extendsColumnValues[] = $val; | |
691 | } | |
692 | } | |
693 | ||
694 | if (empty($extendsColumnValues)) { | |
695 | return $errors; | |
696 | } | |
697 | ||
698 | $fieldTypeValues = CRM_Core_BAO_UFGroup::groupTypeValues($gid, $fieldType); | |
699 | if (!CRM_Utils_Array::value($fieldType, $fieldTypeValues)) { | |
700 | return; | |
701 | } | |
702 | ||
703 | $disallowedTypes = array_diff($extendsColumnValues, $fieldTypeValues[$fieldType]); | |
704 | if (!empty($disallowedTypes)) { | |
705 | $errors['field_name'] = ts('Profile is already having custom fields extending different group types, you can not add or update this custom field.'); | |
706 | } | |
707 | } | |
708 | } | |
709 | ||
710 | /** | |
711 | * global validation rules for the form | |
712 | * | |
713 | * @param array $fields posted values of the form | |
714 | * | |
715 | * @return array list of errors to be posted back to the form | |
716 | * @static | |
717 | * @access public | |
718 | */ | |
719 | static function formRule($fields, $files, $self) { | |
720 | $is_required = CRM_Utils_Array::value('is_required', $fields, FALSE); | |
721 | $is_registration = CRM_Utils_Array::value('is_registration', $fields, FALSE); | |
722 | $is_view = CRM_Utils_Array::value('is_view', $fields, FALSE); | |
723 | $in_selector = CRM_Utils_Array::value('in_selector', $fields, FALSE); | |
724 | $is_active = CRM_Utils_Array::value('is_active', $fields, FALSE); | |
725 | ||
726 | $errors = array(); | |
727 | if ($is_view && $is_registration) { | |
728 | $errors['is_registration'] = ts('View Only cannot be selected if this field is to be included on the registration form'); | |
729 | } | |
730 | if ($is_view && $is_required) { | |
731 | $errors['is_view'] = ts('A View Only field cannot be required'); | |
732 | } | |
733 | ||
734 | $fieldName = $fields['field_name'][0]; | |
735 | if (!$fieldName) { | |
736 | $errors['field_name'] = ts('Please select a field name'); | |
737 | } | |
738 | ||
739 | if ($in_selector && in_array($fieldName, array( | |
740 | 'Contribution', 'Participant', 'Membership', 'Activity'))) { | |
741 | $errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $fieldName)); | |
742 | } | |
743 | ||
744 | $isCustomField = FALSE; | |
745 | $profileFieldName = CRM_Utils_Array::value(1, $fields['field_name']); | |
746 | if ($profileFieldName) { | |
747 | //get custom field id | |
748 | $customFieldId = explode('_', $profileFieldName); | |
749 | if ($customFieldId[0] == 'custom') { | |
750 | $customField = new CRM_Core_DAO_CustomField(); | |
751 | $customField->id = $customFieldId[1]; | |
752 | $customField->find(TRUE); | |
753 | $isCustomField = TRUE; | |
754 | if (!empty($fields['field_id']) && !$customField->is_active && $is_active) { | |
755 | $errors['field_name'] = ts('Cannot set this field "Active" since the selected custom field is disabled.'); | |
756 | } | |
757 | ||
758 | //check if profile already has a different multi-record custom set field configured | |
759 | $customGroupId = CRM_Core_BAO_CustomField::isMultiRecordField($profileFieldName); | |
760 | if ($customGroupId) { | |
761 | if ($profileMultiRecordCustomGid = CRM_Core_BAO_UFField::checkMultiRecordFieldExists($self->_gid)) { | |
762 | if ($customGroupId != $profileMultiRecordCustomGid) { | |
763 | $errors['field_name'] = ts("You cannot configure multi-record custom fields belonging to different custom sets in one profile"); | |
764 | } | |
765 | } | |
766 | } | |
767 | } | |
768 | } | |
769 | ||
770 | //check profile is configured for double option process | |
771 | //adding group field, email field should be present in the group | |
772 | //fixed for issue CRM-2861 & CRM-4153 | |
773 | if (CRM_Core_BAO_UFGroup::isProfileDoubleOptin()) { | |
50c3e74b | 774 | if (CRM_Utils_Array::value(1, $fields['field_name']) == 'group') { |
6a488035 TO |
775 | $dao = new CRM_Core_BAO_UFField(); |
776 | $dao->uf_group_id = $fields['group_id']; | |
777 | $dao->find(); | |
778 | $emailField = FALSE; | |
779 | while ($dao->fetch()) { | |
780 | //check email field is present in the group | |
781 | if ($dao->field_name == 'email') { | |
782 | $emailField = TRUE; | |
783 | break; | |
784 | } | |
785 | } | |
786 | ||
787 | if (!$emailField) { | |
788 | $disableSettingURL = CRM_Utils_System::url( | |
789 | 'civicrm/admin/setting/preferences/mailing', | |
790 | 'reset=1' | |
791 | ); | |
792 | ||
793 | $errors['field_name'] = ts('Your site is currently configured to require double-opt in when users join (subscribe) to Group(s) via a Profile form. In this mode, you need to include an Email field in a Profile BEFORE you can add the Group(s) field. This ensures that an opt-in confirmation email can be sent. Your site administrator can disable double opt-in on the civimail admin settings: <em>%1</em>', array(1 => $disableSettingURL)); | |
794 | } | |
795 | } | |
796 | } | |
797 | ||
798 | //fix for CRM-3037 | |
799 | $fieldType = $fields['field_name'][0]; | |
800 | ||
801 | //get the group type. | |
802 | $groupType = CRM_Core_BAO_UFGroup::calculateGroupType($self->_gid, FALSE, CRM_Utils_Array::value('field_id', $fields)); | |
803 | ||
804 | switch ($fieldType) { | |
805 | case 'Contact': | |
806 | self::formRuleSubType($fieldType, $groupType, $errors); | |
807 | break; | |
808 | ||
809 | case 'Individual': | |
810 | if (in_array('Activity', $groupType) || | |
811 | in_array('Household', $groupType) || | |
812 | in_array('Organization', $groupType) | |
813 | ) { | |
814 | ||
815 | //CRM-7603 - need to support activity + individual. | |
816 | //$errors['field_name'] = | |
817 | //ts( 'Cannot add or update profile field type Individual with combination of Household or Organization or Activity' ); | |
818 | if (in_array('Household', $groupType) || | |
819 | in_array('Organization', $groupType) | |
820 | ) { | |
821 | $errors['field_name'] = ts('Cannot add or update profile field type Individual with combination of Household or Organization'); | |
822 | } | |
823 | } | |
824 | else { | |
825 | self::formRuleSubType($fieldType, $groupType, $errors); | |
826 | } | |
827 | break; | |
828 | ||
829 | case 'Household': | |
830 | if (in_array('Activity', $groupType) || in_array('Individual', $groupType) || in_array('Organization', $groupType)) { | |
831 | $errors['field_name'] = ts('Cannot add or update profile field type Household with combination of Individual or Organization or Activity'); | |
832 | } | |
833 | else { | |
834 | self::formRuleSubType($fieldType, $groupType, $errors); | |
835 | } | |
836 | break; | |
837 | ||
838 | case 'Organization': | |
839 | if (in_array('Activity', $groupType) || in_array('Household', $groupType) || in_array('Individual', $groupType)) { | |
840 | $errors['field_name'] = ts('Cannot add or update profile field type Organization with combination of Household or Individual or Activity'); | |
841 | } | |
842 | else { | |
843 | self::formRuleSubType($fieldType, $groupType, $errors); | |
844 | } | |
845 | break; | |
846 | ||
847 | case 'Activity': | |
848 | if (in_array('Individual', $groupType) || | |
849 | in_array('Membership', $groupType) || | |
850 | in_array('Contribution', $groupType) || | |
851 | in_array('Organization', $groupType) || | |
852 | in_array('Household', $groupType) || | |
853 | in_array('Participant', $groupType) | |
854 | ) { | |
855 | ||
856 | //CRM-7603 - need to support activity + contact type. | |
857 | //$errors['field_name'] = | |
858 | //ts( 'Cannot add or update profile field type Activity with combination Participant or Membership or Contribution or Household or Organization or Individual' ); | |
859 | if (in_array('Membership', $groupType) || | |
860 | in_array('Contribution', $groupType) || | |
861 | in_array('Participant', $groupType) | |
862 | ) { | |
863 | $errors['field_name'] = ts('Cannot add or update profile field type Activity with combination Participant or Membership or Contribution'); | |
864 | } | |
865 | } | |
866 | else { | |
867 | self::formRuleSubType($fieldType, $groupType, $errors); | |
868 | } | |
869 | ||
870 | if ($isCustomField && !isset($errors['field_name'])) { | |
871 | self::formRuleCustomDataExtentColumnValue($customField, $self->_gid, $fieldType, $errors); | |
872 | } | |
873 | break; | |
874 | ||
875 | case 'Participant': | |
876 | if (in_array('Membership', $groupType) || in_array('Contribution', $groupType) | |
877 | || in_array('Organization', $groupType) || in_array('Household', $groupType) || in_array('Activity', $groupType) | |
878 | ) { | |
7132ac1f | 879 | $errors['field_name'] = ts('Cannot add or update profile field type Participant with combination of Activity or Membership or Contribution or Household or Organization.'); |
6a488035 TO |
880 | } |
881 | else { | |
882 | self::formRuleSubType($fieldType, $groupType, $errors); | |
883 | } | |
884 | break; | |
885 | ||
886 | case 'Contribution': | |
887 | //special case where in we allow contribution + oganization fields, for on behalf feature | |
888 | $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', | |
889 | 'on_behalf_organization', 'id', 'name' | |
890 | ); | |
891 | ||
892 | if (in_array('Participant', $groupType) || in_array('Membership', $groupType) | |
893 | || ($profileId != $self->_gid && in_array('Organization', $groupType)) || in_array('Household', $groupType) || in_array('Activity', $groupType) | |
894 | ) { | |
895 | $errors['field_name'] = ts('Cannot add or update profile field type Contribution with combination of Activity or Membership or Participant or Household or Organization'); | |
896 | } | |
897 | else { | |
898 | self::formRuleSubType($fieldType, $groupType, $errors); | |
899 | } | |
900 | break; | |
901 | ||
902 | case 'Membership': | |
903 | //special case where in we allow contribution + oganization fields, for on behalf feature | |
904 | $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', | |
905 | 'on_behalf_organization', 'id', 'name' | |
906 | ); | |
907 | ||
908 | if (in_array('Participant', $groupType) || in_array('Contribution', $groupType) | |
909 | || ($profileId != $self->_gid && in_array('Organization', $groupType)) || in_array('Household', $groupType) || in_array('Activity', $groupType) | |
910 | ) { | |
911 | $errors['field_name'] = ts('Cannot add or update profile field type Membership with combination of Activity or Participant or Contribution or Household or Organization'); | |
912 | } | |
913 | else { | |
914 | self::formRuleSubType($fieldType, $groupType, $errors); | |
915 | } | |
916 | break; | |
917 | ||
918 | default: | |
919 | $profileType = CRM_Core_BAO_UFField::getProfileType($fields['group_id'], TRUE, FALSE, TRUE); | |
920 | if (CRM_Contact_BAO_ContactType::isaSubType($fieldType)) { | |
921 | if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) { | |
922 | if ($fieldType != $profileType) { | |
923 | $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(1 => $fieldType, 2 => $profileType)); | |
924 | } | |
925 | } | |
926 | else { | |
927 | $basicType = CRM_Contact_BAO_ContactType::getBasicType($fieldType); | |
928 | if ($profileType && | |
929 | $profileType != $basicType && | |
930 | $profileType != 'Contact' | |
931 | ) { | |
932 | $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(1 => $fieldType, 2 => $profileType)); | |
933 | } | |
934 | } | |
935 | } | |
50c3e74b DL |
936 | elseif ( |
937 | CRM_Utils_Array::value(1, $fields['field_name']) == 'contact_sub_type' && | |
938 | !in_array($profileType, array('Individual', 'Household', 'Organization')) && | |
6a488035 TO |
939 | !in_array($profileType, CRM_Contact_BAO_ContactType::subTypes()) |
940 | ) { | |
941 | $errors['field_name'] = ts('Cannot add or update profile field Contact Subtype as profile type is not one of Individual, Household or Organization.'); | |
942 | } | |
943 | } | |
944 | return empty($errors) ? TRUE : $errors; | |
945 | } | |
946 | } |