Ejemplo n.º 1
0
 /**
  * global validation rules for the form
  *
  * @param array $fields posted values of the form
  *
  * @return array list of errors to be posted back to the form
  * @static
  * @access public
  */
 static function formRule(&$values, $files, $self)
 {
     // If $values['_qf_Participant_next'] is Delete or
     // $values['event_id'] is empty, then return
     // instead of proceeding further.
     if ($values['_qf_Participant_next'] == 'Delete' || !$values['event_id']) {
         return true;
     }
     //check if contact is selected in standalone mode
     if (isset($values['contact_select_id']) && !$values['contact_select_id']) {
         $errorMsg['contact'] = ts('Please select a valid contact or create new contact');
     }
     if (CRM_Utils_Array::value('payment_processor_id', $values)) {
         // make sure that credit card number and cvv are valid
         require_once 'CRM/Utils/Rule.php';
         if (CRM_Utils_Array::value('credit_card_type', $values)) {
             if (CRM_Utils_Array::value('credit_card_number', $values) && !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])) {
                 $errorMsg['credit_card_number'] = ts("Please enter a valid Credit Card Number");
             }
             if (CRM_Utils_Array::value('cvv2', $values) && !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type'])) {
                 $errorMsg['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
             }
         }
     }
     if (CRM_Utils_Array::value('record_contribution', $values) && !CRM_Utils_Array::value('contribution_type_id', $values)) {
         $errorMsg['contribution_type_id'] = ts("Please enter the associated Contribution Type");
     }
     // validate contribution status for 'Failed'.
     if ($self->_onlinePendingContributionId && CRM_Utils_Array::value('record_contribution', $values) && CRM_Utils_Array::value('contribution_status_id', $values) == array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(null, 'name'))) {
         $errorMsg['contribution_status_id'] = ts("Please select a valid payment status before updating.");
     }
     if ($self->_mode && CRM_Utils_Array::value('priceSetId', $values) && !CRM_Utils_System::isNull($self->_values['fee']['fields'])) {
         $lineItem = array();
         require_once "CRM/Price/BAO/Set.php";
         CRM_Price_BAO_Set::processAmount($self->_values['fee']['fields'], $values, $lineItem);
         if ($values['amount'] < 0) {
             $errorMsg['_qf_default'] = ts("Event Fee(s) can not be less than zero. Please select the options accordingly");
         }
     }
     return empty($errorMsg) ? true : $errorMsg;
 }
