3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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.
49 * The field id, used when editing the field
56 * The set of fields that we can view/edit in the user field framework
63 * The title for field.
70 * The set of fields sent to the select element.
74 protected $_selectFields;
77 * store fields with if locationtype exits status.
81 protected $_hasLocationTypes;
84 * Is this profile has searchable field.
85 * or is any field having in selector true.
89 protected $_hasSearchableORInSelector;
92 * Set variables up before form is built.
96 public function preProcess() {
97 $this->_gid
= CRM_Utils_Request
::retrieve('gid', 'Positive', $this);
98 $this->_id
= CRM_Utils_Request
::retrieve('id', 'Positive', $this);
100 $this->_title
= CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup', $this->_gid
, 'title');
102 $this->setPageTitle(ts('Profile Field'));
104 $url = CRM_Utils_System
::url('civicrm/admin/uf/group/field',
105 "reset=1&action=browse&gid={$this->_gid}"
108 $session = CRM_Core_Session
::singleton();
109 $session->pushUserContext($url);
112 'title' => ts('CiviCRM Profile Fields'),
116 CRM_Utils_System
::appendBreadCrumb($breadCrumb);
119 $showBestResult = CRM_Utils_Request
::retrieve('sbr', 'Positive', CRM_Core_DAO
::$_nullArray);
120 if ($showBestResult) {
121 $this->assign('showBestResult', $showBestResult);
124 $this->_fields
= CRM_Contact_BAO_Contact
::importableFields('All', TRUE, TRUE, TRUE, TRUE, TRUE);
125 $this->_fields
= array_merge(CRM_Activity_BAO_Activity
::exportableFields('Activity'), $this->_fields
);
127 //unset campaign related fields.
128 if (isset($this->_fields
['activity_campaign_id'])) {
129 $this->_fields
['activity_campaign_id']['title'] = ts('Campaign');
130 if (isset($this->_fields
['activity_campaign'])) {
131 unset($this->_fields
['activity_campaign']);
135 if (CRM_Core_Permission
::access('CiviContribute')) {
136 $this->_fields
= array_merge(CRM_Contribute_BAO_Contribution
::getContributionFields(FALSE), $this->_fields
);
137 $this->_fields
= array_merge(CRM_Core_BAO_UFField
::getContribBatchEntryFields(), $this->_fields
);
140 if (CRM_Core_Permission
::access('CiviMember')) {
141 $this->_fields
= array_merge(CRM_Member_BAO_Membership
::getMembershipFields(), $this->_fields
);
144 if (CRM_Core_Permission
::access('CiviEvent')) {
145 $this->_fields
= array_merge(CRM_Event_BAO_Query
::getParticipantFields(), $this->_fields
);
148 if (CRM_Core_Permission
::access('CiviCase')) {
149 $this->_fields
= array_merge(CRM_Case_BAO_Query
::getFields(), $this->_fields
);
152 $this->_fields
= array_merge($this->_fields
, CRM_Contact_BAO_Query_Hook
::singleton()->getFields());
154 $this->_selectFields
= array();
155 foreach ($this->_fields
as $name => $field) {
156 // lets skip note for now since we dont support it
157 if ($name == 'note') {
160 $this->_selectFields
[$name] = $field['title'];
161 $this->_hasLocationTypes
[$name] = CRM_Utils_Array
::value('hasLocationType', $field);
164 // lets add group, tag and current_employer to this list
165 $this->_selectFields
['group'] = ts('Group(s)');
166 $this->_selectFields
['tag'] = ts('Tag(s)');
167 $this->_selectFields
['current_employer'] = ts('Current Employer');
168 $this->_selectFields
['phone_and_ext'] = ts('Phone and Extension');
170 //CRM-4363 check for in selector or searchable fields.
171 $this->_hasSearchableORInSelector
= CRM_Core_BAO_UFField
::checkSearchableORInSelector($this->_gid
);
173 $this->assign('fieldId', $this->_id
);
175 $fieldTitle = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFField', $this->_id
, 'label');
176 $this->assign('fieldTitle', $fieldTitle);
181 * Build the form object.
185 public function buildQuickForm() {
186 if ($this->_action
& CRM_Core_Action
::DELETE
) {
187 $this->addButtons(array(
190 'name' => ts('Delete Profile Field'),
191 'spacing' => ' ',
196 'name' => ts('Cancel'),
203 if (isset($this->_id
)) {
204 $params = array('id' => $this->_id
);
205 CRM_Core_BAO_UFField
::retrieve($params, $defaults);
207 // set it to null if so (avoids crappy E_NOTICE errors below
208 $defaults['location_type_id'] = CRM_Utils_Array
::value('location_type_id', $defaults);
210 $specialFields = CRM_Core_BAO_UFGroup
::getLocationFields();
212 if (!$defaults['location_type_id'] &&
213 $defaults["field_type"] != "Formatting" &&
214 in_array($defaults['field_name'], $specialFields)
216 $defaults['location_type_id'] = 0;
219 $defaults['field_name'] = array(
220 $defaults['field_type'],
221 ($defaults['field_type'] == "Formatting" ?
"" : $defaults['field_name']),
222 ($defaults['field_name'] == "url") ?
$defaults['website_type_id'] : $defaults['location_type_id'],
223 CRM_Utils_Array
::value('phone_type_id', $defaults),
225 $this->_gid
= $defaults['uf_group_id'];
228 $defaults['is_active'] = 1;
231 $otherModules = array_values(CRM_Core_BAO_UFGroup
::getUFJoinRecord($this->_gid
));
232 $this->assign('otherModules', $otherModules);
234 if ($this->_action
& CRM_Core_Action
::ADD
) {
235 $fieldValues = array('uf_group_id' => $this->_gid
);
236 $defaults['weight'] = CRM_Utils_Weight
::getDefaultWeight('CRM_Core_DAO_UFField', $fieldValues);
239 // lets trim all the whitespace
240 $this->applyFilter('__ALL__', 'trim');
242 //hidden field to catch the group id in profile
243 $this->add('hidden', 'group_id', $this->_gid
);
245 //hidden field to catch the field id in profile
246 $this->add('hidden', 'field_id', $this->_id
);
248 $fields = CRM_Core_BAO_UFField
::getAvailableFields($this->_gid
, $defaults);
250 $noSearchable = $hasWebsiteTypes = array();
251 $addressCustomFields = array_keys(CRM_Core_BAO_CustomField
::getFieldsForImport('Address'));
253 foreach ($fields as $key => $value) {
254 foreach ($value as $key1 => $value1) {
255 //CRM-2676, replacing the conflict for same custom field name from different custom group.
256 if ($customFieldId = CRM_Core_BAO_CustomField
::getKeyID($key1)) {
257 $customGroupId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomField', $customFieldId, 'custom_group_id');
258 $customGroupName = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_CustomGroup', $customGroupId, 'title');
259 $this->_mapperFields
[$key][$key1] = $value1['title'] . ' :: ' . $customGroupName;
260 if (in_array($key1, $addressCustomFields)) {
261 $noSearchable[] = $value1['title'] . ' :: ' . $customGroupName;
265 $this->_mapperFields
[$key][$key1] = $value1['title'];
267 $hasLocationTypes[$key][$key1] = CRM_Utils_Array
::value('hasLocationType', $value1);
268 $hasWebsiteTypes[$key][$key1] = CRM_Utils_Array
::value('hasWebsiteType', $value1);
269 // hide the 'is searchable' field for 'File' custom data
270 if (isset($value1['data_type']) &&
271 isset($value1['html_type']) &&
272 (($value1['data_type'] == 'File' && $value1['html_type'] == 'File')
273 ||
($value1['data_type'] == 'Link' && $value1['html_type'] == 'Link')
276 if (!in_array($value1['title'], $noSearchable)) {
277 $noSearchable[] = $value1['title'];
282 $this->assign('noSearchable', $noSearchable);
284 $this->_location_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Address', 'location_type_id');
285 $defaultLocationType = CRM_Core_BAO_LocationType
::getDefault();
286 $this->_website_types
= CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Website', 'website_type_id');
289 * FIXME: dirty hack to make the default option show up first. This
290 * avoids a mozilla browser bug with defaults on dynamically constructed
293 if ($defaultLocationType) {
294 $defaultLocation = $this->_location_types
[$defaultLocationType->id
];
295 unset($this->_location_types
[$defaultLocationType->id
]);
296 $this->_location_types
= array(
297 $defaultLocationType->id
=> $defaultLocation,
298 ) +
$this->_location_types
;
301 $this->_location_types
= array('Primary') +
$this->_location_types
;
303 // since we need a hierarchical list to display contact types & subtypes,
304 // this is what we going to display in first selector
305 $contactTypes = CRM_Contact_BAO_ContactType
::getSelectElements(FALSE, FALSE);
306 unset($contactTypes['']);
308 $contactTypes = !empty($contactTypes) ?
array('Contact' => 'Contacts') +
$contactTypes : array();
309 $sel1 = array('' => '- select -') +
$contactTypes;
311 if (!empty($fields['Activity'])) {
312 $sel1['Activity'] = 'Activity';
315 if (CRM_Core_Permission
::access('CiviEvent')) {
316 $sel1['Participant'] = 'Participants';
319 if (!empty($fields['Contribution'])) {
320 $sel1['Contribution'] = 'Contributions';
323 if (!empty($fields['Membership'])) {
324 $sel1['Membership'] = 'Membership';
327 if (!empty($fields['Case'])) {
328 $sel1['Case'] = 'Case';
331 if (!empty($fields['Formatting'])) {
332 $sel1['Formatting'] = 'Formatting';
335 foreach ($sel1 as $key => $sel) {
337 $sel2[$key] = $this->_mapperFields
[$key];
341 $phoneTypes = CRM_Core_PseudoConstant
::get('CRM_Core_DAO_Phone', 'phone_type_id');
344 foreach ($sel1 as $k => $sel) {
346 foreach ($this->_location_types
as $key => $value) {
347 $sel4[$k]['phone'][$key] = &$phoneTypes;
348 $sel4[$k]['phone_and_ext'][$key] = &$phoneTypes;
353 foreach ($sel1 as $k => $sel) {
355 if (is_array($this->_mapperFields
[$k])) {
356 foreach ($this->_mapperFields
[$k] as $key => $value) {
357 if ($hasLocationTypes[$k][$key]) {
358 $sel3[$k][$key] = $this->_location_types
;
360 elseif ($hasWebsiteTypes[$k][$key]) {
361 $sel3[$k][$key] = $this->_website_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 (!isset($defaults['field_name'][$k])) {
388 $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n";
393 if (!empty($formValues['field_name'])) {
394 for ($key = 1; $key < 4; $key++
) {
395 if (!isset($formValues['field_name'][$key])) {
396 $js .= "{$formName}['field_name[$key]'].style.display = 'none';\n";
399 $js .= "{$formName}['field_name[$key]'].style.display = '';\n";
404 for ($k = 1; $k < 4; $k++
) {
405 if (!isset($defaults['field_name'][$k])) {
406 $js .= "{$formName}['field_name[$k]'].style.display = 'none';\n";
412 foreach ($sel2 as $k => $v) {
413 if (is_array($sel2[$k])) {
418 $sel->setOptions(array($sel1, $sel2, $sel3, $sel4));
420 // proper interpretation of spec in CRM-8732
421 if (!isset($this->_id
) && in_array('Search Profile', $otherModules)) {
422 $defaults['visibility'] = 'Public Pages and Listings';
425 $js .= "</script>\n";
426 $this->assign('initHideBoxes', $js);
431 CRM_Core_SelectValues
::ufVisibility(),
433 array('onChange' => "showHideSeletorSearch(this.value);")
437 $js = array('onChange' => "mixProfile();");
438 // should the field appear in selectors (as a column)?
439 $this->add('checkbox', 'in_selector', ts('Results Column?'), NULL, NULL, $js);
440 $this->add('checkbox', 'is_searchable', ts('Searchable?'), NULL, NULL, $js);
442 $attributes = CRM_Core_DAO
::getAttribute('CRM_Core_DAO_UFField');
445 $this->add('text', 'weight', ts('Order'), $attributes['weight'], TRUE);
446 $this->addRule('weight', ts('is a numeric field'), 'numeric');
448 $this->add('textarea', 'help_pre', ts('Field Pre Help'), $attributes['help_pre']);
449 $this->add('textarea', 'help_post', ts('Field Post Help'), $attributes['help_post']);
451 $this->add('checkbox', 'is_required', ts('Required?'));
453 $this->add('checkbox', 'is_multi_summary', ts('Include in multi-record listing?'));
454 $this->add('checkbox', 'is_active', ts('Active?'));
455 $this->add('checkbox', 'is_view', ts('View Only?'));
457 // $this->add( 'checkbox', 'is_registration', ts( 'Display in Registration Form?' ) );
458 //$this->add( 'checkbox', 'is_match' , ts( 'Key to Match Contacts?' ) );
460 $this->add('text', 'label', ts('Field Label'), $attributes['label']);
463 if ($this->_hasSearchableORInSelector
) {
464 $js = array('onclick' => "return verify( );");
468 $this->addButtons(array(
471 'name' => ts('Save'),
477 'name' => ts('Save and New'),
483 'name' => ts('Cancel'),
488 $this->addFormRule(array('CRM_UF_Form_Field', 'formRule'), $this);
490 // if view mode pls freeze it with the done button.
491 if ($this->_action
& CRM_Core_Action
::VIEW
) {
493 $this->addElement('button', 'done', ts('Done'),
494 array('onclick' => "location.href='civicrm/admin/uf/group/field?reset=1&action=browse&gid=" . $this->_gid
. "'")
498 $this->setDefaults($defaults);
506 public function postProcess() {
507 $ids = array('uf_group' => $this->_gid
);
508 if ($this->_action
& CRM_Core_Action
::DELETE
) {
509 $fieldValues = array('uf_group_id' => $this->_gid
);
510 CRM_Utils_Weight
::delWeight('CRM_Core_DAO_UFField', $this->_id
, $fieldValues);
511 $deleted = CRM_Core_BAO_UFField
::del($this->_id
);
513 //update group_type every time. CRM-3608
514 if ($this->_gid
&& $deleted) {
515 //get the profile type.
516 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($this->_gid
, TRUE);
517 CRM_Core_BAO_UFGroup
::updateGroupTypes($this->_gid
, $fieldsType);
520 CRM_Core_Session
::setStatus(ts('Selected Profile Field has been deleted.'), ts('Profile Field Deleted'), 'success');
524 // store the submitted values in an array
525 $params = $this->controller
->exportValues('Field');
526 if ($params['visibility'] == 'User and User Admin Only') {
527 $params['is_searchable'] = $params['in_selector'] = 0;
530 if ($this->_action
& CRM_Core_Action
::UPDATE
) {
531 $ids['uf_field'] = $this->_id
;
535 if (isset($params['field_name'][1]) && isset($this->_selectFields
[$params['field_name'][1]])) {
536 // we dont get a name for a html formatting element
537 $name = $this->_selectFields
[$params['field_name'][1]];
540 //Hack for Formatting Field Name
541 if ($params['field_name'][0] == 'Formatting') {
542 $params['field_name'][1] = 'formatting_' . rand(1000, 9999);
545 //check for duplicate fields
546 if ($params["field_name"][0] != "Formatting" && CRM_Core_BAO_UFField
::duplicateField($params, $ids)) {
547 CRM_Core_Session
::setStatus(ts('The selected field already exists in this profile.'), ts('Field Not Added'), 'error');
551 $params['weight'] = CRM_Core_BAO_UFField
::autoWeight($params);
552 $ufField = CRM_Core_BAO_UFField
::add($params, $ids);
554 //reset other field is searchable and in selector settings, CRM-4363
555 if ($this->_hasSearchableORInSelector
&&
556 in_array($ufField->field_type
, array('Participant', 'Contribution', 'Membership', 'Activity', 'Case'))
558 CRM_Core_BAO_UFField
::resetInSelectorANDSearchable($this->_gid
);
561 $config = CRM_Core_Config
::singleton();
562 $showBestResult = FALSE;
563 if (in_array($ufField->field_name
, array(
566 )) && count($config->countryLimit
) > 1
568 // get state or country field weight if exists
569 $field = 'state_province';
570 if ($ufField->field_name
== 'state_province') {
573 $ufFieldDAO = new CRM_Core_DAO_UFField();
574 $ufFieldDAO->field_name
= $field;
575 $ufFieldDAO->location_type_id
= $ufField->location_type_id
;
576 $ufFieldDAO->uf_group_id
= $ufField->uf_group_id
;
578 if ($ufFieldDAO->find(TRUE)) {
579 if ($field == 'country' && $ufFieldDAO->weight
> $ufField->weight
) {
580 $showBestResult = TRUE;
582 elseif ($field == 'state_province' && $ufFieldDAO->weight
< $ufField->weight
) {
583 $showBestResult = TRUE;
588 //update group_type every time. CRM-3608
589 if ($this->_gid
&& is_a($ufField, 'CRM_Core_DAO_UFField')) {
590 // get the profile type.
591 $fieldsType = CRM_Core_BAO_UFGroup
::calculateGroupType($this->_gid
, TRUE);
592 CRM_Core_BAO_UFGroup
::updateGroupTypes($this->_gid
, $fieldsType);
594 CRM_Core_Session
::setStatus(ts('Your CiviCRM Profile Field \'%1\' has been saved to \'%2\'.',
595 array(1 => $name, 2 => $this->_title
)
596 ), ts('Profile Field Saved'), 'success');
598 $buttonName = $this->controller
->getButtonName();
600 $session = CRM_Core_Session
::singleton();
601 if ($buttonName == $this->getButtonName('next', 'new')) {
602 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/uf/group/field/add',
603 "reset=1&action=add&gid={$this->_gid}&sbr={$showBestResult}"
607 $session->replaceUserContext(CRM_Utils_System
::url('civicrm/admin/uf/group/field',
608 "reset=1&action=browse&gid={$this->_gid}"
610 $session->set('showBestResult', $showBestResult);
615 * Validation rule for subtype.
617 * @param string $fieldType
619 * @param array $groupType
620 * Contains all groupTypes.
621 * @param array $errors
622 * List of errors to be posted back to the form.
624 public static function formRuleSubType($fieldType, $groupType, &$errors) {
625 if (in_array($fieldType, array(
631 $individualSubTypes = CRM_Contact_BAO_ContactType
::subTypes('Individual');
632 foreach ($groupType as $value) {
633 if (!in_array($value, $individualSubTypes) &&
634 !in_array($value, array(
644 $errors['field_name'] = ts('Cannot add or update profile field "%1" with combination of Household or Organization or any subtypes of Household or Organization.', array(1 => $fieldType));
650 $basicType = CRM_Contact_BAO_ContactType
::getBasicType($groupType);
652 if (!is_array($basicType)) {
653 $basicType = array($basicType);
655 if (!in_array($fieldType, $basicType) && $fieldType != 'Contact') {
656 $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of subtype other than "%1".',
657 array(1 => $fieldType)
665 * Validation rule for custom data extends entity column values.
667 * @param Object $customField
671 * @param string $fieldType
672 * Group type of the field.
673 * @param array $errors
677 * list of errors to be posted back to the form
679 public static function formRuleCustomDataExtentColumnValue($customField, $gid, $fieldType, &$errors) {
680 // fix me : check object $customField
681 if (in_array($fieldType, array(
688 $params = array('id' => $customField->custom_group_id
);
689 $customGroup = array();
690 CRM_Core_BAO_CustomGroup
::retrieve($params, $customGroup);
691 if (($fieldType != CRM_Utils_Array
::value('extends', $customGroup)) ||
empty($customGroup['extends_entity_column_value'])) {
695 $extendsColumnValues = array();
696 foreach (explode(CRM_Core_DAO
::VALUE_SEPARATOR
, $customGroup['extends_entity_column_value']) as $val) {
698 $extendsColumnValues[] = $val;
702 if (empty($extendsColumnValues)) {
706 $fieldTypeValues = CRM_Core_BAO_UFGroup
::groupTypeValues($gid, $fieldType);
707 if (empty($fieldTypeValues[$fieldType])) {
711 $disallowedTypes = array_diff($extendsColumnValues, $fieldTypeValues[$fieldType]);
712 if (!empty($disallowedTypes)) {
713 $errors['field_name'] = ts('Profile is already having custom fields extending different group types, you can not add or update this custom field.');
719 * Validation rule to prevent multiple fields of primary location type within the same communication type.
721 * @param array $fields
723 * @param string $profileFieldName
725 * @param array $groupFields
726 * List of fields already in the group.
727 * @param array $errors
731 public static function formRulePrimaryCheck($fields, $profileFieldName, $groupFields, &$errors) {
732 //FIXME: This may need to also apply to website fields if they are refactored to allow more than one per profile
733 $checkPrimary = array('phone' => 'civicrm_phone.phone', 'phone_and_ext' => 'civicrm_phone.phone');
735 $primaryOfSameTypeFound = NULL;
736 $fieldID = empty($fields['field_id']) ?
0 : $fields['field_id'];
737 // Is this a primary location type field of interest
738 if (array_key_exists($profileFieldName, $checkPrimary)) {
739 $whereCheck = $checkPrimary[$profileFieldName];
741 $potentialLocationType = CRM_Utils_Array
::value(2, $fields['field_name']);
743 if ($whereCheck && $potentialLocationType == 0) {
744 $primaryOfSameTypeFound = '';
746 foreach ($groupFields as $groupField) {
748 if ($groupField['where'] == $whereCheck && is_null($groupField['location_type_id']) && $groupField['field_id'] != $fieldID) {
749 $primaryOfSameTypeFound = $groupField['title'];
753 if ($primaryOfSameTypeFound) {
754 $errors['field_name'] = ts('You have already added a primary location field of this type: %1', array(1 => $primaryOfSameTypeFound));
760 * Global validation rules for the form.
762 * @param array $fields
763 * Posted values of the form.
769 * list of errors to be posted back to the form
771 public static function formRule($fields, $files, $self) {
772 $is_required = CRM_Utils_Array
::value('is_required', $fields, FALSE);
773 $is_registration = CRM_Utils_Array
::value('is_registration', $fields, FALSE);
774 $is_view = CRM_Utils_Array
::value('is_view', $fields, FALSE);
775 $in_selector = CRM_Utils_Array
::value('in_selector', $fields, FALSE);
776 $is_active = CRM_Utils_Array
::value('is_active', $fields, FALSE);
779 if ($is_view && $is_registration) {
780 $errors['is_registration'] = ts('View Only cannot be selected if this field is to be included on the registration form');
782 if ($is_view && $is_required) {
783 $errors['is_view'] = ts('A View Only field cannot be required');
786 $entityName = $fields['field_name'][0];
788 $errors['field_name'] = ts('Please select a field name');
791 if ($in_selector && in_array($entityName, array(
798 $errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $entityName));
801 $isCustomField = FALSE;
802 $profileFieldName = CRM_Utils_Array
::value(1, $fields['field_name']);
803 if ($profileFieldName) {
804 //get custom field id
805 $customFieldId = explode('_', $profileFieldName);
806 if ($customFieldId[0] == 'custom') {
807 $customField = new CRM_Core_DAO_CustomField();
808 $customField->id
= $customFieldId[1];
809 $customField->find(TRUE);
810 $isCustomField = TRUE;
811 if (!empty($fields['field_id']) && !$customField->is_active
&& $is_active) {
812 $errors['field_name'] = ts('Cannot set this field "Active" since the selected custom field is disabled.');
815 //check if profile already has a different multi-record custom set field configured
816 $customGroupId = CRM_Core_BAO_CustomField
::isMultiRecordField($profileFieldName);
817 if ($customGroupId) {
818 if ($profileMultiRecordCustomGid = CRM_Core_BAO_UFField
::checkMultiRecordFieldExists($self->_gid
)) {
819 if ($customGroupId != $profileMultiRecordCustomGid) {
820 $errors['field_name'] = ts("You cannot configure multi-record custom fields belonging to different custom sets in one profile");
827 // Get list of fields already in the group
828 $groupFields = CRM_Core_BAO_UFGroup
::getFields($fields['group_id'], FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE);
829 // Check if we already added a primary field of the same communication type
830 self
::formRulePrimaryCheck($fields, $profileFieldName, $groupFields, $errors);
832 //check profile is configured for double option process
833 //adding group field, email field should be present in the group
834 //fixed for issue CRM-2861 & CRM-4153
835 if (CRM_Core_BAO_UFGroup
::isProfileDoubleOptin()) {
836 if (CRM_Utils_Array
::value(1, $fields['field_name']) == 'group') {
837 $dao = new CRM_Core_BAO_UFField();
838 $dao->uf_group_id
= $fields['group_id'];
841 while ($dao->fetch()) {
842 //check email field is present in the group
843 if ($dao->field_name
== 'email') {
850 $disableSettingURL = CRM_Utils_System
::url(
851 'civicrm/admin/setting/preferences/mailing',
855 $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));
861 $fieldType = $fields['field_name'][0];
863 //get the group type.
864 $groupType = CRM_Core_BAO_UFGroup
::calculateGroupType($self->_gid
, FALSE, CRM_Utils_Array
::value('field_id', $fields));
866 switch ($fieldType) {
868 self
::formRuleSubType($fieldType, $groupType, $errors);
872 if (in_array('Activity', $groupType) ||
873 in_array('Household', $groupType) ||
874 in_array('Organization', $groupType)
877 //CRM-7603 - need to support activity + individual.
878 //$errors['field_name'] =
879 //ts( 'Cannot add or update profile field type Individual with combination of Household or Organization or Activity' );
880 if (in_array('Household', $groupType) ||
881 in_array('Organization', $groupType)
883 $errors['field_name'] = ts('Cannot add or update profile field type Individual with combination of Household or Organization');
887 self
::formRuleSubType($fieldType, $groupType, $errors);
892 if (in_array('Activity', $groupType) ||
in_array('Individual', $groupType) ||
in_array('Organization', $groupType)) {
893 $errors['field_name'] = ts('Cannot add or update profile field type Household with combination of Individual or Organization or Activity');
896 self
::formRuleSubType($fieldType, $groupType, $errors);
901 if (in_array('Activity', $groupType) ||
in_array('Household', $groupType) ||
in_array('Individual', $groupType)) {
902 $errors['field_name'] = ts('Cannot add or update profile field type Organization with combination of Household or Individual or Activity');
905 self
::formRuleSubType($fieldType, $groupType, $errors);
910 if (in_array('Individual', $groupType) ||
911 in_array('Membership', $groupType) ||
912 in_array('Contribution', $groupType) ||
913 in_array('Organization', $groupType) ||
914 in_array('Household', $groupType) ||
915 in_array('Participant', $groupType)
918 //CRM-7603 - need to support activity + contact type.
919 //$errors['field_name'] =
920 //ts( 'Cannot add or update profile field type Activity with combination Participant or Membership or Contribution or Household or Organization or Individual' );
921 if (in_array('Membership', $groupType) ||
922 in_array('Contribution', $groupType) ||
923 in_array('Participant', $groupType)
925 $errors['field_name'] = ts('Cannot add or update profile field type Activity with combination Participant or Membership or Contribution');
929 self
::formRuleSubType($fieldType, $groupType, $errors);
932 if ($isCustomField && !isset($errors['field_name'])) {
933 self
::formRuleCustomDataExtentColumnValue($customField, $self->_gid
, $fieldType, $errors);
938 if (in_array('Membership', $groupType) ||
in_array('Contribution', $groupType)
939 ||
in_array('Organization', $groupType) ||
in_array('Household', $groupType) ||
in_array('Activity', $groupType)
941 $errors['field_name'] = ts('Cannot add or update profile field type Participant with combination of Activity or Membership or Contribution or Household or Organization.');
944 self
::formRuleSubType($fieldType, $groupType, $errors);
949 //special case where in we allow contribution + oganization fields, for on behalf feature
950 $profileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup',
951 'on_behalf_organization', 'id', 'name'
954 if (in_array('Participant', $groupType) ||
in_array('Membership', $groupType)
955 ||
($profileId != $self->_gid
&& in_array('Organization', $groupType)) ||
in_array('Household', $groupType) ||
in_array('Activity', $groupType)
957 $errors['field_name'] = ts('Cannot add or update profile field type Contribution with combination of Activity or Membership or Participant or Household or Organization');
960 self
::formRuleSubType($fieldType, $groupType, $errors);
965 //special case where in we allow contribution + oganization fields, for on behalf feature
966 $profileId = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_UFGroup',
967 'on_behalf_organization', 'id', 'name'
970 if (in_array('Participant', $groupType) ||
in_array('Contribution', $groupType)
971 ||
($profileId != $self->_gid
&& in_array('Organization', $groupType)) ||
in_array('Household', $groupType) ||
in_array('Activity', $groupType)
973 $errors['field_name'] = ts('Cannot add or update profile field type Membership with combination of Activity or Participant or Contribution or Household or Organization');
976 self
::formRuleSubType($fieldType, $groupType, $errors);
981 $profileType = CRM_Core_BAO_UFField
::getProfileType($fields['group_id'], TRUE, FALSE, TRUE);
982 if (CRM_Contact_BAO_ContactType
::isaSubType($fieldType)) {
983 if (CRM_Contact_BAO_ContactType
::isaSubType($profileType)) {
984 if ($fieldType != $profileType) {
985 $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(
992 $basicType = CRM_Contact_BAO_ContactType
::getBasicType($fieldType);
994 $profileType != $basicType &&
995 $profileType != 'Contact'
997 $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(
1005 CRM_Utils_Array
::value(1, $fields['field_name']) == 'contact_sub_type' &&
1006 !in_array($profileType, array('Individual', 'Household', 'Organization')) &&
1007 !in_array($profileType, CRM_Contact_BAO_ContactType
::subTypes())
1009 $errors['field_name'] = ts('Cannot add or update profile field Contact Subtype as profile type is not one of Individual, Household or Organization.');
1012 return empty($errors) ?
TRUE : $errors;