<?php
/***************************************************************************
*                                                                          *
* Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
*                                                                          *
* This  is  commercial  software,  only  users  who have purchased a valid *
* license  and  accept  to the terms of the  License Agreement can install *
* and use this program.                                                    *
*                                                                          *
****************************************************************************
* PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
* "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
****************************************************************************/
//
//$Id: AjaxController.php, v 1.0 2011/06/14 20:52:11
//
class BuyboxController extends Controller
{
	public function indexAction()
	{
		$ObjBuybox		=   new BuyboxModel();
                $ObjBusiness		=   new BusinessModel();
                $ObjProductList         =   new ProductListModel();
		$languageIDs		=   $this->ObjView->languageIDs;
                $productTitle         	=   $this->decodeUrlString($this->getRequestValue("productTitle"));
                $sortVal         	=   $this->getRequestValue("sortby");
                $orderby         	=   $this->getRequestValue("orderby");
                $link                   =   "/buybox/index/";
                if(isset($_POST['submitSearch']) && $_POST['submitSearch']=='Search'){
                    $searchTerm         =   $this->encodeUrlString(trim($_POST['productName']));
                    $this->setRedirect($link.'productTitle/'.$searchTerm.'/');
                }
                $sortby = '';
                $resProducts            =   $ObjBuybox->getBuyBoxProducts($languageIDs,$productTitle,$sortby,$orderby);
                $resOrderProducts       =   array();

                for($i=0;$i<count($resProducts);$i++){
                    $businessID                 =   $resProducts[$i]['businessID'];
                    $baseProductID                 =   $resProducts[$i]['baseProductID'];
                    $productID                     =   $resProducts[$i]['productID'];
                    if($productID==$baseProductID){
                        $resOrderProducts['product_'.$baseProductID]  = $resProducts[$i];
                        $resOrderProducts['product_'.$baseProductID]['busRating']     =   $ObjBusiness->selectUserRating($businessID);
                        $resOrderProducts['product_'.$baseProductID]['userRating']	=   $ObjProductList->fetchProductReviews($productID);
                        
                    }else{
                        if(!empty($resOrderProducts['product_'.$baseProductID])) {
                            $baseArrayCnt      =  count($resOrderProducts['product_'.$baseProductID]);
                        } else {
                            $baseArrayCnt      =  0;
                        }
                        if(!empty($resOrderProducts['product_'.$baseProductID]['subProducts'])) {
                            $subArrayCnt       =  count($resOrderProducts['product_'.$baseProductID]['subProducts']);
                        } else {
                            $subArrayCnt       =  0;
                        }
                        if($baseArrayCnt>0){
                            $resOrderProducts['product_'.$baseProductID]['subProducts'][$subArrayCnt]  = $resProducts[$i];
                            $resOrderProducts['product_'.$baseProductID]['subProducts'][$subArrayCnt]['busRating']	=   $ObjBusiness->selectUserRating($businessID);
                            $resOrderProducts['product_'.$baseProductID]['subProducts'][$subArrayCnt]['userRating']	=   $ObjProductList->fetchProductReviews($productID);
                        }
                    }
                }
                $resOrderProducts = array_values($resOrderProducts);
                $sortmerchant = '';
               
                if($sortVal==''){
                    $sortVal = 'product';
                    $orderby = 'asc';
                }
                if($sortVal=='product'){
                    $sortby = 'productTitle';
                }
                if($sortVal=='merchant'){
                    $sortby = 'businessName';
                    $sortmerchant = 'sort'.$orderby;
                }
                if($sortVal=='price'){
                    $sortby = 'productOptionProductPrice';
                }
                if($sortVal=='commision'){
                    $sortby = 'productCommissionRate';
                }
                 
                if(($sortby=='businessName')||($sortby=='productTitle')){
                    $sortName = 'Yes';
                }else{
                    $sortName = 'No';
                }
                if($sortVal!='' && $orderby=='asc'){
                    //Sort Entire Products by Business Name ASC
                    $resOrderProducts = $this->sortArrAsc($resOrderProducts,$sortName,$sortby);
                    $this->ObjView->assign('sort'.$sortVal, 'asc');
                }else  if($sortVal!='' && $orderby=='desc'){
                    //Sort Entire Products by Business Name DESC
                    $resOrderProducts = $this->sortArrDesc($resOrderProducts,$sortName,$sortby);
                    $this->ObjView->assign('sort'.$sortVal, 'desc');
                }else{
                    $this->ObjView->assign('sort'.$sortVal, '');
                }
                
                //echo "<pre>"; print_r($resOrderProducts); exit;
                
                
		$this->ObjView->assign('resProducts', $resOrderProducts);           
                $this->ObjView->assign('link', $link);
                $this->ObjView->assign('sortVal', $sortVal); 
                $this->ObjView->assign('sortOrder', $orderby); 
                $this->ObjView->assign('productName', $productTitle); 
		$this->ObjView->display('BuyBox.tpl'); 
	}
        
        public function setBaseProductAction(){
            $ObjBuybox		=   new BuyboxModel();
            $buyboxID           =   $this->getRequestValue("id");
            $baseProductID         =   $this->getRequestValue("productID");
            if($baseProductID>0 && $buyboxID>0){
                $setBase =   $ObjBuybox->setBaseproduct($buyboxID,$baseProductID);
                $this->setMessage("language.setBaseProductUpdate", "message_success");
                $this->setRedirect('/buybox/index/');
                
            }
        }
        
        public function sortArrAsc($resOrderProducts,$string='No',$fieldVal){
            //Sort Base Products by ASC
            usort($resOrderProducts, function($a, $b) use ($string, $fieldVal) {
                if($string=='Yes'){
                    return strcasecmp($a[$fieldVal],$b[$fieldVal]);
                }else{
                    return $a[$fieldVal]-$b[$fieldVal];
                }
            });
            //Sort Sub Products by ASC
            if($fieldVal=='productTitle'){
                $fieldVal = 'businessName';
            }
            for($i=0;$i<count($resOrderProducts);$i++){
				if($resOrderProducts[$i]['subProducts']!=''){
					usort($resOrderProducts[$i]['subProducts'], function($c, $d) use ($string, $fieldVal) {
						if($string=='Yes'){
							return strcasecmp($c[$fieldVal],$d[$fieldVal]);
						}else{
							return $c[$fieldVal]-$d[$fieldVal];
						}
					});
				}
            }
            return $resOrderProducts;
        }
        
        public function sortArrDesc($resOrderProducts,$string='No',$fieldVal){
            //Sort Base Products by DESC
            usort($resOrderProducts, function($a, $b) use ($string, $fieldVal) {
                if($string=='Yes'){
                    return strcasecmp($b[$fieldVal],$a[$fieldVal]);
                }else{
                    return $b[$fieldVal]-$a[$fieldVal];
                }
            });
            //Sort Sub Products by DESC
            for($i=0;$i<count($resOrderProducts);$i++){
                usort($resOrderProducts[$i]['subProducts'], function($c, $d) use ($string, $fieldVal) {
                    if($string=='Yes' && $fieldVal=='productTitle'){
                        return strcasecmp($c['businessName'],$d['businessName']);
                    }else if($string=='Yes' && $fieldVal!='productTitle'){
                        return strcasecmp($d[$fieldVal],$c[$fieldVal]);
                    }else{
                        return $d[$fieldVal]-$c[$fieldVal];
                    }
                });
            }
            return $resOrderProducts;
        }
}