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