<?php
class ControllerCiReviewProCiReview extends Controller {
	public function __construct($registry) {
		parent :: __construct($registry);
		$this->load->language('product/product');
		$this->load->language('cireviewpro/cireview');
		$this->load->model('cireviewpro/cireview');
		$this->load->model('catalog/product');
		$this->load->model('account/customer');
		$this->load->model('account/address');
		$this->load->model('tool/image');
	}
	public function index() {
		if($this->config->get('cireviewpro_status')) {
			$this->document->addStyle('catalog/view/theme/default/stylesheet/cireviewpro/cireview.css');
			$this->document->addScript('catalog/view/javascript/cireviewpro/rating/bootstrap-rating-input.js');
			$data['product_id'] = $this->request->get['product_id'];
			$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
			$data['product_name'] = $product_info['name'];
			$data['heading_title'] = $this->language->get('heading_title');
			$data['text_clear_filters'] = $this->language->get('text_clear_filters');
			$data['sorts'] = array();
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_default'),
				'value' => 'r.cireview_id-ASC',
				'class' => 'ci-reviewid_desc',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'r.cireview_id-ASC',
			);
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_rating_desc'),
				'value' => 'r.rating-DESC',
				'class' => 'ci-rating_desc purple',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'r.rating-DESC',
			);
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_rating_asc'),
				'value' => 'r.rating-ASC',
				'class' => 'ci-rating_asc green',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'r.rating-ASC',
			);
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_date_desc'),
				'value' => 'r.date_added-DESC',
				'class' => 'ci-date_desc orange',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'r.date_added-DESC',
			);
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_date_asc'),
				'value' => 'r.date_added-ASC',
				'class' => 'ci-date_asc purple',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'r.date_added-ASC',
			);
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_votesup_desc'),
				'value' => 'votes_up-DESC',
				'class' => 'ci-votes_up_desc green',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'votes_up-DESC',
			);
			$data['sorts'][] = array(
				'text'  => $this->language->get('text_votesdown_desc'),
				'value' => 'votes_down-DESC',
				'class' => 'ci-votes_down_desc orange',
				'selected' => $this->config->get('cireviewpro_reviewsortdefault') == 'votes_down-DESC',
			);
			$data['reviewfilters'] = $this->config->get('cireviewpro_reviewfilters');
			$data['reviewfiltersimage'] = $this->config->get('cireviewpro_reviewfiltersimage');
			$data['reviewfilterssearch'] = $this->config->get('cireviewpro_reviewfilterssearch');
			$data['reviewfilterstype'] = $this->config->get('cireviewpro_reviewfilterstype');
			$data['reviewsortdefault'] =  $this->config->get('cireviewpro_reviewsortdefault');
			$data['reviewauthorfl_bgcolor'] =  $this->config->get('cireviewpro_reviewauthorfl_bgcolor');
			$data['reviewauthorfl_color'] =  $this->config->get('cireviewpro_reviewauthorfl_color');
			$data['reviewauthorradius'] =  (int)$this->config->get('cireviewpro_reviewauthorradius');

			// if search is disable then filter image and search also not works.
			if(!$data['reviewfilters']) {
				$data['reviewfiltersimage'] = false;
				$data['reviewfilterssearch'] = false;
			}

			$reviewfiltersshow = $this->config->get('cireviewpro_reviewfiltersshow');
			if(!isset($reviewfiltersshow['show'])) {
				$data['sorts'] = array();
			} else {
				foreach ($data['sorts'] as $key => &$sorts) {
					if(!in_array($sorts['value'], $reviewfiltersshow['show'])) {
						unset($data['sorts'][$key]);
					} else {
						$sorts['sort_order'] = (isset($reviewfiltersshow['sort_order'][$sorts['value']]) && $reviewfiltersshow['sort_order'][$sorts['value']] != '') ? (int)$reviewfiltersshow['sort_order'][$sorts['value']] : 0;
					}
				}
			}
			$sortings = array();
			foreach ($data['sorts'] as $key => $row) {
			    $sortings[$key] = $row['sort_order'];
			}
			array_multisort($sortings, SORT_ASC, $data['sorts']);
			$data['imagefilters'] = array();
			$data['imagefilters'][] = array(
				'text'  => $this->language->get('text_withimg'),
				'value' => 1,
				'class' => 'pink',
				'selected' => false
			);
			$data['imagefilters'][] = array(
				'text'  => $this->language->get('text_withoutimg'),
				'value' => 0,
				'class' => 'grey',
				'selected' => false
			);
			if ($data['reviewfilterstype'] == 'dropdown') {
				array_unshift($data['sorts'], array(
					'text'  => $this->language->get('text_select'),
					'value' => '',
					'class' => 'ci- orange',
					'selected' => false,
				));
				array_unshift($data['imagefilters'], array(
					'text'  => $this->language->get('text_select'),
					'value' => '',
					'selected' => false
				));
			}
			$data['text_cisearch'] = $this->language->get('text_cisearch');
			$data['text_sort'] = $this->language->get('text_sort');
			$data['text_imagefilter'] = $this->language->get('text_imagefilter');
			$data['text_reviewreport'] = $this->language->get('text_reviewreport');
			$data['text_other'] = $this->language->get('text_other');
			$data['text_other_reason'] = $this->language->get('text_other_reason');
			$data['text_loading'] = $this->language->get('text_loading');
			$data['text_note'] = $this->language->get('text_note');
			$data['text_write'] = $this->language->get('text_write') .' '. sprintf($this->language->get('text_writereview'), $product_info['name']);
			$data['text_login'] = sprintf($this->language->get('text_login'), $this->url->link('account/login', '', true), $this->url->link('account/register', '', true));
			$data['text_purchasefirst'] = $this->language->get('text_purchasefirst');
			$data['text_withimg'] = $this->language->get('text_withimg');
			$data['text_withoutimg'] = $this->language->get('text_withoutimg');
			$data['text_customer_image'] = $this->language->get('text_customer_image');
			$data['text_customer_image_all'] = $this->language->get('text_customer_image_all');
			$data['text_view_image_gallery'] = $this->language->get('text_view_image_gallery');
			$data['text_by'] = $this->language->get('text_by');
			$data['text_on'] = $this->language->get('text_on');
			$data['entry_name'] = $this->language->get('entry_name');
			$data['entry_email'] = $this->language->get('entry_email');
			$data['entry_reviewtitle'] = $this->language->get('entry_reviewtitle');
			$data['entry_review'] = $this->language->get('entry_review');
			$data['entry_attachimages'] = $this->language->get('entry_attachimages');
			$data['entry_rating'] = $this->language->get('entry_rating');
			$data['button_continue'] = $this->language->get('button_continue');
			$data['button_write'] = $this->language->get('button_write');
			$data['button_upload'] = $this->language->get('button_upload');
			$data['button_cancel'] = $this->language->get('button_cancel');
			$data['button_submit'] = $this->language->get('button_submit');
			$ratingtypes = $this->model_cireviewpro_cireview->getCiRatingTypes($this->request->get['product_id']);
			$data['ratingtypes'] = array();
			foreach ($ratingtypes as $ratingtype) {
				$data['ratingtypes'][] = array(
					'ciratingtype_id' => $ratingtype['ciratingtype_id'],
					'name' => $ratingtype['name'],
				);
			}
			$data['status'] = $this->config->get('cireviewpro_status');
			$data['rating'] = $this->config->get('cireviewpro_rating');
			$data['email'] = $this->config->get('cireviewpro_reviewemail');
			$data['title'] = $this->config->get('cireviewpro_reviewtitle');
			$data['title_require'] = $this->config->get('cireviewpro_reviewtitle_require');
			$data['author'] = $this->config->get('cireviewpro_reviewauthor');
			$data['author_require'] = $this->config->get('cireviewpro_reviewauthor_require');
			$data['text'] = $this->config->get('cireviewpro_reviewtext');
			$data['text_require'] = $this->config->get('cireviewpro_reviewtext_require');
			$data['reviewgraph_color'] = $this->config->get('cireviewpro_reviewgraph_color');
			$data['reviewgraph'] = $this->config->get('cireviewpro_reviewgraph');
			$data['reviewimp'] = $this->config->get('cireviewpro_reviewimp');
			$data['sharetype'] = $this->config->get('cireviewpro_sharetype');
			$data['reviewgraph_option'] = $this->config->get('cireviewpro_reviewgraph_option');
			$data['rewardsuccessalert'] = $this->config->get('cireviewpro_rewardsuccessalert');
			$data['reviewpolicy'] = $this->config->get('cireviewpro_reviewpolicy');
			$data['reviewaddon'] = $this->config->get('cireviewpro_reviewaddon');
			$reviewpolicypage_info = (array)$this->config->get('cireviewpro_reviewpolicypage');
			if(isset($reviewpolicypage_info[(int)$this->config->get('config_language_id')])) {
				$data['reviewpolicypage'] = $reviewpolicypage_info[(int)$this->config->get('config_language_id')];
				/*check if array is not empty*/
			} else if($reviewpolicypage_info) {
				reset($reviewpolicypage_info);
				$first_key = key($reviewpolicypage_info);
				$data['reviewpolicypage'] = $reviewpolicypage_info[$first_key];
			}
			if(isset($data['reviewpolicypage']['message'])) {
				$data['reviewpolicypage']['message'] = html_entity_decode($data['reviewpolicypage']['message'], ENT_QUOTES, 'UTF-8');
			}
			// no. of stars
			$data['ratingstars'] = 5;
			// if((int)$this->config->get('cireviewpro_ratingstars')) {
			// 	$data['ratingstars'] = (int)$this->config->get('cireviewpro_ratingstars');
			// }
			$data['cireviewgraph'] =  $this->cireviewGraph();
			$data['aggeratereview'] = $this->aggerateReview();
			// attach images in review
			$data['reviewimages'] = (int)$this->config->get('cireviewpro_reviewimages');
			// get uploaded images of user
			$attach_images = $this->model_cireviewpro_cireview->getUploadedImage();
			$data['attach_images'] = array();
			$cireview_image = array();
			$thumbwidth = (int)$this->config->get('cireviewpro_reviewimagesthumb_width');
			$thumbheight = (int)$this->config->get('cireviewpro_reviewimagesthumb_height');
			if(empty($thumbwidth) || is_null($thumbwidth) ) {
				$thumbwidth = 100;
			}
			if(empty($thumbheight) || is_null($thumbheight) ) {
				$thumbheight = 100;
			}
			$popupwidth = (int)$this->config->get('cireviewpro_reviewimagespopup_width');
			$popupheight = (int)$this->config->get('cireviewpro_reviewimagespopup_height');
			if(empty($popupwidth) || is_null($popupwidth) ) {
				$popupwidth = 500;
			}
			if(empty($popupheight) || is_null($popupheight) ) {
				$popupheight = 500;
			}
			foreach ($attach_images as $attach_image) {
				if(!empty($attach_image['image']) && file_exists(DIR_IMAGE .$attach_image['image'] )) {
					$thumb = $this->model_tool_image->resize($attach_image['image'], $thumbwidth, $thumbheight ) ;
					$popup = $this->model_tool_image->resize($attach_image['image'], $popupwidth, $popupheight ) ;
				} else {
					$thumb = $this->model_tool_image->resize('no_image.png', $thumbwidth, $thumbheight ) ;
					$popup = $this->model_tool_image->resize('no_image.png', $popupwidth, $popupheight ) ;
				}
				$cireview_image[] = $attach_image['cireview_image_id'];
				$image_description = $this->model_cireviewpro_cireview->getCiReviewImagesDescriptions($attach_image['cireview_image_id']);
				$data['attach_images'][] = array(
					'cireview_image_id' => $attach_image['cireview_image_id'],
					'thumb' => $thumb,
					'popup' => $popup,
					'alt' => !empty($image_description['alt']) ? htmlentities($image_description['alt'], ENT_QUOTES, 'UTF-8') : '',
					'title' => !empty($image_description['title']) ? htmlentities($image_description['title'], ENT_QUOTES, 'UTF-8') : '',
				);
			}
			$data['cireview_image'] = '';
			if($cireview_image) {
				$data['cireview_image'] = implode(",", $cireview_image);
			}
			if ($this->config->get('cireviewpro_reviewguest') || $this->customer->isLogged()) {
				$data['cireview_guest'] = true;
			} else {
				$data['cireview_guest'] = false;
			}
			$data['cireview_purchasefirst'] = false;
			// first we ask to login for review
			if ($this->config->get('cireviewpro_reviewpurchaseonly') && !$this->customer->isLogged()) {
				// $data['cireview_guest'] = false;
				$data['cireview_purchasefirst'] = true;
			}
			if ($this->config->get('cireviewpro_reviewpurchaseonly') && $this->customer->isLogged()) {
				// check if customer buy this product and not give review yet
				$purchasefirst = $this->model_cireviewpro_cireview->isRequirePurchaseFirst($this->request->get['product_id'],(array)$this->config->get('cireviewpro_verifypurchase_orderstatus'));
				//$purchasefirst = $this->model_cireviewpro_cireview->isProductRequirePurchase($this->request->get['product_id']);
				//$purchasefirst = $this->model_cireviewpro_cireview->isProductOrderCompleted($this->request->get['product_id'],(array)$this->config->get('cireviewpro_verifypurchase_orderstatus'));
				$data['cireview_purchasefirst'] = !($purchasefirst);
			}
			if ($this->customer->isLogged()) {
				$data['customer_name'] = $this->customer->getFirstName() . '&nbsp;' . $this->customer->getLastName();
				$data['customer_email'] = $this->customer->getEmail();
			} else {
				$data['customer_name'] = '';
				$data['customer_email'] = '';
			}
			// Captcha
			if (VERSION >= '3.0.0.0' && $this->config->get('cireviewpro_captcha') && $this->config->get('captcha_' . $this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
					$data['captcha'] = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha'));
			} else if (VERSION < '3.0.0.0' && $this->config->get('cireviewpro_captcha') && $this->config->get($this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
				if (VERSION <= '2.2.0.0') {
					$data['captcha'] = $this->load->controller('captcha/' . $this->config->get('config_captcha'));
				} else {
					$data['captcha'] = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha'));
				}
			} else {
				$data['captcha'] = '';
			}
			// abuse reasons
			$ciabreasons = $this->model_cireviewpro_cireview->getCiAbReasons($this->request->get['product_id']);
			$data['ciabreasons'] = array();
			foreach ($ciabreasons as $ciabreason) {
				$data['ciabreasons'][] = array(
					'ciabreason_id' => $ciabreason['ciabreason_id'],
					'name' => $ciabreason['name'],
					'details' => $ciabreason['details'],
				);
			}


			$data['theme_name'] = $this->model_cireviewpro_cireview->getTheme();

			if(utf8_strrpos($data['theme_name'], 'journal') !== false) {
				$data['journal_class'] = 'journal-wrap';
			} else{
				$data['journal_class'] = '';
			}

			/*get product all reviews to show in view source only*/
			$data['review'] = '';
			$data['reviewviewsource'] = $this->config->get('cireviewpro_reviewviewsource');
			if($this->config->get('cireviewpro_reviewviewsource')) {
				$data['review'] = $this->review();
			}
			$data['topreviews'] = $this->topReviews();
			/*rich snippets starts*/
			$data['rich'] = '';
			$totalreviews = 0;
			$results = array();
			if($this->config->get('cireviewpro_richsnippets')) {
				$filter_data = array(
	                'product_id' => $this->request->get['product_id'],
	                'sort' => 'r.date_added',
	                'order' => 'DESC',
	            );
	            $totalreviews = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
				$results = $this->model_cireviewpro_cireview->getCiReviewsByProductId($filter_data);
			}
			if($totalreviews > 0) {
				$rich_snippets = array();
				$rich_snippets["@context"] = "http://schema.org";
				$rich_snippets["@type"] = "Product";
				$rich_snippets["description"] = str_replace (array('"'), array('&#34;'), html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'));
				$rich_snippets["name"] = $product_info['name'];
				$rich_snippets["image"] = "";
				if(file_exists(DIR_IMAGE . $product_info['image'])) {
					$rich_snippets["image"] = $this->getFrontUrl('image/') . $product_info['image'];
				}
				$rich_snippets["url"] = $this->url->link('product/product','product_id='. $product_info['product_id'], true);
				$rich_snippets["aggregateRating"] = array();
				$rich_snippets["aggregateRating"]["@type"] = "AggregateRating";
				$rich_snippets["aggregateRating"]["ratingValue"] = round($this->model_cireviewpro_cireview->getAvgRatingOfProducts($product_info['product_id']),2);
				$rich_snippets["aggregateRating"]["reviewCount"] = $totalreviews;
				if($results) {
					$rich_snippets["review"] = array();
					foreach($results as $key => $result) {
						$rich_snippets["review"][$key] = array();
						$rich_snippets["review"][$key]["@type"] = "Review";
						$rich_snippets["review"][$key]["author"] = $result['author'];
						$rich_snippets["review"][$key]["datePublished"] = $result['date_added'];
						$rich_snippets["review"][$key]["reviewBody"] = $result['text'];
						$rich_snippets["review"][$key]["reviewRating"] = array();
						$rich_snippets["review"][$key]["reviewRating"]["@type"] = "Rating";
						$rich_snippets["review"][$key]["reviewRating"]["bestRating"] = "5";
						$rich_snippets["review"][$key]["reviewRating"]["ratingValue"] = $result['rating'];
						$rich_snippets["review"][$key]["reviewRating"]["worstRating"] = "1";
					}
				}
				/*
				JSON_PRETTY_PRINT
				JSON_UNESCAPED_SLASHES
				*/
				$data['rich'] = json_encode($rich_snippets, JSON_UNESCAPED_SLASHES) ;
			}
			/*rich snippets ends*/
			$data['text_reviewterm'] = '';
			if ($this->config->get('cireviewpro_reviewterm_id')) {
				$this->load->model('catalog/information');
				$information_info = $this->model_catalog_information->getInformation($this->config->get('cireviewpro_reviewterm_id'));
				if ($information_info) {
					$data['text_reviewterm'] = sprintf($this->language->get('text_reviewterm'), $this->url->link('information/information/agree', 'information_id=' . $this->config->get('cireviewpro_reviewterm_id'), true), $information_info['title'], $information_info['title']);
				}
			}
			/*Show customer images starts*/
			$data['reviewratingcount'] = $this->config->get('cireviewpro_reviewratingcount');
			$data['reviewimggallery'] = 0;
			$data['customerimages'] = array();
			$data['galleryimages'] = array();
			$data['galleryreviews'] = array();
			if((int)$this->config->get('cireviewpro_reviewimggallery')) {
				$data['reviewimggallery'] = (int)$this->config->get('cireviewpro_reviewimggallery');
				$cireview_ids = $this->model_cireviewpro_cireview->getCiReviewIdsByVerifiedImp(array(
	                'product_id' => $this->request->get['product_id'],
	            ));
				$cireviewids = array();
				$reviewcustomerimages = array();
				$galleryimages = array();
				foreach ($cireview_ids as $key => $value) {
					$cireviewids[] = $value['cireview_id'];
				}
				if($cireviewids) {
					$cpage = 1;
					$climit = 4;
					$reviewcustomerimages = $this->model_cireviewpro_cireview->getCiReviewCustomerImages(array(
		                'cireview_ids' => $cireviewids,
		                'product_id' => $this->request->get['product_id'],
		                'group_by' => 'cri.cireview_id',
		                'start' => ($cpage - 1) * $climit,
						'limit' => $climit,
		            ));
		            $galleryimages = $this->model_cireviewpro_cireview->getCiReviewCustomerImages(array(
		                'product_id' => $this->request->get['product_id'],
		                'group_by' => 'cri.cireview_image_id',
		            ));
				}
				$i = 0;
				$review_ids = array();
				$review_galleryids = array();
				foreach ($galleryimages as $key => $value) {
					if(!empty($value['image']) && file_exists(DIR_IMAGE .$value['image'] )) {
						$thumb = $this->model_tool_image->resize($value['image'], 75, 75 );
						$data['galleryimages'][$i] = array(
							'galleryid' => $i,
							'thumb' => $thumb,
							'cireview_id' => $value['cireview_id'],
							'review_id' => $value['review_id'],
							'cireview_image_id' => $value['cireview_image_id'],
						);
						if(!in_array($value['review_id'], $review_ids)) {
							$review_ids[] = $value['review_id'];
						}
						$review_galleryids[$value['review_id']][] = $i;
						$i++;
					}
				}

				if($review_ids) {
					foreach ($reviewcustomerimages as $key => $value) {
						if(!empty($value['image']) && file_exists(DIR_IMAGE .$value['image'] )) {
							$thumb = $this->model_tool_image->resize($value['image'], 100, 100 );
							if(isset($review_galleryids[$value['review_id']]) && isset($review_galleryids[$value['review_id']][0])) {
								$galleryid = $review_galleryids[$value['review_id']][0];
								foreach($review_galleryids[$value['review_id']] as $galleryids) {
									if($data['galleryimages'][$galleryids]['cireview_image_id'] == $value['cireview_image_id']) {
										$galleryid = $galleryids;
									}
								}
								$data['customerimages'][] = array(
									'thumb' => $thumb,
									'cireview_id' => $value['cireview_id'],
									'review_id' => $value['review_id'],
									'cireview_image_id' => $value['cireview_image_id'],
									'galleryid' => $galleryid,
								);
							}
						}
					}
					// echo "\n\n";
					// echo "customerimages";
					// echo "\n\n";
					// print_r($data['customerimages']);
					// echo "galleryimages";
					// echo "\n\n";
					// print_r($data['galleryimages']);
					// echo "review_ids";
					// echo "\n\n";
					// print_r($review_ids);
					// echo "review_galleryids";
					// echo "\n\n";
					// print_r($review_galleryids);
					// die;
					$thumbwidth = 50;// (int)$this->config->get('cireviewpro_reviewimagesthumb_width');
					$thumbheight = 50;// (int)$this->config->get('cireviewpro_reviewimagesthumb_height');
					if(empty($thumbwidth) || is_null($thumbwidth) ) {
						$thumbwidth = 100;
					}
					if(empty($thumbheight) || is_null($thumbheight) ) {
						$thumbheight = 100;
					}
					$popupwidth = 500;// (int)$this->config->get('cireviewpro_reviewimagespopup_width');
					$popupheight = 500;// (int)$this->config->get('cireviewpro_reviewimagespopup_height');
					if(empty($popupwidth) || is_null($popupwidth) ) {
						$popupwidth = 500;
					}
					if(empty($popupheight) || is_null($popupheight) ) {
						$popupheight = 500;
					}
					$galleryreviews = $this->model_cireviewpro_cireview->getCiReviews(array('filter_review_ids' => $review_ids));
					foreach ($galleryreviews as $result) {
						$cireview_info = $this->model_cireviewpro_cireview->getCiReviewByReviewId($result['review_id']);
						$result['attach_images'] = array();
						$attach_images = $this->model_cireviewpro_cireview->getCiReviewAttachImages($result['cireview_id']);
						foreach ($attach_images as $attach_image) {
							if(!empty($attach_image['image']) && file_exists(DIR_IMAGE .$attach_image['image'] )) {
								$thumb = $this->model_tool_image->resize($attach_image['image'], $thumbwidth, $thumbheight ) ;
								$popup = $this->model_tool_image->resize($attach_image['image'], $popupwidth, $popupheight ) ;
							} else {
								$thumb = $this->model_tool_image->resize('no_image.png', $thumbwidth, $thumbheight ) ;
								$popup = $this->model_tool_image->resize('no_image.png', $popupwidth, $popupheight ) ;
							}
							$image_description = $this->model_cireviewpro_cireview->getCiReviewImagesDescriptions($attach_image['cireview_image_id']);
							$galleryid = $review_galleryids[$result['review_id']][0];
							foreach($review_galleryids[$result['review_id']] as $galleryids) {
								if($data['galleryimages'][$galleryids]['cireview_image_id'] == $attach_image['cireview_image_id']) {
									$galleryid = $galleryids;
								}
							}
							$result['attach_images'][] = array(
								'cireview_image_id' => $attach_image['cireview_image_id'],
								'galleryid' => $galleryid,
								'thumb' => $thumb,
								'active' => 0,
								'popup' => $popup,
								'alt' => !empty($image_description['alt']) ? htmlentities($image_description['alt'], ENT_QUOTES, 'UTF-8') : '',
								'title' => !empty($image_description['title']) ? htmlentities($image_description['title'], ENT_QUOTES, 'UTF-8') : '',
							);
						}
						$reviewdateformat = $this->config->get('cireviewpro_reviewdateformat');
						if(empty($reviewdateformat)) {
							$reviewdateformat = $this->language->get('date_format_short');
						}
						$date_added = date($reviewdateformat, strtotime($result['date_added']));
						if($this->config->get('cireviewpro_reviewaddon') == 'DAYSAGO') {
							// load language cireviewpro/cireview first.
							$date_added = $this->model_cireviewpro_cireview->timeElapsedString($result['date_added']);
						}
						$cireviewverify_info = $this->model_cireviewpro_cireview->getCiReviewVerify($result['review_id']);
						$verified = false;
						if($cireviewverify_info) {
							$verified = $cireviewverify_info['review'] == 1;
						}
						// fverif task start
						if (!$verified) {
							$verified = $result['fverify'] == 1;
						}
						// fverif task end
						$data['gallery_review'] = array(
							'galleryids' => $review_galleryids[$result['review_id']],
							'review_id'     => $result['review_id'],
							'imp'     => $result['imp'],
							'active_attachimage'     => '',
							'verified'     => $verified,
							'cireview_id'     => $result['cireview_id'],
							'author'     => $result['author'],
							'author_flag'     => (int)$this->config->get('cireviewpro_reviewauthorflag') ? $this->model_cireviewpro_cireview->authorFlag($result['customer_id'], $result['review_id']) : '',
							'author_fl'     => (int)$this->config->get('cireviewpro_reviewauthorfl') ? $this->model_cireviewpro_cireview->nameFirstChars($result['author']) : '',
							'text'       => nl2br($result['text']),
							'reviewtitle'       => (int)$this->config->get('cireviewpro_reviewpagetitleshow') ? nl2br($result['title']) : '',
							'comment'       => (int)$this->config->get('cireviewpro_reviewreply') ? nl2br($result['comment']) : '',
							'attach_images'     => $result['attach_images'],
							/*17-09-2019 half start starts*/
							'rating'     => $this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0,
							'html_rating'     => $this->getRatingStarsHtml($this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0),
							'show_rating'     => (int)$result['rating'],
							/*17-09-2019 half start ends*/
							'date_added' => $date_added,
							'share' => $this->url->link('cireviewpro/cireviews', 'review_id=' . $result['review_id']),
						);
						$data['gallery_review']['gallery_html'] = $this->view('cireviewpro/cireview_gallery', $data);
						$data['galleryreviews'][$result['review_id']] = $data['gallery_review'];
						unset($data['gallery_review']);
					}
				}
				// echo "galleryreviews";
				// echo "\n\n";
				// print_r($data['galleryreviews']);
				// die;
			}

			if(empty($data['customerimages'])) {
				$data['reviewimggallery'] = 0;
			}
			/*Show customer images ends*/
			/*get top positive & negative review starts*/
			$data['reviewtopposneg'] = (int)$this->config->get('cireviewpro_reviewtopposneg');
			$data['reviewposneg'] = $this->positiveNegativeReview();
			/*get top positive & negative review ends*/
			$data['customcss'] = $this->config->get('cireviewpro_customcss');
			return $this->view('cireviewpro/cireview', $data);
		}
	}
	private function langauge(&$data) {
	}
	private function view($path, &$data) {
		// remove twig, tpl extension from path
		$pathinfo = pathinfo($path);
		if(isset($pathinfo['extension']) && in_array($pathinfo['extension'], array('tpl','twig'))) {
			$path = str_replace(".".$pathinfo['extension'], '', $path);
		}
		if(VERSION < '2.2.0.0') {
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/'. $path .'.tpl')) {
				return $this->load->view($this->config->get('config_template') . '/template/'. $path .'.tpl', $data);
			} else {
				return $this->load->view('default/template/'. $path .'.tpl', $data);
			}
		} else{
			return $this->load->view($path, $data);
		}
	}
	private function getFrontUrl($url='') {
		if ($this->request->server['HTTPS']) {
			return HTTP_SERVER . $url;
		} else {
			return HTTPS_SERVER . $url;
		}
	}
	private function positiveNegativeReview() {
		if (!(int)$this->config->get('cireviewpro_reviewtopposneg')) {
			return '';
		}
		$data['reviewtopposneg'] = (int)$this->config->get('cireviewpro_reviewtopposneg');
		$data['text_positive_negative'] = $this->language->get('text_positive_negative');
		$data['text_top_positive'] = $this->language->get('text_top_positive');
		$data['text_top_negative'] = $this->language->get('text_top_negative');
		$data['text_verified'] = $this->language->get('text_verified');
		$data['text_author'] = $this->language->get('text_author');
		$data['text_title'] = $this->language->get('text_title');
		$data['text_date_added'] = $this->language->get('text_date_added');
		$data['text_rating'] = $this->language->get('text_rating');
		$data['reviewratingcount'] = $this->config->get('cireviewpro_reviewratingcount');
		// no. of stars
		$data['ratingstars'] = 5;
		// if((int)$this->config->get('cireviewpro_ratingstars')) {
		// 	$data['ratingstars'] = (int)$this->config->get('cireviewpro_ratingstars');
		// }
		$data['positive_reviews'] = array();
		$page = 1;
		$limit = 1;
		$filter_data = array(
			'filter_rating' => 5,
			'product_id' => $this->request->get['product_id'],
			'sort' => 'r.date_added',
			'order' => 'DESC',
			'start' => ($page - 1) * $limit,
			'limit' => $limit,
		);
		$positive_review_total = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
		$results = $this->model_cireviewpro_cireview->getCiReviewsByProductId($filter_data);
		$data['text_all_positive'] = sprintf( $this->language->get('text_all_positive'), $positive_review_total);
		foreach ($results as $result) {
			$cireview_info = $this->model_cireviewpro_cireview->getCiReviewByReviewId($result['review_id']);
			$result['cireview_id'] = 0;
			$result['comment'] = '';
			$result['title'] = '';
			$result['imp'] = 0;
			// fverif task start
			$result['fverify'] = 0;
			// fverif task end
			$result['cireview_ratings'] = array();
			if($cireview_info) {
				$result['cireview_id'] = $cireview_info['cireview_id'];
				$result['comment'] = $cireview_info['comment'];
				$result['title'] = $cireview_info['title'];
				$result['imp'] = (int)$cireview_info['imp'];
				// fverif task start
				$result['fverify'] = (int)$cireview_info['fverify'];
				// fverif task end
			}
			$reviewdateformat = $this->config->get('cireviewpro_reviewdateformat');
			if(empty($reviewdateformat)) {
				$reviewdateformat = $this->language->get('date_format_short');
			}
			$date_added = date($reviewdateformat, strtotime($result['date_added']));
			if($this->config->get('cireviewpro_reviewaddon') == 'DAYSAGO') {
				// load language cireviewpro/cireview first.
				$date_added = $this->model_cireviewpro_cireview->timeElapsedString($result['date_added']);
			}
			$cireviewverify_info = $this->model_cireviewpro_cireview->getCiReviewVerify($result['review_id']);
			$verified = false;
			if($cireviewverify_info) {
				$verified = $cireviewverify_info['review'] == 1;
			}
			// fverif task start
			if (!$verified) {
				$verified = $result['fverify'] == 1;
			}
			// fverif task end
			$data['positive_reviews'][] = array(
				'review_id'     => $result['review_id'],
				'imp'     => $result['imp'],
				'verified'     => $verified,
				'cireview_id'     => $result['cireview_id'],
				'author'     => $result['author'],
				'author_flag'     => (int)$this->config->get('cireviewpro_reviewauthorflag') ? $this->model_cireviewpro_cireview->authorFlag($result['customer_id'], $result['review_id']) : '',
				'author_fl'     => (int)$this->config->get('cireviewpro_reviewauthorfl') ? $this->model_cireviewpro_cireview->nameFirstChars($result['author']) : '',
				'text'       => nl2br(utf8_substr($result['text'], 0,200)),
				'reviewtitle'       => (int)$this->config->get('cireviewpro_reviewpagetitleshow') ? nl2br($result['title']) : '',
				'comment'       => (int)$this->config->get('cireviewpro_reviewreply') ? nl2br($result['comment']) : '',
				/*17-09-2019 half start starts*/
				'rating'     => $this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0,
				'html_rating'     => $this->getRatingStarsHtml($this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0),
				'show_rating'     => (int)$result['rating'],
				/*17-09-2019 half start ends*/
				'date_added' => $date_added,
				'share' => $this->url->link('cireviewpro/cireviews', 'review_id=' . $result['review_id']),
			);
		}
		$data['negative_reviews'] = array();
		$page = 1;
		$limit = 1;
		$filter_data = array(
			'filter_rating' => 1,
			'product_id' => $this->request->get['product_id'],
			'sort' => 'r.date_added',
			'order' => 'DESC',
			'start' => ($page - 1) * $limit,
			'limit' => $limit,
		);
		$negative_review_total = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
		$results = $this->model_cireviewpro_cireview->getCiReviewsByProductId($filter_data);
		$data['text_all_negative'] = sprintf($this->language->get('text_all_negative'), $negative_review_total);
		foreach ($results as $result) {
			$cireview_info = $this->model_cireviewpro_cireview->getCiReviewByReviewId($result['review_id']);
			$result['cireview_id'] = 0;
			$result['comment'] = '';
			$result['title'] = '';
			$result['imp'] = 0;
			// fverif task start
			$result['fverify'] = 0;
			// fverif task end
			$result['cireview_ratings'] = array();
			if($cireview_info) {
				$result['cireview_id'] = $cireview_info['cireview_id'];
				$result['comment'] = $cireview_info['comment'];
				$result['title'] = $cireview_info['title'];
				$result['imp'] = (int)$cireview_info['imp'];
				// fverif task start
				$result['fverify'] = (int)$cireview_info['fverify'];
				// fverif task end
			}
			$reviewdateformat = $this->config->get('cireviewpro_reviewdateformat');
			if(empty($reviewdateformat)) {
				$reviewdateformat = $this->language->get('date_format_short');
			}
			$date_added = date($reviewdateformat, strtotime($result['date_added']));
			if($this->config->get('cireviewpro_reviewaddon') == 'DAYSAGO') {
				// load language cireviewpro/cireview first.
				$date_added = $this->model_cireviewpro_cireview->timeElapsedString($result['date_added']);
			}
			$cireviewverify_info = $this->model_cireviewpro_cireview->getCiReviewVerify($result['review_id']);
			$verified = false;
			if($cireviewverify_info) {
				$verified = $cireviewverify_info['review'] == 1;
			}
			// fverif task start
			if (!$verified) {
				$verified = $result['fverify'] == 1;
			}
			// fverif task end
			$data['negative_reviews'][] = array(
				'review_id'     => $result['review_id'],
				'imp'     => $result['imp'],
				'verified'     => $verified,
				'cireview_id'     => $result['cireview_id'],
				'author'     => $result['author'],
				'author_flag'     => (int)$this->config->get('cireviewpro_reviewauthorflag') ? $this->model_cireviewpro_cireview->authorFlag($result['customer_id'], $result['review_id']) : '',
				'author_fl'     => (int)$this->config->get('cireviewpro_reviewauthorfl') ? $this->model_cireviewpro_cireview->nameFirstChars($result['author']) : '',
				'text'       => nl2br(utf8_substr($result['text'], 0,200)),
				'reviewtitle'       => (int)$this->config->get('cireviewpro_reviewpagetitleshow') ? nl2br($result['title']) : '',
				'comment'       => (int)$this->config->get('cireviewpro_reviewreply') ? nl2br($result['comment']) : '',
				/*17-09-2019 half start starts*/
				'rating'     => $this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0,
				'html_rating'     => $this->getRatingStarsHtml($this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0),
				'show_rating'     => (int)$result['rating'],
				/*17-09-2019 half start ends*/
				'date_added' => $date_added,
				'share' => $this->url->link('cireviewpro/cireviews', 'review_id=' . $result['review_id']),
			);
		}
		if(!empty($data['negative_reviews']) && !empty($data['positive_reviews'])) {
			return $this->view('cireviewpro/cireview_posneg', $data);
		}
	}
	private function topReviews() {
		if($this->config->get('cireviewpro_reviewimp')) {
			// no. of stars
			$data['ratingstars'] = 5;
			// if((int)$this->config->get('cireviewpro_ratingstars')) {
			// 	$data['ratingstars'] = (int)$this->config->get('cireviewpro_ratingstars');
			// }
			$data['text_topreview'] = $this->language->get('text_topreview');
			/*$default = $this->config->get('cireviewpro_reviewsortdefault');
			$default_part = explode('-', $default);
			if(count($default_part)==2) {
				if(!isset($this->request->get['sort'])) {
					$this->request->get['sort'] = $default_part[0];
				}
				if(!isset($this->request->get['order'])) {
					$this->request->get['order'] = $default_part[1];
				}
			}*/
			if(isset($this->request->get['sort'])) {
				$sort = $this->request->get['sort'];
			} else {
				$sort = 'r.rating';
			}
			if(isset($this->request->get['order'])) {
				$order = $this->request->get['order'];
			} else {
				$order = 'DESC';
			}
			$sort = 'r.rating';
			$order = 'DESC';
			$filter_data = array(
				'product_id' => $this->request->get['product_id'],
				'filter_imp' => 1,
				'sort' => $sort,
				'order' => $order,
			);
			$results = $this->model_cireviewpro_cireview->getCiReviewsByProductId($filter_data);
			$data['reviews'] = array();
			foreach ($results as $result) {
				$cireview_info = $this->model_cireviewpro_cireview->getCiReviewByReviewId($result['review_id']);
				$result['cireview_id'] = 0;
				$result['comment'] = '';
				$result['title'] = '';
				$result['imp'] = 0;
				// fverif task start
				$result['fverify'] = 0;
				// fverif task end
				$result['cireview_ratings'] = array();
				if($cireview_info) {
					$result['cireview_id'] = $cireview_info['cireview_id'];
					$result['comment'] = $cireview_info['comment'];
					$result['title'] = $cireview_info['title'];
					$result['imp'] = (int)$cireview_info['imp'];
					// fverif task start
					$result['fverify'] = (int)$cireview_info['fverify'];
					// fverif task end
					if($this->config->get('cireviewpro_reviewrating')) {
						$cireview_ratings = $this->model_cireviewpro_cireview->getCiReviewRatings($cireview_info['cireview_id']);
						foreach($cireview_ratings as $cireview_rating) {
							$ciratingtype_name = '';
							$ratingtype = $this->model_cireviewpro_cireview->getCiRatingType($cireview_rating['ciratingtype_id']);
							if($ratingtype) {
								$ciratingtype_name = $ratingtype['name'];
							} else {
								$ciratingtype_names = json_decode($cireview_rating['ciratingtype_name'],1);
								if(!empty($ciratingtype_names)) {
									if(isset($ciratingtype_names[(int)$this->config->get('config_language_id')])) {
										$ciratingtype_name = $ciratingtype_names[(int)$this->config->get('config_language_id')];
									} else {
										reset($ciratingtype_names);
										$ciratingtype_name = end($ciratingtype_names);
									}
								}
							}
							$result['cireview_ratings'][] = array(
								'cireview_rating_id' => $cireview_rating['cireview_rating_id'],
								'ciratingtype_id' => $cireview_rating['ciratingtype_id'],
								'ciratingtype_name' => $ciratingtype_name,
								/*17-09-2019 half start starts*/
								'rating' => $cireview_rating['rating'],
								'html_rating'     => $this->getRatingStarsHtml($cireview_rating['rating']),
								'show_rating' => (int)$cireview_rating['rating'],
								/*17-09-2019 half start ends*/
							);
						}
					}
				}
				$cireviewverify_info = $this->model_cireviewpro_cireview->getCiReviewVerify($result['review_id']);
				$verified = false;
				if($cireviewverify_info) {
					$verified = $cireviewverify_info['review'] == 1;
				}
				// fverif task start
				if (!$verified) {
					$verified = $result['fverify'] == 1;
				}
				// fverif task end
				$data['reviews'][] = array(
					'review_id'     => $result['review_id'],
					'imp'     => $result['imp'],
					'verified'     => $verified,
					'cireview_id'     => $result['cireview_id'],
					'author'     => $result['author'],
					'author_flag'     => (int)$this->config->get('cireviewpro_reviewauthorflag') ? $this->model_cireviewpro_cireview->authorFlag($result['customer_id'], $result['review_id']) : '',
					'author_fl'     => (int)$this->config->get('cireviewpro_reviewauthorfl') ? $this->model_cireviewpro_cireview->nameFirstChars($result['author']) : '',
					'text'       => nl2br($result['text']),
					'reviewtitle'       => (int)$this->config->get('cireviewpro_reviewpagetitleshow') ? nl2br($result['title']) : '',
					'comment'       => (int)$this->config->get('cireviewpro_reviewreply') ? nl2br($result['comment']) : '',
					/*17-09-2019 half start starts*/
					'rating'     => $this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0,
					'html_rating'     => $this->getRatingStarsHtml($this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0),
					'show_rating'     => (int)$result['rating'],
					/*17-09-2019 half start ends*/
					'cireview_ratings'     => $result['cireview_ratings'],
					'date_added' => date($this->config->get('cireviewpro_reviewdateformat'), strtotime($result['date_added'])),
				);
			}
			return $this->view('cireviewpro/cireview_top', $data);
		}
	}

	private function cireviewGraph() {
		// no. of stars
		$data['ratingstars'] = 5;
		// if((int)$this->config->get('cireviewpro_ratingstars')) {
		// 	$data['ratingstars'] = (int)$this->config->get('cireviewpro_ratingstars');
		// }
		$data['text_star'] = $this->language->get('text_star');
		$data['reviewgraph_color'] = $this->config->get('cireviewpro_reviewgraph_color');
		$data['sharetype'] = $this->config->get('cireviewpro_sharetype');
		$data['reviewgraph_option'] = $this->config->get('cireviewpro_reviewgraph_option');
		$data['reviewgraph'] = (int)$this->config->get('cireviewpro_reviewgraph');
		$data['reviewgraph_text'] = (int)$this->config->get('cireviewpro_reviewgraph_text');
		$data['reviewgraph_recommend'] = (int)$this->config->get('cireviewpro_reviewgraph_recommend');
		$filter_data = array(
			'product_id' => $this->request->get['product_id'],
		);
		$data['review_total'] = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
		$ratingreviews = $this->model_cireviewpro_cireview->getProductReviewsByRating($this->request->get['product_id']);
		$reviewsrating = array();
		$options_reviewrating = array();
		for($i=1;$i<=$data['ratingstars'];$i++) {
			$options_reviewrating[$i] = 0;
		}
		foreach($ratingreviews as $ratingreview) {
			if($ratingreview['rating']) {
			$reviewsrating[$ratingreview['rating']] = $ratingreview['total'];
			}
		}
		$ratingreviewss = ($reviewsrating + array_diff_key($options_reviewrating ,$reviewsrating));
		// ksort($ratingreviewss);
		krsort($ratingreviewss);
		$data['ratingreviews'] = array();
		foreach ($ratingreviewss as $reviewrating => $totalreviews) {
			$data['ratingreviews'][] = array(
				'percent' => ($data['review_total']) ? round(($totalreviews * 100) / $data['review_total'], 2) : 0,
				'reviewrating' => $reviewrating,
				'text_rating' => $this->language->get('text_rating'.$reviewrating),
			);
		}

		$liked = 0;
		if($data['review_total']) {
			$review_liked = ($ratingreviewss[5] + $ratingreviewss[4]);
			$liked = round($review_liked/$data['review_total']*100);
		}
		$data['text_recommendation'] = str_replace('[LIKED]', $liked, $this->language->get('text_recommendation'));
		return $this->view('cireviewpro/cireview_graph', $data);
	}


	/*17-09-2019 half start starts*/
	protected function getRatingStarsHtml($rating) {
		$data['rating'] = $rating;
		return $this->view('cireviewpro/cireview_stars', $data);
	}
	/*use this function to call using load controller*/
	public function loadRatingStarsHtml($rating) {
		return $this->getRatingStarsHtml($rating);

	}
	/*17-09-2019 half start ends*/

	private function aggerateReview() {
		// no. of stars
		$data['ratingstars'] = 5;
		// if((int)$this->config->get('cireviewpro_ratingstars')) {
		// 	$data['ratingstars'] = (int)$this->config->get('cireviewpro_ratingstars');
		// }
		$filter_data = array(
			/*'filter_rating' => $cirating_filter,*/
			'product_id' => $this->request->get['product_id'],
		);
		$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
		if($product_info) {
			$data['href_review'] = $this->url->link('cireviewpro/cireviews', 'cireview_product_id='. $product_info['product_id'] , true);
			$reviewtotal = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
			$data['text_write_review'] = $this->language->get('text_write_review');
			$data['reviewratingcount'] = $this->config->get('cireviewpro_reviewratingcount');
			$data['text_rating'] = $this->language->get('text_rating');
			$data['text_reviewover'] = sprintf($this->language->get('text_reviewover'), $product_info['name']);
			/*17-09-2019 half start starts*/
			$avgrating = $this->model_cireviewpro_cireview->getAvgRatingOfProducts($product_info['product_id']);

			$data['avg_rating'] = round($avgrating, 2);
			$data['html_avg_rating'] = $this->getRatingStarsHtml($data['avg_rating']);
			$data['show_avg_rating'] = $data['avg_rating'];
			/*17-09-2019 half start ends*/
			$data['cireview_ratings'] = array();
			$cirating_types = $this->model_cireviewpro_cireview->getCiRatingTypes($this->request->get['product_id']);
			foreach($cirating_types as $cirating_type) {
				/*17-09-2019 half start starts*/
				$avgrating = $this->model_cireviewpro_cireview->getCiReviewRatingAvg($cirating_type['ciratingtype_id'], $product_info['product_id']);
				$avg_rating = round($avgrating,2);
				/*17-09-2019 half start ends*/
				//$avgrating
				$data['cireview_ratings'][] = array(
					'ciratingtype_id' 	=> $cirating_type['ciratingtype_id'],
					'ciratingtype_name' 	=> $cirating_type['name'],
					/*17-09-2019 half start starts*/
					'rating' 				=> $avg_rating,
					'html_rating' 				=> $this->getRatingStarsHtml($avg_rating),
					'show_rating' => $avg_rating,
					/*17-09-2019 half start ends*/
				);
			}
			$data['text_total_reviews'] = sprintf($this->language->get('text_total_reviews'), $reviewtotal) ;
			return $this->view('cireviewpro/cireview_aggerate', $data);
		}
	}
	public function review() {
		if(isset($this->request->get['cirating_filter'])) {
			$cirating_filter = $this->request->get['cirating_filter'];
		} else {
			$cirating_filter = 0;
		}
		if(isset($this->request->get['cirating_dofilter'])) {
			$cirating_dofilter = $this->request->get['cirating_dofilter'];
		} else {
			$cirating_dofilter = '';
		}
		if(isset($this->request->get['cirating_cireviewsearch'])) {
			$cirating_cireviewsearch = $this->request->get['cirating_cireviewsearch'];
		} else {
			$cirating_cireviewsearch = '';
		}
		// no. of stars
		$data['ratingstars'] = 5;
		// if((int)$this->config->get('cireviewpro_ratingstars')) {
		// 	$data['ratingstars'] = (int)$this->config->get('cireviewpro_ratingstars');
		// }
		$default = $this->config->get('cireviewpro_reviewsortdefault');
		$default_part = explode('-', $default);
		/*Set GET variables create issue when currency and language actions controller setup redirect paths to maintain current URL*/
		// if(count($default_part)==2) {
		// 	if(!isset($this->request->get['sort'])) {
		// 		$this->request->get['sort'] = $default_part[0];
		// 	}
		// 	if(!isset($this->request->get['order'])) {
		// 		$this->request->get['order'] = $default_part[1];
		// 	}
		// }
		if(isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} elseif (isset($default_part[0])) {
			$sort = $default_part[0];
		} else {
			$sort = 'r.date_added';
		}
		if(isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} elseif (isset($default_part[1])) {
			$order = $default_part[1];
		} else {
			$order = 'DESC';
		}
		$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
		if($product_info) {
			$data['text_no_reviews'] = $this->language->get('text_no_reviews');
			$data['text_author'] = $this->language->get('text_author');
			$data['text_title'] = $this->language->get('text_title');
			$data['text_date_added'] = $this->language->get('text_date_added');
			$data['text_rating'] = $this->language->get('text_rating');
			$data['text_yes'] = $this->language->get('text_yes');
			$data['text_no'] = $this->language->get('text_no');
			$data['text_reviewabuse'] = $this->language->get('text_reviewabuse');
			$data['text_verified'] = $this->language->get('text_verified');
			$data['heading_title'] = $this->language->get('heading_title');
			$data['text_replyby'] = $this->language->get('text_replyby');
			if (isset($this->request->get['page'])) {
				$page = $this->request->get['page'];
			} else {
				$page = 1;
			}
			$data['product_id'] = $this->request->get['product_id'];
			$data['reviews'] = array();
			$limit = 5;
			if($this->config->get('cireviewpro_reviewlimit')) {
				$limit = $this->config->get('cireviewpro_reviewlimit');
			}
			$filter_data = array(
				/*'filter_rating' => $cirating_filter,*/
				'product_id' => $this->request->get['product_id'],
				'sort' => $sort,
				'order' => $order,
				'start' => ($page - 1) * $limit,
				'limit' => $limit,
			);
			$reviewtotal = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
			$filter_data = array(
				'filter_rating' => $cirating_filter,
				'filter_attachimages' => $cirating_dofilter,
				'filter_cireviewsearch' => $cirating_cireviewsearch,
				'product_id' => $this->request->get['product_id'],
				'sort' => $sort,
				'order' => $order,
			);
			$data['ajax'] = 0;
			if(isset($this->request->get['ajax']) && $this->request->get['ajax']=1) {
				$filter_data = array_merge($filter_data, array(
					'start' => ($page - 1) * $limit,
					'limit' => $limit,
				));
				$data['ajax'] = 1;
			}
			$review_total = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
			// show reply comment/reply by admin
			$data['reviewreplyauthor'] = $this->config->get('cireviewpro_reviewreplyauthor');
			$results = $this->model_cireviewpro_cireview->getCiReviewsByProductId($filter_data);
			$thumbwidth = (int)$this->config->get('cireviewpro_reviewimagesthumb_width');
			$thumbheight = (int)$this->config->get('cireviewpro_reviewimagesthumb_height');
			if(empty($thumbwidth) || is_null($thumbwidth) ) {
				$thumbwidth = 100;
			}
			if(empty($thumbheight) || is_null($thumbheight) ) {
				$thumbheight = 100;
			}
			$popupwidth = (int)$this->config->get('cireviewpro_reviewimagespopup_width');
			$popupheight = (int)$this->config->get('cireviewpro_reviewimagespopup_height');
			if(empty($popupwidth) || is_null($popupwidth) ) {
				$popupwidth = 500;
			}
			if(empty($popupheight) || is_null($popupheight) ) {
				$popupheight = 500;
			}
			$data['reviewabuse'] = false;
			if($this->config->get('cireviewpro_reviewabuse')) {
				if($this->customer->isLogged() || $this->config->get('cireviewpro_reviewabuseguest')) {
					$data['reviewabuse'] = true;
				}
			}
			$data['reviewvote'] = false;
			if($this->config->get('cireviewpro_reviewgetvote')) {
				if($this->customer->isLogged() || $this->config->get('cireviewpro_reviewvoteguest')) {
					$data['reviewvote'] = true;
				}
			}
			$reviewvote_info = $this->config->get('cireviewpro_reviewvote');
			if(isset($reviewvote_info[(int)$this->config->get('config_language_id')])) {
				$reviewvote = $reviewvote_info[(int)$this->config->get('config_language_id')];
			} else {
				reset($reviewvote_info);
				$first_key = key($reviewvote_info);
				$reviewvote = $reviewvote_info[$first_key];
			}
			foreach($reviewvote as $key => $votetext) {
				if(empty($votetext)) {
					$reviewvote[$key] =	$this->language->get('text_reviewvote_'.$key);
				}
			}
			foreach ($results as $result) {
				$cireview_info = $this->model_cireviewpro_cireview->getCiReviewByReviewId($result['review_id']);
				$result['cireview_ratings'] = array();
				$result['attach_images'] = array();
				$result['votes'] = array();
				$result['votes']['before_text'] = $reviewvote['before'];
				if($this->config->get('cireviewpro_reviewvotetype') == 'PERCENT') {
				$result['votes']['after_text'] = str_replace('{PERCENTAGE}','0',$reviewvote['percent'] );
				}
				if($this->config->get('cireviewpro_reviewvotetype') == 'OUTOF') {
					$result['votes']['after_text'] = str_replace(array('{VOTES}', '{TOTAL_VOTES}'), array('0', '0'), $reviewvote['outof']);
				}
				$result['cireview_id'] = 0;
				$result['comment'] = '';
				$result['title'] = '';
				$result['imp'] = 0;
				// fverif task start
				$result['fverify'] = 0;
				// fverif task end
				$result['cireview_ratings'] = array();
				if($cireview_info) {
					$result['cireview_id'] = $cireview_info['cireview_id'];
					$result['comment'] = $cireview_info['comment'];
					$result['title'] = $cireview_info['title'];
					$result['imp'] = (int)$cireview_info['imp'];
					// fverif task start
					$result['fverify'] = (int)$cireview_info['fverify'];
					// fverif task end
					if($data['reviewvote']) {
						$total_voteup = 0;
						$total_votedown = 0;
						$total_votes = 0;
						/*12-09-2019 Vote Feature Update Starts*/
						$total_voteup = $cireview_info['votes_up'];
						$total_votedown = $cireview_info['votes_down'];
						$total_votes = $total_voteup + $total_votedown;
						if ($total_votes) {
							if($this->config->get('cireviewpro_reviewvotetype') == 'PERCENT') {
							$result['votes']['after_text'] = str_replace('{PERCENTAGE}', round($total_voteup*100/$total_votes,2), $reviewvote['percent'] );
							}
							if($this->config->get('cireviewpro_reviewvotetype') == 'OUTOF') {
								$result['votes']['after_text'] = str_replace(array('{VOTES}', '{TOTAL_VOTES}'), array($total_voteup, $total_votes), $reviewvote['outof']);
							}
						}
						/*$cireview_votes = $this->model_cireviewpro_cireview->getCiReviewVotes($cireview_info['cireview_id']);
						if($cireview_votes) {
							$total_votes = count($cireview_votes);
							foreach ($cireview_votes as $cireview_vote) {
								if($cireview_vote['vote']==1) {
									$total_voteup++;
								}
								if($cireview_vote['vote']==0) {
									$total_votedown++;
								}
							}
							if($this->config->get('cireviewpro_reviewvotetype') == 'PERCENT') {
							$result['votes']['after_text'] = str_replace('{PERCENTAGE}', round($total_voteup*100/$total_votes,2), $reviewvote['percent'] );
							}
							if($this->config->get('cireviewpro_reviewvotetype') == 'OUTOF') {
								$result['votes']['after_text'] = str_replace(array('{VOTES}', '{TOTAL_VOTES}'), array($total_voteup, $total_votes), $reviewvote['outof']);
							}
						}*/
						/*12-09-2019 Vote Feature Update Ends*/
					}
					if($this->config->get('cireviewpro_reviewrating')) {
						$cireview_ratings = $this->model_cireviewpro_cireview->getCiReviewRatings($cireview_info['cireview_id']);
						foreach($cireview_ratings as $cireview_rating) {
							$ciratingtype_name = '';
							$ratingtype = $this->model_cireviewpro_cireview->getCiRatingType($cireview_rating['ciratingtype_id']);
							if($ratingtype) {
								$ciratingtype_name = $ratingtype['name'];
							} else {
								$ciratingtype_names = json_decode($cireview_rating['ciratingtype_name'],1);
								if(!empty($ciratingtype_names)) {
									if(isset($ciratingtype_names[(int)$this->config->get('config_language_id')])) {
										$ciratingtype_name = $ciratingtype_names[(int)$this->config->get('config_language_id')];
									} else {
										reset($ciratingtype_names);
										$ciratingtype_name = end($ciratingtype_names);
									}
								}
							}




							$result['cireview_ratings'][] = array(
								'cireview_rating_id' => $cireview_rating['cireview_rating_id'],
								'ciratingtype_id' => $cireview_rating['ciratingtype_id'],
								'ciratingtype_name' => $ciratingtype_name,
								/*17-09-2019 half start starts*/
								'rating' => (int)$cireview_rating['rating'],
								'html_rating' => $this->getRatingStarsHtml((int)$cireview_rating['rating']),
								'show_rating' => (int)$cireview_rating['rating'],
								/*17-09-2019 half start ends*/
							);
						}
					}
					$attach_images = $this->model_cireviewpro_cireview->getCiReviewAttachImages($cireview_info['cireview_id']);
					foreach ($attach_images as $attach_image) {
						if(!empty($attach_image['image']) && file_exists(DIR_IMAGE .$attach_image['image'] )) {
							$thumb = $this->model_tool_image->resize($attach_image['image'], $thumbwidth, $thumbheight ) ;
							$popup = $this->model_tool_image->resize($attach_image['image'], $popupwidth, $popupheight ) ;
						} else {
							$thumb = $this->model_tool_image->resize('no_image.png', $thumbwidth, $thumbheight ) ;
							$popup = $this->model_tool_image->resize('no_image.png', $popupwidth, $popupheight ) ;
						}
						$image_description = $this->model_cireviewpro_cireview->getCiReviewImagesDescriptions($attach_image['cireview_image_id']);
						$result['attach_images'][] = array(
							'cireview_image_id' => $attach_image['cireview_image_id'],
							'thumb' => $thumb,
							'popup' => $popup,
							'alt' => !empty($image_description['alt']) ? htmlentities($image_description['alt'], ENT_QUOTES, 'UTF-8') : '',
							'title' => !empty($image_description['title']) ? htmlentities($image_description['title'], ENT_QUOTES, 'UTF-8') : '',
						);
					}
				}
				$reviewdateformat = $this->config->get('cireviewpro_reviewdateformat');
				if(empty($reviewdateformat)) {
					$reviewdateformat = $this->language->get('date_format_short');
				}
				$date_added = date($reviewdateformat, strtotime($result['date_added']));
				if($this->config->get('cireviewpro_reviewaddon') == 'DAYSAGO') {
					// load language cireviewpro/cireview first.
					$date_added = $this->model_cireviewpro_cireview->timeElapsedString($result['date_added']);
				}
				$cireviewverify_info = $this->model_cireviewpro_cireview->getCiReviewVerify($result['review_id']);
				$verified = false;
				if($cireviewverify_info) {
					$verified = $cireviewverify_info['review'] == 1;
				}
				// fverif task start
				if (!$verified) {
					$verified = $result['fverify'] == 1;
				}
				// fverif task end
				$data['reviews'][] = array(
					'review_id'     => $result['review_id'],
					'imp'     => $result['imp'],
					'verified'     => $verified,
					'cireview_id'     => $result['cireview_id'],
					'author'     => $result['author'],
					'author_flag'     => (int)$this->config->get('cireviewpro_reviewauthorflag') ? $this->model_cireviewpro_cireview->authorFlag($result['customer_id'], $result['review_id']) : '',
					'author_fl'     => (int)$this->config->get('cireviewpro_reviewauthorfl') ? $this->model_cireviewpro_cireview->nameFirstChars($result['author']) : '',
					'text'       => nl2br($result['text']),
					'reviewtitle'       => (int)$this->config->get('cireviewpro_reviewpagetitleshow') ? nl2br($result['title']) : '',
					'comment'       => (int)$this->config->get('cireviewpro_reviewreply') ? nl2br($result['comment']) : '',
					'attach_images'     => $result['attach_images'],
					/*17-09-2019 half start starts*/
					'rating'     => $this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0,
					'html_rating'     => $this->getRatingStarsHtml($this->config->get('cireviewpro_reviewrating') ? (int)$result['rating'] : 0),
					'show_rating'     => (int)$result['rating'],
					/*17-09-2019 half start ends*/
					'votes'     => $result['votes'],
					'cireview_ratings'     => $result['cireview_ratings'],
					'date_added' => $date_added,
					'share' => $this->url->link('cireviewpro/cireviews', 'review_id=' . $result['review_id']),
				);
			}
			$data['reviewshare'] = $this->config->get('cireviewpro_reviewshare');
			$data['sharetype'] = $this->config->get('cireviewpro_sharetype');
			$data['reviewratingcount'] = $this->config->get('cireviewpro_reviewratingcount');

			$url = '';
			if (isset($this->request->get['cirating_filter'])) {
				$url .= '&cirating_filter=' . $this->request->get['cirating_filter'];
			}
			if (isset($this->request->get['cirating_dofilter'])) {
				$url .= '&cirating_dofilter=' . $this->request->get['cirating_dofilter'];
			}
			if (isset($this->request->get['cirating_cireviewsearch'])) {
				$url .= '&cirating_cireviewsearch=' . $this->request->get['cirating_cireviewsearch'];
			}

			$pagination = new Pagination();
			$pagination->total = $review_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->url = $this->url->link('cireviewpro/cireview/review', 'product_id=' . $this->request->get['product_id'] . '&ajax=1&page={page}' . $url);
			$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));
			$data['product_id'] = $product_info['product_id'];
			$output = $this->view('cireviewpro/cireviews_product_tab', $data);
			if(isset($this->request->get['ajax']) && $this->request->get['ajax']=1) {
				$this->response->setOutput($output);
			} else {
				return $output;
			}
		}
	}
	public function write() {
		$this->response->addHeader('Content-Type: application/json');
		$json = array();
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$purchasefirst = false;
			// check purchasefirst condition when customer is login and verify purchase for review or coupon or reward is enable
			if($this->customer->isLogged() && ($this->config->get('cireviewpro_reviewpurchaseonly') || $this->config->get('cireviewpro_reviewcouponpurchaseonly') || $this->config->get('cireviewpro_reviewrewardpurchaseonly'))) {
				$purchasefirst = $this->model_cireviewpro_cireview->isRequirePurchaseFirst($this->request->get['product_id'],(array)$this->config->get('cireviewpro_verifypurchase_orderstatus'));
			}
			$verifypurchasereward = true;
			$verifypurchasecoupon = true;
			if($this->config->get('cireviewpro_reviewcouponpurchaseonly')) {
				//echo "purchasefirst :" . $purchasefirst;
				//echo "\n";
				$verifypurchasecoupon = !$purchasefirst;
				//echo '$verifypurchasecoupon:' .$verifypurchasecoupon;
				//echo "\n";
				// echo $verifypurchasecoupon;die;
				// if guest then no need to give coupon, with varifypurchase only enabled
				if(!$this->customer->isLogged()) {
					$verifypurchasecoupon = false;
				}
			}
			if($this->config->get('cireviewpro_reviewrewardpurchaseonly')) {
				//echo '$purchasefirst: ' . $purchasefirst;
				$verifypurchasereward = !$purchasefirst;
				//echo "\n";
				//echo '$verifypurchasereward: ' . $verifypurchasereward;
				//echo "\n";
			}
			// check customer login and make sure review can give for verify purchase only. when order status in order status for verify purchase.
			if($this->customer->isLogged() && $this->config->get('cireviewpro_reviewpurchaseonly')) {
				/*31-10-2018*/
				if(!$purchasefirst) {
					$json['error'] = $this->language->get('error_purchasereviewgiven');
					$this->response->setOutput(json_encode($json));
					$this->response->output();
					exit();
				}
			}
			// check customer login and max reviews attempts against product
			// now we also make sure Allow Review To Purchasers only is disable as well
			if($this->customer->isLogged() && (int)$this->config->get('cireviewpro_reviewmax') > 0 && !$this->config->get('cireviewpro_reviewpurchaseonly')) {
				$customer_reviews = $this->model_cireviewpro_cireview->getCustomerTotalReviews($this->customer->getId(), $this->request->get['product_id']);
				if($customer_reviews->num_rows >= (int)$this->config->get('cireviewpro_reviewmax')) {
					$json['error'] = $this->language->get('error_max_reviews');
					$this->response->setOutput(json_encode($json));
					$this->response->output();
					exit();
				}
			}
			// Agree to terms
			if ($this->config->get('cireviewpro_reviewterm_id')) {
				$this->load->model('catalog/information');
				$information_info = $this->model_catalog_information->getInformation($this->config->get('cireviewpro_reviewterm_id'));
				if ($information_info && !isset($this->request->post['reviewterm'])) {
					$json['error'] = sprintf($this->language->get('error_agree'), $information_info['title']);
					$this->response->setOutput(json_encode($json));
					$this->response->output();
					exit();
				}
			}
			if($this->config->get('cireviewpro_reviewauthor') && $this->config->get('cireviewpro_reviewauthor_require')) {
				if ((utf8_strlen($this->request->post['ciname']) < 3) || (utf8_strlen($this->request->post['ciname']) > 25)) {
					$json['name'] = $this->language->get('error_name');
				}
			}
			if($this->config->get('cireviewpro_reviewtitle') && $this->config->get('cireviewpro_reviewtitle_require')) {
				if ((utf8_strlen($this->request->post['cititle']) < 3) || (utf8_strlen($this->request->post['cititle']) > 255)) {
					$json['title'] = $this->language->get('error_title');
				}
			}
			if($this->config->get('cireviewpro_reviewtext') && $this->config->get('cireviewpro_reviewtext_require')) {
				if ((utf8_strlen($this->request->post['cireview']) < 25) || (utf8_strlen($this->request->post['cireview']) > 1000)) {
					$json['text'] = $this->language->get('error_text');
				}
			}
			if($this->config->get('cireviewpro_reviewemail')) {
				if ((utf8_strlen($this->request->post['ciemail']) > 96) || !filter_var($this->request->post['ciemail'], FILTER_VALIDATE_EMAIL)) {
					$json['email'] = $this->language->get('error_email');
				}
			}
			if($this->config->get('cireviewpro_rating')) {
				$stars = 5;
				if($this->config->get('cireviewpro_ratingstars')) {
					$stars = $this->config->get('cireviewpro_ratingstars');
				}
				if(isset($this->request->post['cirating'])) {
					foreach($this->request->post['cirating'] as $ciratingtype_id => $rating) {
						if (empty($rating) || $rating < 0 || $rating > $stars) {
							$ciratingtype_info = $this->model_cireviewpro_cireview->getCiRatingType($ciratingtype_id);
							if($ciratingtype_info) {
								$json['rating'][$ciratingtype_id] = sprintf($this->language->get('error_cirating'), $ciratingtype_info['name'] );
							} else {
								$json['rating'][$ciratingtype_id] = $this->language->get('error_rating');
							}
						}
					}
				}
				if(!isset($this->request->post['cirating'])) {
					$ratingtypes = $this->model_cireviewpro_cireview->getCiRatingTypes($this->request->get['product_id']);
					foreach ($ratingtypes as $ratingtype) {
						$json['rating'][$ratingtype['ciratingtype_id']] =sprintf($this->language->get('error_cirating'), $ratingtype['name'] );
					}
				}
			}
			// Captcha
			if (VERSION >= '3.0.0.0' && $this->config->get('cireviewpro_captcha') && $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['captcha'] = $captcha;
				}
			} else if (VERSION < '3.0.0.0' && $this->config->get('cireviewpro_captcha') && $this->config->get($this->config->get('config_captcha') . '_status') && in_array('review', (array)$this->config->get('config_captcha_page'))) {
				if (VERSION <= '2.2.0.0') {
					$captcha = $this->load->controller('captcha/' . $this->config->get('config_captcha') . '/validate');
				} else {
					$captcha = $this->load->controller('extension/captcha/' . $this->config->get('config_captcha') . '/validate');
				}
				if ($captcha) {
					$json['captcha'] = $captcha;
				}
			}
			if (!$json) {
				if(!$this->config->get('cireviewpro_reviewemail')) {
					$this->request->post['ciemail'] = $this->customer->getEmail();
				}
				if(!$this->config->get('cireviewpro_reviewtitle')) {
					$this->request->post['cititle'] = '';
				}
				if(!$this->config->get('cireviewpro_reviewauthor')) {
					$this->request->post['ciname'] = '';
					if($this->customer->isLogged()) {
						$this->request->post['ciname'] = $this->customer->getFirstName() .' ' . $this->customer->getLastName();
					}
				}
				if(!$this->config->get('cireviewpro_reviewtext')) {
					$this->request->post['cireview'] = '';
				}
				$reviewsuccessmsg_info = (array)$this->config->get('cireviewpro_reviewsuccessmsg');
				$reviewsuccessmsg = array();
				if(isset($reviewsuccessmsg_info[(int)$this->config->get('config_language_id')])) {
					$reviewsuccessmsg = $reviewsuccessmsg_info[(int)$this->config->get('config_language_id')];
				} else {
					reset($reviewsuccessmsg_info);
					$first_key = key($reviewsuccessmsg_info);
					$reviewsuccessmsg = $reviewsuccessmsg_info[$first_key];
				}
				if(empty($reviewsuccessmsg['message']) || (!empty($reviewsuccessmsg['message']) && $reviewsuccessmsg['message'] == '<p><br></p>') ) {
					$reviewsuccessmsg['message'] = $this->language->get('success_cireview');
				}
				if(empty($reviewsuccessmsg['messagepending']) || (!empty($reviewsuccessmsg['messagepending']) && $reviewsuccessmsg['messagepending'] == '<p><br></p>') ) {
					$reviewsuccessmsg['messagepending'] = $this->language->get('success_cireview_pending');
				}
				if(empty($reviewsuccessmsg['rewardpoints']) || (!empty($reviewsuccessmsg['rewardpoints']) && $reviewsuccessmsg['rewardpoints'] == '<p><br></p>') ) {
					$reviewsuccessmsg['rewardpoints'] = '<br/>'.$this->language->get('success_rewardpoints');
				}
				if(empty($reviewsuccessmsg['couponcode']) || (!empty($reviewsuccessmsg['couponcode']) && $reviewsuccessmsg['couponcode'] == '<p><br></p>') ) {
					$reviewsuccessmsg['couponcode'] = '<br/>'.$this->language->get('success_coupon');
				}
				if(empty($reviewsuccessmsg['title']) || (!empty($reviewsuccessmsg['title']) && $reviewsuccessmsg['title'] == '<p><br></p>') ) {
					$reviewsuccessmsg['title'] = $this->language->get('success_cireview_title');
				}
				/*decode html entites for all messages*/
				foreach ($reviewsuccessmsg as $key => &$value) {
					$value = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
				}
				if($this->config->get('cireviewpro_rewardsuccessalert') == 'POPUP') {
					$json['reviewsuccessmsg_title'] = $reviewsuccessmsg['title'];
				}
				$json['success'] = $reviewsuccessmsg['messagepending'] ;
				$json['refresh'] = false;
				if($this->customer->isLogged() && ($this->config->get('cireviewpro_reviewapprove')=='LOGGED' || $this->config->get('cireviewpro_reviewapprove')=='BOTH')) {
					$json['success'] = $reviewsuccessmsg['message'];
					$json['refresh'] = true;
				} elseif($this->config->get('cireviewpro_reviewapprove')=='BOTH') {
					$json['success'] = $reviewsuccessmsg['message'];
					$json['refresh'] = true;
				}
				$this->request->post['coupon_code'] = '';
				$coupon_code = '';
				$this->request->post['coupon_id'] = 0;
				if($this->config->get('cireviewpro_reviewcoupon') && ($this->customer->isLogged() || $this->config->get('cireviewpro_reviewcouponguest')) && $verifypurchasecoupon) {
					// generate coupon here and save the code in our records as well.
					/*echo "I am inside coupon system. Shall i need to be here?";
					echo "\n";
					die;*/
					do {
						$code = token(10);
						$coupon_info = $this->model_cireviewpro_cireview->getCouponByCode($code);
					} while ($coupon_info);
					$coupon_code = $this->request->post['coupon_code'] = $code;
					$days = 1;
					if((int)$this->config->get('cireviewpro_reviewcoupondays')) {
						$days = (int)$this->config->get('cireviewpro_reviewcoupondays');
					}
					$date_start = date('Y-m-d');
					$date_end = date('Y-m-d', strtotime('+ '. $days .' DAYS' . $date_start));
					$coupon = array(
						'name' => $code .' - '. $this->request->post['ciname'],
						'code' => $code,
						'discount' => $this->config->get('cireviewpro_reviewcoupondiscount'),
						'type' => $this->config->get('cireviewpro_reviewcoupontype'),
						'total' => $this->config->get('cireviewpro_reviewcoupontotal'),
						'logged' => $this->config->get('cireviewpro_reviewcouponlogged'),
						'shipping' => $this->config->get('cireviewpro_reviewcouponshipping'),
						'date_start' => $date_start,
						'date_end' => $date_end,
						'uses_total' => $this->config->get('cireviewpro_reviewcouponuses_total'),
						'uses_customer' => $this->config->get('cireviewpro_reviewcouponuses_customer'),
						'status' => 1,
						'coupon_product' => (array)$this->config->get('cireviewpro_reviewcoupon_product'),
						'coupon_category' => (array)$this->config->get('cireviewpro_reviewcoupon_category'),
					);
					$coupon_id = $this->model_cireviewpro_cireview->addCoupon($coupon);
					$this->request->post['coupon_id'] = $coupon_id;
					$json['success'] .= str_replace('{COUPON_CODE}', $coupon_code, $reviewsuccessmsg['couponcode']);
				}
				$this->request->post['reward_points'] = '';
				$this->request->post['customer_reward_id'] = 0;
				if($this->customer->isLogged() && ($this->config->get('cireviewpro_reviewreward') && (int)$this->config->get('cireviewpro_rewardpoints') > 0) && $verifypurchasereward) {
					/*echo "I am inside reward points. Shall i need to be here?";
					echo "\n";
					die;*/
					$reward_points = (int)$this->config->get('cireviewpro_rewardpoints');
					$point = array(
						'reward_points' => $reward_points,
						'description' => $this->config->get('cireviewpro_rewarddesc'),
					);
					$customer_reward_id = $this->model_cireviewpro_cireview->addRewardPoints($point);
					if($customer_reward_id) {
						$this->request->post['reward_points'] = $reward_points;
						$this->request->post['customer_reward_id'] = $customer_reward_id;
						$json['success'] .= str_replace('{REWARD_POINTS}', $reward_points, $reviewsuccessmsg['rewardpoints']);
					}
				}
				$this->model_cireviewpro_cireview->addCiReview($this->request->get['product_id'], $this->request->post);
				$filter_data = array(
					'product_id' => $this->request->get['product_id'],
				);
				if($json['refresh']) {
					$data['review_total'] = $review_total = $this->model_cireviewpro_cireview->getTotalCiReviewsByProductId($filter_data);
					$json['tab_review'] = sprintf($this->language->get('tab_review'), $review_total);
					$data['reviewgraph_option'] = $this->config->get('cireviewpro_reviewgraph_option');
					$json['cireviewgraph'] = $this->cireviewGraph();
					$json['aggeratereview'] = $this->aggerateReview();
				}
			}
		}
		$this->response->setOutput(json_encode($json));
	}
	private function mkdir($dir) {
		if(!is_dir($dir)) {
			$oldmask = umask(0);
			mkdir($dir, 0777);
			umask($oldmask);
		}
	}
	public function upload() {
		$json = array();
		$this->response->addHeader('Content-Type: application/json');
		$this->load->language('tool/upload');
		if(!isset($this->request->post['cireview_images'])) {
			$json['error'] = $this->language->get('text_invalid');
			$this->response->setOutput(json_encode($json));
			$this->response->output();
			exit();
		}
 		$ids = array_filter(explode(",", $this->request->post['cireview_images']));
		$total_images = count($ids);
		if($total_images >= $this->config->get('cireviewpro_reviewimageslimit')) {
			$json['error'] = sprintf($this->language->get('text_maximage'), $this->config->get('cireviewpro_reviewimageslimit'));
			$this->response->setOutput(json_encode($json));
			$this->response->output();
			exit();
		}
		if (!empty($this->request->files['ciattachfile']['name']) && is_file($this->request->files['ciattachfile']['tmp_name'])) {
			// Sanitize the filename
			$filename = basename(preg_replace('/[^a-zA-Z0-9\.\-\s+]/', '', html_entity_decode($this->request->files['ciattachfile']['name'], ENT_QUOTES, 'UTF-8')));
			// Validate the filename length
			if ((utf8_strlen($filename) < 3) || (utf8_strlen($filename) > 64)) {
				$json['error'] = $this->language->get('error_filename');
			}
			// Allowed file extension types
			$allowed = array();
			$extension_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('cireviewpro_file_ext_allowed'));
			$filetypes = explode("\n", $extension_allowed);
			foreach ($filetypes as $filetype) {
				$allowed[] = trim($filetype);
			}
			if (!in_array(utf8_strtolower(utf8_substr(strrchr($filename, '.'), 1)), $allowed)) {
				$json['error'] = $this->language->get('error_filetype');
			}
			// Allowed file mime types
			$allowed = array();
			$mime_allowed = preg_replace('~\r?\n~', "\n", $this->config->get('cireviewpro_file_mime_allowed'));
			$filetypes = explode("\n", $mime_allowed);
			foreach ($filetypes as $filetype) {
				$allowed[] = trim($filetype);
			}
			if (!in_array($this->request->files['ciattachfile']['type'], $allowed)) {
				$json['error'] = $this->language->get('error_filetype');
			}
			// Check to see if any PHP files are trying to be uploaded
			$content = file_get_contents($this->request->files['ciattachfile']['tmp_name']);
			if (preg_match('/\<\?php/i', $content)) {
				$json['error'] = $this->language->get('error_filetype');
			}
			// Return any upload error
			if ($this->request->files['ciattachfile']['error'] != UPLOAD_ERR_OK) {
				$json['error'] = $this->language->get('error_upload_' . $this->request->files['ciattachfile']['error']);
			}
		} else {
			$json['error'] = $this->language->get('error_upload');
		}
		if (!$json) {
			$dir = 'catalog/cireviewpro_images/';
			if($this->config->get('cireviewpro_reviewimagespath')) {
				// remove trailing slah from path.
				do{
					if(utf8_substr($this->config->get('cireviewpro_reviewimagespath'), -1) == '/') {
					    $this->config->set('cireviewpro_reviewimagespath', utf8_substr($this->config->get('cireviewpro_reviewimagespath'), 0, -1));
					}
				}while(utf8_substr($this->config->get('cireviewpro_reviewimagespath'), -1) == '/');
				$dir = $this->config->get('cireviewpro_reviewimagespath').'/';
			}
			$odir = $dir;
			$dirs = explode("/", $dir);
			$mkdir = '';
			foreach ($dirs as $key => $value) {
				$mkdir .= $value.'/';
				$this->mkdir(DIR_IMAGE . $mkdir);
			}
			$pathfinfo = pathinfo($filename);
			$mask = $pathfinfo['filename'];
			$filename = 'as1'.time().'sd4' .'.'. $pathfinfo['extension'];
			$ext = $pathfinfo['extension'];
			$file = $dir. $filename;
			move_uploaded_file($this->request->files['ciattachfile']['tmp_name'], DIR_IMAGE . $file);
			// hold all images for temporary
			// id is cireview_image_id
			$this->model_cireviewpro_cireview->addUpload($file, $mask, $ext);
			$json['attach_images'] = array();
			$thumbwidth = (int)$this->config->get('cireviewpro_reviewimagesthumb_width');
			$thumbheight = (int)$this->config->get('cireviewpro_reviewimagesthumb_height');
			if(empty($thumbwidth) || is_null($thumbwidth) ) {
				$thumbwidth = 100;
			}
			if(empty($thumbheight) || is_null($thumbheight) ) {
				$thumbheight = 100;
			}
			$popupwidth = (int)$this->config->get('cireviewpro_reviewimagespopup_width');
			$popupheight = (int)$this->config->get('cireviewpro_reviewimagespopup_height');
			if(empty($popupwidth) || is_null($popupwidth) ) {
				$popupwidth = 500;
			}
			if(empty($popupheight) || is_null($popupheight) ) {
				$popupheight = 500;
			}
			$attach_images = $this->model_cireviewpro_cireview->getUploadedImage();
			$ids = array();
			foreach($attach_images as $attach_image) {
				$ids[] = $attach_image['cireview_image_id'];
				if(!empty($attach_image['image']) && file_exists(DIR_IMAGE .$attach_image['image'] )) {
					$thumb = $this->model_tool_image->resize($attach_image['image'], $thumbwidth, $thumbheight ) ;
					$popup = $this->model_tool_image->resize($attach_image['image'], $popupwidth, $popupheight ) ;
				} else {
					$thumb = $this->model_tool_image->resize('no_image.png', $thumbwidth, $thumbheight ) ;
					$popup = $this->model_tool_image->resize('no_image.png', $popupwidth, $popupheight ) ;
				}
				$image_description = $this->model_cireviewpro_cireview->getCiReviewImagesDescriptions($attach_image['cireview_image_id']);
				$json['attach_images'][] = array(
					'cireview_image_id' => $attach_image['cireview_image_id'],
					'thumb' => $thumb,
					'popup' => $popup,
					'alt' => !empty($image_description['alt']) ? htmlentities($image_description['alt'], ENT_QUOTES, 'UTF-8') : '',
					'title' => !empty($image_description['title']) ? htmlentities($image_description['title'], ENT_QUOTES, 'UTF-8') : '',
				);
			}
			$json['code'] = implode(",", $ids);
			$json['success'] = $this->language->get('text_upload');
		}
		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function delete() {
		$json = array();
		if(!empty($this->request->post['id'])) {
			$this->model_cireviewpro_cireview->removeUpload($this->request->post['id']);
			$json['success'] = $this->language->get('text_delete');;
			$json['attach_images'] = array();
			$thumbwidth = (int)$this->config->get('cireviewpro_reviewimagesthumb_width');
			$thumbheight = (int)$this->config->get('cireviewpro_reviewimagesthumb_height');
			if(empty($thumbwidth) || is_null($thumbwidth) ) {
				$thumbwidth = 100;
			}
			if(empty($thumbheight) || is_null($thumbheight) ) {
				$thumbheight = 100;
			}
			$popupwidth = (int)$this->config->get('cireviewpro_reviewimagespopup_width');
			$popupheight = (int)$this->config->get('cireviewpro_reviewimagespopup_height');
			if(empty($popupwidth) || is_null($popupwidth) ) {
				$popupwidth = 500;
			}
			if(empty($popupheight) || is_null($popupheight) ) {
				$popupheight = 500;
			}
			$attach_images = $this->model_cireviewpro_cireview->getUploadedImage();
			$ids = array();
			foreach($attach_images as $attach_image) {
				if(!empty($attach_image['image']) && file_exists(DIR_IMAGE .$attach_image['image'] )) {
					$thumb = $this->model_tool_image->resize($attach_image['image'], $thumbwidth, $thumbheight ) ;
					$popup = $this->model_tool_image->resize($attach_image['image'], $popupwidth, $popupheight ) ;
				} else {
					$thumb = $this->model_tool_image->resize('no_image.png', $thumbwidth, $thumbheight ) ;
					$popup = $this->model_tool_image->resize('no_image.png', $popupwidth, $popupheight ) ;
				}
				$ids[] = $attach_image['cireview_image_id'];
				$image_description = $this->model_cireviewpro_cireview->getCiReviewImagesDescriptions($attach_image['cireview_image_id']);
				$json['attach_images'][] = array(
					'cireview_image_id' => $attach_image['cireview_image_id'],
					'thumb' => $thumb,
					'popup' => $popup,
					'alt' => !empty($image_description['alt']) ? htmlentities($image_description['alt'], ENT_QUOTES, 'UTF-8') : '',
					'title' => !empty($image_description['title']) ? htmlentities($image_description['title'], ENT_QUOTES, 'UTF-8') : '',
				);
			}
			$json['code'] = '';
			if($ids) {	$json['code'] = implode(",", $ids); }
		}
		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}
	public function cireviewAbuse() {
		$json = array();
		$this->response->addHeader('Content-Type: application/json');
		if(!isset($this->request->post['ciabreason'])) {
			$json['error'] = $this->language->get('error_ciabreason');
			$this->response->setOutput(json_encode($json));
			$this->response->output();
			exit();
		}
		// abuse reasons
		$ciabreasons = $this->model_cireviewpro_cireview->getCiAbReasons($this->request->get['product_id']);
		$ciabreasonss = array();
		$ciabreasons_ = array();
		foreach ($ciabreasons as $ciabreason) {
			$ciabreasonss[] = $ciabreason['ciabreason_id'];
			$ciabreasons_[$ciabreason['ciabreason_id']] = array(
				'ciabreason_id' => $ciabreason['ciabreason_id'],
				'name' => $ciabreason['name'],
				'details' => $ciabreason['details'],
			);
		}
		if(isset($this->request->post['ciabreason']) && !in_array($this->request->post['ciabreason'], $ciabreasonss) && $this->request->post['ciabreason'] != 'OTHER' ) {
			$json['error'] = $this->language->get('error_ciabreason_invalid');
			$this->response->setOutput(json_encode($json));
			$this->response->output();
			exit();
		}
		if(isset($this->request->post['ciabreason']) && ($this->request->post['ciabreason']=='OTHER' || (isset($ciabreasons_[$this->request->post['ciabreason']]) && $ciabreasons_[$this->request->post['ciabreason']]['details']==1)) && (utf8_strlen($this->request->post['ciabreason_other']) < 10 || utf8_strlen($this->request->post['ciabreason_other']) > 1000) ) {
			$json['error'] = $this->language->get('error_ciabreason_other');
			$this->response->setOutput(json_encode($json));
			$this->response->output();
			exit();
		}
		if(isset($ciabreasons_[$this->request->post['ciabreason']])) {
			$this->request->post['ciabreason_name'] = $ciabreasons_[$this->request->post['ciabreason']]['name'];
			if($ciabreasons_[$this->request->post['ciabreason']]['details'] == 0) {
				$this->request->post['ciabreason_other'] = '';
			}
		} else {
			$this->request->post['ciabreason_name'] = $this->language->get('text_other');
			$this->request->post['ciabreason'] = 'OTHER';
		}
		$cireview_abuse = $this->model_cireviewpro_cireview->addCiAbReason($this->request->post);
		$json['success'] = $this->language->get('success_abuse');
		$this->response->setOutput(json_encode($json));
	}
	public function cireviewVote() {
		$json = array();
		$this->response->addHeader('Content-Type: application/json');
		if(!empty($this->request->post['review_id']) && !empty($this->request->post['product_id']) && !empty($this->request->post['cireview_id'])) {
			// check if already voted or not ?
			// we use cookies and session id to manage this.
			if(isset($this->request->cookies[$this->request->post['cireview_id'].'_cireview_'.$this->request->post['review_id'].'_'.$this->request->post['product_id']])) {
				$json['error'] = $this->language->get('error_alreadyvoted');
				$this->response->setOutput(json_encode($json));
				$this->response->output();
				exit();
			}
			$is_voted = $this->model_cireviewpro_cireview->isCiReviewVoted($this->request->post);
			if($is_voted) {
				$json['error'] = $this->language->get('error_alreadyvoted');
				$this->response->setOutput(json_encode($json));
				$this->response->output();
				exit();
			}
			$cireview_vote_id = $this->model_cireviewpro_cireview->addCiReviewVote($this->request->post);
			$json['success'] = $this->language->get('success_vote');
			setcookie($this->request->post['cireview_id'].'_cireview_'.$this->request->post['review_id'].'_'.$this->request->post['product_id'], 'true'); // Not ask until browser is open. Once browser closes ask again to confirm.
			$reviewvote_info = $this->config->get('cireviewpro_reviewvote');
			if(isset($reviewvote_info[(int)$this->config->get('config_language_id')])) {
				$reviewvote = $reviewvote_info[(int)$this->config->get('config_language_id')];
			} else {
				reset($reviewvote_info);
				$first_key = key($reviewvote_info);
				$reviewvote = $reviewvote_info[$first_key];
			}
			/*fill out empty language string*/
			foreach($reviewvote as $key => $votetext) {
				if(empty($votetext)) {
					$reviewvote[$key] =	$this->language->get('text_reviewvote_'.$key);
				}
			}
			if($this->request->post['action']==1) {
				$json['before_text'] = $reviewvote['yes'];
			} else {
				$json['before_text'] = $reviewvote['no'];
			}
			/*12-09-2019 Vote Feature Update Starts*/
			$cireview_info = $this->model_cireviewpro_cireview->getCiReviewByReviewId($this->request->post['review_id']);
			$total_voteup = $cireview_info['votes_up'];
			$total_votedown = $cireview_info['votes_down'];
			$total_votes = $total_voteup + $total_votedown;
			if ($total_votes) {
				if($this->config->get('cireviewpro_reviewvotetype') == 'PERCENT') {
				$json['after_text'] = str_replace('{PERCENTAGE}',  round($total_voteup*100/$total_votes,2), $reviewvote['percent'] );
				}
				if($this->config->get('cireviewpro_reviewvotetype') == 'OUTOF') {
					$json['after_text'] = str_replace(array('{VOTES}', '{TOTAL_VOTES}'), array($total_voteup, $total_votes), $reviewvote['outof']);
				}
			}

			/*$cireview_votes = $this->model_cireviewpro_cireview->getCiReviewVotes($this->request->post['cireview_id']);
			if($cireview_votes) {
				$total_votes = count($cireview_votes);
				$total_voteup = 0;
				$total_votedown = 0;
				foreach ($cireview_votes as $cireview_vote) {
					if($cireview_vote['vote']==1) {
						$total_voteup++;
					}
					if($cireview_vote['vote']==0) {
						$total_votedown++;
					}
				}
				if($this->config->get('cireviewpro_reviewvotetype') == 'PERCENT') {
				$json['after_text'] = str_replace('{PERCENTAGE}',  round($total_voteup*100/$total_votes,2), $reviewvote['percent'] );
				}
				if($this->config->get('cireviewpro_reviewvotetype') == 'OUTOF') {
					$json['after_text'] = str_replace(array('{VOTES}', '{TOTAL_VOTES}'), array($total_voteup, $total_votes), $reviewvote['outof']);
				}
			}*/
			/*12-09-2019 Vote Feature Update Ends*/
		}
		$this->response->setOutput(json_encode($json));
	}
}
if(!function_exists('token')) {
	function token($length = 32) {
		// Create random token
		$string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
		$max = utf8_strlen($string) - 1;
		$token = '';
		for ($i = 0; $i < $length; $i++) {
			$token .= $string[mt_rand(0, $max)];
		}
		return $token;
	}
}