Ejemplo n.º 2
0
 /**
  * Make sure that credit card number and cvv are valid.
  * Called within the scope of a QF formRule function
  *
  * @param array $values
  * @param array $errors
  */
 public static function validateCreditCard($values, &$errors)
 {
     if (!empty($values['credit_card_type']) || !empty($values['credit_card_number'])) {
         if (!empty($values['credit_card_number']) && !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])) {
             $errors['credit_card_number'] = ts('Please enter a valid Card Number');
         }
         if (!empty($values['cvv2']) && !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type'])) {
             $errors['cvv2'] = ts('Please enter a valid Card Verification Number');
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * global validation rules for the form
  *
  * @param array $fields posted values of the form
  *
  * @return array list of errors to be posted back to the form
  * @static
  * @access public
  */
 static function formRule($values, $files, $self)
 {
     // If $values['_qf_Participant_next'] is Delete or
     // $values['event_id'] is empty, then return
     // instead of proceeding further.
     if (CRM_Utils_Array::value('_qf_Participant_next', $values) == 'Delete' || !$values['event_id']) {
         return true;
     }
     //check if contact is selected in standalone mode
     if (isset($values['contact_select_id'][1]) && !$values['contact_select_id'][1]) {
         $errorMsg['contact[1]'] = ts('Please select a contact or create new contact');
     }
     if (CRM_Utils_Array::value('payment_processor_id', $values)) {
         // make sure that credit card number and cvv are valid
         require_once 'CRM/Utils/Rule.php';
         if (CRM_Utils_Array::value('credit_card_type', $values)) {
             if (CRM_Utils_Array::value('credit_card_number', $values) && !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])) {
                 $errorMsg['credit_card_number'] = ts("Please enter a valid Credit Card Number");
             }
             if (CRM_Utils_Array::value('cvv2', $values) && !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type'])) {
                 $errorMsg['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
             }
         }
     }
     if (CRM_Utils_Array::value('record_contribution', $values) && !CRM_Utils_Array::value('contribution_type_id', $values)) {
         $errorMsg['contribution_type_id'] = ts("Please enter the associated Contribution Type");
     }
     // validate contribution status for 'Failed'.
     if ($self->_onlinePendingContributionId && CRM_Utils_Array::value('record_contribution', $values) && CRM_Utils_Array::value('contribution_status_id', $values) == array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(null, 'name'))) {
         $errorMsg['contribution_status_id'] = ts("Please select a valid payment status before updating.");
     }
     // do the amount validations.
     //skip for update mode since amount is freeze, CRM-6052
     if (!$self->_id && !CRM_Utils_Array::value('total_amount', $values) && empty($self->_values['line_items']) || $self->_id && !$self->_paymentId && is_array($self->_values['line_items'])) {
         if ($priceSetId = CRM_Utils_Array::value('priceSetId', $values)) {
             require_once 'CRM/Price/BAO/Field.php';
             CRM_Price_BAO_Field::priceSetValidation($priceSetId, $values, $errorMsg);
         }
     }
     return CRM_Utils_Array::crmIsEmptyArray($errorMsg) ? true : $errorMsg;
 }
 /**
  * Function for validation
  *
  * @param array $params (ref.) an assoc array of name/value pairs
  *
  * @return mixed true or array of errors
  * @access public
  * @static
  */
 public function formRule($params, $files, $self)
 {
     $errors = array();
     $priceSetId = CRM_Utils_Array::value('price_set_id', $params);
     if ($priceSetId) {
         CRM_Price_BAO_Field::priceSetValidation($priceSetId, $params, $errors);
         $priceFieldIDS = array();
         foreach ($self->_priceSet['fields'] as $priceIds => $dontCare) {
             if (!empty($params['price_' . $priceIds])) {
                 if (is_array($params['price_' . $priceIds])) {
                     foreach ($params['price_' . $priceIds] as $priceFldVal => $isSet) {
                         if ($isSet) {
                             $priceFieldIDS[] = $priceFldVal;
                         }
                     }
                 } else {
                     $priceFieldIDS[] = $params['price_' . $priceIds];
                 }
             }
         }
         if (!empty($priceFieldIDS)) {
             $ids = implode(',', $priceFieldIDS);
             $count = CRM_Price_BAO_Set::getMembershipCount($ids);
             foreach ($count as $id => $occurance) {
                 if ($occurance > 1) {
                     $errors['_qf_default'] = ts('Select at most one option associated with the same membership type.');
                 }
             }
             foreach ($priceFieldIDS as $priceFieldId) {
                 if ($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_FieldValue', $priceFieldId, 'membership_type_id')) {
                     $self->_memTypeSelected[$id] = $id;
                 }
             }
         }
     } elseif (!CRM_Utils_Array::value(1, $params['membership_type_id'])) {
         $errors['membership_type_id'] = ts('Please select a membership type.');
     } else {
         $self->_memTypeSelected[] = $params['membership_type_id'][1];
     }
     // Return error if empty $self->_memTypeSelected
     if ($priceSetId && empty($errors) && empty($self->_memTypeSelected)) {
         $errors['_qf_default'] = ts('Select at least one membership option.');
     }
     if (!empty($errors) && count($self->_memTypeSelected) > 1) {
         $memberOfContacts = CRM_Member_BAO_MembershipType::getMemberOfContactByMemTypes($self->_memTypeSelected);
         $duplicateMemberOfContacts = array_count_values($memberOfContacts);
         foreach ($duplicateMemberOfContacts as $countDuplicate) {
             if ($countDuplicate > 1) {
                 $errors['_qf_default'] = ts('Please do not select more than one membership associated with the same organization.');
             }
         }
     }
     //check if contact is selected in standalone mode
     if (isset($params['contact_select_id'][1]) && !$params['contact_select_id'][1]) {
         $errors['contact[1]'] = ts('Please select a contact or create new contact');
     }
     if (!empty($errors)) {
         return $errors;
     }
     if ($priceSetId && !$self->_mode && !CRM_Utils_Array::value('record_contribution', $params)) {
         $errors['record_contribution'] = ts('Record Membership Payment is required when you using price set.');
     }
     if (CRM_Utils_Array::value('payment_processor_id', $params)) {
         // make sure that credit card number and cvv are valid
         if (CRM_Utils_Array::value('credit_card_type', $params)) {
             if (CRM_Utils_Array::value('credit_card_number', $params) && !CRM_Utils_Rule::creditCardNumber($params['credit_card_number'], $params['credit_card_type'])) {
                 $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number");
             }
             if (CRM_Utils_Array::value('cvv2', $params) && !CRM_Utils_Rule::cvv($params['cvv2'], $params['credit_card_type'])) {
                 $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
             }
         }
     }
     $joinDate = NULL;
     if (CRM_Utils_Array::value('join_date', $params)) {
         $joinDate = CRM_Utils_Date::processDate($params['join_date']);
         foreach ($self->_memTypeSelected as $memType) {
             $startDate = NULL;
             if (CRM_Utils_Array::value('start_date', $params)) {
                 $startDate = CRM_Utils_Date::processDate($params['start_date']);
             }
             // if end date is set, ensure that start date is also set
             // and that end date is later than start date
             // If selected membership type has duration unit as 'lifetime'
             // and end date is set, then give error
             $endDate = NULL;
             if (CRM_Utils_Array::value('end_date', $params)) {
                 $endDate = CRM_Utils_Date::processDate($params['end_date']);
             }
             $membershipDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($memType);
             if ($startDate && CRM_Utils_Array::value('period_type', $membershipDetails) == 'rolling') {
                 if ($startDate < $joinDate) {
                     $errors['start_date'] = ts('Start date must be the same or later than Member since.');
                 }
             }
             if ($endDate) {
                 if ($membershipDetails['duration_unit'] == 'lifetime') {
                     $errors['end_date'] = ts('The selected Membership Type has a lifetime duration. You cannot specify an End Date for lifetime memberships. Please clear the End Date OR select a different Membership Type.');
                 } else {
                     if (!$startDate) {
                         $errors['start_date'] = ts('Start date must be set if end date is set.');
                     }
                     if ($endDate < $startDate) {
                         $errors['end_date'] = ts('End date must be the same or later than start date.');
                     }
                 }
             }
             //  Default values for start and end dates if not supplied
             //  on the form
             $defaultDates = CRM_Member_BAO_MembershipType::getDatesForMembershipType($memType, $joinDate, $startDate, $endDate);
             if (!$startDate) {
                 $startDate = CRM_Utils_Array::value('start_date', $defaultDates);
             }
             if (!$endDate) {
                 $endDate = CRM_Utils_Array::value('end_date', $defaultDates);
             }
             //CRM-3724, check for availability of valid membership status.
             if (!CRM_Utils_Array::value('is_override', $params) && !isset($errors['_qf_default'])) {
                 $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate, $endDate, $joinDate, 'today', TRUE);
                 if (empty($calcStatus)) {
                     $url = CRM_Utils_System::url('civicrm/admin/member/membershipStatus', 'reset=1&action=browse');
                     $errors['_qf_default'] = ts('There is no valid Membership Status available for selected membership dates.');
                     $status = ts('Oops, it looks like there is no valid membership status available for the given membership dates. You can <a href="%1">Configure Membership Status Rules</a>.', array(1 => $url));
                     if (!$self->_mode) {
                         $status .= ' ' . ts('OR You can sign up by setting Status Override? to true.');
                     }
                     CRM_Core_Session::setStatus($status);
                 }
             }
         }
     } else {
         $errors['join_date'] = ts('Please enter the Member Since.');
     }
     if (isset($params['is_override']) && $params['is_override'] && !CRM_Utils_Array::value('status_id', $params)) {
         $errors['status_id'] = ts('Please enter the status.');
     }
     //total amount condition arise when membership type having no
     //minimum fee
     if (isset($params['record_contribution'])) {
         if (!$params['contribution_type_id']) {
             $errors['contribution_type_id'] = ts('Please enter the contribution Type.');
         }
         if (CRM_Utils_System::isNull($params['total_amount'])) {
             $errors['total_amount'] = ts('Please enter the contribution.');
         }
     }
     // validate contribution status for 'Failed'.
     if ($self->_onlinePendingContributionId && CRM_Utils_Array::value('record_contribution', $params) && CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'))) {
         $errors['contribution_status_id'] = ts("Please select a valid payment status before updating.");
     }
     return empty($errors) ? TRUE : $errors;
 }
 function validatePaymentValues($self, $fields)
 {
     if (CRM_Utils_Array::value('bypass_payment', $self->_params[0]) || $self->_allowWaitlist || empty($self->_fields) || CRM_Utils_Array::value('amount', $self->_params[0]) > 0) {
         return true;
     }
     $validatePayement = false;
     if (CRM_Utils_Array::value('priceSetId', $fields)) {
         require_once 'CRM/Price/BAO/Set.php';
         $lineItem = array();
         CRM_Price_BAO_Set::processAmount($self->_values['fee'], $fields, $lineItem);
         if ($fields['amount'] > 0) {
             $validatePayement = true;
             // $self->_forcePayement = true;
             // return false;
         }
     } else {
         if (CRM_Utils_Array::value('amount', $fields) && CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) > 0) {
             $validatePayement = true;
         }
     }
     if (!$validatePayement) {
         return true;
     }
     foreach ($self->_fields as $name => $fld) {
         if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
             return false;
         }
     }
     // make sure that credit card number and cvv are valid
     require_once 'CRM/Utils/Rule.php';
     require_once 'CRM/Core/OptionGroup.php';
     if (CRM_Utils_Array::value('credit_card_type', $self->_params[0])) {
         if (CRM_Utils_Array::value('credit_card_number', $self->_params[0]) && !CRM_Utils_Rule::creditCardNumber($self->_params[0]['credit_card_number'], $self->_params[0]['credit_card_type'])) {
             return false;
         }
         if (CRM_Utils_Array::value('cvv2', $self->_params[0]) && !CRM_Utils_Rule::cvv($self->_params[0]['cvv2'], $self->_params[0]['credit_card_type'])) {
             return false;
         }
     }
     $elements = array('email_greeting' => 'email_greeting_custom', 'postal_greeting' => 'postal_greeting_custom', 'addressee' => 'addressee_custom');
     foreach ($elements as $greeting => $customizedGreeting) {
         if ($greetingType = CRM_Utils_Array::value($greeting, $self->_params[0])) {
             $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
             if ($customizedValue == $greetingType && !CRM_Utils_Array::value($customizedGreeting, $self->_params[0])) {
                 return false;
             }
         }
     }
 }
 /**
  * global form rule
  *
  * @param array $fields  the input form values
  * @param array $files   the uploaded files if any
  * @param array $options additional user data
  *
  * @return true if no errors, else array of errors
  * @access public
  * @static
  */
 static function formRule($fields, $files, $self)
 {
     $errors = array();
     //check that either an email or firstname+lastname is included in the form(CRM-9587)
     self::checkProfileComplete($fields, $errors, $self->_eventId);
     //To check if the user is already registered for the event(CRM-2426)
     if (!$self->_skipDupeRegistrationCheck) {
         $self->checkRegistration($fields, $self);
     }
     //check for availability of registrations.
     if (!$self->_allowConfirmation && !CRM_Utils_Array::value('bypass_payment', $fields) && is_numeric($self->_availableRegistrations) && CRM_Utils_Array::value('additional_participants', $fields) >= $self->_availableRegistrations) {
         $errors['additional_participants'] = ts("There is only enough space left on this event for %1 participant(s).", array(1 => $self->_availableRegistrations));
     }
     // during confirmation don't allow to increase additional participants, CRM-4320
     if ($self->_allowConfirmation && CRM_Utils_Array::value('additional_participants', $fields) && is_array($self->_additionalParticipantIds) && $fields['additional_participants'] > count($self->_additionalParticipantIds)) {
         $errors['additional_participants'] = ts("Oops. It looks like you are trying to increase the number of additional people you are registering for. You can confirm registration for a maximum of %1 additional people.", array(1 => count($self->_additionalParticipantIds)));
     }
     //don't allow to register w/ waiting if enough spaces available.
     if (CRM_Utils_Array::value('bypass_payment', $fields)) {
         if (!is_numeric($self->_availableRegistrations) || !CRM_Utils_Array::value('priceSetId', $fields) && CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations) {
             $errors['bypass_payment'] = ts("Oops. There are enough available spaces in this event. You can not add yourself to the waiting list.");
         }
     }
     if (CRM_Utils_Array::value('additional_participants', $fields) && !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])) {
         $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
     }
     // priceset validations
     if (CRM_Utils_Array::value('priceSetId', $fields)) {
         //format params.
         $formatted = self::formatPriceSetParams($self, $fields);
         $ppParams = array($formatted);
         $priceSetErrors = self::validatePriceSet($self, $ppParams);
         $primaryParticipantCount = self::getParticipantCount($self, $ppParams);
         //get price set fields errors in.
         $errors = array_merge($errors, CRM_Utils_Array::value(0, $priceSetErrors, array()));
         $totalParticipants = $primaryParticipantCount;
         if (CRM_Utils_Array::value('additional_participants', $fields)) {
             $totalParticipants += $fields['additional_participants'];
         }
         if (!CRM_Utils_Array::value('bypass_payment', $fields) && !$self->_allowConfirmation && is_numeric($self->_availableRegistrations) && $self->_availableRegistrations < $totalParticipants) {
             $errors['_qf_default'] = ts("Only %1 Registrations available.", array(1 => $self->_availableRegistrations));
         }
         $lineItem = array();
         CRM_Price_BAO_Set::processAmount($self->_values['fee'], $fields, $lineItem);
         if ($fields['amount'] < 0) {
             $errors['_qf_default'] = ts('Event Fee(s) can not be less than zero. Please select the options accordingly');
         }
     }
     if ($self->_values['event']['is_monetary']) {
         if (is_array($self->_paymentProcessor)) {
             $payment = CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor, $this);
             $error = $payment->checkConfig($self->_mode);
             if ($error) {
                 $errors['_qf_default'] = $error;
             }
         }
         // return if this is express mode
         $config = CRM_Core_Config::singleton();
         if ($self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
             if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) {
                 return empty($errors) ? TRUE : $errors;
             }
         }
         $isZeroAmount = $skipPayementValidation = FALSE;
         if (CRM_Utils_Array::value('priceSetId', $fields)) {
             if (CRM_Utils_Array::value('amount', $fields) == 0) {
                 $isZeroAmount = TRUE;
             }
         } elseif (CRM_Utils_Array::value('amount', $fields) && (isset($self->_values['discount'][$fields['amount']]) && CRM_Utils_Array::value('value', $self->_values['discount'][$fields['amount']]) == 0)) {
             $isZeroAmount = TRUE;
         } elseif (CRM_Utils_Array::value('amount', $fields) && (isset($self->_values['fee'][$fields['amount']]) && CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) == 0)) {
             $isZeroAmount = TRUE;
         }
         if ($isZeroAmount && !($self->_forcePayement && CRM_Utils_Array::value('additional_participants', $fields))) {
             $skipPayementValidation = TRUE;
         }
         // also return if paylater mode or zero fees for valid members
         if (CRM_Utils_Array::value('is_pay_later', $fields) || CRM_Utils_Array::value('bypass_payment', $fields) || $skipPayementValidation || !$self->_allowConfirmation && ($self->_requireApproval || $self->_allowWaitlist)) {
             return empty($errors) ? TRUE : $errors;
         }
         if (property_exists($self, '_paymentFields')) {
             foreach ($self->_paymentFields as $name => $fld) {
                 if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
                     $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
                 }
             }
         }
     }
     // make sure that credit card number and cvv are valid
     if (CRM_Utils_Array::value('credit_card_type', $fields)) {
         if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
             $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number');
         }
         if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
             $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number');
         }
     }
     foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
         if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
             $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
             if ($customizedValue == $greetingType && empty($fields[$greeting . '_custom'])) {
                 $errors[$customizedGreeting] = ts('Custom %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', ' ', $greeting))));
             }
         }
     }
     return empty($errors) ? TRUE : $errors;
 }
 /**
  * global form rule
  *
  * @param array $fields  the input form values
  * @param array $files   the uploaded files if any
  * @param array $options additional user data
  *
  * @return true if no errors, else array of errors
  * @access public
  * @static
  */
 static function formRule($fields, $files, $self)
 {
     foreach ($self->_fields as $name => $fld) {
         if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
             $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
         }
     }
     // make sure that credit card number and cvv are valid
     if (CRM_Utils_Array::value('credit_card_type', $fields)) {
         if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
             $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number');
         }
         if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
             $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number');
         }
     }
     return empty($errors) ? TRUE : $errors;
 }
