From c193e7861544e757e9c79f8bf2a60ef5ba058888 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 29 Jun 2017 08:47:51 +1200 Subject: [PATCH] CRM-20786 combine functions in order to move to class --- CRM/Utils/DeprecatedUtils.php | 303 ++++++++++++++++------------------ 1 file changed, 143 insertions(+), 160 deletions(-) diff --git a/CRM/Utils/DeprecatedUtils.php b/CRM/Utils/DeprecatedUtils.php index 4443653446..75cd9346e5 100644 --- a/CRM/Utils/DeprecatedUtils.php +++ b/CRM/Utils/DeprecatedUtils.php @@ -944,7 +944,149 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { // get the formatted location blocks into params - w/ 3.0 format, CRM-4605 if (!empty($values['location_type_id'])) { - _civicrm_api3_deprecated_add_formatted_location_blocks($values, $params); + static $fields = NULL; + if ($fields == NULL) { + $fields = array(); + } + + foreach (array( + 'Phone', + 'Email', + 'IM', + 'OpenID', + 'Phone_Ext', + ) as $block) { + $name = strtolower($block); + if (!array_key_exists($name, $values)) { + continue; + } + + if ($name == 'phone_ext') { + $block = 'Phone'; + } + + // block present in value array. + if (!array_key_exists($name, $params) || !is_array($params[$name])) { + $params[$name] = array(); + } + + if (!array_key_exists($block, $fields)) { + $className = "CRM_Core_DAO_$block"; + $fields[$block] =& $className::fields(); + } + + $blockCnt = count($params[$name]); + + // copy value to dao field name. + if ($name == 'im') { + $values['name'] = $values[$name]; + } + + _civicrm_api3_store_values($fields[$block], $values, + $params[$name][++$blockCnt] + ); + + if (empty($params['id']) && ($blockCnt == 1)) { + $params[$name][$blockCnt]['is_primary'] = TRUE; + } + + // we only process single block at a time. + return TRUE; + } + + // handle address fields. + if (!array_key_exists('address', $params) || !is_array($params['address'])) { + $params['address'] = array(); + } + + $addressCnt = 1; + foreach ($params['address'] as $cnt => $addressBlock) { + if (CRM_Utils_Array::value('location_type_id', $values) == + CRM_Utils_Array::value('location_type_id', $addressBlock) + ) { + $addressCnt = $cnt; + break; + } + $addressCnt++; + } + + if (!array_key_exists('Address', $fields)) { + $fields['Address'] = CRM_Core_DAO_Address::fields(); + } + + // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. + // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving + // the address in CRM_Core_BAO_Address::create method + if (!empty($values['location_type_id'])) { + static $customFields = array(); + if (empty($customFields)) { + $customFields = CRM_Core_BAO_CustomField::getFields('Address'); + } + // make a copy of values, as we going to make changes + $newValues = $values; + foreach ($values as $key => $val) { + $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); + if ($customFieldID && array_key_exists($customFieldID, $customFields)) { + // mark an entry in fields array since we want the value of custom field to be copied + $fields['Address'][$key] = NULL; + + $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]); + switch ($htmlType) { + case 'CheckBox': + case 'AdvMulti-Select': + case 'Multi-Select': + if ($val) { + $mulValues = explode(',', $val); + $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); + $newValues[$key] = array(); + foreach ($mulValues as $v1) { + foreach ($customOption as $v2) { + if ((strtolower($v2['label']) == strtolower(trim($v1))) || + (strtolower($v2['value']) == strtolower(trim($v1))) + ) { + if ($htmlType == 'CheckBox') { + $newValues[$key][$v2['value']] = 1; + } + else { + $newValues[$key][] = $v2['value']; + } + } + } + } + } + break; + } + } + } + // consider new values + $values = $newValues; + } + + _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); + + $addressFields = array( + 'county', + 'country', + 'state_province', + 'supplemental_address_1', + 'supplemental_address_2', + 'supplemental_address_3', + 'StateProvince.name', + ); + + foreach ($addressFields as $field) { + if (array_key_exists($field, $values)) { + if (!array_key_exists('address', $params)) { + $params['address'] = array(); + } + $params['address'][$addressCnt][$field] = $values[$field]; + } + } + + if ($addressCnt == 1) { + + $params['address'][$addressCnt]['is_primary'] = TRUE; + } return TRUE; } @@ -995,165 +1137,6 @@ function _civicrm_api3_deprecated_add_formatted_param(&$values, &$params) { } } -/** - * This function format location blocks w/ v3.0 format. - * - * @param array $values - * The variable(s) to be added. - * @param array $params - * The structured parameter list. - * - * @return bool - */ -function _civicrm_api3_deprecated_add_formatted_location_blocks(&$values, &$params) { - static $fields = NULL; - if ($fields == NULL) { - $fields = array(); - } - - foreach (array( - 'Phone', - 'Email', - 'IM', - 'OpenID', - 'Phone_Ext', - ) as $block) { - $name = strtolower($block); - if (!array_key_exists($name, $values)) { - continue; - } - - if ($name == 'phone_ext') { - $block = 'Phone'; - } - - // block present in value array. - if (!array_key_exists($name, $params) || !is_array($params[$name])) { - $params[$name] = array(); - } - - if (!array_key_exists($block, $fields)) { - $className = "CRM_Core_DAO_$block"; - $fields[$block] =& $className::fields(); - } - - $blockCnt = count($params[$name]); - - // copy value to dao field name. - if ($name == 'im') { - $values['name'] = $values[$name]; - } - - _civicrm_api3_store_values($fields[$block], $values, - $params[$name][++$blockCnt] - ); - - if (empty($params['id']) && ($blockCnt == 1)) { - $params[$name][$blockCnt]['is_primary'] = TRUE; - } - - // we only process single block at a time. - return TRUE; - } - - // handle address fields. - if (!array_key_exists('address', $params) || !is_array($params['address'])) { - $params['address'] = array(); - } - - $addressCnt = 1; - foreach ($params['address'] as $cnt => $addressBlock) { - if (CRM_Utils_Array::value('location_type_id', $values) == - CRM_Utils_Array::value('location_type_id', $addressBlock) - ) { - $addressCnt = $cnt; - break; - } - $addressCnt++; - } - - if (!array_key_exists('Address', $fields)) { - require_once 'CRM/Core/DAO/Address.php'; - $fields['Address'] = CRM_Core_DAO_Address::fields(); - } - - // Note: we doing multiple value formatting here for address custom fields, plus putting into right format. - // The actual formatting (like date, country ..etc) for address custom fields is taken care of while saving - // the address in CRM_Core_BAO_Address::create method - if (!empty($values['location_type_id'])) { - static $customFields = array(); - if (empty($customFields)) { - $customFields = CRM_Core_BAO_CustomField::getFields('Address'); - } - // make a copy of values, as we going to make changes - $newValues = $values; - foreach ($values as $key => $val) { - $customFieldID = CRM_Core_BAO_CustomField::getKeyID($key); - if ($customFieldID && array_key_exists($customFieldID, $customFields)) { - // mark an entry in fields array since we want the value of custom field to be copied - $fields['Address'][$key] = NULL; - - $htmlType = CRM_Utils_Array::value('html_type', $customFields[$customFieldID]); - switch ($htmlType) { - case 'CheckBox': - case 'AdvMulti-Select': - case 'Multi-Select': - if ($val) { - $mulValues = explode(',', $val); - $customOption = CRM_Core_BAO_CustomOption::getCustomOption($customFieldID, TRUE); - $newValues[$key] = array(); - foreach ($mulValues as $v1) { - foreach ($customOption as $v2) { - if ((strtolower($v2['label']) == strtolower(trim($v1))) || - (strtolower($v2['value']) == strtolower(trim($v1))) - ) { - if ($htmlType == 'CheckBox') { - $newValues[$key][$v2['value']] = 1; - } - else { - $newValues[$key][] = $v2['value']; - } - } - } - } - } - break; - } - } - } - // consider new values - $values = $newValues; - } - - _civicrm_api3_store_values($fields['Address'], $values, $params['address'][$addressCnt]); - - $addressFields = array( - 'county', - 'country', - 'state_province', - 'supplemental_address_1', - 'supplemental_address_2', - 'supplemental_address_3', - 'StateProvince.name', - ); - - foreach ($addressFields as $field) { - if (array_key_exists($field, $values)) { - if (!array_key_exists('address', $params)) { - $params['address'] = array(); - } - $params['address'][$addressCnt][$field] = $values[$field]; - } - } - - if ($addressCnt == 1) { - - $params['address'][$addressCnt]['is_primary'] = TRUE; - } - - return TRUE; -} - /** * * @param array $params -- 2.25.1