From c3fc9a44df1ba99273e9053c0d1e728210aa8cdb Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 29 Jun 2017 08:37:56 +1200 Subject: [PATCH] CRM-20786 Move deprecated utils functions to the import classes --- CRM/Event/Import/Parser/Participant.php | 165 ++++++++++++++++++++++- CRM/Utils/DeprecatedUtils.php | 172 ------------------------ 2 files changed, 164 insertions(+), 173 deletions(-) diff --git a/CRM/Event/Import/Parser/Participant.php b/CRM/Event/Import/Parser/Participant.php index 5d5b2431ee..d19eadd171 100644 --- a/CRM/Event/Import/Parser/Participant.php +++ b/CRM/Event/Import/Parser/Participant.php @@ -340,7 +340,7 @@ class CRM_Event_Import_Parser_Participant extends CRM_Event_Import_Parser { $formatValues[$key] = $field; } - $formatError = _civicrm_api3_deprecated_participant_formatted_param($formatValues, $formatted, TRUE); + $formatError = $this->formatValues($formatted, $formatValues); if ($formatError) { array_unshift($values, $formatError['error_message']); @@ -509,4 +509,167 @@ class CRM_Event_Import_Parser_Participant extends CRM_Event_Import_Parser { public function fini() { } + /** + * Format values + * + * @todo lots of tidy up needed here - very old function relocated. + * + * @param array $values + * @param array $params + * + * @return array|null + */ + protected function formatValues(&$values, $params) { + $fields = CRM_Event_DAO_Participant::fields(); + _civicrm_api3_store_values($fields, $params, $values); + + $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); + + foreach ($params as $key => $value) { + // ignore empty values or empty arrays etc + if (CRM_Utils_System::isNull($value)) { + continue; + } + + // Handling Custom Data + if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { + $values[$key] = $value; + $type = $customFields[$customFieldID]['html_type']; + if ($type == 'CheckBox' || $type == 'Multi-Select') { + $mulValues = explode(',', $value); + $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); + $values[$key] = array(); + foreach ($mulValues as $v1) { + foreach ($customOption as $customValueID => $customLabel) { + $customValue = $customLabel['value']; + if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || + (strtolower(trim($customValue)) == strtolower(trim($v1))) + ) { + if ($type == 'CheckBox') { + $values[$key][$customValue] = 1; + } + else { + $values[$key][] = $customValue; + } + } + } + } + } + elseif ($type == 'Select' || $type == 'Radio') { + $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); + foreach ($customOption as $customFldID => $customValue) { + $val = CRM_Utils_Array::value('value', $customValue); + $label = CRM_Utils_Array::value('label', $customValue); + $label = strtolower($label); + $value = strtolower(trim($value)); + if (($value == $label) || ($value == strtolower($val))) { + $values[$key] = $val; + } + } + } + } + + switch ($key) { + case 'participant_contact_id': + if (!CRM_Utils_Rule::integer($value)) { + return civicrm_api3_create_error("contact_id not valid: $value"); + } + $dao = new CRM_Core_DAO(); + $qParams = array(); + $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", + $qParams + ); + if (!$svq) { + return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); + } + $values['contact_id'] = $values['participant_contact_id']; + unset($values['participant_contact_id']); + break; + + case 'participant_register_date': + if (!CRM_Utils_Rule::dateTime($value)) { + return civicrm_api3_create_error("$key not a valid date: $value"); + } + break; + + case 'event_title': + $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); + $values['event_id'] = $id; + break; + + case 'event_id': + if (!CRM_Utils_Rule::integer($value)) { + return civicrm_api3_create_error("Event ID is not valid: $value"); + } + $dao = new CRM_Core_DAO(); + $qParams = array(); + $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value", + $qParams + ); + if (!$svq) { + return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); + } + break; + + case 'participant_status_id': + if (!CRM_Utils_Rule::integer($value)) { + return civicrm_api3_create_error("Event Status ID is not valid: $value"); + } + break; + + case 'participant_status': + $status = CRM_Event_PseudoConstant::participantStatus(); + $values['participant_status_id'] = CRM_Utils_Array::key($value, $status);; + break; + + case 'participant_role_id': + case 'participant_role': + $role = CRM_Event_PseudoConstant::participantRole(); + $participantRoles = explode(",", $value); + foreach ($participantRoles as $k => $v) { + $v = trim($v); + if ($key == 'participant_role') { + $participantRoles[$k] = CRM_Utils_Array::key($v, $role); + } + else { + $participantRoles[$k] = $v; + } + } + $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); + unset($values[$key]); + break; + + default: + break; + } + } + + if (array_key_exists('participant_note', $params)) { + $values['participant_note'] = $params['participant_note']; + } + + // CRM_Event_BAO_Participant::create() handles register_date, + // status_id and source. So, if $values contains + // participant_register_date, participant_status_id or participant_source, + // convert it to register_date, status_id or source + $changes = array( + 'participant_register_date' => 'register_date', + 'participant_source' => 'source', + 'participant_status_id' => 'status_id', + 'participant_role_id' => 'role_id', + 'participant_fee_level' => 'fee_level', + 'participant_fee_amount' => 'fee_amount', + 'participant_id' => 'id', + ); + + foreach ($changes as $orgVal => $changeVal) { + if (isset($values[$orgVal])) { + $values[$changeVal] = $values[$orgVal]; + unset($values[$orgVal]); + } + } + + return NULL; + } + } diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index 4443653446..d97bab2d5f 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -40,178 +40,6 @@ require_once 'api/v3/utils.php'; -/** - * take the input parameter list as specified in the data model and - * convert it into the same format that we use in QF and BAO object - * - * @param array $params - * Associative array of property name/value. - * pairs to insert in new contact. - * @param array $values - * The reformatted properties that we can use internally. - * - * @param array|bool $create Is the formatted Values array going to - * be used for CRM_vent_BAO_Participant:create() - * - * @return array|CRM_Error - */ -function _civicrm_api3_deprecated_participant_formatted_param($params, &$values, $create = FALSE) { - $fields = CRM_Event_DAO_Participant::fields(); - _civicrm_api3_store_values($fields, $params, $values); - - require_once 'CRM/Core/OptionGroup.php'; - $customFields = CRM_Core_BAO_CustomField::getFields('Participant', FALSE, FALSE, NULL, NULL, FALSE, FALSE, FALSE); - - foreach ($params as $key => $value) { - // ignore empty values or empty arrays etc - if (CRM_Utils_System::isNull($value)) { - continue; - } - - // Handling Custom Data - if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) { - $values[$key] = $value; - $type = $customFields[$customFieldID]['html_type']; - if ($type == 'CheckBox' || $type == 'Multi-Select') { - $mulValues = explode(',', $value); - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - $values[$key] = array(); - foreach ($mulValues as $v1) { - foreach ($customOption as $customValueID => $customLabel) { - $customValue = $customLabel['value']; - if ((strtolower(trim($customLabel['label'])) == strtolower(trim($v1))) || - (strtolower(trim($customValue)) == strtolower(trim($v1))) - ) { - if ($type == 'CheckBox') { - $values[$key][$customValue] = 1; - } - else { - $values[$key][] = $customValue; - } - } - } - } - } - elseif ($type == 'Select' || $type == 'Radio') { - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - foreach ($customOption as $customFldID => $customValue) { - $val = CRM_Utils_Array::value('value', $customValue); - $label = CRM_Utils_Array::value('label', $customValue); - $label = strtolower($label); - $value = strtolower(trim($value)); - if (($value == $label) || ($value == strtolower($val))) { - $values[$key] = $val; - } - } - } - } - - switch ($key) { - case 'participant_contact_id': - if (!CRM_Utils_Rule::integer($value)) { - return civicrm_api3_create_error("contact_id not valid: $value"); - } - $dao = new CRM_Core_DAO(); - $qParams = array(); - $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value", - $qParams - ); - if (!$svq) { - return civicrm_api3_create_error("Invalid Contact ID: There is no contact record with contact_id = $value."); - } - $values['contact_id'] = $values['participant_contact_id']; - unset($values['participant_contact_id']); - break; - - case 'participant_register_date': - if (!CRM_Utils_Rule::dateTime($value)) { - return civicrm_api3_create_error("$key not a valid date: $value"); - } - break; - - case 'event_title': - $id = CRM_Core_DAO::getFieldValue("CRM_Event_DAO_Event", $value, 'id', 'title'); - $values['event_id'] = $id; - break; - - case 'event_id': - if (!CRM_Utils_Rule::integer($value)) { - return civicrm_api3_create_error("Event ID is not valid: $value"); - } - $dao = new CRM_Core_DAO(); - $qParams = array(); - $svq = $dao->singleValueQuery("SELECT id FROM civicrm_event WHERE id = $value", - $qParams - ); - if (!$svq) { - return civicrm_api3_create_error("Invalid Event ID: There is no event record with event_id = $value."); - } - break; - - case 'participant_status_id': - if (!CRM_Utils_Rule::integer($value)) { - return civicrm_api3_create_error("Event Status ID is not valid: $value"); - } - break; - - case 'participant_status': - $status = CRM_Event_PseudoConstant::participantStatus(); - $values['participant_status_id'] = CRM_Utils_Array::key($value, $status);; - break; - - case 'participant_role_id': - case 'participant_role': - $role = CRM_Event_PseudoConstant::participantRole(); - $participantRoles = explode(",", $value); - foreach ($participantRoles as $k => $v) { - $v = trim($v); - if ($key == 'participant_role') { - $participantRoles[$k] = CRM_Utils_Array::key($v, $role); - } - else { - $participantRoles[$k] = $v; - } - } - require_once 'CRM/Core/DAO.php'; - $values['role_id'] = implode(CRM_Core_DAO::VALUE_SEPARATOR, $participantRoles); - unset($values[$key]); - break; - - default: - break; - } - } - - if (array_key_exists('participant_note', $params)) { - $values['participant_note'] = $params['participant_note']; - } - - if ($create) { - // CRM_Event_BAO_Participant::create() handles register_date, - // status_id and source. So, if $values contains - // participant_register_date, participant_status_id or participant_source, - // convert it to register_date, status_id or source - $changes = array( - 'participant_register_date' => 'register_date', - 'participant_source' => 'source', - 'participant_status_id' => 'status_id', - 'participant_role_id' => 'role_id', - 'participant_fee_level' => 'fee_level', - 'participant_fee_amount' => 'fee_amount', - 'participant_id' => 'id', - ); - - foreach ($changes as $orgVal => $changeVal) { - if (isset($values[$orgVal])) { - $values[$changeVal] = $values[$orgVal]; - unset($values[$orgVal]); - } - } - } - - return NULL; -} - /** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object -- 2.25.1