Ejemplo n.º 8
0
 /**
  * global form rule
  *
  * @param array $fields  the input form values
  * @param array $files   the uploaded files if any
  * @param array $options additional user data
  *
  * @return true if no errors, else array of errors
  * @access public
  * @static
  */
 static function formRule($fields, $files, $self)
 {
     $errors = array();
     $amount = self::computeAmount($fields, $self);
     if (CRM_Utils_Array::value('selectMembership', $fields) && $fields['selectMembership'] != 'no_thanks' || CRM_Utils_Array::value('priceSetId', $fields) && $self->_useForMember) {
         $lifeMember = CRM_Member_BAO_Membership::getAllContactMembership($self->_userID, FALSE, TRUE);
         $membershipOrgDetails = CRM_Member_BAO_MembershipType::getMembershipTypeOrganization();
         $unallowedOrgs = array();
         foreach (array_keys($lifeMember) as $memTypeId) {
             $unallowedOrgs[] = $membershipOrgDetails[$memTypeId];
         }
     }
     //check for atleast one pricefields should be selected
     if (CRM_Utils_Array::value('priceSetId', $fields)) {
         $priceField = new CRM_Price_DAO_Field();
         $priceField->price_set_id = $fields['priceSetId'];
         $priceField->orderBy('weight');
         $priceField->find();
         $check = array();
         $membershipIsActive = TRUE;
         $previousId = $otherAmount = FALSE;
         while ($priceField->fetch()) {
             if ($self->_quickConfig && ($priceField->name == 'contribution_amount' || $priceField->name == 'membership_amount')) {
                 $previousId = $priceField->id;
                 if ($priceField->name == 'membership_amount' && !$priceField->is_active) {
                     $membershipIsActive = FALSE;
                 }
             }
             if ($priceField->name == 'other_amount') {
                 if ($self->_quickConfig && !CRM_Utils_Array::value("price_{$priceField->id}", $fields) && array_key_exists("price_{$previousId}", $fields) && isset($fields["price_{$previousId}"]) && $self->_values['fee'][$previousId]['name'] == 'contribution_amount' && empty($fields["price_{$previousId}"])) {
                     $otherAmount = $priceField->id;
                 } elseif (!empty($fields["price_{$priceField->id}"])) {
                     $otherAmountVal = $fields["price_{$priceField->id}"];
                     $min = CRM_Utils_Array::value('min_amount', $self->_values);
                     $max = CRM_Utils_Array::value('max_amount', $self->_values);
                     if ($min && $otherAmountVal < $min) {
                         $errors["price_{$priceField->id}"] = ts('Contribution amount must be at least %1', array(1 => $min));
                     }
                     if ($max && $otherAmountVal > $max) {
                         $errors["price_{$priceField->id}"] = ts('Contribution amount cannot be more than %1.', array(1 => $max));
                     }
                 }
             }
             if (!empty($fields["price_{$priceField->id}"]) || $previousId == $priceField->id && isset($fields["price_{$previousId}"]) && empty($fields["price_{$previousId}"])) {
                 $check[] = $priceField->id;
             }
         }
         if (empty($check)) {
             if ($self->_useForMember == 1 && $membershipIsActive) {
                 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
             } else {
                 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
             }
         }
         if ($otherAmount && !empty($check)) {
             $errors["price_{$otherAmount}"] = ts('Amount is required field.');
         }
         if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) {
             $priceFieldIDS = array();
             $priceFieldMemTypes = array();
             foreach ($self->_priceSet['fields'] as $priceId => $value) {
                 if (!empty($fields['price_' . $priceId]) || $self->_quickConfig && $value['name'] == 'membership_amount' && !CRM_Utils_Array::value('is_required', $self->_membershipBlock)) {
                     if (CRM_Utils_Array::value('price_' . $priceId, $fields) && is_array($fields['price_' . $priceId])) {
                         foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
                             if ($isSet) {
                                 $priceFieldIDS[] = $priceFldVal;
                             }
                         }
                     } elseif (!$value['is_enter_qty'] && CRM_Utils_Array::value('price_' . $priceId, $fields)) {
                         // The check for {!$value['is_enter_qty']} is done since, quantity fields allow entering
                         // quantity. And the quantity can't be conisdered as civicrm_price_field_value.id, CRM-9577
                         $priceFieldIDS[] = $fields['price_' . $priceId];
                     }
                     if (CRM_Utils_Array::value('options', $value)) {
                         foreach ($value['options'] as $val) {
                             if (CRM_Utils_Array::value('membership_type_id', $val)) {
                                 $priceFieldMemTypes[] = $val['membership_type_id'];
                             }
                         }
                     }
                 }
             }
             if (!empty($lifeMember)) {
                 foreach ($priceFieldIDS as $priceFieldId) {
                     if (($id = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_FieldValue', $priceFieldId, 'membership_type_id')) && in_array($membershipOrgDetails[$id], $unallowedOrgs)) {
                         $errors['_qf_default'] = ts('You already have a lifetime membership and cannot select a membership with a shorter term.');
                         break;
                     }
                 }
             }
             if (!empty($priceFieldIDS)) {
                 $ids = implode(',', $priceFieldIDS);
                 $priceFieldIDS['id'] = $fields['priceSetId'];
                 $self->set('memberPriceFieldIDS', $priceFieldIDS);
                 $count = CRM_Price_BAO_Set::getMembershipCount($ids);
                 foreach ($count as $id => $occurance) {
                     if ($occurance > 1) {
                         $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.');
                     }
                 }
             }
             if (empty($priceFieldMemTypes)) {
                 $errors['_qf_default'] = ts('Please select at least one membership option.');
             }
         }
         $fieldId = $memPresent = $membershipLabel = $fieldOption = $proceFieldAmount = NULL;
         if ($self->_separateMembershipPayment == 0 && $self->_quickConfig) {
             foreach ($self->_priceSet['fields'] as $fieldKey => $fieldVal) {
                 if ($fieldVal['name'] == 'membership_amount') {
                     $fieldId = $fieldVal['id'];
                     $fieldOption = $fields['price_' . $fieldId];
                     $memPresent = TRUE;
                 } else {
                     if (CRM_Utils_Array::value('price_' . $fieldKey, $fields) && $memPresent && ($fieldVal['name'] == 'other_amount' || $fieldVal['name'] == 'contribution_amount')) {
                         $fieldId = $fieldVal['id'];
                         if ($fieldVal['name'] == 'other_amount') {
                             $proceFieldAmount = $self->_submitValues['price_' . $fieldId];
                         } else {
                             $proceFieldAmount = $fieldVal['options'][$self->_submitValues['price_' . $fieldId]]['amount'];
                         }
                         unset($fields['price_' . $fieldId]);
                         break;
                     }
                 }
             }
         }
         CRM_Price_BAO_Set::processAmount($self->_values['fee'], $fields, $lineItem);
         if ($proceFieldAmount) {
             if ($proceFieldAmount < $lineItem[$fieldOption]['line_total']) {
                 $errors["price_{$fieldId}"] = ts('The Membership you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($lineItem[$fieldOption]['line_total'])));
             }
             $fields['amount'] = $proceFieldAmount;
         }
         if ($fields['amount'] < 0) {
             $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
         }
         $amount = $fields['amount'];
     }
     if (isset($fields['selectProduct']) && $fields['selectProduct'] != 'no_thanks' && $self->_values['amount_block_is_active']) {
         $productDAO = new CRM_Contribute_DAO_Product();
         $productDAO->id = $fields['selectProduct'];
         $productDAO->find(TRUE);
         $min_amount = $productDAO->min_contribution;
         if ($amount < $min_amount) {
             $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount)));
         }
     }
     if ($self->_values['honor_block_is_active'] && CRM_Utils_Array::value('honor_type_id', $fields)) {
         // make sure there is a first name and last name if email is not there
         if (!CRM_Utils_Array::value('honor_email', $fields)) {
             if (!CRM_Utils_Array::value('honor_first_name', $fields) || !CRM_Utils_Array::value('honor_last_name', $fields)) {
                 $errors['honor_last_name'] = ts('In Honor Of - First Name and Last Name, OR an Email Address is required.');
             }
         }
     }
     if (CRM_Utils_Array::value('is_recur', $fields)) {
         if ($fields['frequency_interval'] <= 0) {
             $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).');
         }
         if ($fields['frequency_unit'] == '0') {
             $errors['frequency_unit'] = ts('Please select a period (e.g. months, years ...) for how often you want to make this recurring contribution (EXAMPLE: Every 3 MONTHS).');
         }
     }
     if (CRM_Utils_Array::value('is_recur', $fields) && CRM_Utils_Array::value('payment_processor', $fields) == 0) {
         $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
     }
     if (CRM_Utils_Array::value('is_for_organization', $fields) && !property_exists($self, 'organizationName')) {
         if (!CRM_Utils_Array::value('organization_name', $fields['onbehalf'])) {
             if (CRM_Utils_Array::value('org_option', $fields) && !$fields['onbehalfof_id']) {
                 $errors['organization_id'] = ts('Please select an organization or enter a new one.');
             } elseif (!CRM_Utils_Array::value('org_option', $fields)) {
                 $errors['onbehalf']['organization_name'] = ts('Please enter the organization name.');
             }
         }
         foreach ($fields['onbehalf'] as $key => $value) {
             if (strstr($key, 'email')) {
                 $emailLocType = explode('-', $key);
             }
         }
         if (!CRM_Utils_Array::value("email-{$emailLocType[1]}", $fields['onbehalf'])) {
             $errors['onbehalf']["email-{$emailLocType[1]}"] = ts('Organization email is required.');
         }
     }
     // validate PCP fields - if not anonymous, we need a nick name value
     if ($self->_pcpId && CRM_Utils_Array::value('pcp_display_in_roll', $fields) && CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0 && CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '') {
         $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
     }
     // return if this is express mode
     $config = CRM_Core_Config::singleton();
     if ($self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
         if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) {
             return $errors;
         }
     }
     //validate the pledge fields.
     if (CRM_Utils_Array::value('pledge_block_id', $self->_values)) {
         //validation for pledge payment.
         if (CRM_Utils_Array::value('pledge_id', $self->_values)) {
             if (empty($fields['pledge_amount'])) {
                 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
             }
         } elseif (CRM_Utils_Array::value('is_pledge', $fields)) {
             if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == FALSE) {
                 $errors['pledge_installments'] = ts('Please enter a valid pledge installment.');
             } else {
                 if (CRM_Utils_Array::value('pledge_installments', $fields) == NULL) {
                     $errors['pledge_installments'] = ts('Pledge Installments is required field.');
                 } elseif (CRM_Utils_array::value('pledge_installments', $fields) == 1) {
                     $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.');
                 } elseif (CRM_Utils_array::value('pledge_installments', $fields) == 0) {
                     $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
                 }
             }
             //validation for Pledge Frequency Interval.
             if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == FALSE) {
                 $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
             } else {
                 if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == NULL) {
                     $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
                 } elseif (CRM_Utils_array::value('pledge_frequency_interval', $fields) == 0) {
                     $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
                 }
             }
         }
     }
     // also return if paylater mode
     if (CRM_Utils_Array::value('payment_processor', $fields) == 0) {
         return empty($errors) ? TRUE : $errors;
     }
     // if the user has chosen a free membership or the amount is less than zero
     // i.e. we skip calling the payment processor and hence dont need credit card
     // info
     if ((double) $amount <= 0.0) {
         return $errors;
     }
     if (isset($self->_paymentFields)) {
         foreach ($self->_paymentFields as $name => $fld) {
             if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
                 $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
             }
         }
     }
     // make sure that credit card number and cvv are valid
     if (CRM_Utils_Array::value('credit_card_type', $fields)) {
         if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
             $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number');
         }
         if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
             $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number');
         }
     }
     foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
         if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
             $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
             if ($customizedValue == $greetingType && empty($fielse[$greeting . '_custom'])) {
                 $errors[$greeting . '_custom'] = ts('Custom %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', " ", $greeting))));
             }
         }
     }
     return empty($errors) ? TRUE : $errors;
 }
 function validatePaymentValues($self, $fields)
 {
     if (CRM_Utils_Array::value('bypass_payment', $self->_params[0]) || $self->_allowWaitlist || empty($self->_fields) || CRM_Utils_Array::value('amount', $self->_params[0]) > 0) {
         return TRUE;
     }
     $validatePayement = FALSE;
     if (CRM_Utils_Array::value('priceSetId', $fields)) {
         $lineItem = array();
         CRM_Price_BAO_Set::processAmount($self->_values['fee'], $fields, $lineItem);
         if ($fields['amount'] > 0) {
             $validatePayement = TRUE;
             // $self->_forcePayement = true;
             // return false;
         }
     } elseif (CRM_Utils_Array::value('amount', $fields) && CRM_Utils_Array::value('value', $self->_values['fee'][$fields['amount']]) > 0) {
         $validatePayement = TRUE;
     }
     if (!$validatePayement) {
         return TRUE;
     }
     foreach ($self->_fields as $name => $fld) {
         if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
             return FALSE;
         }
     }
     // make sure that credit card number and cvv are valid
     if (CRM_Utils_Array::value('credit_card_type', $self->_params[0])) {
         if (CRM_Utils_Array::value('credit_card_number', $self->_params[0]) && !CRM_Utils_Rule::creditCardNumber($self->_params[0]['credit_card_number'], $self->_params[0]['credit_card_type'])) {
             return FALSE;
         }
         if (CRM_Utils_Array::value('cvv2', $self->_params[0]) && !CRM_Utils_Rule::cvv($self->_params[0]['cvv2'], $self->_params[0]['credit_card_type'])) {
             return FALSE;
         }
     }
     foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) {
         if ($greetingType = CRM_Utils_Array::value($greeting, $self->_params[0])) {
             $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
             if ($customizedValue == $greetingType && empty($self->_params[0][$greeting . '_custom'])) {
                 return FALSE;
             }
         }
     }
 }
