<?php
class ControllerReportProductImage extends Controller { 
	public function index() {   
		$this->load->language('report/product_report');

		$this->document->setTitle($this->language->get('heading_title'));
		
   		$this->data['breadcrumbs'] = array();
		$url = '';
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
					'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
					'href'      => $this->url->link('report/product_report', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);		

		$this->load->model('report/product');

		$this->data['Excel'] = $this->url->link('report/product_report1', 'SSL');
		$this->data['products'] = array();

		$data = array(
			'filter_date_start'	     => '', 
			'filter_date_end'	       => '', 
			'filter_order_status_id' => '',
			'limit'                  => $this->config->get('config_admin_limit')
		);

		$product_total = 1;
		$this->session->data['product_details'] = '';
		
		if (isset($this->request->get['product_id'])) {
			$product_id = $this->request->get['product_id'];
		} else {
			$product_id = 108;
		}

		$products_dtl = $this->model_report_product->getProductReports($product_id); 

		$products_gemcount = $this->model_report_product->getProductGemCount($product_id);
		$products_metalcount = $this->model_report_product->getProductMetalCount($product_id);

		if($products_metalcount>1)$products_metalavail_2 = $this->model_report_product->getProductDefaultMetalAvail($product_id,2);
		if($products_metalcount>2)$products_metalavail_3 = $this->model_report_product->getProductDefaultMetalAvail($product_id,3);
		if($products_metalcount>3)$products_metalavail_4 = $this->model_report_product->getProductDefaultMetalAvail($product_id,4);
		if($products_metalcount>4)$products_metalavail_5 = $this->model_report_product->getProductDefaultMetalAvail($product_id,5);
		if($products_gemcount>1)$products_gemavail_2 = $this->model_report_product->getProductDefaultGemAvail($product_id,2);
		if($products_gemcount>2)$products_gemavail_3 = $this->model_report_product->getProductDefaultGemAvail($product_id,3);
		if($products_gemcount>3)$products_gemavail_4 = $this->model_report_product->getProductDefaultGemAvail($product_id,4);
		if($products_gemcount>4)$products_gemavail_5 = $this->model_report_product->getProductDefaultGemAvail($product_id,5);

		$prdname = $this->request->get['product_id'];
		
		$metal_part='';
		if($products_metalcount>1)$metal_part = $metal_part . '_M2-' . $products_metalavail_2['product_metalavail_id'];
		if($products_metalcount>2)$metal_part = $metal_part . '_M3-' . $products_metalavail_3['product_metalavail_id'];
//	if($products_metalcount>3)$metal_part = $metal_part . '_M4-' . $products_metalavail_4['product_metalavail_id'];
//	if($products_metalcount>4)$metal_part = $metal_part . '_M5-' . $products_metalavail_5['product_metalavail_id'];
		$gem_part='';
		if($products_gemcount>1)$gem_part = $gem_part . '_G2-' . $products_gemavail_2['product_gemavail_id'];
		if($products_gemcount>2)$gem_part = $gem_part . '_G3-' . $products_gemavail_3['product_gemavail_id'];
		if($products_gemcount>3)$gem_part = $gem_part . '_G4-' . $products_gemavail_4['product_gemavail_id'];
		if($products_gemcount>4)$gem_part = $gem_part . '_G5-' . $products_gemavail_5['product_gemavail_id'];

		if($products_gemcount>1)$gemimage2 = $this->resize( $products_gemavail_2['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_gemcount>2)$gemimage3 = $this->resize( $products_gemavail_3['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_gemcount>3)$gemimage4 = $this->resize( $products_gemavail_4['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_gemcount>4)$gemimage5 = $this->resize( $products_gemavail_5['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_metalcount>1)$metalimage2 = $this->resize( $products_metalavail_2['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_metalcount>2)$metalimage3 = $this->resize( $products_metalavail_3['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_metalcount>3)$metalimage4 = $this->resize( $products_metalavail_4['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
		if($products_metalcount>4)$metalimage5 = $this->resize( $products_metalavail_5['image'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );

 		if($products_gemcount>1)$gemimage2 = imagecreatefrompng(DIR_IMAGE . $gemimage2 );
 		if($products_gemcount>2)$gemimage3 = imagecreatefrompng(DIR_IMAGE . $gemimage3 );
 		if($products_gemcount>3)$gemimage4 = imagecreatefrompng(DIR_IMAGE . $gemimage4 );
 		if($products_gemcount>4)$gemimage5 = imagecreatefrompng(DIR_IMAGE . $gemimage5 );
   	
//   	echo '<br> image : ' . DIR_IMAGE . $metalimage2 .'<br>'; 
		if($products_metalcount>1)$metalimage2 = imagecreatefrompng(DIR_IMAGE . $metalimage2 );
 		if($products_metalcount>2)$metalimage3 = imagecreatefrompng(DIR_IMAGE . $metalimage3 );
			
    foreach($products_dtl as $product){
			$prdname =  $product_id . '_M1-' . $product['product_metalavail_id'] . $metal_part . '_G1-' . $product['product_gemavail_id'] . $gem_part . '.png';

			$marge_right = 0;
			$marge_bottom = 0;

			$im = imagecreatefrompng(DIR_IMAGE. 'TEMP/no_image.png');
			$gemimage1    = $this->resize( $product['gemimage'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );
			$metalimage1  = $this->resize( $product['metalimage'], $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height') );

			$metalimage1 = imagecreatefrompng(DIR_IMAGE . $metalimage1 );
  	 	$sx_m1 = imagesx($metalimage1);
			$sy_m1 = imagesy($metalimage1);
			imagecopy($im, $metalimage1, imagesx($im) - $sx_m1 - $marge_right, imagesy($im) - $sy_m1 - $marge_bottom, 0, 0, imagesx($metalimage1), imagesy($metalimage1));
	 		

			if($products_metalcount>1){
	  	 	$sx_m2 = imagesx($metalimage2);
				$sy_m2 = imagesy($metalimage2);
				imagecopy($im, $metalimage2, imagesx($im) - $sx_m2 - $marge_right, imagesy($im) - $sy_m2 - $marge_bottom, 0, 0, imagesx($metalimage2), imagesy($metalimage2));
			}
			if($products_metalcount>2){
  	 		$sx_m3 = imagesx($metalimage3);
				$sy_m3 = imagesy($metalimage3);
				imagecopy($im, $metalimage3, imagesx($im) - $sx_m3 - $marge_right, imagesy($im) - $sy_m3 - $marge_bottom, 0, 0, imagesx($metalimage3), imagesy($metalimage3));
			}

				$gemimage1 = imagecreatefrompng(DIR_IMAGE . $gemimage1 );
	  	 	$sx1 = imagesx($gemimage1);
				$sy1 = imagesy($gemimage1);
				imagecopy($im, $gemimage1, imagesx($im) - $sx1 - $marge_right, imagesy($im) - $sy1 - $marge_bottom, 0, 0, imagesx($gemimage1), imagesy($gemimage1));

			if($products_gemcount>1){
  	 		$sx2 = imagesx($gemimage2); 
				$sy2 = imagesy($gemimage2);
				imagecopy($im, $gemimage2, imagesx($im) - $sx2 - $marge_right, imagesy($im) - $sy2 - $marge_bottom, 0, 0, imagesx($gemimage2), imagesy($gemimage2));
			}
			if($products_gemcount>2){
  		 	$sx3 = imagesx($gemimage3);
				$sy3 = imagesy($gemimage3);
				imagecopy($im, $gemimage3, imagesx($im) - $sx3 - $marge_right, imagesy($im) - $sy3 - $marge_bottom, 0, 0, imagesx($gemimage3), imagesy($gemimage3));
			}
			if($products_gemcount>3){
  		 	$sx4 = imagesx($gemimage4);
				$sy4 = imagesy($gemimage4);
				imagecopy($im, $gemimage4, imagesx($im) - $sx4 - $marge_right, imagesy($im) - $sy4 - $marge_bottom, 0, 0, imagesx($gemimage4), imagesy($gemimage4));
			}
			if($products_gemcount>4){
	  	 	$sx5 = imagesx($gemimage5);
				$sy5 = imagesy($gemimage5);
				imagecopy($im, $gemimage5, imagesx($im) - $sx5 - $marge_right, imagesy($im) - $sy5 - $marge_bottom, 0, 0, imagesx($gemimage5), imagesy($gemimage5));
			}
//				echo '<br>'.DIR_IMAGE. 'TEMP/' . $prdname .'<br>';
				imagepng($im , DIR_IMAGE . 'TEMP/' .$prdname);
				$this->png2jpg(DIR_IMAGE. 'TEMP/' .  $prdname , DIR_IMAGE . 'TEMP/' . $prdname . '.jpg'  , 75);
				imagedestroy($im);

				$updateSQL = "update product_combinations set image='". 'TEMP/' .  $prdname  ."' where product_id='". $product_id ."' and product_metalavail_id ='" . $product['product_metalavail_id'] . "' and product_gemavail_id ='" . $product['product_gemavail_id'] . "'";
				$query = $this->db->query($updateSQL);
			  //echo $updateSQL . '<br>';
				echo "<a  target='_blank'  href= '" . HTTP_IMAGE. "TEMP/" .  $prdname. "'> Product #'. $product_id .' Combination Updated Successfully with M1 #" . $product['product_metalavail_id'] . ' ; G1 ' . $product['product_gemavail_id'] . '</a><br>';

		}
$updateSQL = "update product a set a.image=(SELECT b.image FROM `product_combinations` b where b.product_id=a.product_id and metal_default=1 and gem_default=1 ) where product_id='". $product_id ."' ";
$query1 = $this->db->query($updateSQL);


		$url = '';
		
		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = '';
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('report/product_report', 'token=' . $this->session->data['token'] . $url . '&page={page}');
			
		$this->data['pagination'] = $pagination->render();		
		
		$this->template = 'report/product_image.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}	 
	
	public function updateMetalDetails() {   
		$this->load->language('report/product_report');
		$this->load->model('report/product');

		if (isset($this->request->get['metal_id'])) {
			$metal_id = $this->request->get['metal_id'];
			$products_all_metalavail = $this->model_report_product->updateAllProductsMetalDtl($metal_id);
		}
	
	}
	public function updateGemDetails() {    
		$this->load->language('report/product_report');
		$this->load->model('report/product');

		if (isset($this->request->get['gem_id'])) {
			$gem_id = $this->request->get['gem_id'];
			$products_all_gemavail = $this->model_report_product->updateAllProductsGemDtl($gem_id);
		}
	}

	public	function png2jpg($originalFile, $outputFile, $quality) {
		
//			echo '<br>' . $originalFile .'<br>';
    	$image = imagecreatefrompng($originalFile);
    	imagejpeg($image, $outputFile, $quality);
    	imagedestroy($image);
	}
	
	public function resize($filename, $width, $height) {
		// $this->log->write( DIR_IMAGE . $filename );

		if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) {
			$this->log->write(' Image Not found : ' . DIR_IMAGE . $filename );

			return;
		} 
		 //$this->log->write( DIR_IMAGE . $filename );
		$info = pathinfo($filename);
		$extension = $info['extension'];
		
		$old_image = $filename;
		$new_image = 'cache/' . utf8_substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension;
		
		if (!file_exists(DIR_IMAGE . $new_image) || (filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image))) {
			$path = '';
			
			$directories = explode('/', dirname(str_replace('../', '', $new_image)));
			
			foreach ($directories as $directory) {
				$path = $path . '/' . $directory;
				
				if (!file_exists(DIR_IMAGE . $path)) {
					@mkdir(DIR_IMAGE . $path, 0777);
				}		
			}
			
			$image = new Image(DIR_IMAGE . $old_image);
			$image->resize($width, $height);
			$image->save(DIR_IMAGE . $new_image);
		}
	
		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			return   $new_image;
		} else {
			return   $new_image;
		}	
	}
	
	
	
}
?>