Example #1
0
 /**
  * Global validation rules for the form.
  *
  * @param array $fields
  *   Posted values of the form.
  *
  * @param $files
  * @param $self
  *
  * @return array
  *   list of errors to be posted back to the form
  */
 public static function formRule($fields, $files, $self)
 {
     $is_required = CRM_Utils_Array::value('is_required', $fields, FALSE);
     $is_registration = CRM_Utils_Array::value('is_registration', $fields, FALSE);
     $is_view = CRM_Utils_Array::value('is_view', $fields, FALSE);
     $in_selector = CRM_Utils_Array::value('in_selector', $fields, FALSE);
     $is_active = CRM_Utils_Array::value('is_active', $fields, FALSE);
     $errors = array();
     if ($is_view && $is_registration) {
         $errors['is_registration'] = ts('View Only cannot be selected if this field is to be included on the registration form');
     }
     if ($is_view && $is_required) {
         $errors['is_view'] = ts('A View Only field cannot be required');
     }
     $entityName = $fields['field_name'][0];
     if (!$entityName) {
         $errors['field_name'] = ts('Please select a field name');
     }
     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 => $entityName));
     }
     $isCustomField = FALSE;
     $profileFieldName = CRM_Utils_Array::value(1, $fields['field_name']);
     if ($profileFieldName) {
         //get custom field id
         $customFieldId = explode('_', $profileFieldName);
         if ($customFieldId[0] == 'custom') {
             $customField = new CRM_Core_DAO_CustomField();
             $customField->id = $customFieldId[1];
             $customField->find(TRUE);
             $isCustomField = TRUE;
             if (!empty($fields['field_id']) && !$customField->is_active && $is_active) {
                 $errors['field_name'] = ts('Cannot set this field "Active" since the selected custom field is disabled.');
             }
             //check if profile already has a different multi-record custom set field configured
             $customGroupId = CRM_Core_BAO_CustomField::isMultiRecordField($profileFieldName);
             if ($customGroupId) {
                 if ($profileMultiRecordCustomGid = CRM_Core_BAO_UFField::checkMultiRecordFieldExists($self->_gid)) {
                     if ($customGroupId != $profileMultiRecordCustomGid) {
                         $errors['field_name'] = ts("You cannot configure multi-record custom fields belonging to different custom sets in one profile");
                     }
                 }
             }
         }
     }
     // 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
     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
     if (CRM_Core_BAO_UFGroup::isProfileDoubleOptin()) {
         if (CRM_Utils_Array::value(1, $fields['field_name']) == 'group') {
             $dao = new CRM_Core_BAO_UFField();
             $dao->uf_group_id = $fields['group_id'];
             $dao->find();
             $emailField = FALSE;
             while ($dao->fetch()) {
                 //check email field is present in the group
                 if ($dao->field_name == 'email') {
                     $emailField = TRUE;
                     break;
                 }
             }
             if (!$emailField) {
                 $disableSettingURL = CRM_Utils_System::url('civicrm/admin/setting/preferences/mailing', 'reset=1');
                 $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));
             }
         }
     }
     //fix for CRM-3037
     $fieldType = $fields['field_name'][0];
     //get the group type.
     $groupType = CRM_Core_BAO_UFGroup::calculateGroupType($self->_gid, FALSE, CRM_Utils_Array::value('field_id', $fields));
     switch ($fieldType) {
         case 'Contact':
             self::formRuleSubType($fieldType, $groupType, $errors);
             break;
         case 'Individual':
             if (in_array('Activity', $groupType) || in_array('Household', $groupType) || in_array('Organization', $groupType)) {
                 //CRM-7603 - need to support activity + individual.
                 //$errors['field_name'] =
                 //ts( 'Cannot add or update profile field type Individual with combination of Household or Organization or Activity' );
                 if (in_array('Household', $groupType) || in_array('Organization', $groupType)) {
                     $errors['field_name'] = ts('Cannot add or update profile field type Individual with combination of Household or Organization');
                 }
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             break;
         case 'Household':
             if (in_array('Activity', $groupType) || in_array('Individual', $groupType) || in_array('Organization', $groupType)) {
                 $errors['field_name'] = ts('Cannot add or update profile field type Household with combination of Individual or Organization or Activity');
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             break;
         case 'Organization':
             if (in_array('Activity', $groupType) || in_array('Household', $groupType) || in_array('Individual', $groupType)) {
                 $errors['field_name'] = ts('Cannot add or update profile field type Organization with combination of Household or Individual or Activity');
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             break;
         case 'Activity':
             if (in_array('Individual', $groupType) || in_array('Membership', $groupType) || in_array('Contribution', $groupType) || in_array('Organization', $groupType) || in_array('Household', $groupType) || in_array('Participant', $groupType)) {
                 //CRM-7603 - need to support activity + contact type.
                 //$errors['field_name'] =
                 //ts( 'Cannot add or update profile field type Activity with combination Participant or Membership or Contribution or Household or Organization or Individual' );
                 if (in_array('Membership', $groupType) || in_array('Contribution', $groupType) || in_array('Participant', $groupType)) {
                     $errors['field_name'] = ts('Cannot add or update profile field type Activity with combination Participant or Membership or Contribution');
                 }
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             if ($isCustomField && !isset($errors['field_name'])) {
                 self::formRuleCustomDataExtentColumnValue($customField, $self->_gid, $fieldType, $errors);
             }
             break;
         case 'Participant':
             if (in_array('Membership', $groupType) || in_array('Contribution', $groupType) || in_array('Organization', $groupType) || in_array('Household', $groupType) || in_array('Activity', $groupType)) {
                 $errors['field_name'] = ts('Cannot add or update profile field type Participant with combination of Activity or Membership or Contribution or Household or Organization.');
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             break;
         case 'Contribution':
             //special case where in we allow contribution + oganization fields, for on behalf feature
             $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'on_behalf_organization', 'id', 'name');
             if (in_array('Participant', $groupType) || in_array('Membership', $groupType) || $profileId != $self->_gid && in_array('Organization', $groupType) || in_array('Household', $groupType) || in_array('Activity', $groupType)) {
                 $errors['field_name'] = ts('Cannot add or update profile field type Contribution with combination of Activity or Membership or Participant or Household or Organization');
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             break;
         case 'Membership':
             //special case where in we allow contribution + oganization fields, for on behalf feature
             $profileId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_UFGroup', 'on_behalf_organization', 'id', 'name');
             if (in_array('Participant', $groupType) || in_array('Contribution', $groupType) || $profileId != $self->_gid && in_array('Organization', $groupType) || in_array('Household', $groupType) || in_array('Activity', $groupType)) {
                 $errors['field_name'] = ts('Cannot add or update profile field type Membership with combination of Activity or Participant or Contribution or Household or Organization');
             } else {
                 self::formRuleSubType($fieldType, $groupType, $errors);
             }
             break;
         default:
             $profileType = CRM_Core_BAO_UFField::getProfileType($fields['group_id'], TRUE, FALSE, TRUE);
             if (CRM_Contact_BAO_ContactType::isaSubType($fieldType)) {
                 if (CRM_Contact_BAO_ContactType::isaSubType($profileType)) {
                     if ($fieldType != $profileType) {
                         $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(1 => $fieldType, 2 => $profileType));
                     }
                 } else {
                     $basicType = CRM_Contact_BAO_ContactType::getBasicType($fieldType);
                     if ($profileType && $profileType != $basicType && $profileType != 'Contact') {
                         $errors['field_name'] = ts('Cannot add or update profile field type "%1" with combination of "%2".', array(1 => $fieldType, 2 => $profileType));
                     }
                 }
             } elseif (CRM_Utils_Array::value(1, $fields['field_name']) == 'contact_sub_type' && !in_array($profileType, array('Individual', 'Household', 'Organization')) && !in_array($profileType, CRM_Contact_BAO_ContactType::subTypes())) {
                 $errors['field_name'] = ts('Cannot add or update profile field Contact Subtype as profile type is not one of Individual, Household or Organization.');
             }
     }
     return empty($errors) ? TRUE : $errors;
 }