Ejemplo n.º 10
0
 /** 
  * global form rule 
  * 
  * @param array $fields  the input form values 
  * @param array $files   the uploaded files if any 
  * @param array $options additional user data 
  * 
  * @return true if no errors, else array of errors 
  * @access public 
  * @static 
  */
 static function formRule(&$fields, &$files, &$self)
 {
     //To check if the user is already registered for the event(CRM-2426)
     self::checkRegistration($fields, $self);
     //check for availability of registrations.
     if (!$self->_allowConfirmation && !CRM_Utils_Array::value('bypass_payment', $fields) && is_numeric($self->_availableRegistrations) && CRM_Utils_Array::value('additional_participants', $fields) >= $self->_availableRegistrations) {
         $errors['additional_participants'] = ts("There is only enough space left on this event for %1 participant(s).", array(1 => $self->_availableRegistrations));
     }
     // during confirmation don't allow to increase additional participants, CRM-4320
     if ($self->_allowConfirmation && CRM_Utils_Array::value('additional_participants', $fields) && is_array($self->_additionalParticipantIds) && $fields['additional_participants'] > count($self->_additionalParticipantIds)) {
         $errors['additional_participants'] = ts("Oops. It looks like you are trying to increase the number of additional people you are registering for. You can confirm registration for a maximum of %1 additional people.", array(1 => count($self->_additionalParticipantIds)));
     }
     //don't allow to register w/ waiting if enough spaces available.
     if (CRM_Utils_Array::value('bypass_payment', $fields)) {
         if (!is_numeric($self->_availableRegistrations) || CRM_Utils_Array::value('additional_participants', $fields) < $self->_availableRegistrations) {
             $errors['bypass_payment'] = ts("Oops. There are enough available spaces in this event. You can not add yourself to the waiting list.");
         }
     }
     $email = $fields["email-{$self->_bltID}"];
     require_once 'CRM/Core/BAO/UFMatch.php';
     if (CRM_Core_BAO_UFMatch::isDuplicateUser($email)) {
         $errors["email-{$self->_bltID}"] = ts('The email %1 already exists in the database.', array(1 => $email));
     }
     if (CRM_Utils_Array::value('additional_participants', $fields) && !CRM_Utils_Rule::positiveInteger($fields['additional_participants'])) {
         $errors['additional_participants'] = ts('Please enter a whole number for Number of additional people.');
     }
     //check for atleast one pricefields should be selected
     if (CRM_Utils_Array::value('priceSetId', $fields)) {
         $priceField = new CRM_Price_DAO_Field();
         $priceField->price_set_id = $fields['priceSetId'];
         $priceField->find();
         $check = array();
         while ($priceField->fetch()) {
             if (!empty($fields["price_{$priceField->id}"])) {
                 $check[] = $priceField->id;
             }
         }
         if (empty($check)) {
             $errors['_qf_default'] = ts("Select at least one option from Event Fee(s).");
         }
         $lineItem = array();
         require_once "CRM/Price/BAO/Set.php";
         CRM_Price_BAO_Set::processAmount($self->_values['fee']['fields'], $fields, $lineItem);
         if ($fields['amount'] < 0) {
             $errors['_qf_default'] = ts("Event Fee(s) can not be less than zero. Please select the options accordingly");
         }
     }
     if ($self->_values['event']['is_monetary']) {
         if (is_array($self->_paymentProcessor)) {
             $payment =& CRM_Core_Payment::singleton($self->_mode, 'Event', $self->_paymentProcessor, $this);
             $error = $payment->checkConfig($self->_mode);
             if ($error) {
                 $errors['_qf_default'] = $error;
             }
         }
         // return if this is express mode
         $config =& CRM_Core_Config::singleton();
         if ($self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
             if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) {
                 return empty($errors) ? true : $errors;
             }
         }
         $zeroAmount = $fields['amount'];
         // also return if paylater mode or zero fees for valid members
         if (CRM_Utils_Array::value('is_pay_later', $fields) || CRM_Utils_Array::value('bypass_payment', $fields)) {
             if (CRM_Utils_Array::value('priceSetId', $fields)) {
                 foreach ($fields as $key => $val) {
                     if (substr($key, 0, 6) == 'price_' && $val != 0) {
                         return empty($errors) ? true : $errors;
                     }
                 }
             } else {
                 return empty($errors) ? true : $errors;
             }
         } else {
             if (CRM_Utils_Array::value('priceSetId', $fields)) {
                 //here take all value(amount) of optoin value id
                 $check = array();
                 foreach ($fields as $key => $val) {
                     if (substr($key, 0, 6) == 'price_' && $val != 0) {
                         $htmlType = CRM_Core_DAO::getFieldValue('CRM_Price_BAO_Field', substr($key, 6), 'html_type');
                         if (is_array($val)) {
                             //$keys is the id of the option value
                             foreach ($val as $keys => $vals) {
                                 if (!$keys) {
                                     continue;
                                 }
                                 $check[] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $keys, 'name');
                             }
                         } else {
                             if ($htmlType == 'Text') {
                                 $check[] = $val;
                             } else {
                                 //$val is the id of the option value
                                 $check[] = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $val, 'name');
                             }
                         }
                     }
                 }
                 //validation for submitted each value is zero
                 //if not zero give credit card validation error else
                 //bypass it.
                 $level = count($check);
                 $j = 0;
                 for ($i = 0; $i < $level; $i++) {
                     if ($check[$i] >= 0) {
                         $j += $check[$i];
                     }
                 }
                 if ($j == 0) {
                     return empty($errors) ? true : $errors;
                 }
             } else {
                 if ($zeroAmount) {
                     if (CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', $zeroAmount, 'value', 'id') == 0) {
                         return empty($errors) ? true : $errors;
                     }
                 }
             }
         }
         //is pay later and priceset is used avoid credit card and
         //billing address validation
         if ((CRM_Utils_Array::value('is_pay_later', $fields) || CRM_Utils_Array::value('bypass_payment', $fields)) && $fields['priceSetId'] || !$self->_allowConfirmation && ($self->_requireApproval || $self->_allowWaitlist)) {
             return empty($errors) ? true : $errors;
         }
         foreach ($self->_fields as $name => $fld) {
             if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
                 $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
             }
         }
     }
     // make sure that credit card number and cvv are valid
     require_once 'CRM/Utils/Rule.php';
     if (CRM_Utils_Array::value('credit_card_type', $fields)) {
         if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
             $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number");
         }
         if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
             $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
         }
     }
     $elements = array('email_greeting' => 'email_greeting_custom', 'postal_greeting' => 'postal_greeting_custom', 'addressee' => 'addressee_custom');
     foreach ($elements as $greeting => $customizedGreeting) {
         if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
             $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
             if ($customizedValue == $greetingType && !CRM_Utils_Array::value($customizedGreeting, $fields)) {
                 $errors[$customizedGreeting] = ts('Custom  %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', " ", $greeting))));
             }
         }
     }
     return empty($errors) ? true : $errors;
 }
