3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * form to process actions on the field aspect of Custom
39 class CRM_UF_Form_Field
extends CRM_Core_Form
{
42 * the uf group id saved to the session for an update
50 * The field id, used when editing the field
58 * The set of fields that we can view/edit in the user field framework
74 * The set of fields sent to the select element
79 protected $_selectFields;
82 * to store fields with if locationtype exits status
87 protected $_hasLocationTypes;
90 * is this profile has searchable field
91 * or is any field having in selector true.
96 protected $_hasSearchableORInSelector;
99 * Function to set variables up before form is built
104 public function preProcess() {
105 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
106 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
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'));
111 $url = CRM_Utils_System
::url('civicrm/admin/uf/group/field',
112 "reset=1&action=browse&gid={$this->_gid}"
115 $session = CRM_Core_Session
::singleton();
116 $session->pushUserContext($url);
117 $breadCrumb = array(array('title' => ts('CiviCRM Profile Fields'),
120 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
123 $showBestResult = CRM_Utils_Request
::retrieve('sbr', 'Positive', CRM_Core_DAO
::$_nullArray);
124 if ($showBestResult) {
125 $this->assign('showBestResult', $showBestResult);
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
);
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']);
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
);
144 if (CRM_Core_Permission
::access('CiviMember')) {
145 $this->_fields
= array_merge(CRM_Member_BAO_Membership
::getMembershipFields(), $this->_fields
);
148 if (CRM_Core_Permission
::access('CiviEvent')) {
149 $this->_fields
= array_merge(CRM_Event_BAO_Query
::getParticipantFields(), $this->_fields
);
152 if (CRM_Core_Permission
::access('CiviCase')) {
153 $this->_fields
= array_merge(CRM_Case_BAO_Query
::getFields(), $this->_fields
);
156 $this->_fields
= array_merge($this->_fields
, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
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') {
164 $this->_selectFields
[$name] = $field['title'];
165 $this->_hasLocationTypes
[$name] = CRM_Utils_Array
::value('hasLocationType', $field);
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');
174 //CRM-4363 check for in selector or searchable fields.
175 $this->_hasSearchableORInSelector
= CRM_Core_BAO_UFField
::checkSearchableORInSelector($this->_gid
);
177 $this->assign('fieldId', $this->_id
);
179 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $this->_id
, 'label');
180 $this->assign('fieldTitle', $fieldTitle);
185 * Function to actually build the form
190 public function buildQuickForm() {
191 if ($this->_action
& CRM_Core_Action
::DELETE
) {
192 $this->addButtons(array(
195 'name' => ts('Delete Profile Field'),
196 'spacing' => ' ',
201 'name' => ts('Cancel'),
208 if (isset($this->_id
)) {
209 $params = array('id' => $this->_id
);
210 CRM_Core_BAO_UFField
::retrieve($params, $defaults);
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);
215 $specialFields = CRM_Core_BAO_UFGroup
::getLocationFields();
217 if (!$defaults['location_type_id'] &&
218 $defaults["field_type"] != "Formatting" &&
219 in_array($defaults['field_name'], $specialFields)
221 $defaults['location_type_id'] = 0;
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),
230 $this->_gid
= $defaults['uf_group_id'];
233 $defaults['is_active'] = 1;
236 $otherModules = array_values(CRM_Core_BAO_UFGroup
::getUFJoinRecord($this->_gid
));
237 $this->assign('otherModules', $otherModules);
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);
244 // lets trim all the whitespace
245 $this->applyFilter('__ALL__', 'trim');
247 //hidden field to catch the group id in profile
248 $this->add('hidden', 'group_id', $this->_gid
);
250 //hidden field to catch the field id in profile
251 $this->add('hidden', 'field_id', $this->_id
);
253 $fields = CRM_Core_BAO_UFField
::getAvailableFields($this->_gid
, $defaults);
255 $noSearchable = array();
256 $addressCustomFields = array_keys(CRM_Core_BAO_CustomField
::getFieldsForImport('Address'));
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;
270 $this->_mapperFields
[$key][$key1] = $value1['title'];
272 $hasLocationTypes[$key][$key1] = CRM_Utils_Array
::value('hasLocationType', $value1);
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')
281 if (!in_array($value1['title'], $noSearchable)) {
282 $noSearchable[] = $value1['title'];
287 $this->assign('noSearchable', $noSearchable);
289 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
290 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
293 * FIXME: dirty hack to make the default option show up first. This
294 * avoids a mozilla browser bug with defaults on dynamically constructed
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
;
304 $this->_location_types
= array('Primary') +
$this->_location_types
;
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['']);
311 $contactTypes = !empty($contactTypes) ?
array('Contact' => 'Contacts') +
$contactTypes : array();
312 $sel1 = array('' => '- select -') +
$contactTypes;
314 if (!empty($fields['Activity'])) {
315 $sel1['Activity'] = 'Activity';
318 if (CRM_Core_Permission
::access('CiviEvent')) {
319 $sel1['Participant'] = 'Participants';
322 if (!empty($fields['Contribution'])) {
323 $sel1['Contribution'] = 'Contributions';
326 if (!empty($fields['Membership'])) {
327 $sel1['Membership'] = 'Membership';
330 if (!empty($fields['Case'])) {
331 $sel1['Case'] = 'Case';
334 if (!empty($fields['Formatting'])) {
335 $sel1['Formatting'] = 'Formatting';
338 foreach ($sel1 as $key => $sel) {
340 $sel2[$key] = $this->_mapperFields
[$key];
344 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
347 foreach ($sel1 as $k => $sel) {
349 foreach ($this->_location_types
as $key => $value) {
350 $sel4[$k]['phone'][$key] = &$phoneTypes;
351 $sel4[$k]['phone_and_ext'][$key] = &$phoneTypes;
356 foreach ($sel1 as $k => $sel) {
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
;
371 $this->_defaults
= array();
372 $js = "<script type='text/javascript'>\n";
373 $formName = "document.{$this->_name}";
375 $alreadyMixProfile = FALSE;
376 if (CRM_Core_BAO_UFField
::checkProfileType($this->_gid
)) {
377 $alreadyMixProfile = TRUE;
379 $this->assign('alreadyMixProfile', $alreadyMixProfile);
381 $sel = &$this->addElement('hierselect', 'field_name', ts('Field Name'));
383 $formValues = $this->exportValues();
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";
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";
400 $js .= "{$formName}['field_name[$k]'].style.display = '';\n";
406 for ($k = 1; $k < 4; $k++
) {
407 if (!isset($defaults['field_name'][$k])) {
408 $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n";
414 foreach ($sel2 as $k => $v) {
415 if (is_array($sel2[$k])) {
420 // CRM_Core_Error::debug(array($sel1, $sel2, $sel3, $sel4));
421 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
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';
428 $js .= "</script>\n";
429 $this->assign('initHideBoxes', $js);
434 CRM_Core_SelectValues
::ufVisibility(),
436 array('onChange' => "showHideSeletorSearch(this.value);")
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);
445 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_UFField');
448 $this->add('text', 'weight', ts('Order'), $attributes['weight'], TRUE);
449 $this->addRule('weight', ts('is a numeric field'), 'numeric');
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']);
454 $this->add('checkbox', 'is_required', ts('Required?'));
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?'));
460 // $this->add( 'checkbox', 'is_registration', ts( 'Display in Registration Form?' ) );
461 //$this->add( 'checkbox', 'is_match' , ts( 'Key to Match Contacts?' ) );
463 $this->add('text', 'label', ts('Field Label'), $attributes['label']);
466 if ($this->_hasSearchableORInSelector
) {
467 $js = array('onclick' => "return verify( );");
471 $this->addButtons(array(
474 'name' => ts('Save'),
480 'name' => ts('Save and New'),
486 'name' => ts('Cancel'),
491 $this->addFormRule(array('CRM_UF_Form_Field', 'formRule'), $this);
493 // if view mode pls freeze it with the done button.
494 if ($this->_action
& CRM_Core_Action
::VIEW
) {
496 $this->addElement('button', 'done', ts('Done'),
497 array('onclick' => "location.href='civicrm/admin/uf/group/field?reset=1&action=browse&gid=" . $this->_gid
. "'")
501 if (isset($defaults['field_name']) && CRM_Utils_Array
::value(1, $defaults['field_name']) == 'url-1') {
502 $defaults['field_name'][1] = 'url';
505 $this->setDefaults($defaults);
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
);
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);
528 CRM_Core_Session
::setStatus(ts('Selected Profile Field has been deleted.'), ts('Profile Field Deleted'), 'success');
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;
538 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
539 $ids['uf_field'] = $this->_id
;
544 if (isset($params['field_name'][1]) && isset($this->_selectFields
[$params['field_name'][1]])) {
545 // we dont get a name for a html formatting element
546 $name = $this->_selectFields
[$params['field_name'][1]];
549 //Hack for Formatting Field Name
550 if ($params['field_name'][0] == 'Formatting') {
551 $params['field_name'][1] = 'formatting_' . rand(1000, 9999);
554 // temporary hack to for website
555 if ($params['field_name'][1] == 'url') {
556 $params['field_name'][1] = 'url-1';
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');
565 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
566 $ufField = CRM_Core_BAO_UFField
::add($params, $ids);
568 //reset other field is searchable and in selector settings, CRM-4363
569 if ($this->_hasSearchableORInSelector
&&
570 in_array($ufField->field_type
, array('Participant', 'Contribution', 'Membership', 'Activity', 'Case'))
572 CRM_Core_BAO_UFField
::resetInSelectorANDSearchable($this->_gid
);
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') {
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
;
589 if ($ufFieldDAO->find(TRUE)) {
590 if ($field == 'country' && $ufFieldDAO->weight
> $ufField->weight
) {
591 $showBestResult = TRUE;
593 elseif ($field == 'state_province' && $ufFieldDAO->weight
< $ufField->weight
) {
594 $showBestResult = TRUE;
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);
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');
609 $buttonName = $this->controller
->getButtonName();
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}"
619 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/uf/group/field',
620 "reset=1&action=browse&gid={$this->_gid}"
622 $session->set('showBestResult', $showBestResult);
627 * validation rule for subtype.
629 * @param array $groupType contains all groupTypes.
631 * @param string $fieldType type of field.
633 * @param array $errors
635 * @return array list of errors to be posted back to the form
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',
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));
656 $basicType = CRM_Contact_BAO_ContactType
::getBasicType($groupType);
658 if (!is_array($basicType)) {
659 $basicType = array($basicType);
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)
671 * validation rule for custom data extends entity column values.
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
678 * @return Array list of errors to be posted back to the form
682 static function formRuleCustomDataExtentColumnValue($customField, $gid, $fieldType, &$errors) {
683 // fix me : check object $customField
684 if (in_array($fieldType, array(
685 'Participant', 'Contribution', 'Membership', 'Activity', 'Case'))) {
686 $params = array('id' => $customField->custom_group_id
);
687 $customGroup = array();
688 CRM_Core_BAO_CustomGroup
::retrieve($params, $customGroup);
689 if (($fieldType != CRM_Utils_Array
::value('extends', $customGroup)) ||
empty($customGroup['extends_entity_column_value'])) {
693 $extendsColumnValues = array();
694 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $customGroup['extends_entity_column_value']) as $val) {
696 $extendsColumnValues[] = $val;
700 if (empty($extendsColumnValues)) {
704 $fieldTypeValues = CRM_Core_BAO_UFGroup
::groupTypeValues($gid, $fieldType);
705 if (empty($fieldTypeValues[$fieldType])) {
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.');
717 * global validation rules for the form
719 * @param array $fields posted values of the form
721 * @return array list of errors to be posted back to the form
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);
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');
736 if ($is_view && $is_required) {
737 $errors['is_view'] = ts('A View Only field cannot be required');
740 $fieldName = $fields['field_name'][0];
742 $errors['field_name'] = ts('Please select a field name');
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));
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.');
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");
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()) {
780 if (CRM_Utils_Array
::value(1, $fields['field_name']) == 'group') {
781 $dao = new CRM_Core_BAO_UFField();
782 $dao->uf_group_id
= $fields['group_id'];
785 while ($dao->fetch()) {
786 //check email field is present in the group
787 if ($dao->field_name
== 'email') {
794 $disableSettingURL = CRM_Utils_System
::url(
795 'civicrm/admin/setting/preferences/mailing',
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));
805 $fieldType = $fields['field_name'][0];
807 //get the group type.
808 $groupType = CRM_Core_BAO_UFGroup
::calculateGroupType($self->_gid
, FALSE, CRM_Utils_Array
::value('field_id', $fields));
810 switch ($fieldType) {
812 self
::formRuleSubType($fieldType, $groupType, $errors);
816 if (in_array('Activity', $groupType) ||
817 in_array('Household', $groupType) ||
818 in_array('Organization', $groupType)
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)
827 $errors['field_name'] = ts('Cannot add or update profile field type Individual with combination of Household or Organization');
831 self
::formRuleSubType($fieldType, $groupType, $errors);
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');
840 self
::formRuleSubType($fieldType, $groupType, $errors);
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');
849 self
::formRuleSubType($fieldType, $groupType, $errors);
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)
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)
869 $errors['field_name'] = ts('Cannot add or update profile field type Activity with combination Participant or Membership or Contribution');
873 self
::formRuleSubType($fieldType, $groupType, $errors);
876 if ($isCustomField && !isset($errors['field_name'])) {
877 self
::formRuleCustomDataExtentColumnValue($customField, $self->_gid
, $fieldType, $errors);
882 if (in_array('Membership', $groupType) ||
in_array('Contribution', $groupType)
883 ||
in_array('Organization', $groupType) ||
in_array('Household', $groupType) ||
in_array('Activity', $groupType)
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.');
888 self
::formRuleSubType($fieldType, $groupType, $errors);
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'
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)
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');
904 self
::formRuleSubType($fieldType, $groupType, $errors);
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'
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)
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');
920 self
::formRuleSubType($fieldType, $groupType, $errors);
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));
933 $basicType = CRM_Contact_BAO_ContactType
::getBasicType($fieldType);
935 $profileType != $basicType &&
936 $profileType != 'Contact'
938 $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(1 => $fieldType, 2 => $profileType));
943 CRM_Utils_Array
::value(1, $fields['field_name']) == 'contact_sub_type' &&
944 !in_array($profileType, array('Individual', 'Household', 'Organization')) &&
945 !in_array($profileType, CRM_Contact_BAO_ContactType
::subTypes())
947 $errors['field_name'] = ts('Cannot add or update profile field Contact Subtype as profile type is not one of Individual, Household or Organization.');
950 return empty($errors) ?
TRUE : $errors;