From 325ddbb697ac4873429adfce1519277b37fcd0f3 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 16 Feb 2015 12:21:49 +0300 Subject: [PATCH] fix clear array && explode fio --- .../classes/general/ICrmOrderActions.php | 67 +++++++++++-------- 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/intaro.intarocrm/classes/general/ICrmOrderActions.php b/intaro.intarocrm/classes/general/ICrmOrderActions.php index b80c6df4..6f9c4c9b 100755 --- a/intaro.intarocrm/classes/general/ICrmOrderActions.php +++ b/intaro.intarocrm/classes/general/ICrmOrderActions.php @@ -1310,12 +1310,8 @@ class ICrmOrderActions $resOrder['site'] = $arFields['LID']; // parse fio - if(count($contactNameArr) == 1) { - $resOrder['firstName'] = $contactNameArr[0]; - } else { - $resOrder['lastName'] = $contactNameArr[0]; - $resOrder['firstName'] = $contactNameArr[1]; - $resOrder['patronymic'] = $contactNameArr[2]; + if(count($contactNameArr) > 0) { + $resOrder = array_merge($resOrder, $contactNameArr); } // custom orderType function @@ -1396,18 +1392,19 @@ class ICrmOrderActions * @return array */ public static function clearArr($arr) { - if(!$arr || !is_array($arr)) - return false; - - foreach($arr as $key => $value) { - if((!($value) && $value !== 0) || (is_array($value) && empty($value))) - unset($arr[$key]); - - if(is_array($value) && !empty($value)) - $arr[$key] = self::clearArr($value); + if (is_array($arr) === false) { + return $arr; } - return $arr; + $result = array(); + foreach ($arr as $index => $node ) { + $result[ $index ] = is_array($node) === true ? self::clearArr($node) : trim($node); + if ($result[ $index ] == '' || $result[ $index ] === null || count($result[ $index ]) < 1) { + unset($result[ $index ]); + } + } + + return $result; } /** @@ -1434,21 +1431,33 @@ class ICrmOrderActions return $APPLICATION->ConvertCharset($str, 'utf-8', SITE_CHARSET); } - public static function explodeFIO($str) { - if(!$str) - return array(); - - $array = explode(" ", self::toJSON($str), 3); - $newArray = array(); - - foreach($array as $ar) { - if(!$ar) - continue; - - $newArray[] = $ar; + public static function explodeFIO($fio) { + $newFio = empty($fio) ? false : explode(" ", self::toJSON($fio), 3); + $result = array(); + switch (count($newFio)) { + default: + case 0: + $result['firstName'] = $fio; + break; + case 1: + $result['firstName'] = $newFio[0]; + break; + case 2: + $result = array( + 'lastName' => $newFio[1], + 'firstName' => $newFio[0] + ); + break; + case 3: + $result = array( + 'lastName' => $newFio[1], + 'firstName' => $newFio[0], + 'patronymic' => $newFio[2] + ); + break; } - return $newArray; + return $result; } public static function addOrderProperty($code, $value, $order) {