Ejemplo n.º 11
0
 /**
  * Function for validation
  *
  * @param array $params (ref.) an assoc array of name/value pairs
  *
  * @return mixed true or array of errors
  * @access public
  * @static
  */
 public function formRule(&$params, &$files, $self)
 {
     $errors = array();
     //check if contact is selected in standalone mode
     if (isset($params['contact_select_id']) && !$params['contact_select_id']) {
         $errors['contact'] = ts('Please select a contact or create new contact');
     }
     if (!$params['membership_type_id'][1]) {
         $errors['membership_type_id'] = ts('Please select a membership type.');
     }
     if ($params['membership_type_id'][1] && CRM_Utils_Array::value('payment_processor_id', $params)) {
         // make sure that credit card number and cvv are valid
         require_once 'CRM/Utils/Rule.php';
         if (CRM_Utils_Array::value('credit_card_type', $params)) {
             if (CRM_Utils_Array::value('credit_card_number', $params) && !CRM_Utils_Rule::creditCardNumber($params['credit_card_number'], $params['credit_card_type'])) {
                 $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number");
             }
             if (CRM_Utils_Array::value('cvv2', $params) && !CRM_Utils_Rule::cvv($params['cvv2'], $params['credit_card_type'])) {
                 $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
             }
         }
     }
     $joinDate = CRM_Utils_Date::processDate($params['join_date']);
     if ($joinDate) {
         require_once 'CRM/Member/BAO/MembershipType.php';
         $membershipDetails = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($params['membership_type_id'][1]);
         $startDate = CRM_Utils_Date::processDate($params['start_date']);
         if ($startDate && $membershipDetails['period_type'] == 'rolling') {
             if ($startDate < $joinDate) {
                 $errors['start_date'] = ts('Start date must be the same or later than join date.');
             }
         }
         // if end date is set, ensure that start date is also set
         // and that end date is later than start date
         // If selected membership type has duration unit as 'lifetime'
         // and end date is set, then give error
         $endDate = CRM_Utils_Date::processDate($params['end_date']);
         if ($endDate) {
             if ($membershipDetails['duration_unit'] == 'lifetime') {
                 $errors['end_date'] = ts('The selected Membership Type has a lifetime duration. You cannot specify an End Date for lifetime memberships. Please clear the End Date OR select a different Membership Type.');
             } else {
                 if (!$startDate) {
                     $errors['start_date'] = ts('Start date must be set if end date is set.');
                 }
                 if ($endDate < $startDate) {
                     $errors['end_date'] = ts('End date must be the same or later than start date.');
                 }
             }
         }
         //CRM-3724, check for availability of valid membership status.
         if (!CRM_Utils_Array::value('is_override', $params)) {
             require_once 'CRM/Member/BAO/MembershipStatus.php';
             $calcStatus = CRM_Member_BAO_MembershipStatus::getMembershipStatusByDate($startDate, $endDate, $joinDate, 'today', true);
             if (empty($calcStatus)) {
                 $url = CRM_Utils_System::url('civicrm/admin/member/membershipStatus', 'reset=1&action=browse');
                 $errors['_qf_default'] = ts('There is no valid Membership Status available for selected membership dates.');
                 $status = ts('Oops, it looks like there is no valid membership status available for the given membership dates. You can <a href="%1">Configure Membership Status Rules</a>.', array(1 => $url));
                 if (!$self->_mode) {
                     $status .= ' ' . ts('OR You can sign up by setting Status Override? to true.');
                 }
                 CRM_Core_Session::setStatus($status);
             }
         }
     } else {
         $errors['join_date'] = ts('Please enter the join date.');
     }
     if (isset($params['is_override']) && $params['is_override'] && !$params['status_id']) {
         $errors['status_id'] = ts('Please enter the status.');
     }
     //total amount condition arise when membership type having no
     //minimum fee
     if (isset($params['record_contribution'])) {
         if (!$params['contribution_type_id']) {
             $errors['contribution_type_id'] = ts('Please enter the contribution Type.');
         }
         if (!$params['total_amount']) {
             $errors['total_amount'] = ts('Please enter the contribution.');
         }
     }
     // validate contribution status for 'Failed'.
     if ($self->_onlinePendingContributionId && CRM_Utils_Array::value('record_contribution', $params) && CRM_Utils_Array::value('contribution_status_id', $params) == array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(null, 'name'))) {
         $errors['contribution_status_id'] = ts("Please select a valid payment status before updating.");
     }
     return empty($errors) ? true : $errors;
 }
 /** 
  * global form rule 
  * 
  * @param array $fields  the input form values 
  * @param array $files   the uploaded files if any 
  * @param array $options additional user data 
  * 
  * @return true if no errors, else array of errors 
  * @access public 
  * @static 
  */
 static function formRule($fields, $files, $self)
 {
     $errors = array();
     $amount = self::computeAmount($fields, $self);
     //check for atleast one pricefields should be selected
     if (CRM_Utils_Array::value('priceSetId', $fields)) {
         $priceField = new CRM_Price_DAO_Field();
         $priceField->price_set_id = $fields['priceSetId'];
         $priceField->find();
         $check = array();
         while ($priceField->fetch()) {
             if (!empty($fields["price_{$priceField->id}"])) {
                 $check[] = $priceField->id;
             }
         }
         if (empty($check)) {
             if ($self->_useForMember == 1) {
                 $errors['_qf_default'] = ts('Select at least one option from Membership Type(s).');
             } else {
                 $errors['_qf_default'] = ts('Select at least one option from Contribution(s).');
             }
         }
         require_once 'CRM/Price/BAO/Set.php';
         if ($self->_useForMember == 1 && !empty($check)) {
             $priceFieldIDS = array();
             $priceFieldMemTypes = array();
             foreach ($self->_priceSet['fields'] as $priceId => $value) {
                 if (!empty($fields['price_' . $priceId])) {
                     if (is_array($fields['price_' . $priceId])) {
                         foreach ($fields['price_' . $priceId] as $priceFldVal => $isSet) {
                             if ($isSet) {
                                 $priceFieldIDS[] = $priceFldVal;
                             }
                         }
                     } else {
                         $priceFieldIDS[] = $fields['price_' . $priceId];
                     }
                     if (CRM_Utils_Array::value('options', $value)) {
                         foreach ($value['options'] as $val) {
                             if (CRM_Utils_Array::value('membership_type_id', $val)) {
                                 $priceFieldMemTypes[] = $val['membership_type_id'];
                             }
                         }
                     }
                 }
             }
             $ids = implode(',', $priceFieldIDS);
             $priceFieldIDS['id'] = $fields['priceSetId'];
             $self->set('memberPriceFieldIDS', $priceFieldIDS);
             $count = CRM_Price_BAO_Set::getMembershipCount($ids);
             foreach ($count as $id => $occurance) {
                 if ($occurance > 1) {
                     $errors['_qf_default'] = ts('You have selected multiple memberships for the same organization or entity. Please review your selections and choose only one membership per entity. Contact the site administrator if you need assistance.');
                 }
             }
             if (empty($priceFieldMemTypes)) {
                 $errors['_qf_default'] = ts('Please select at least one membership option.');
             }
         }
         CRM_Price_BAO_Set::processAmount($self->_values['fee'], $fields, $lineItem);
         if ($fields['amount'] < 0) {
             $errors['_qf_default'] = ts('Contribution can not be less than zero. Please select the options accordingly');
         }
         $amount = $fields['amount'];
     }
     if (isset($fields['selectProduct']) && $fields['selectProduct'] != 'no_thanks' && $self->_values['amount_block_is_active']) {
         require_once 'CRM/Contribute/DAO/Product.php';
         require_once 'CRM/Utils/Money.php';
         $productDAO = new CRM_Contribute_DAO_Product();
         $productDAO->id = $fields['selectProduct'];
         $productDAO->find(true);
         $min_amount = $productDAO->min_contribution;
         if ($amount < $min_amount) {
             $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount)));
         }
     }
     if ($self->_values['honor_block_is_active'] && CRM_Utils_Array::value('honor_type_id', $fields)) {
         // make sure there is a first name and last name if email is not there
         if (!CRM_Utils_Array::value('honor_email', $fields)) {
             if (!CRM_Utils_Array::value('honor_first_name', $fields) || !CRM_Utils_Array::value('honor_last_name', $fields)) {
                 $errors['honor_last_name'] = ts('In Honor Of - First Name and Last Name, OR an Email Address is required.');
             }
         }
     }
     if (isset($fields['is_recur']) && $fields['is_recur']) {
         if ($fields['frequency_interval'] <= 0) {
             $errors['frequency_interval'] = ts('Please enter a number for how often you want to make this recurring contribution (EXAMPLE: Every 3 months).');
         }
         if ($fields['frequency_unit'] == '0') {
             $errors['frequency_unit'] = ts('Please select a period (e.g. months, years ...) for how often you want to make this recurring contribution (EXAMPLE: Every 3 MONTHS).');
         }
     }
     if (CRM_Utils_Array::value('is_recur', $fields) && $fields['is_pay_later']) {
         $errors['is_pay_later'] = ' ';
         $errors['_qf_default'] = ts('You cannot set up a recurring contribution if you are not paying online by credit card.');
     }
     if (CRM_Utils_Array::value('is_for_organization', $fields) && !property_exists($self, 'organizationName')) {
         if (!CRM_Utils_Array::value('organization_name', $fields['onbehalf'])) {
             if (CRM_Utils_Array::value('org_option', $fields) && !$fields['onbehalfof_id']) {
                 $errors['organization_id'] = ts('Please select an organization or enter a new one.');
             } else {
                 if (!CRM_Utils_Array::value('org_option', $fields)) {
                     $errors['onbehalf']['organization_name'] = ts('Please enter the organization name.');
                 }
             }
         }
         foreach ($fields['onbehalf'] as $key => $value) {
             if (strstr($key, 'email')) {
                 $emailLocType = explode('-', $key);
             }
         }
         if (!CRM_Utils_Array::value("email-{$emailLocType[1]}", $fields['onbehalf'])) {
             $errors['onbehalf']["email-{$emailLocType[1]}"] = ts('Organization email is required.');
         }
     }
     if (CRM_Utils_Array::value('selectMembership', $fields) && $fields['selectMembership'] != 'no_thanks') {
         require_once 'CRM/Member/BAO/Membership.php';
         require_once 'CRM/Member/BAO/MembershipType.php';
         if (!empty($self->_membershipTypeValues)) {
             $memTypeDetails = $self->_membershipTypeValues[$fields['selectMembership']];
         } else {
             $memTypeDetails = CRM_Member_BAO_Membership::buildMembershipTypeValues($self, $fields['selectMembership']);
         }
         if ($self->_values['amount_block_is_active'] && !CRM_Utils_Array::value('is_separate_payment', $self->_membershipBlock)) {
             require_once 'CRM/Utils/Money.php';
             if ($amount < CRM_Utils_Array::value('minimum_fee', $memTypeDetails)) {
                 $errors['selectMembership'] = ts('The Membership you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($memTypeDetails['minimum_fee'])));
             }
         } else {
             if (CRM_Utils_Array::value('minimum_fee', $memTypeDetails)) {
                 // we dont have an amount, so lets get an amount for cc checks
                 $amount = $memTypeDetails['minimum_fee'];
             }
         }
     }
     if ($self->_values['is_monetary']) {
         //validate other amount.
         $checkOtherAmount = false;
         if (CRM_Utils_Array::value('amount', $fields) == 'amount_other_radio' || CRM_Utils_Array::value('amount_other', $fields)) {
             $checkOtherAmount = true;
         }
         $otherAmountVal = CRM_Utils_Array::value('amount_other', $fields);
         if ($checkOtherAmount || $otherAmountVal) {
             if (!$otherAmountVal) {
                 $errors['amount_other'] = ts('Amount is required field.');
             }
             //validate for min and max.
             if ($otherAmountVal) {
                 $min = CRM_Utils_Array::value('min_amount', $self->_values);
                 $max = CRM_Utils_Array::value('max_amount', $self->_values);
                 if ($min && $otherAmountVal < $min) {
                     $errors['amount_other'] = ts('Contribution amount must be at least %1', array(1 => $min));
                 }
                 if ($max && $otherAmountVal > $max) {
                     $errors['amount_other'] = ts('Contribution amount cannot be more than %1.', array(1 => $max));
                 }
             }
         }
     }
     // validate PCP fields - if not anonymous, we need a nick name value
     if ($self->_pcpId && CRM_Utils_Array::value('pcp_display_in_roll', $fields) && CRM_Utils_Array::value('pcp_is_anonymous', $fields) == 0 && CRM_Utils_Array::value('pcp_roll_nickname', $fields) == '') {
         $errors['pcp_roll_nickname'] = ts('Please enter a name to include in the Honor Roll, or select \'contribute anonymously\'.');
     }
     // return if this is express mode
     $config = CRM_Core_Config::singleton();
     if ($self->_paymentProcessor['billing_mode'] & CRM_Core_Payment::BILLING_MODE_BUTTON) {
         if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) {
             return $errors;
         }
     }
     //validate the pledge fields.
     if (CRM_Utils_Array::value('pledge_block_id', $self->_values)) {
         //validation for pledge payment.
         if (CRM_Utils_Array::value('pledge_id', $self->_values)) {
             if (empty($fields['pledge_amount'])) {
                 $errors['pledge_amount'] = ts('At least one payment option needs to be checked.');
             }
         } else {
             if (CRM_Utils_Array::value('is_pledge', $fields)) {
                 if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_installments', $fields)) == false) {
                     $errors['pledge_installments'] = ts('Please enter a valid pledge installment.');
                 } else {
                     if (CRM_Utils_Array::value('pledge_installments', $fields) == null) {
                         $errors['pledge_installments'] = ts('Pledge Installments is required field.');
                     } else {
                         if (CRM_Utils_array::value('pledge_installments', $fields) == 1) {
                             $errors['pledge_installments'] = ts('Pledges consist of multiple scheduled payments. Select one-time contribution if you want to make your gift in a single payment.');
                         } else {
                             if (CRM_Utils_array::value('pledge_installments', $fields) == 0) {
                                 $errors['pledge_installments'] = ts('Pledge Installments field must be > 1.');
                             }
                         }
                     }
                 }
                 //validation for Pledge Frequency Interval.
                 if (CRM_Utils_Rule::positiveInteger(CRM_Utils_Array::value('pledge_frequency_interval', $fields)) == false) {
                     $errors['pledge_frequency_interval'] = ts('Please enter a valid Pledge Frequency Interval.');
                 } else {
                     if (CRM_Utils_Array::value('pledge_frequency_interval', $fields) == null) {
                         $errors['pledge_frequency_interval'] = ts('Pledge Frequency Interval. is required field.');
                     } else {
                         if (CRM_Utils_array::value('pledge_frequency_interval', $fields) == 0) {
                             $errors['pledge_frequency_interval'] = ts('Pledge frequency interval field must be > 0');
                         }
                     }
                 }
             }
         }
     }
     // also return if paylater mode
     if (CRM_Utils_Array::value('is_pay_later', $fields)) {
         return empty($errors) ? true : $errors;
     }
     // if the user has chosen a free membership or the amount is less than zero
     // i.e. we skip calling the payment processor and hence dont need credit card
     // info
     if ((double) $amount <= 0.0) {
         return $errors;
     }
     foreach ($self->_fields as $name => $fld) {
         if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
             $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
         }
     }
     // make sure that credit card number and cvv are valid
     require_once 'CRM/Utils/Rule.php';
     if (CRM_Utils_Array::value('credit_card_type', $fields)) {
         if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
             $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number');
         }
         if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
             $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number');
         }
     }
     $elements = array('email_greeting' => 'email_greeting_custom', 'postal_greeting' => 'postal_greeting_custom', 'addressee' => 'addressee_custom');
     foreach ($elements as $greeting => $customizedGreeting) {
         if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) {
             $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name');
             if ($customizedValue == $greetingType && !CRM_Utils_Array::value($customizedGreeting, $fields)) {
                 $errors[$customizedGreeting] = ts('Custom %1 is a required field if %1 is of type Customized.', array(1 => ucwords(str_replace('_', " ", $greeting))));
             }
         }
     }
     return empty($errors) ? true : $errors;
 }
