<?php
class ControllerMadeshopReviewProduct extends Controller {
	public function index() {
        $this->load->language('madeshop/review_product');
		$data['text_no_reviews'] = $this->language->get('text_no_reviews');
		$data['review_info'] = $this->language->get('review_info');
        $data['text_ready'] = $this->language->get('text_ready');

		$this->load->model('madeshop/review_product');

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		$limit = 50;

		$data['reviews'] = array();

		$product_id = $this->request->get['pid'];
		$results = $this->model_madeshop_review_product->getReviewsByProductId($product_id, ($page - 1) * $limit, $limit);
		$data['review_total'] = $review_total = $this->model_madeshop_review_product->getTotalReviews($product_id);

        $review_num = $this->model_madeshop_review_product->getTotalNum($product_id);
		
		$arr = array(1=>0,2=>0,3=>0,4=>0,5=>0);
		
		$rev_t = $review_num + $arr; krsort($rev_t);
		
		$data['review_num'] = $rev_t;

		foreach ($results as $result) {
			$data['reviews'][] = array(
				'review_id'  => $result['review_id'],
				'author'     => $result['author'],
				'text'       => nl2br($result['text']),
				'mtext'       => nl2br($result['mtext']),
				'rating'     => (int)$result['rating'],
				'yes'        => (int)$result['yes'],
				'unlike'     => (int)$result['unlike'],
				'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added']))
			);
		}
        $pagination = new Mpagination();

		$pagination->total = $review_total;
		$pagination->page = $page;
		$pagination->limit = $limit;
		$pagination->url = $this->url->link('madeshop/review_product', 'product_id=' . $product_id . '&page={page}');

		$data['pagination'] = $pagination->render();

		$data['results'] = sprintf($this->language->get('text_pagination'), ($review_total) ? (($page - 1) * $limit) + 1 : 0, ((($page - 1) * $limit) > ($review_total - $limit)) ? $review_total : ((($page - 1) * $limit) + $limit), $review_total, ceil($review_total / $limit));

		$this->response->setOutput($this->load->view('madeshop/review_product', $data));
	}
	
	public function write() {
		$this->load->language('product/product');

		$json = array();

		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 25)) {
				$json['error'] = $this->language->get('error_name');
			}

			if ((utf8_strlen($this->request->post['text']) < 25) || (utf8_strlen($this->request->post['text']) > 1000)) {
				$json['error'] = $this->language->get('error_text');
			}

			if (empty($this->request->post['rating']) || $this->request->post['rating'] < 0 || $this->request->post['rating'] > 5) {
				$json['error'] = $this->language->get('error_rating');
			}

			// Captcha
			if ($this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
				$captcha = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha') . '/validate');

				if ($captcha) {
					$json['error'] = $captcha;
				}
			}

			if (!isset($json['error'])) {
				$this->load->model('catalog/review');

				$this->model_catalog_review->addReview($this->request->get['product_id'], $this->request->post);

				$json['success'] = $this->language->get('text_success');
			}
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	
	public function vote() {
       
        $json = '';
	    $votes = array();
	    $id = $this->request->post['id'];
	
	    if (isset($this->request->cookie['vote'])) {
	        $votes = explode(',', $this->request->cookie['vote']);
	    }
	
        $varray = array_slice($votes, 0);
        
        if (in_array($id, $varray)) {
           $json = false;
        } else  {
        	$this->load->model('madeshop/review_product');
            $this->model_madeshop_review_product->addVote($this->request->post);
        	$json = true;
        }	

    	$votes2 = array_diff($votes, array($id));
    	array_unshift($votes2, $id);
    	setcookie('vote', implode(',',$votes2), time() + 60 * 60 * 24 * 30, '/', $this->request->server['HTTP_HOST']);

        $this->response->addHeader('Content-Type: application/json');
        $this->response->setOutput(json_encode($json));
	
	}

}