public function testTypeCheckWithWrongInput() { $values = array('String' => 1, 'Boolean' => 'US'); foreach ($values as $type => $value) { $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value); $this->assertEquals($valid, NULL, 'Checking type ' . $type . ' for returned CustomField Type.'); } }
function testTypeCheckWithWrongInput() { $values = array(); $values = array('String' => 1, 'Boolean' => 'US'); require_once 'CRM/Core/BAO/CustomValue.php'; foreach ($values as $type => $value) { $valid = CRM_Core_BAO_CustomValue::typecheck($type, $value); $this->assertEquals($valid, null, 'Checking type ' . $type . ' for returned CustomField Type.'); } }
/** * @return bool */ public function validate() { if (CRM_Utils_System::isNull($this->_value)) { return TRUE; } switch ($this->_name) { case 'contact_id': // note: we validate extistence of the contact in API, upon // insert (it would be too costlty to do a db call here) return CRM_Utils_Rule::integer($this->_value); case 'register_date': return CRM_Utils_Rule::date($this->_value); /* @codingStandardsIgnoreStart case 'event_id': static $events = null; if (!$events) { $events = CRM_Event_PseudoConstant::event(); } if (in_array($this->_value, $events)) { return true; } else { return false; } break; @codingStandardsIgnoreEnd */ /* @codingStandardsIgnoreStart case 'event_id': static $events = null; if (!$events) { $events = CRM_Event_PseudoConstant::event(); } if (in_array($this->_value, $events)) { return true; } else { return false; } break; @codingStandardsIgnoreEnd */ default: break; } // check whether that's a valid custom field id // and if so, check the contents' validity if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) { static $customFields = NULL; if (!$customFields) { $customFields = CRM_Core_BAO_CustomField::getFields('Participant'); } if (!array_key_exists($customFieldID, $customFields)) { return FALSE; } return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value); } return TRUE; }
/** * function to check if an error in custom data * * @param String $errorMessage A string containing all the error-fields. * * @access public */ function isErrorInCustomData($params, &$errorMessage) { $session =& CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); //CRM-5125 //add custom fields for contact sub type if (!empty($this->_contactSubType)) { $csType = $this->_contactSubType; } if (CRM_Utils_Array::value('contact_sub_type', $params)) { $csType = CRM_Utils_Array::value('contact_sub_type', $params); } $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], false, false, $csType); foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $customFields)) { self::addToErrorMsg(ts('field ID'), $errorMessage); } /* validate the data against the CF type */ if ($value) { if ($customFields[$customFieldID]['data_type'] == 'Date') { if (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { $value = $params[$key]; } else { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } else { if ($customFields[$customFieldID]['data_type'] == 'Boolean') { if (CRM_Utils_String::strtoboolstr($value) === false) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } // need not check for label filed import $htmlType = array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Select', 'Radio', 'Multi-Select State/Province', 'Multi-Select Country'); if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) || $customFields[$customFieldID]['data_type'] == 'Boolean') { $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $value); if (!$valid) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } // check for values for custom fields for checkboxes and multiselect if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') { $value = trim($value); $value = str_replace('|', ',', $value); $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($mulValues as $v1) { if (strlen($v1) == 0) { continue; } $flag = false; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($v1)) || strtolower(trim($v2['value'])) == strtolower(trim($v1))) { $flag = true; } } if (!$flag) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } else { if ($customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); $flag = false; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($value)) || strtolower(trim($v2['value'])) == strtolower(trim($value))) { $flag = true; } } if (!$flag) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } else { if ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') { $mulValues = explode(',', $value); foreach ($mulValues as $stateValue) { if ($stateValue) { if (self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvinceAbbreviation()) || self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvince())) { continue; } else { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } } else { if ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') { $mulValues = explode(',', $value); foreach ($mulValues as $countryValue) { if ($countryValue) { CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', true, 'name', 'is_active'); CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', true, 'iso_code'); $config =& CRM_Core_Config::singleton(); $limitCodes = $config->countryLimit(); $error = true; foreach (array($countryNames, $countryIsoCodes, $limitCodes) as $values) { if (in_array(trim($countryValue), $values)) { $error = false; break; } } if ($error) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } } } } } } } else { if (is_array($params[$key]) && isset($params[$key]["contact_type"])) { //CRM-5125 //supporting custom data of related contact subtypes if (array_key_exists($key, $this->_relationships)) { $relation = $key; } else { if (CRM_Utils_Array::key($key, $this->_relationships)) { $relation = CRM_Utils_Array::key($key, $this->_relationships); } } if (!empty($relation)) { list($id, $first, $second) = CRM_Utils_System::explode('_', $relation, 3); $direction = "contact_sub_type_{$second}"; require_once 'CRM/Contact/BAO/RelationshipType.php'; $relationshipType =& new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = $id; if ($relationshipType->find(true)) { if (isset($relationshipType->{$direction})) { $params[$key]['contact_sub_type'] = $relationshipType->{$direction}; } } $relationshipType->free(); } self::isErrorInCustomData($params[$key], $errorMessage); } } } }
function validate() { if (CRM_Utils_System::isNull($this->_value)) { return true; } switch ($this->_name) { case 'contact_id': // note: we validate extistence of the contact in API, upon // insert (it would be too costlty to do a db call here) return CRM_Utils_Rule::integer($this->_value); break; case 'receive_date': case 'cancel_date': case 'receipt_date': case 'thankyou_date': return CRM_Utils_Rule::date($this->_value); break; case 'non_deductible_amount': case 'total_amount': case 'fee_amount': case 'net_amount': return CRM_Utils_Rule::money($this->_value); break; case 'trxn_id': static $seenTrxnIds = array(); if (in_array($this->_value, $seenTrxnIds)) { return false; } elseif ($this->_value) { $seenTrxnIds[] = $this->_value; return true; } else { $this->_value = null; return true; } break; case 'currency': return CRM_Utils_Rule::currencyCode($this->_value); break; case 'contribution_type': static $contributionTypes = null; if (!$contributionTypes) { $contributionTypes =& CRM_Contribute_PseudoConstant::contributionType(); } if (in_array($this->_value, $contributionTypes)) { return true; } else { return false; } break; case 'payment_instrument': static $paymentInstruments = null; if (!$paymentInstruments) { $paymentInstruments =& CRM_Contribute_PseudoConstant::paymentInstrument(); } if (in_array($this->_value, $paymentInstruments)) { return true; } else { return false; } break; default: break; } // check whether that's a valid custom field id // and if so, check the contents' validity if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($this->_name)) { static $customFields = null; if (!$customFields) { $customFields =& CRM_Core_BAO_CustomField::getFields('Contribution'); } if (!array_key_exists($customFieldID, $customFields)) { return false; } return CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $this->_value); } return true; }
/** * Extract the get params from the url, validate and store it in session. * * @param CRM_Core_Form $form * The form object. * @param string $type * The type of custom group we are using. * * @return array */ public static function extractGetParams(&$form, $type) { if (empty($_GET)) { return array(); } $groupTree = CRM_Core_BAO_CustomGroup::getTree($type); $customValue = array(); $htmlType = array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Select', 'Radio'); foreach ($groupTree as $group) { if (!isset($group['fields'])) { continue; } foreach ($group['fields'] as $key => $field) { $fieldName = 'custom_' . $key; $value = CRM_Utils_Request::retrieve($fieldName, 'String', $form, FALSE, NULL, 'GET'); if ($value) { $valid = FALSE; if (!in_array($field['html_type'], $htmlType) || $field['data_type'] == 'Boolean') { $valid = CRM_Core_BAO_CustomValue::typecheck($field['data_type'], $value); } if ($field['html_type'] == 'CheckBox' || $field['html_type'] == 'AdvMulti-Select' || $field['html_type'] == 'Multi-Select') { $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, TRUE); $val = array(); foreach ($mulValues as $v1) { foreach ($customOption as $coID => $coValue) { if (strtolower(trim($coValue['label'])) == strtolower(trim($v1))) { $val[$coValue['value']] = 1; } } } if (!empty($val)) { $value = $val; $valid = TRUE; } else { $value = NULL; } } elseif ($field['html_type'] == 'Select' || $field['html_type'] == 'Radio' && $field['data_type'] != 'Boolean') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, TRUE); foreach ($customOption as $customID => $coValue) { if (strtolower(trim($coValue['label'])) == strtolower(trim($value))) { $value = $coValue['value']; $valid = TRUE; } } } elseif ($field['data_type'] == 'Date') { if (!empty($value)) { $time = NULL; if (!empty($field['time_format'])) { $time = CRM_Utils_Request::retrieve($fieldName . '_time', 'String', $form, FALSE, NULL, 'GET'); } list($value, $time) = CRM_Utils_Date::setDateDefaults($value . ' ' . $time); if (!empty($field['time_format'])) { $customValue[$fieldName . '_time'] = $time; } } $valid = TRUE; } if ($valid) { $customValue[$fieldName] = $value; } } } } return $customValue; }
/** * Function to extract the get params from the url, validate * and store it in session * * @param CRM_Core_Form $form the form object * @param string $type the type of custom group we are using * @return void * @access public * @static */ function extractGetParams(&$form, $type) { // if not GET params return if (empty($_GET)) { return; } $groupTree =& CRM_Core_BAO_CustomGroup::getTree($type); $customFields =& CRM_Core_BAO_CustomField::getFields($type); $customValue = array(); $htmlType = array('CheckBox', 'Multi-Select', 'Select', 'Radio'); foreach ($groupTree as $group) { foreach ($group['fields'] as $key => $field) { $fieldName = 'custom_' . $key; $value = CRM_Utils_Request::retrieve($fieldName, $form); if ($value) { if (!in_array($customFields[$key][3], $htmlType) || $customFields[$key][2] == 'Boolean') { $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$key][2], $value); } if ($customFields[$key][3] == 'CheckBox' || $customFields[$key][3] == 'Multi-Select') { $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, true); $val = array(); foreach ($mulValues as $v1) { foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($v1))) { $val[$v2['value']] = 1; } } } if (!empty($val)) { $value = $val; $valid = true; } else { $value = null; } } else { if ($customFields[$key][3] == 'Select' || $customFields[$key][3] == 'Radio' && $customFields[$key][2] != 'Boolean') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($key, true); foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($value))) { $value = $v2['value']; $valid = true; } } } else { if ($customFields[$key][2] == 'Date') { require_once 'CRM/Utils/Date.php'; if (is_numeric($value)) { $value = CRM_Utils_Date::unformat($value, null); } else { $value = CRM_Utils_Date::unformat($value, $separator = '-'); } $valid = true; } } } if ($valid) { $customValue[$fieldName] = $value; } } } } $form->set('customGetValues', $customValue); $form->set('groupTree', $groupTree); }
/** * Validate a formatted contact parameter list. * * @param array $params * Structured parameter list (as in crm_format_params). * * @return bool|CRM_Core_Error */ function _civicrm_api3_deprecated_validate_formatted_contact(&$params) { // Look for offending email addresses if (array_key_exists('email', $params)) { foreach ($params['email'] as $count => $values) { if (!is_array($values)) { continue; } if ($email = CRM_Utils_Array::value('email', $values)) { // validate each email if (!CRM_Utils_Rule::email($email)) { return civicrm_api3_create_error('No valid email address'); } // check for loc type id. if (empty($values['location_type_id'])) { return civicrm_api3_create_error('Location Type Id missing.'); } } } } // Validate custom data fields if (array_key_exists('custom', $params) && is_array($params['custom'])) { foreach ($params['custom'] as $key => $custom) { if (is_array($custom)) { foreach ($custom as $fieldId => $value) { $valid = CRM_Core_BAO_CustomValue::typecheck(CRM_Utils_Array::value('type', $value), CRM_Utils_Array::value('value', $value)); if (!$valid && $value['is_required']) { return civicrm_api3_create_error('Invalid value for custom field \'' . CRM_Utils_Array::value('name', $custom) . '\''); } if (CRM_Utils_Array::value('type', $custom) == 'Date') { $params['custom'][$key][$fieldId]['value'] = str_replace('-', '', $params['custom'][$key][$fieldId]['value']); } } } } } return civicrm_api3_create_success(TRUE); }
function _crm_format_custom_params(&$params, &$values, $extends) { $values['custom'] = array(); $customFields = CRM_Core_BAO_CustomField::getFields($extends); foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $customFields)) { return _crm_error('Invalid custom field ID'); } $fieldType = null; // modified for CRM-1586 // check data type for importing custom field (labels) with data type Integer/Float/Money /* validate the data against the CF type */ if ($customFields[$customFieldID]['data_type'] == "Int" || $customFields[$customFieldID]['data_type'] == "Float" || $customFields[$customFieldID]['data_type'] == "Money") { if ($customFields[$customFieldID]['html_type'] == "Text") { $fieldType = $customFields[$customFieldID]['data_type']; } else { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID); foreach ($customOption as $customValue => $customLabel) { //check wether $value is label or value if (strtolower($customValue) == strtolower(trim($value))) { $fieldType = "String"; } else { if (strtolower($customValue) == strtolower(trim($value))) { $fieldType = $customFields[$customFieldID]['data_type']; } } } } } else { //set the Field type $fieldType = $customFields[$customFieldID]['data_type']; } $valid = null; //Validate the datatype of $value $valid = CRM_Core_BAO_CustomValue::typecheck($fieldType, $value); //return error, if not valid custom field if (!$valid) { return _crm_error('Invalid value for custom field ' . $customFields[$customFieldID][1]); } // fix the date field if so if ($customFields[$customFieldID]['data_type'] == 'Date') { $value = str_replace('-', '', $value); } // fixed for checkbox and multiselect $newMulValues = array(); if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') { $value = str_replace(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, ',', trim($value, CRM_Core_BAO_CustomOption::VALUE_SEPERATOR)); $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $custumOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($mulValues as $v1) { foreach ($customOption as $customValue => $customLabel) { if (strtolower($customLabel) == strtolower(trim($v1)) || strtolower($customValue) == strtolower(trim($v1))) { $newMulValues[] = $customValue; } } } $value = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $newMulValues) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR; } else { if ($customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio') { $custumOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($customOption as $customValue => $customLabel) { if (strtolower($customLabel) == strtolower(trim($value)) || strtolower($customValue) == strtolower(trim($value))) { $value = $customValue; break; } } } } $values['custom'][$customFieldID] = array('value' => $value, 'extends' => $customFields[$customFieldID]['extends'], 'type' => $customFields[$customFieldID]['data_type'], 'custom_field_id' => $customFieldID); } } }
/** * Check if an error in custom data. * * @param array $params * @param string $errorMessage * A string containing all the error-fields. * * @param null $csType * @param null $relationships */ public static function isErrorInCustomData($params, &$errorMessage, $csType = NULL, $relationships = NULL) { $session = CRM_Core_Session::singleton(); $dateType = $session->get("dateTypes"); if (!empty($params['contact_sub_type'])) { $csType = CRM_Utils_Array::value('contact_sub_type', $params); } if (empty($params['contact_type'])) { $params['contact_type'] = 'Individual'; } $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType); $addressCustomFields = CRM_Core_BAO_CustomField::getFields('Address'); $customFields = $customFields + $addressCustomFields; foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $customFields)) { self::addToErrorMsg(ts('field ID'), $errorMessage); } // validate null values for required custom fields of type boolean if (!empty($customFields[$customFieldID]['is_required']) && (empty($params['custom_' . $customFieldID]) && !is_numeric($params['custom_' . $customFieldID])) && $customFields[$customFieldID]['data_type'] == 'Boolean') { self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage); } //For address custom fields, we do get actual custom field value as an inner array of //values so need to modify if (array_key_exists($customFieldID, $addressCustomFields)) { $value = $value[0][$key]; } /* validate the data against the CF type */ if ($value) { if ($customFields[$customFieldID]['data_type'] == 'Date') { if (array_key_exists($customFieldID, $addressCustomFields) && CRM_Utils_Date::convertToDefaultDate($params[$key][0], $dateType, $key)) { $value = $params[$key][0][$key]; } elseif (CRM_Utils_Date::convertToDefaultDate($params, $dateType, $key)) { $value = $params[$key]; } else { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } elseif ($customFields[$customFieldID]['data_type'] == 'Boolean') { if (CRM_Utils_String::strtoboolstr($value) === FALSE) { self::addToErrorMsg($customFields[$customFieldID]['label'] . '::' . $customFields[$customFieldID]['groupTitle'], $errorMessage); } } // need not check for label filed import $htmlType = array('CheckBox', 'Multi-Select', 'AdvMulti-Select', 'Select', 'Radio', 'Multi-Select State/Province', 'Multi-Select Country'); if (!in_array($customFields[$customFieldID]['html_type'], $htmlType) || $customFields[$customFieldID]['data_type'] == 'Boolean' || $customFields[$customFieldID]['data_type'] == 'ContactReference') { $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID]['data_type'], $value); if (!$valid) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } // check for values for custom fields for checkboxes and multiselect if ($customFields[$customFieldID]['html_type'] == 'CheckBox' || $customFields[$customFieldID]['html_type'] == 'AdvMulti-Select' || $customFields[$customFieldID]['html_type'] == 'Multi-Select') { $value = trim($value); $value = str_replace('|', ',', $value); $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); foreach ($mulValues as $v1) { if (strlen($v1) == 0) { continue; } $flag = FALSE; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($v1)) || strtolower(trim($v2['value'])) == strtolower(trim($v1))) { $flag = TRUE; } } if (!$flag) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } elseif ($customFields[$customFieldID]['html_type'] == 'Select' || $customFields[$customFieldID]['html_type'] == 'Radio' && $customFields[$customFieldID]['data_type'] != 'Boolean') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); $flag = FALSE; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($value)) || strtolower(trim($v2['value'])) == strtolower(trim($value))) { $flag = TRUE; } } if (!$flag) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select State/Province') { $mulValues = explode(',', $value); foreach ($mulValues as $stateValue) { if ($stateValue) { if (self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvinceAbbreviation()) || self::in_value(trim($stateValue), CRM_Core_PseudoConstant::stateProvince())) { continue; } else { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } } elseif ($customFields[$customFieldID]['html_type'] == 'Multi-Select Country') { $mulValues = explode(',', $value); foreach ($mulValues as $countryValue) { if ($countryValue) { CRM_Core_PseudoConstant::populate($countryNames, 'CRM_Core_DAO_Country', TRUE, 'name', 'is_active'); CRM_Core_PseudoConstant::populate($countryIsoCodes, 'CRM_Core_DAO_Country', TRUE, 'iso_code'); $config = CRM_Core_Config::singleton(); $limitCodes = $config->countryLimit(); $error = TRUE; foreach (array($countryNames, $countryIsoCodes, $limitCodes) as $values) { if (in_array(trim($countryValue), $values)) { $error = FALSE; break; } } if ($error) { self::addToErrorMsg($customFields[$customFieldID]['label'], $errorMessage); } } } } } } elseif (is_array($params[$key]) && isset($params[$key]["contact_type"])) { //CRM-5125 //supporting custom data of related contact subtypes $relation = NULL; if ($relationships) { if (array_key_exists($key, $relationships)) { $relation = $key; } elseif (CRM_Utils_Array::key($key, $relationships)) { $relation = CRM_Utils_Array::key($key, $relationships); } } if (!empty($relation)) { list($id, $first, $second) = CRM_Utils_System::explode('_', $relation, 3); $direction = "contact_sub_type_{$second}"; $relationshipType = new CRM_Contact_BAO_RelationshipType(); $relationshipType->id = $id; if ($relationshipType->find(TRUE)) { if (isset($relationshipType->{$direction})) { $params[$key]['contact_sub_type'] = $relationshipType->{$direction}; } } $relationshipType->free(); } self::isErrorInCustomData($params[$key], $errorMessage, $csType, $relationships); } } }
/** * Validate a formatted contact parameter list. * * @param array $params Structured parameter list (as in crm_format_params) * * @return bool|CRM_Core_Error * @access public */ function _civicrm_validate_formatted_contact(&$params) { /* Look for offending email addresses */ if (array_key_exists('email', $params)) { foreach ($params['email'] as $count => $values) { if (!is_array($values)) { continue; } if ($email = CRM_Utils_Array::value('email', $values)) { //validate each email if (!CRM_Utils_Rule::email($email)) { return civicrm_create_error('No valid email address'); } //check for loc type id. if (!CRM_Utils_Array::value('location_type_id', $values)) { return civicrm_create_error('Location Type Id missing.'); } } } } /* Validate custom data fields */ if (is_array($params['custom'])) { foreach ($params['custom'] as $key => $custom) { if (is_array($custom)) { $valid = CRM_Core_BAO_CustomValue::typecheck($custom['type'], $custom['value']); if (!$valid) { return civicrm_create_error('Invalid value for custom field \'' . $custom['name'] . '\''); } if ($custom['type'] == 'Date') { $params['custom'][$key]['value'] = str_replace('-', '', $params['custom'][$key]['value']); } } } } return civicrm_create_success(true); }
/** * Validate a formatted contribution parameter list. * * @param array $params Structured parameter list (as in crm_format_params) * * @return bool|CRM_Core_Error * @access public */ function _crm_validate_formatted_contribution(&$params) { static $domainID = null; if (!$domainID) { $config =& CRM_Core_Config::singleton(); $domainID = $config->domainID(); } foreach ($params as $key => $value) { switch ($key) { case 'contact_id': if (!CRM_Utils_Rule::integer($value)) { return _crm_error("contact_id not valid: {$value}"); } $dao =& new CRM_Core_DAO(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE domain_id = {$domainID} AND id = {$value}"); if (!$svq) { return _crm_error("there's no contact with contact_id of {$value}"); } break; case 'receive_date': case 'cancel_date': case 'receipt_date': case 'thankyou_date': if (!CRM_Utils_Rule::date($value)) { return _crm_error("{$key} not a valid date: {$value}"); } break; case 'non_deductible_amount': case 'total_amount': case 'fee_amount': case 'net_amount': if (!CRM_Utils_Rule::money($value)) { return _crm_error("{$key} not a valid amount: {$value}"); } break; case 'currency': if (!CRM_Utils_Rule::currencyCode($value)) { return _crm_error("currency not a valid code: {$value}"); } break; default: break; } } /* Validate custom data fields */ if (is_array($params['custom'])) { foreach ($params['custom'] as $key => $custom) { if (is_array($custom)) { $valid = CRM_Core_BAO_CustomValue::typecheck($custom['type'], $custom['value']); if (!$valid) { return _crm_error('Invalid value for custom field \'' . $custom['name'] . '\''); } if ($custom['type'] == 'Date') { $params['custom'][$key]['value'] = str_replace('-', '', $params['custom'][$key]['value']); } } } } return true; }
/** * function to check if an error in custom data * * @param String $errorMessage A string containing all the error-fields. * * @access public */ function isErrorInCustomData($params, &$errorMessage) { $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type']); foreach ($params as $key => $value) { if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { /* check if it's a valid custom field id */ if (!array_key_exists($customFieldID, $customFields)) { //return _crm_error('Invalid custom field ID'); CRM_Import_Parser_Contact::addToErrorMsg('field ID', $errorMessage); } /* validate the data against the CF type */ //CRM_Core_Error::debug( $value, $customFields[$customFieldID] ); if ($value) { // need not check for label filed import $htmlType = array('CheckBox', 'Multi-Select', 'Select', 'Radio'); if (!in_array($customFields[$customFieldID][3], $htmlType) || $customFields[$customFieldID][2] == 'Boolean') { $valid = CRM_Core_BAO_CustomValue::typecheck($customFields[$customFieldID][2], $value); if (!$valid) { //return _crm_error('Invalid value for custom field :' .$customFields[$customFieldID][0]); CRM_Import_Parser_Contact::addToErrorMsg($customFields[$customFieldID][0], $errorMessage); } } // check for values for custom fields for checkboxes and multiselect if ($customFields[$customFieldID][3] == 'CheckBox' || $customFields[$customFieldID][3] == 'Multi-Select') { $value = str_replace("|", ",", $value); $mulValues = explode(',', $value); $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); foreach ($mulValues as $v1) { $flag = false; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($v1))) { $flag = true; } } if (!$flag) { //return _crm_error('Invalid value for custom field :' .$customFields[$customFieldID][0]); CRM_Import_Parser_Contact::addToErrorMsg($customFields[$customFieldID][0], $errorMessage); } } } else { if ($customFields[$customFieldID][3] == 'Select' || $customFields[$customFieldID][3] == 'Radio' && $customFields[$customFieldID][2] != 'Boolean') { $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, true); $flag = false; foreach ($customOption as $v2) { if (strtolower(trim($v2['label'])) == strtolower(trim($value))) { $flag = true; } } if (!$flag) { //return _crm_error('Invalid value for custom field :' .$customFields[$customFieldID][0]); CRM_Import_Parser_Contact::addToErrorMsg($customFields[$customFieldID][0], $errorMessage); } } } } } } //return true; }