// Service : packagedetail
$mainframe = JFactory::getApplication();
$option = JRequest::getVar('option','com_jewebservices','','string');
$post = JRequest::get ('post');  
$uri = JURI::getInstance();
$url= $uri->root();
$this->_table_prefix = "#__jetour_";
global $HTTP_RAW_POST_DATA;
 
$post = json_decode($HTTP_RAW_POST_DATA, true);
//require_once (JPATH_ROOT.'/components/com_jetour/helpers/thumbnail.php');	
//require_once (JPATH_ROOT.'/components/com_jetour/helpers/image.class.php');	
/*echo "<pre>";
print_r($post);
exit;*/
//$post['package_id'] = 502;
/*$post['countryid'] = 37;
$post['themeid'] = 2;*/

$image_path=$url.'components/com_jetour/assets/';
if($post['package_id']){		 
	$query =' SELECT p.id,p.thumbimage,p.packagetype_id,p.start_date,p.end_date,p.pname,p.day,p.night,p.min_price,p.currency,p.pack_desc FROM '.$this->_table_prefix.'package as p  WHERE p.published=1 '.
				' AND ( `start_date` <= DATE(NOW()) AND `end_date` >= DATE(NOW()) ) '.
				' AND p.id='.$post['package_id'].
				' ORDER BY p.id';
 
	$db->setQuery($query);

	$packagedata =  $db->loadObject();


if(count($packagedata)){
 $packagelist = array();
  
  $packagelistdata = array();
  $theme_id = "";
  $packagelistdata['packageid'] = $packagedata->id;
$packagelistdata['packagesharelink'] = rtrim($url,'/').JRoute::_('index.php?option=com_jetour&view=holidaypackages&Itemid=101&package_id='.$packagedata->id);
  	$favoritepackage = '';
  	if($post['user_id']){
		$favoriteq	= 'SELECT DISTINCT package_id FROM '.$this->_table_prefix.'favoritepackage WHERE user_id='.$post['user_id'].' AND package_id='.$packagedata->id;
		$db->setQuery($favoriteq);
		$favoritepackage =  $db->loadResult();
	}
  	if($favoritepackage==$packagedata->id)
		$packagelistdata['favorite_package'] = 1;
	else
		$packagelistdata['favorite_package'] = 0;
  
  	$sql="SELECT t.thname FROM ".$this->_table_prefix."package_theme as pt INNER JOIN ".$this->_table_prefix."theme as t ON t.id=pt.theme_id WHERE pt.package_id=".$packagedata->id;
	$db->setQuery($sql);
	$thmid = $db->loadColumn();
  	$theme_id = implode(', ',$thmid);
  
  	$sqlreg="SELECT region_id FROM ".$this->_table_prefix."package_stopfront WHERE package_id=".$packagedata->id;
	$db->setQuery($sqlreg);
	$regionidss = implode(',',$db->loadColumn());
	
	if(count($db->loadColumn())){
  
  	$sql="SELECT city_name FROM ".$this->_table_prefix."city WHERE city_id IN(".$regionidss.")" ;	
	$db->setQuery($sql);
	$regions = implode (', ',$db->loadColumn());
	
	}else{
	$regions = "";
	}
   if($packagedata->currency=='0')
	$currencysymbol = '¥';
	 if($packagedata->currency=='1')
	$currencysymbol = '€';
	 if($packagedata->currency=='2')
	$currencysymbol = 'LBP';
	 if($packagedata->currency=='3')
	$currencysymbol = '$';
  
   if($packagedata->packagetype_id==6){
	 $packagelistdata['flighttypes'] = "Fixed Departure";
  }else{
  	if(strpos($theme_id,'Charter')!==FALSE)
	 $packagelistdata['flighttypes'] = "Charter Flight";
        else if(strpos($theme_id,'Fixed Departure')!==FALSE)
	 $packagelistdata['flighttypes'] = "Fixed Departure";
	 else
	 $packagelistdata['flighttypes'] = "";
  }
  
  
  $packagelistdata['packagename'] = $packagedata->pname;
  
  $areplace = array('<p> </p>','<div> </div>','<br /><br /><br />');
 
   $packagelistdata['pack_description'] = $packagedata->pack_desc;

 $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
    $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
  //  $result = str_replace($escapers, $replacements, $packagedata->pack_desc);
//str_replace($escapers, $replacements, $packagedata->pack_desc);
$descriptionss = str_replace('<p>','', preg_replace("@[\\r|\\n|\\t]+@", "", $packagedata->pack_desc));
$descriptionss = str_replace('</p>','<br>', $descriptionss);
$descriptionss = str_replace('<br> <br>','<br>', $descriptionss);
  $packagelistdata['pack_description_iphon'] = $descriptionss;
  $packagelistdata['more_details'] = $url.'index.php?option=com_jetour&view=holidaypackages&apllications_page=1&package_id='.$packagedata->id.'&tmpl=component';
  $packagelistdata['packagedays'] = $packagedata->day;
  $packagelistdata['packagenight'] = $packagedata->night;
  
  $queryimage = 'SELECT pimage FROM '.$this->_table_prefix.'image WHERE p_id='.$packagedata->id.' AND main_image=1';
				  
  $db->setQuery($queryimage);
		
 $bigimage = $db->loadResult(); 
 //echo JPATH_ROOT.DS.'components/com_jetour/assets/package_images/'.$bigimage;exit;
 
   /*if(isset($post['iwidth']) && isset($post['iheight']))
  { 
			if($bigimage!='') { 
				
			$dest 	= JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$bigimage; 
				
			$dest1 	= JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage;
			
			if(!file_exists($dest1))
			{
				$filetype123 =JFile::getExt($bigimage);
				copy($dest,$dest1);	
				
				switch($filetype123){
				   case "jpe": case "jpeg":  case "jpg":
					   $filetype123 = IMAGETYPE_JPEG;
					   break;
				   case 'png':
					   $filetype123 = IMAGETYPE_PNG;
					   break;
				   case 'gif':
					   $filetype123 = IMAGETYPE_GIF;
					   break;
					}
						
			    $img_th 	= new Thumbnail();
				
		  		$thumboption 	= array('type' => $filetype123,'width' => $post['iwidth'],'height'  => $post['iheight'],'method'  => THUMBNAIL_METHOD_SCALE_MAX);
				
		  		Thumbnail::output($dest, $dest1, $thumboption);
				
			}
	   		 //$slidelistdata['slideimages'] = $image_path.$slidedata[$d]->imgname;
			//$slidelistdata['slideimages']=$imagethumb_path.$post['img_width'].'_'.$post['img_height'].$bigimage;
			//$slidelistdata['slidethumbimagesnew'] = $imagethumb_path.$slidedata[$d]->imgname;
			//$packagelistdata['thumbimage'] = str_replace(' ','%20',$image_path.$myimage_name);
			} 
			
	}*/
			
			/*if($bigimage!='') { 	
					
				$filetype = strtoupper(JFile::getExt($bigimage));

				if($filetype=='JPG' || $filetype=='JPEG' || $filetype=='PNG' || $filetype=='GIF') {
					
 					$pthumb_width	= '200';

					$pthumb_height	= '128';

					$dest 	= JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$bigimage; 
				
					$dest1 	= JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage;
					
 
					if($filetype=='JPG') {

						$my_type = 'IMAGETYPE_JPEG';

					} else {

						$my_type = 'IMAGETYPE_'.$filetype;

					}

					if(!file_exists($dest1)) {
					 
					$save_to_file = true;
					$image_quality = 100;
					$image_type = -1;
					$max_x = $post['iwidth'];
					$max_y = $post['iheight'];
					$cut_x = 0;
					$cut_y = 0;
				  
					$img = new Zubrag_image;
					
					// initialize
					$img->max_x        = $max_x;
					$img->max_y        = $max_y;
					$img->cut_x        = $cut_x;
					$img->cut_y        = $cut_y;
					$img->quality      = $image_quality;
					$img->save_to_file = $save_to_file;
					$img->image_type   = $image_type;
				
					$img->GenerateThumbFile($dest, $dest1);}
					
					}
				}*/
				
				/*$imagick = new Imagick(realpath(JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$bigimage));
				$imagick->setbackgroundcolor('rgb(64, 64, 64)');
				$imagick->thumbnailImage($post['iwidth'], $post['iheight'], true, true);*/
				
				
	
				/*$im = new Imagick();
				$im->pingImage($image);
				$im->readImage( $image );
				//$im->thumbnailImage( $post['iwidth'], $post['iheight'] );
				$im->cropThumbnailImage( $post['iwidth'], $post['iheight'] );
				 
				$im->writeImage(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/Crop_'.$post['iwidth'].'_'.$post['iheight'].$bigimage);
				$im->destroy();*/
				if($bigimage!='') { 
				if(!file_exists(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage)) {
				$image = JPATH_ROOT.'/components/com_jetour/assets/package_images/'.$bigimage;
				$im = new Imagick($image);
                             
				$geo = $im->getImageGeometry();

				// crop the image
				if(($geo['width']/$post['iwidth']) < ($geo['height']/$post['iheight']))
				{
					$im->cropImage($geo['width'], floor($post['iheight']*$geo['width']/$post['iwidth']), 0, (($geo['height']-($post['iheight']*$geo['width']/$post['iwidth']))/2));
				}
				else
				{
					$im->cropImage(ceil($post['iwidth']*$geo['height']/$post['iheight']), $geo['height'], (($geo['width']-($post['iwidth']*$geo['height']/$post['iheight']))/2), 0);
				}
				
				$im->ThumbnailImage( $post['iwidth'], $post['iheight'] );
				
				$im->writeImage(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage);
				$im->destroy();
				}
				}
				  if($bigimage!='') { 
						if( file_exists(JPATH_ROOT.'/'.'components/com_jetour/assets/package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage) ) {
							$myimage_name	= 'package_images/'.$post['iwidth'].'_'.$post['iheight'].$bigimage;
						} else {
							$myimage_name	= 'images/noimage.jpg';
						}
					} else {
						$myimage_name	= 'images/noimage.jpg';
					}
  
  
  $packagelistdata['thumbimage'] = str_replace(' ','%20',$image_path.$myimage_name);
  $packagelistdata['start_date'] = $packagedata->start_date;
  $packagelistdata['end_date'] = $packagedata->end_date;
  $packagelistdata['themename'] = $theme_id;
  $packagelistdata['regionname'] = $regions;
  $packagelistdata['startingprice'] = $currencysymbol.' '.$packagedata->min_price;
  
  
  $packagelist[] = $packagelistdata; 
   
  
 $arr = array('success' => 1, 'packagelist' => $packagelistdata);
 echo json_encode($arr,JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_AMP);exit;
}else{
 $arr = array('success' => 0, 'message' => "No Records Found" ); 
 echo json_encode($arr);exit;
}
}else{
	$arr = array('success' => 0, 'message' => "No Records Found" ); 
 	echo json_encode($arr);exit;
}