From: Lola Slade Date: Wed, 30 Apr 2014 01:31:35 +0000 (-0700) Subject: CRM-14470 - Prevent users from adding two fields of the same field type X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=b29a9e66a180e2e9c96dc1d9cc73f0d046ab3986;p=civicrm-core.git CRM-14470 - Prevent users from adding two fields of the same field type (eg. phone) with location type 'is_primary'. --- diff --git a/CRM/UF/Form/Field.php b/CRM/UF/Form/Field.php index c2882867b0..12d848060f 100644 --- a/CRM/UF/Form/Field.php +++ b/CRM/UF/Form/Field.php @@ -713,6 +713,43 @@ class CRM_UF_Form_Field extends CRM_Core_Form { } } + /** + * validation rule to prevent multiple fields of primary location type and the same communication type. + * + * @param Array $fields Submitted fields + * @param String $profileFieldName Group Id. + * @param Array $groupFields List of fields already in the group + * @param Array $errors Collect errors + * + * @static + * @access public + */ + static function formRulePrimaryCheck($fields, $profileFieldName, $groupFields, &$errors) { + //FIXME: This may need to also apply to website fields if they are refactored to allow more than one per profile + $checkPrimary = array('phone' => 'civicrm_phone.phone', 'phone_and_ext' => 'civicrm_phone.phone'); + $whereCheck = NULL; + $primaryOfSameTypeFound = NULL; + // Is this a primary location type field of interest + if (array_key_exists($profileFieldName, $checkPrimary)) { + $whereCheck = $checkPrimary[$profileFieldName]; + } + $potentialLocationType = CRM_Utils_Array::value(2, $fields['field_name']); + + if ($whereCheck && $potentialLocationType == 0) { + $primaryOfSameTypeFound = ''; + + foreach ($groupFields as $groupField) { + // if it is a phone + if ($groupField['where'] == $whereCheck && is_null($groupField['location_type_id'])) { + $primaryOfSameTypeFound = $groupField['title']; + } + } + if ($primaryOfSameTypeFound) { + $errors['field_name'] = ts('You have already added a primary location field of this type: %1', $primaryOfSameTypeFound); + } + } + } + /** * global validation rules for the form * @@ -737,14 +774,14 @@ class CRM_UF_Form_Field extends CRM_Core_Form { $errors['is_view'] = ts('A View Only field cannot be required'); } - $fieldName = $fields['field_name'][0]; - if (!$fieldName) { + $entityName = $fields['field_name'][0]; + if (!$entityName) { $errors['field_name'] = ts('Please select a field name'); } - if ($in_selector && in_array($fieldName, array( + if ($in_selector && in_array($entityName, array( 'Contribution', 'Participant', 'Membership', 'Activity'))) { - $errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $fieldName)); + $errors['in_selector'] = ts("'In Selector' cannot be checked for %1 fields.", array(1 => $entityName)); } $isCustomField = FALSE; @@ -773,6 +810,13 @@ class CRM_UF_Form_Field extends CRM_Core_Form { } } + // Get list of fields already in the group + $groupFields = CRM_Core_BAO_UFGroup::getFields($fields['group_id'], FALSE, NULL, NULL, NULL, TRUE, NULL, TRUE); + // Check if we already added a primary field of the same communication type + if (empty($fields['field_id'])) { + self::formRulePrimaryCheck($fields, $profileFieldName, $groupFields, $errors); + } + //check profile is configured for double option process //adding group field, email field should be present in the group //fixed for issue CRM-2861 & CRM-4153 diff --git a/templates/CRM/UF/Form/Field.hlp b/templates/CRM/UF/Form/Field.hlp index 817e66aefb..de20114e61 100644 --- a/templates/CRM/UF/Form/Field.hlp +++ b/templates/CRM/UF/Form/Field.hlp @@ -1,3 +1,18 @@ +{htxt id='field_name_0-title'} +{ts}Field Name{/ts} +{/htxt} +{htxt id='field_name_0'} + {capture assign="ltURL"}{crmURL p="civicrm/admin/locationType" q="reset=1"}{/capture} +

+ {ts}Choose the field to add to the profile.{/ts}
+ {ts}Restrictions:{/ts}
+ {ts}Only one field of each communication type (EG. phone) can use the primary location type.{/ts} +

+

+ {ts 1=$ltURL}When you choose the primary location type the collected data will use the default location type from the Location types setup.{/ts} +

+{/htxt} + {htxt id='label-title'} {ts}Field Label{/ts} {/htxt} diff --git a/templates/CRM/UF/Form/Field.tpl b/templates/CRM/UF/Form/Field.tpl index 5863edb675..7b6d37c624 100644 --- a/templates/CRM/UF/Form/Field.tpl +++ b/templates/CRM/UF/Form/Field.tpl @@ -34,7 +34,7 @@
{include file="CRM/common/formButtons.tpl" location="top"}
- +
{$form.field_name.label}{$form.field_name.label} {help id='field_name_0'} {$form.field_name.html}
 {ts}Select the type of CiviCRM record and the field you want to include in this Profile.{/ts}