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