Ejemplo n.º 13
0
 static function formRule($fields, $files, $self)
 {
     $errors = array();
     if ($self->payment_required && !CRM_Utils_Array::value('is_pay_later', $self->_submitValues)) {
         $payment =& CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor, $this);
         $error = $payment->checkConfig($self->_mode);
         if ($error) {
             $errors['_qf_default'] = $error;
         }
         foreach ($self->_fields as $name => $field) {
             if ($field['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
                 $errors[$name] = ts('%1 is a required field.', array(1 => $field['title']));
             }
         }
         if (CRM_Utils_Array::value('credit_card_type', $fields)) {
             if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
                 $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number");
             }
             if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
                 $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
             }
         }
     }
     return empty($errors) ? TRUE : $errors;
 }
Ejemplo n.º 14
0
 /**
  * Make sure that credit card number and cvv are valid.
  * Called within the scope of a QF formRule function
  *
  * @param int $processorID
  * @param array $values
  * @param array $errors
  */
 public static function validateCreditCard($processorID = NULL, $values, &$errors)
 {
     if (!empty($values['credit_card_type']) || !empty($values['credit_card_number'])) {
         if (!empty($values['credit_card_type'])) {
             $processorCards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($processorID);
             if (!empty($processorCards) && !in_array($values['credit_card_type'], $processorCards)) {
                 $errors['credit_card_type'] = ts('This procesor does not support credit card type ' . $values['credit_card_type']);
             }
         }
         if (!empty($values['credit_card_number']) && !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type'])) {
             $errors['credit_card_number'] = ts('Please enter a valid Card Number');
         }
         if (!empty($values['cvv2']) && !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type'])) {
             $errors['cvv2'] = ts('Please enter a valid Card Verification Number');
         }
     }
 }
