<?php

class ModuleReview extends Controller
{
    public function fetch()
    {
 //   	echo "<script>alert('REQUEST_URI=".$_SERVER['REQUEST_URI']."');</script>";
        $currency =& $this->locator->get('currency');
        $database =& $this->locator->get('database');
        $language =& $this->locator->get('language');
        $image    =& $this->locator->get('image');
        $module   =& $this->locator->get('module');
        $request  =& $this->locator->get('request');
        $session  =& $this->locator->get('session');
        $response =& $this->locator->get('response');
        $shipping =& $this->locator->get('shipping');
        $tax      =& $this->locator->get('tax');
        $url      =& $this->locator->get('url');
        $customer =& $this->locator->get('customer');
        $template = $this->locator->create('template');

        $product_id = (int) $request->get('product_id');  
        $page=$request->has('page')?$request->get('page'):1;

        $product_reviews = $database->getRows($database->splitQuery("select * from `review` where product_id=$product_id order by date_added desc", $page, 10));

        $page_data = array();
        for ($i = 1; $i <= $database->getPages(); $i++) {
              $page_data[] = array(
                'text'  => $language->get('text_pages', $i, $database->getPages()),
                'value' => $i
              );
        }
        $template->set('pages', $page_data);
        $template->set('page', $page);

        $template->set('review_count', $database->countRows("select * from `review` where product_id=$product_id"));
        foreach ($product_reviews as $k=>$review) {
           if (empty($review['content'])) { $product_reviews[$k]['content']="No review left for this order. Default review rating given"; }

           switch ($review['product_size']) {
            case 1:
                {
                    $product_reviews[$k]['product_size']=$review['product_size']*16; break;
                }
            case 2:
                {
                    $product_reviews[$k]['product_size']=$review['product_size']*36; break;
                }
            case 3:
                {
                    $product_reviews[$k]['product_size']=$review['product_size']*41; break;
                }
            default:
                {
                    break;
                }
           }
            switch ($review['product_color']) {
            case 1:
                {
                    $product_reviews[$k]['product_color']=$review['product_color']*16; break;
                }
            case 2:
                {
                    $product_reviews[$k]['product_color']=$review['product_color']*36; break;
                }
            case 3:
                {
                    $product_reviews[$k]['product_color']=$review['product_color']*41; break;
                }
            default:
                {
                    break;
                }
           }
            switch ($review['product_material']) {
            case 1:
                {
                    $product_reviews[$k]['product_material']=$review['product_material']*16; break;
                }
            case 2:
                {
                    $product_reviews[$k]['product_material']=$review['product_material']*36; break;
                }
            case 3:
                {
                    $product_reviews[$k]['product_material']=$review['product_material']*41; break;
                }
            default:
                {
                    break;
                }
           }

           $product_reviews[$k]['images']=$database->getRows("select review_image_filename from `review_image` where review_id=".$review['review_id']);
           foreach ($product_reviews[$k]['images'] as $i=>$f_review_image) {
            $product_reviews[$k]['images'][$i]=CDN_REVIEW_IMAGE.$f_review_image['review_image_filename'];
           }

           $country_info=$database->getRow("select country_id,name,icon from country where name='".$review['country_name']."' ");
           $product_reviews[$k]['country_info']=array(
                  'country_id'=>$country_info['country_id'],
                'country_name'=>$country_info['name'],
                'icon'=>CDN_COUNTRY_IMAGE.$country_info['icon']
            );

        }

        $template->set('product_reviews', $product_reviews);
        $template->set('review_total', count($product_reviews)); 
        
        $review=$database->getRow("select avg(rating) as avg from `review` where product_id=$product_id");
           $rating_stars[]=round($review['avg'],1);
           for ($i = 1; $i <= 5; $i++) {
               $star_rows=$database->countRows("select review_id as total from `review` where rating=$i and product_id=$product_id");
            $rating_stars[]=$star_rows;
        }
        $ajax_reviews_url = $url -> href('product', 'ajax_reviews_list');
        $template->set('rating_stars', $rating_stars);
        $template->set('ajax_reviews_url', $ajax_reviews_url.'&product_id='.$product_id);

        return $template->fetch('module/review.tpl');
    }
}
