private function groupResultsByMerchant($results, $ordering, $direction) { $catValues = array(); foreach ($results as $key => $row) { $catValues[$key] = $row->IsCatalog; } if (isset($ordering) && is_array($results)) { // 'stay' ordering should take place before grouping if (strtolower($ordering) == 'stay') { $pricesValues = array(); foreach ($results as $key => $row) { $pricesValues[$key] = $row->TotalPrice; } array_multisort($catValues, SORT_ASC, $pricesValues, $direction == 'desc' ? SORT_DESC : SORT_ASC, $results); // usort($results, function($a,$b) use ( $ordering, $direction) { // return BFCHelper::orderBy($a, $b, 'TotalPrice', $direction); // }); } if (strtolower($ordering) == 'offer') { $pricesValues = array(); foreach ($results as $key => $row) { $pricesValues[$key] = $row->PercentVariation; } array_multisort($catValues, SORT_ASC, $pricesValues, $direction == 'desc' ? SORT_DESC : SORT_ASC, $results); // usort($results, function($a,$b) use ( $ordering, $direction) { // return BFCHelper::orderBy($a, $b, 'PercentVariation', $direction); //// return BFCHelper::orderBySingleDiscount($a, $b, $direction); // }); } } $arr = array(); foreach ($results as $result) { if (!array_key_exists($result->MerchantId, $arr)) { $merchant = new stdClass(); $merchant->MerchantId = $result->MerchantId; $merchant->MrcCategoryName = $result->DefaultLangMrcCategoryName; $merchant->Name = $result->MrcName; $merchant->XGooglePos = $result->MrcLat; $merchant->YGooglePos = $result->MrcLng; $merchant->MerchantTypeId = $result->MerchantTypeId; $merchant->Rating = $result->MrcRating; $merchant->RatingsContext = $result->RatingsContext; $merchant->RatingsType = $result->RatingsType; $merchant->PaymentType = $result->PaymentType; $merchant->reviewValue = $result->MrcAVG; $merchant->reviewCount = $result->MrcAVGCount; $merchant->LogoUrl = $result->LogoUrl; $merchant->Weight = $result->MrcWeight; $merchant->MrcTagsIdList = $result->MrcTagsIdList; $merchant->ImageUrl = $result->MrcImageUrl; $merchant->Resources = array(); $merchant->Resources[] = $result; $arr[$merchant->MerchantId] = $merchant; } else { $merchant = $arr[$result->MerchantId]; $merchant->Resources[] = $result; } } if (!empty($ordering)) { switch (strtolower($ordering)) { case 'stay': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderByStay($a, $b, $direction); }); break; case 'rating': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'Rating', $direction); }); break; case 'reviewvalue': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'reviewValue', $direction); }); break; case 'offer': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a->Resources[0], $b->Resources[0], 'PercentVariation', $direction); // return BFCHelper::orderByDiscount($a, $b, $direction); }); break; default: $mrcCatalog = array(); $mrcIndexes = array(); foreach ($arr as $key => $row) { /* usort($row->Resources, function($a,$b) use ( $ordering, $direction) { return BFCHelper::orderBy($a, $b, 'ResWeight', 'asc'); }); */ $totalResources = count($row->Resources); $catRes = count(array_filter($row->Resources, function ($rs) { return $rs->IsCatalog; })); $mrcCatalog[$key] = $catRes * 100 / $totalResources; $mrcIndexes[$key] = $key; } array_multisort($mrcCatalog, SORT_ASC, $mrcIndexes, SORT_ASC, $arr); // usort($arr, function($a,$b) use ( $ordering, $direction) { // return BFCHelper::orderBy($a, $b, 'PaymentType', 'desc'); // }); } } else { $mrcCatalog = array(); $mrcWeights = array(); foreach ($arr as $key => $row) { $mrcWeights[$key] = $row->Weight; $catValues = array(); $resWeights = array(); foreach ($row->Resources as $k => $rs) { $catValues[$k] = $rs->IsCatalog; $resWeights[$k] = $rs->ResWeight; } array_multisort($catValues, SORT_ASC, $resWeights, SORT_ASC, $row->Resources); /* usort($row->Resources, function($a,$b) use ( $ordering, $direction) { return BFCHelper::orderBy($a, $b, 'ResWeight', 'asc'); }); */ $totalResources = count($row->Resources); $catRes = count(array_filter($row->Resources, function ($rs) { return $rs->IsCatalog; })); $mrcCatalog[$key] = $catRes * 100 / $totalResources; } // array_multisort($mrcCatalog, SORT_ASC, $arr); // usort($arr, function($a,$b) use ( $ordering, $direction) { // return BFCHelper::orderBy($a, $b, 'Weight', 'asc'); // }); // usort($arr->Resources[], function($a,$b) use ( $ordering, $direction) { // return BFCHelper::orderBy($a, $b, 'ResWeight', 'asc'); // }); } return $arr; }
private function groupResultsByMerchant($results, $ordering, $direction) { if (isset($ordering) && is_array($results)) { // 'stay' ordering should take place before grouping if (strtolower($ordering) == 'stay') { usort($results, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'TotalPrice', $direction); }); } if (strtolower($ordering) == 'offer') { usort($results, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'PercentVariation', $direction); // return BFCHelper::orderBySingleDiscount($a, $b, $direction); }); } } $arr = array(); foreach ($results as $result) { if (!array_key_exists($result->MerchantId, $arr)) { $merchant = new stdClass(); $merchant->MerchantId = $result->MerchantId; $merchant->Name = $result->MrcName; $merchant->XGooglePos = $result->MrcLat; $merchant->YGooglePos = $result->MrcLng; $merchant->MerchantTypeId = $result->MerchantTypeId; $merchant->Rating = $result->MrcRating; $merchant->RatingsContext = $result->RatingsContext; $merchant->PaymentType = $result->PaymentType; $merchant->reviewValue = $result->MrcAVG; $merchant->reviewCount = $result->MrcAVGCount; $merchant->LogoUrl = $result->LogoUrl; $merchant->Weight = $result->MrcWeight; $merchant->MrcTagsIdList = $result->MrcTagsIdList; $merchant->ImageUrl = $result->MrcImageUrl; $merchant->Resources = array(); $merchant->Resources[] = $result; $arr[$merchant->MerchantId] = $merchant; } else { $merchant = $arr[$result->MerchantId]; $merchant->Resources[] = $result; } } if (isset($ordering)) { switch (strtolower($ordering)) { case 'stay': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderByStay($a, $b, $direction); }); break; case 'rating': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'Rating', $direction); }); break; case 'reviewvalue': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'reviewValue', $direction); }); break; case 'offer': usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a->Resources[0], $b->Resources[0], 'PercentVariation', $direction); // return BFCHelper::orderByDiscount($a, $b, $direction); }); break; default: usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'PaymentType', 'desc'); }); } } else { usort($arr, function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'Weight', 'asc'); }); usort($arr->Resources[], function ($a, $b) use($ordering, $direction) { return BFCHelper::orderBy($a, $b, 'ResWeight', 'asc'); }); } return $arr; }