Ejemplo n.º 15
0
 /** 
  * global form rule 
  * 
  * @param array $fields  the input form values 
  * @param array $files   the uploaded files if any 
  * @param array $options additional user data 
  * 
  * @return true if no errors, else array of errors 
  * @access public 
  * @static 
  */
 function formRule(&$fields, &$files, &$self)
 {
     $errors = array();
     if ($fields['selectProduct'] && $fields['selectProduct'] != 'no_thanks') {
         require_once 'CRM/Contribute/DAO/Product.php';
         $productDAO =& new CRM_Contribute_DAO_Product();
         $productDAO->id = $fields['selectProduct'];
         $productDAO->find(true);
         $min_amount = $productDAO->min_contribution;
         if ($fields['amount'] == 'amount_other_radio') {
             if ($fields['amount_other'] < $min_amount) {
                 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount)));
             }
         } else {
             if ($fields['amount'] < $min_amount) {
                 $errors['selectProduct'] = ts('The premium you have selected requires a minimum contribution of %1', array(1 => CRM_Utils_Money::format($min_amount)));
             }
         }
     }
     $payment =& CRM_Contribute_Payment::singleton($self->_mode);
     $error = $payment->checkConfig($self->_mode);
     if ($error) {
         $errors['_qf_default'] = $error;
     }
     if ($fields['amount'] == 'amount_other_radio') {
         if ($self->_values['min_amount'] > 0) {
             $min = $self->_values['min_amount'];
             if ($fields['amount_other'] < $min) {
                 $errors['amount_other'] = ts('This amount has to be greater than %1', array(1 => $min));
             }
         }
         if ($self->_values['max_amount'] > 0) {
             $max = $self->_values['max_amount'];
             if ($fields['amount_other'] > $max) {
                 $errors['amount_other'] = ts('This amount has to be less than %1', array(1 => $max));
             }
         }
     }
     // make sure either
     // return if this is express mode
     $config =& CRM_Core_Config::singleton();
     if ($config->paymentBillingMode & CRM_CONTRIBUTE_PAYMENT_BILLING_MODE_BUTTON) {
         if (CRM_Utils_Array::value($self->_expressButtonName . '_x', $fields) || CRM_Utils_Array::value($self->_expressButtonName . '_y', $fields) || CRM_Utils_Array::value($self->_expressButtonName, $fields)) {
             return $errors;
         }
     }
     foreach ($self->_fields as $name => $fld) {
         if ($fld['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) {
             $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title']));
         }
     }
     // make sure that credit card number and cvv are valid
     require_once 'CRM/Utils/Rule.php';
     if (CRM_Utils_Array::value('credit_card_type', $fields)) {
         if (CRM_Utils_Array::value('credit_card_number', $fields) && !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type'])) {
             $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number");
         }
         if (CRM_Utils_Array::value('cvv2', $fields) && !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type'])) {
             $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number");
         }
     }
     return empty($errors) ? true : $errors;
 }