<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * undocumented class
 *
 * @package default
 * @author /bin/bash: niutil: command not found
 **/
class MFuncs extends Model {

	function MFuncs()
	{
		parent::Model();
		log_message('debug', 'MProducts Model Initialized');
		
	}
	  function getKeywords(){
    $data = array();
		$this->db->select('*');
		$this->db->from('keywords');
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }
    $Q->free_result();    
    return $data;    
 }
function createRandomPassword() { 

    $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
    srand((double)microtime()*1000000); 
    $i = 0; 
    $pass = '' ; 

    while ($i <= 7) { 
        $num = rand() % 33; 
        $tmp = substr($chars, $num, 1); 
        $pass = $pass . $tmp; 
        $i++; 
    } 

    return $pass; 

} 
	  function getNameDays(){
    $data_namedays = array();
	  date_default_timezone_set("Europe/Athens");
		$ld=$this->session->userdata('lang_id');
	  $now = date("Y-m-d G:i:s");
		$data=array();
		$this->db->select("*");
		$this->db->from('giortes');
	  	$this->db->where('active','Y');
		$this->db->where('lang_id',$ld);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
	$holiday_date =strtotime($row['date']);

   $ENTdmy = date("dmY",$holiday_date);
  
   //$ENTh = date("H",$holiday_date);
   //$ENTi = date("i",$holiday_date);
   
         $CHKdmy = date('dmY', $today=strtotime("now"));
         //$CHKh = date('H', $today=strtotime("now"));
         //$CHKi = date('i', $today=strtotime("now"));

         if($ENTdmy == $CHKdmy){
        $data_namedays = $row;
//		 if(($ENTh == $CHKh)&&(($CHKi - $ENTi)>30)){
//				  $data_info = array( 
//					'selected' => 'N',
//					'date_selected' => NULL,
//					'ex_id_selected' => 0
//					);
//	  			  $this->db->where('id',$row['id']);
//				  $this->db->update('side_plan',$data_info);	
//		  } elseif( (($ENTh+1) == $CHKh)&&( ((60-$ENTi)+$CHKi)>30 ) ){
//		  $data_info = array( 
//			'selected' => 'N',
//			'date_selected' => NULL,
//			'ex_id_selected' => 0
//			);
//	  	  $this->db->where('id',$row['id']);
//		  $this->db->update('side_plan',$data_info);	
//          } elseif ( ($CHKh - $ENTh)>=2 ) {
//		  $data_info = array( 
//			'selected' => 'N',
//			'date_selected' => NULL,
//			'ex_id_selected' => 0
//			);
//	  	  $this->db->where('id',$row['id']);
//		  $this->db->update('side_plan',$data_info);	
//			 }
         } else{
//		  $data_info = array( 
//			'selected' => 'N',
//			'date_selected' => NULL,
//			'ex_id_selected' => 0
//			);
//	  	  $this->db->where('id',$row['id']);
//		  $this->db->update('side_plan',$data_info);	
         }
			}
		}
		$Q->free_result();
    return $data_namedays;    
 }
 	 function getLinks($section) {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('lang_id',$ld);
		$this->db->where('section',$section);
		$this->db->where('status','active');
		$this->db->orderby("order","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
 	 function getLinksfooter() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('section','footer');
		$this->db->where('lang_id',$ld);
		$this->db->orderby("order","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
 	 function getLinkbyID($id) {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('id',$id);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }
		$Q->free_result();
		return $data;
	}
 	 function getCountries() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('countries');
    	$this->db->orderby("name","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
 	 function getCountriesOld() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('countries');
		$this->db->where('zones != ""');
    	$this->db->orderby("id","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function RemoveTones($string)
 {
  //Custom replacements for www.newsbomb.gr
  $search   = array('ά','έ','ή','ί','ό','ύ','ώ');
  $replace = array('α','ε','η','ι','ο','υ','ω');
  $string = trim( $string );
  $string = str_replace($search, $replace, $string);
  
  return $string;
 }
function stringURLSafe($string)
 {
  //Custom replacements for www.newsbomb.gr
  $search   = array('α','ά','Ά','Α','β','Β','γ', 'Γ', 'δ','Δ','ε','έ','Ε','Έ','ζ','Ζ','η','ή','Η',"Ή",'θ','Θ','ι','ί','ϊ','ΐ','Ι','Ί', 'κ','Κ','λ','Λ','μ','Μ','ν','Ν','ξ','Ξ','ο','ό','Ο','Ό','π','Π','ρ','Ρ','σ','ς', 'Σ','τ','Τ','υ','ύ','Υ','Ύ','φ','Φ','χ','Χ','ψ','Ψ','ω','ώ','Ω','Ώ',' ',"'","'",',','(',')','.','›','»','‹','«','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','...','…','’','%','Ϊ','–', 'Ü', 'ü', '“', '”', '΄','é');
  $replace = array('a', 'a','a','a','v','v','g','g','d','d','e','e','e','e','z','z','i','i','i','i','th','th','i','i','i','i','i','i','k','k','l','l','m','m','n','n','x','x','o','o','o','o','p','p' ,'r','r','s','s','s','t','t','y','y','y','y','f','f','h','h','ps','ps','o','o','o','o','-','-','-','-','','','','','','','','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','','','','','i','-', 'u', 'u','','','','e');
  $string = trim( $string );
  $string = str_replace($search, $replace, $string);

  //replace double byte whitespaces to single byte
  $str = preg_replace('/\xE3\x80\x80/', ' ', $string);
  
  
  // remove any '-' from the string as they will be used as concatenator. 
  // Would be great to let the spaces in but only Firefox is friendly with this
 
  //$str = str_replace('-', ' ', $str);
  
  //replace forbidden characters by whitespaces
  //$forbidden = array('/:/','/#/','/\*/','/"/','/&/','/%/','/\./','/\;/','/]/','/\@/','/\+/','/\=/','/\!/','/\//','/\'/','/\\\/','/\|/','/\[/');
  //$str = preg_replace($forbidden,'', $str);
  $str = preg_replace( '#[:\#\*"@+=;!&%\.\]\/\'\\\\|\[]#',"\x20", $str );
  
  //delete all '?'
  $str = str_replace('?', '', $str);
  
  //trim white spaces at beginning and end of alias
  $str = trim( $str );

  // remove any duplicate whitespace and replace whitespaces by hyphens
  $str =preg_replace('#\x20+#','', $str);
  return $str;
 }
 	 function getUpLinks() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('right_category');
		$this->db->where('lang_id',$ld);
		$this->db->orderby("order","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
 	 function getUpSubLinks($cat_id) {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('right_sub_cat');
		$this->db->where('lang_id',$ld);
		$this->db->where('cat_id',$cat_id);
		$this->db->where('status','active');
		$this->db->orderby("name","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
 	 function getCatImages($cat_id) {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('category_images');
		$this->db->where('lang_id',$ld);
		$this->db->where('cat_id',$cat_id);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }
		$Q->free_result();
		return $data;
	}
	function getRdmProductbyCat($cat_id){
		$data = array();
		$Q = $this->db->query("SELECT * FROM products JOIN product_categories ON products.id=product_categories.p_id WHERE products.published='Yes' AND product_categories.cat_id = ".$cat_id." order by RAND() limit 1");
		if ($Q->num_rows() > 0){
			$data = $Q->row_array();
		}
		$Q->free_result();
		return $data;
	}
function validEmail($email){
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex){
   		$isValid = false;
   }else{
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64){
         // local part length exceeded
         $isValid = false;
      }else if ($domainLen < 1 || $domainLen > 255){
         // domain part length exceeded
         $isValid = false;
      }else if ($local[0] == '.' || $local[$localLen-1] == '.'){
         // local part starts or ends with '.'
         $isValid = false;
      }else if (preg_match('/\\.\\./', $local)){
         // local part has two consecutive dots
         $isValid = false;
      }else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)){
         // character not valid in domain part
         $isValid = false;
      }else if (preg_match('/\\.\\./', $domain)){
         // domain part has two consecutive dots
         $isValid = false;
      }else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',str_replace("\\\\","",$local))){
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',str_replace("\\\\","",$local))){
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A"))){
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

		function getRandProducts() {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM products join product_details on products.id=product_details.p_id join product_categories on product_categories.p_id=products.id join product_subcategories on product_subcategories.p_id=products.id where product_details.lang_id=".$ld." AND products.published='Yes' AND (product_categories.cat_id='19' OR product_subcategories.subcat_id='59' OR product_subcategories.subcat_id='67' OR product_subcategories.subcat_id='63') order by RAND() limit 4");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
		function getRandForeignProducts() {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM products join product_details on products.id=product_details.p_id join product_categories on product_categories.p_id=products.id where product_details.lang_id=".$ld." AND products.published='Yes' AND product_categories.cat_id='31' order by RAND() limit 2");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
		function getRandOfferProducts($limit) {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM products join product_details on products.id=product_details.p_id join product_categories on product_categories.p_id=products.id where product_details.lang_id=".$ld." AND products.published='Yes' AND products.offers='Yes' AND product_categories.cat_id!='21' order by RAND() limit ".$limit."");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
		function getRandHomeCategories() {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM category where lang_id=".$ld." AND home_page='Yes'");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
		function getRandHomeSubCategories() {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM sub_category where lang_id=".$ld." AND home_page='Yes'");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
		function getRandHomeSubSubCategories() {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM sub_sub_category where lang_id=".$ld." AND home_page='Yes'");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getRandProductbyCat($cat_id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('products');
	    $this->db->join('product_details','products.id=product_details.p_id', 'left');
	    $this->db->join('product_categories','product_categories.p_id=products.id', 'left');
		$this->db->where('product_details.lang_id',$ld);
		$this->db->where('products.published','Yes');
		$this->db->where('product_categories.cat_id',$cat_id);
		$this->db->orderby("RAND()");
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
		function getRandProductbyCatOld($cat_id) {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM products join product_details on products.id=product_details.p_id join product_categories on product_categories.p_id=products.id join product_subcategories on product_subcategories.p_id=products.id where product_details.lang_id=".$ld." AND products.published='Yes' AND product_categories.cat_id=".$cat_id." order by RAND() limit 1");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getRandProductbySubCat($sub_id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('products');
	    $this->db->join('product_details','products.id=product_details.p_id', 'left');
	    $this->db->join('product_subcategories','product_subcategories.p_id=products.id', 'left');
		$this->db->where('product_details.lang_id',$ld);
		$this->db->where('products.published','Yes');
		$this->db->where('product_subcategories.subcat_id',$sub_id);
		$this->db->orderby("RAND()");
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
		function getRandProductbySubCatOld($sub_id) {
		$ld=$this->session->userdata('lang_id');
		$data = array();
		$Q = $this->db->query("SELECT DISTINCT * FROM products join product_details on products.id=product_details.p_id join product_categories on product_categories.p_id=products.id join product_subcategories on product_subcategories.p_id=products.id where product_details.lang_id=".$ld." AND products.published='Yes' AND product_subcategories.sub_id=".$sub_id." order by RAND() limit 1");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getRandProductbySubSubCat($sub_sub_id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('products');
	    $this->db->join('product_details','products.id=product_details.p_id', 'left');
	    $this->db->join('product_subsubcategories','product_subsubcategories.p_id=products.id', 'left');
		$this->db->where('product_details.lang_id',$ld);
		$this->db->where('products.published','Yes');
		$this->db->where('product_subsubcategories.subsubcat_id',$sub_sub_id);
		$this->db->orderby("RAND()");
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	 function getProductPhotos($id) {
		$data=array();
		$this->db->select('*');
		$this->db->from('photo');
		$this->db->where('photo_p_id',$id);
		$this->db->orderby("date_added","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}

	 function getCategories() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('category');
		$this->db->where('lang_id',$ld);
		$this->db->orderby("order","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function search($term, $limit, $current, $sorted){
		$data=array();
	if ($current==''){
	$current=0;
	}
		$this->db->select("*");
		$this->db->from('products');
	    $this->db->join('product_details','products.id=product_details.p_id', 'left');
		$this->db->where('product_details.lang_id',id_clean($this->session->userdata('lang_id')));
		$this->db->where('products.published','Yes');
		$this->db->where("(product_details.name LIKE '%$term%' OR product_details.description LIKE '%$term%' OR product_details.suggestions LIKE '%$term%' OR product_details.characteristics LIKE '%$term%' OR product_details.vinifications LIKE '%$term%' OR product_details.microclimate LIKE '%$term%' OR product_details.other_characteristics LIKE '%$term%')");
		if ($sorted=='name') {
		$this->db->orderby("product_details.name",'asc');
		} elseif ($sorted=='priceasc') { 
		$this->db->orderby("products.price",'asc');
		} elseif ($sorted=='pricedesc') {
		$this->db->orderby("products.price",'desc');
		}
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;



//		$data=array();
//		$this->db->select("*");
//		$this->db->from('products');
//	    $this->db->join('product_details','products.id=product_details.p_id', 'left');
//		$this->db->where('product_details.lang_id',id_clean($this->session->userdata('lang_id')));
//		$this->db->where('products.published','Yes');
//		$this->db->where("(product_details.name LIKE '%$term%' OR product_details.description LIKE '%$term%' OR product_details.suggestions LIKE '%$term%' OR product_details.characteristics LIKE '%$term%' OR product_details.vinifications LIKE '%$term%' OR product_details.microclimate LIKE '%$term%' OR product_details.other_characteristics LIKE '%$term%')");
//		//$this->db->where("(locations.location LIKE '%$term%')");
//		$this->db->orderby('product_details.name','asc');	
//		$this->db->limit(50);
//		$Q=$this->db->get();
//		if ($Q->num_rows() > 0){
//			foreach ($Q->result_array() as $row){
//				$data[] = $row;
//			}
//		}
//		$Q->free_result();
//		return $data;
}
function Livesearch($term){
		$data=array();
		$this->db->select("*");
		$this->db->from('products');
	    $this->db->join('product_details','products.id=product_details.p_id', 'left');
		$this->db->where('product_details.lang_id','2');
		$this->db->where('products.published','Yes');
		$this->db->where("product_details.name LIKE '%$term%'");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
}
function getNameDayPrds($limit, $current, $sorted){
		$data=array();
	if ($current==''){
	$current=0;
	}
				$this->db->select("*");
				$this->db->from('products');
				$this->db->join('product_details','products.id=product_details.p_id', 'left');
				$this->db->where('product_details.lang_id',id_clean($this->session->userdata('lang_id')));
				$this->db->where('products.published','Yes');
				$this->db->where('products.nameday', 'Yes');
				if ($sorted=='name') {
				$this->db->orderby("product_details.name",'asc');
				} elseif ($sorted=='priceasc') { 
				$this->db->orderby("products.price",'asc');
				} elseif ($sorted=='pricedesc') {
				$this->db->orderby("products.price",'desc');
				}
				//$this->db->orderby("order","asc");
				$this->db->limit($limit, $current);
				$Q=$this->db->get();
				if($Q->num_rows() > 0){
					foreach($Q->result_array() as $row){
						$data[] = $row;
					}
				}
		$Q->free_result();
		return $data;
}
	 function getUserOrders($id) {
		$data=array();
		$this->db->select('*');
		$this->db->from('orders');
		$this->db->where('user_id',$id);
		$this->db->where('completed','Yes');
		$this->db->orderby("date_added","DESC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	 function GetOrderDetails($order_id) {
		$data=array();
		$this->db->select('*');
		$this->db->from('order_details');
		$this->db->where('order_id',$order_id);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	 function getSubCategories($c_id) {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('sub_category');
		$this->db->where('lang_id',$ld);
		$this->db->where('cat_id',$c_id);
		$this->db->orderby("name","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	 function getSubSubCategories($c_id) {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('sub_sub_category');
		$this->db->where('lang_id',$ld);
		$this->db->where('sub_id',$c_id);
		$this->db->orderby("name","ASC");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getProductCat($id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('product_categories');
		$this->db->where('p_id',$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getProductSubCat($id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('product_subcategories');
		$this->db->where('p_id',$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getProductSubSubCat($id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('product_subsubcategories');
		$this->db->where('p_id',$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getCategory($cat_id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('category');
		$this->db->where('lang_id',$ld);
		$this->db->where("cat_id",$cat_id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getSubCategory($cat_id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('sub_category');
		$this->db->where('lang_id',$ld);
		$this->db->where("sub_id",$cat_id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getSubSubCategory($cat_id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('sub_sub_category');
		$this->db->where('lang_id',$ld);
		$this->db->where("sub_sub_id",$cat_id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	 function getNews() {
		$ld=$this->session->userdata('lang_id');
		$data=array();
		$this->db->select('*');
		$this->db->from('news');
		$this->db->where('lang_id',$ld);
		$this->db->where('active','Yes');
		$this->db->orderby("date","desc");
		$this->db->limit(6);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getNew($id){
		$ld=$this->session->userdata('lang_id');
	    $data = array();
		$this->db->select('*');
		$this->db->from('news');
		$this->db->where('lang_id',$ld);
		$this->db->where("id",$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	
	function getPage($path, $ld){
    $data = array();
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('lang_id',$ld);
		$this->db->where("path",$path);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getVariety($var_id, $ld){
    $data = array();
		$this->db->select('*');
		$this->db->from('varieties');
		$this->db->where('lang_id',$ld);
		$this->db->where("var_id",$var_id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getPagebyID($id){
    $data = array();
		$this->db->select('*');
		$this->db->from('pages');
		$this->db->where('id',$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
	function getPagebyID2($id){
	$ld=$this->session->userdata('lang_id');
    $data = array();
		$this->db->select('*');
		$this->db->from('right_sub_cat');
		$this->db->where('sub_id',$id);
		$this->db->where('lang_id',$ld);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getProductsbyCat($cat_id, $limit, $current) {
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_categories');
		$this->db->join('products','product_categories.p_id=products.id', 'left');
		$this->db->where('product_categories.cat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->orderby("products.id","asc");
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;
	}
function getProductsbyCatPrice($cat_id, $limit, $current, $sorted) {
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_categories');
		$this->db->join('products','product_categories.p_id=products.id', 'left');
		$this->db->where('product_categories.cat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->orderby("products.price",$sorted);
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;
	}
function getProductsbyCatall($cat_id) {
		$data=array();
		$this->db->select("*");
		$this->db->from('product_categories');
		$this->db->join('products','product_categories.p_id=products.id', 'left');
		$this->db->where('product_categories.cat_id',$cat_id);
		$this->db->orderby("products.id","asc");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;
	}
function getProductsbySubCatall($subcat_id) {
		$data=array();
		$this->db->select("*");
		$this->db->from('product_subcategories');
		$this->db->join('products','product_subcategories.p_id=products.id', 'left');
		$this->db->where('product_subcategories.subcat_id',$subcat_id);
		$this->db->orderby("products.id","asc");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;
	}
function getVarieties() {
		$data=array();
		$this->db->select("*");
		$this->db->from('varieties');
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}

function getProductsbyCatPrice_new($cat_id, $limit, $current, $sorted) {
$ld=$this->session->userdata('lang_id');
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_categories');
		$this->db->join('products','product_categories.p_id=products.id', 'left');
		$this->db->join('product_details','product_details.p_id=products.id', 'left');
		$this->db->where('product_categories.cat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->where('product_details.lang_id',$ld);
		if ($sorted=='name') {
		$this->db->orderby("product_details.name",'asc');
		} elseif ($sorted=='priceasc') { 
		$this->db->orderby("products.price",'asc');
		} elseif ($sorted=='pricedesc') {
		$this->db->orderby("products.price",'desc');
		}
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		
		$Q->free_result();
		return $data;
	}
function getProductsbySubCatPrice_new($cat_id, $limit, $current, $sorted) {
//	if ($current==''){
//	$current=0;
//	}
//		$data = array();
//		$Q = $this->db->query('select * from product_subcategories left join products ON product_subcategories.p_id=products.id left join product_details ON product_details.p_id=products.id where product_subcategories.subcat_id='.$cat_id.' AND products.published="Yes" order by product_details.name asc LIMIT '.$current.', '.$limit.'');
//		if ($Q->num_rows() > 0){
//			foreach ($Q->result_array() as $row){
//				$data[] = $row;
//			}
//		}
//		$Q->free_result();
//		return $data;
$ld=$this->session->userdata('lang_id');
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_subcategories');
		$this->db->join('products','product_subcategories.p_id=products.id', 'left');
		$this->db->join('product_details','product_details.p_id=products.id', 'left');
		$this->db->where('product_subcategories.subcat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->where('product_details.lang_id',$ld);
		if ($sorted=='name') {
		$this->db->orderby("product_details.name",'asc');
		} elseif ($sorted=='priceasc') { 
		$this->db->orderby("products.price",'asc');
		} elseif ($sorted=='pricedesc') {
		$this->db->orderby("products.price",'desc');
		}
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function getProductsbySubSubCatPrice_new($cat_id, $limit, $current, $sorted) {
//	if ($current==''){
//	$current=0;
//	}
//		$data = array();
//		$Q = $this->db->query('select * from product_subcategories left join products ON product_subcategories.p_id=products.id left join product_details ON product_details.p_id=products.id where product_subcategories.subcat_id='.$cat_id.' AND products.published="Yes" order by product_details.name asc LIMIT '.$current.', '.$limit.'');
//		if ($Q->num_rows() > 0){
//			foreach ($Q->result_array() as $row){
//				$data[] = $row;
//			}
//		}
//		$Q->free_result();
//		return $data;
$ld=$this->session->userdata('lang_id');
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_subsubcategories');
		$this->db->join('products','product_subsubcategories.p_id=products.id', 'left');
		$this->db->join('product_details','product_details.p_id=products.id', 'left');
		$this->db->where('product_subsubcategories.subsubcat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->where('product_details.lang_id',$ld);
		if ($sorted=='name') {
		$this->db->orderby("product_details.name",'asc');
		} elseif ($sorted=='priceasc') { 
		$this->db->orderby("products.price",'asc');
		} elseif ($sorted=='pricedesc') {
		$this->db->orderby("products.price",'desc');
		}
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}

function getProductsbySubCat($cat_id, $limit, $current) {
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_subcategories');
		$this->db->join('products','product_subcategories.p_id=products.id', 'left');
		$this->db->where('product_subcategories.subcat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->orderby("products.id","asc");
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function getProductsbySubCatPrice($cat_id, $limit, $current, $sorted) {
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('product_subcategories');
		$this->db->join('products','product_subcategories.p_id=products.id', 'left');
		$this->db->where('product_subcategories.subcat_id',$cat_id);
		$this->db->where('products.published','Yes');
		$this->db->orderby("products.price",$sorted);
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function getProductsbyPrice($limit, $current) {
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('products');
		$this->db->join('product_categories','product_categories.p_id=products.id', 'left');
		if ($this->session->userdata('by_price')=='0-10') { 
		$this->db->where("products.price < 10");
		} elseif ($this->session->userdata('by_price')=='10-15') { 
		$this->db->where("products.price > 10 AND price < 15");
		} elseif ($this->session->userdata('by_price')=='15-20') { 
		$this->db->where("products.price > 15 AND price < 20");
		} elseif ($this->session->userdata('by_price')=='20') { 
		$this->db->where("products.price > 20");
		} elseif ($this->session->userdata('by_price')=='offers') { 
		$this->db->where("products.offers = 'Yes'");
		}		
		$this->db->where('products.published','Yes');
		$this->db->where('product_categories.cat_id !=','21');
		$this->db->where('product_categories.cat_id !=','7');
		$this->db->orderby("products.price","asc");
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function getProductsbyPriceNew($cat_id, $limit, $current) {
	if ($current==''){
	$current=0;
	}
		$data=array();
		$this->db->select("*");
		$this->db->from('products');
		$this->db->join('product_categories','product_categories.p_id=products.id', 'left');
		if ($this->session->userdata('by_price')=='0-10') { 
		$this->db->where("products.price < 10");
		} elseif ($this->session->userdata('by_price')=='10-15') { 
		$this->db->where("products.price > 10 AND price < 15");
		} elseif ($this->session->userdata('by_price')=='15-20') { 
		$this->db->where("products.price > 15 AND price < 20");
		} elseif ($this->session->userdata('by_price')=='20') { 
		$this->db->where("products.price > 20");
		} elseif ($this->session->userdata('by_price')=='offers') { 
		$this->db->where("products.offers = 'Yes'");
		}		
		$this->db->where('products.published','Yes');
		$this->db->where('product_categories.cat_id =',$cat_id);
		$this->db->orderby("products.price","asc");
		//$this->db->orderby("order","asc");
		$this->db->limit($limit, $current);
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function getProductsCatbyPrice() {
		$data=array();
		$this->db->select("*");
		$this->db->from('products');
		$this->db->join('product_categories','product_categories.p_id=products.id', 'left');
		$this->db->join('category','product_categories.cat_id=category.cat_id', 'left');
		if ($this->session->userdata('by_price')=='0-10') { 
		$this->db->where("products.price < 10");
		} elseif ($this->session->userdata('by_price')=='10-15') { 
		$this->db->where("products.price > 10 AND price < 15");
		} elseif ($this->session->userdata('by_price')=='15-20') { 
		$this->db->where("products.price > 15 AND price < 20");
		} elseif ($this->session->userdata('by_price')=='20') { 
		$this->db->where("products.price > 20");
		} elseif ($this->session->userdata('by_price')=='offers') { 
		$this->db->where("products.offers = 'Yes'");
		}		
		$this->db->where('products.published','Yes');
		$this->db->where('product_categories.cat_id !=','21');
		$this->db->where('product_categories.cat_id !=','7');
		$this->db->where('product_categories.cat_id !=','25');
		$this->db->groupby("product_categories.cat_id");
		$this->db->orderby("category.order", "asc");
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	
function getProductDetails($id){
   		$ld=$this->session->userdata('lang_id');
			 $data = array();
		$this->db->select('*');
		$this->db->from('products');
		$this->db->join('product_details','product_details.p_id=products.id', 'left');
		$this->db->where('product_details.lang_id',$ld);
		$this->db->where("products.id",$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getOfferOrder($id){
   		$ld=$this->session->userdata('lang_id');
			 $data = array();
		$this->db->select('*');
		$this->db->from('offer_orders');
		$this->db->where("id",$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getCountryZone($id){
   		$ld=$this->session->userdata('lang_id');
			 $data = array();
		$this->db->select('*');
		$this->db->from('countries');
		$this->db->where("id",$id);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getFed_Ex_Cost($weight, $zone){
		$data = array();
		$this->db->select('*');
		$this->db->from('fed_ex_charges');
		$this->db->where('Kg',$weight);
		$this->db->where("zone",$zone);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getTNT_Cost_economy($weight, $zone){
		$data = array();
		$this->db->select('*');
		$this->db->from('tnt_charges');
		$this->db->where('Kg >= '.$weight.'');
		$this->db->where("zone",$zone);
		$this->db->orderby("Kg",'asc');
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getTNT_Cost_express($weight, $zone){
		$data = array();
		$this->db->select('*');
		$this->db->from('tnt_charges_express');
		$this->db->where('Kg >= '.$weight.'');
		$this->db->where("zone",$zone);
		$this->db->orderby("Kg",'asc');
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getTNT_extra_Cost($zone){
		$data = array();
		$this->db->select('*');
		$this->db->from('tnt_extra_pkg');
		$this->db->where("zone",$zone);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function getCourierCost($weight, $table){
		$data = array();
		$this->db->select('*');
		$this->db->from($table);
		$this->db->where('kg',$weight);
		$this->db->limit(1);
		$Q=$this->db->get();
		if ($Q->num_rows() > 0){
      $data = $Q->row_array();
    }

    $Q->free_result();    
    return $data;    
 }
function fCeil($val,$pressision=2){
  $p = pow(10,$pressision);
  $val = $val*$p;
  $val = ceil($val);
  return $val /$p;
}
function insertUser(){
	  $a = md5($this->input->post('password'));
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data_info = array( 
		'fname' => db_clean($this->input->post('fname')),
		'lname' => db_clean($this->input->post('lname')),
		'email' => $this->input->post('email'),
		'password' => dohash($this->input->post('password'), 'md5'),
		'country_id' => $this->input->post('country'),
		'address' => $this->input->post('address'),
		'city' => $this->input->post('city'),
		'floor' => $this->input->post('floor'),
		'name_bell' => $this->input->post('name_bell'),
		'postal' => $this->input->post('postal'),
		'tel' => $this->input->post('tel'),
		'mob' => $this->input->post('mob'),
		'date_added' => $now,
	);
 	$this->db->insert('clients', $data_info);
}
function insertOfferUser($total_cost){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data_info = array( 
		'fname' => db_clean($this->input->post('fname')),
		'lname' => db_clean($this->input->post('lname')),
		'email' => $this->input->post('email'),
		'address' => $this->input->post('address'),
		'city' => $this->input->post('city'),
		'floor' => $this->input->post('floor'),
		'name_bell' => $this->input->post('name_bell'),
		'postal' => $this->input->post('postal'),
		'tel' => $this->input->post('tel'),
		'mob' => $this->input->post('mob'),
		'qty' => $this->input->post('qty'),
		'total' => $total_cost,
		'date_added' => $now,
	);
 	$this->db->insert('offer_orders', $data_info);
								$data2 = array();
								$Q5 = $this->db->query('SELECT LAST_INSERT_ID()');
								if ($Q5->num_rows() > 0){
								   $data2 = $Q5->row_array();
								}
								$Q5->free_result();
								$order_id = $data2;
		$temp_u_id=$order_id['LAST_INSERT_ID()'];
		return $temp_u_id;
}
	function verifyClient($u,$pw){
		$this->db->select('id,email');
		$this->db->where('email',$u);
		$this->db->where('password', md5($pw));
		$this->db->where('status', 'active');
		$this->db->limit(1);
		$Q = $this->db->get('clients');
		$this->session->set_userdata('lastquery', $this->db->last_query());
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return array();
		}		
	}
	function GetUser($email){
		$this->db->select('*');
		$this->db->where('email',$email);
		$this->db->where('status', 'active');
		$this->db->limit(1);
		$Q = $this->db->get('clients');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function CheckUser($email){
		$this->db->select('*');
		$this->db->where('email',$email);
		$this->db->where('status', 'active');
		$this->db->limit(1);
		$Q = $this->db->get('clients');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return true;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function GetUserbyID($id){
		$this->db->select('*');
		$this->db->where('id',$id);
		$this->db->where('status', 'active');
		$this->db->limit(1);
		$Q = $this->db->get('clients');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function GetCountybyPostal_new($postal){
		$this->db->select('*');
		$this->db->where('postal',$postal);
		$this->db->limit(1);
		$Q = $this->db->get('postal_county_new');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function GetOrderbyID($id, $order_id){
		$this->db->select('*');
		$this->db->where('id',$order_id);
		$this->db->where('user_id',$id);
		$this->db->limit(1);
		$Q = $this->db->get('orders');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function getInvoiceDetails($order_id){
		$this->db->select('*');
		$this->db->where('order_id',$order_id);
		$this->db->limit(1);
		$Q = $this->db->get('order_invoice');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function getLatestInvoiceDetails($user_id){
		$this->db->select('*');
		$this->db->where('user_id',$user_id);
		$this->db->orderby("order_id",'desc');
		$this->db->limit(1);
		$Q = $this->db->get('order_invoice');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function GetOrderbyRef($ref){
		$this->db->select('*');
		$this->db->where('ref',$ref);
		$this->db->limit(1);
		$Q = $this->db->get('orders');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
	function GetOfferCode($code){
		$this->db->select('*');
		$this->db->where('code',$code);
		$this->db->limit(1);
		$Q = $this->db->get('offer_codes');
		if ($Q->num_rows() > 0){
			$row = $Q->row_array();
			return $row;
		}else{
			//$this->session->set_flashdata('error', 'Sorry, try again!');	
			return false;
		}		
	}
function insertOrder($id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data_info = array( 
		'user_id' => $id,
		'completed' => 'No',
		'date_added' => $now,
	);
 	$this->db->insert('orders', $data_info);
								$data2 = array();
								$Q5 = $this->db->query('SELECT LAST_INSERT_ID()');
								if ($Q5->num_rows() > 0){
								   $data2 = $Q5->row_array();
								}
								$Q5->free_result();
								$order_id = $data2;
								$o_id=$order_id['LAST_INSERT_ID()'];
	foreach($this->cart->contents() as $items):
	if ($this->cart->has_options($items['rowid']) == TRUE):
	$options = "";
	foreach ($this->cart->product_options($items['rowid']) as $option_name => $option_value):
	$options .= $option_name." ".$option_value."<br>";
	endforeach;
	endif;
		$data_prd = array( 
		'order_id' => $o_id,
		'product_id' =>  $items['id'],
		'price' => $items['price'],
		'quantity' =>  $items['qty'],
		'options' => $options
	);
 	$this->db->insert('order_details', $data_prd);
	endforeach;
	return $o_id;
}
function updateProductPrice($id, $a_pass){
	  $data = array( 
		'password' => 1300
	);
	  $this->db->where('id',$id);
	  $this->db->update('products',$data);	
}
function updateUserPassword($id, $a_pass){
	  $data = array( 
		'password' => md5($a_pass)
	);
	  $this->db->where('id',$id);
	  $this->db->update('clients',$data);	
}
function updatePayMethod($order_id, $id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'pay_method' => $this->input->post('select')
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateReceiptType($order_id, $id){
	  $data = array( 
		'receipt_type' => $this->input->post('select2')
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateOfferUser($total_cost, $u_id){
	  $data_info = array( 
		'fname' => db_clean($this->input->post('fname')),
		'lname' => db_clean($this->input->post('lname')),
		'email' => $this->input->post('email'),
		'address' => $this->input->post('address'),
		'city' => $this->input->post('city'),
		'floor' => $this->input->post('floor'),
		'name_bell' => $this->input->post('name_bell'),
		'postal' => $this->input->post('postal'),
		'tel' => $this->input->post('tel'),
		'mob' => $this->input->post('mob'),
		'qty' => $this->input->post('qty'),
		'total' => $total_cost
	);
	  $this->db->where('id',$u_id);
 	$this->db->update('offer_orders', $data_info);
}
function updateOrderCodeUID($order_id, $id, $code, $code_var){
	  $data = array( 
		'offer_code' => $code
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
	  
	  $data2 = array( 
		'user_id' => $id
	);
	  $this->db->where('code',$code_var);
	  $this->db->update('offer_codes',$data2);	
}
function updateOrderCode($order_id, $id, $code){
	  $data = array( 
		'offer_code' => $code
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateShipMethod($order_id, $id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'ship_method' => $this->input->post('select')
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateShipDestination($order_id, $id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'ship_dest' => $this->input->post('destination')
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateShipDestination2($order_id, $id, $county){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'ship_dest' => $county
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateShipDestination3($order_id, $id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'ship_dest' => 'abroad'
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateShippingCost($order_id, $id, $ship_cost){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'ship_cost' => $ship_cost
	);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function setRefOrder($a, $id){
	  $data = array( 
		'ref' => $a
	);
	  $this->db->where('id',$id);
	  $this->db->update('orders',$data);	
}
function finaliseOrder($order_id){
	  $data = array( 
		'completed' => 'Yes'
	);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function finaliseOrderPayed($order_id){
	  $data = array( 
		'completed' => 'Yes',
		'payed' =>'Yes'
	);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function updateShipAddress($order_id, $id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'same_address' => 'No',
		'country_id' => $this->input->post('country'),
		'address' => $this->input->post('address'),
		'city' => $this->input->post('city'),		
		'postal' => $this->input->post('postal'),
		'floor' => $this->input->post('floor'),
		'name_bell' => $this->input->post('name_bell')
		);
	  $this->db->where('user_id',$id);
	  $this->db->where('id',$order_id);
	  $this->db->update('orders',$data);	
}
function insertInvoiceDetails($order_id, $id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'order_id' => $order_id,
		'user_id' => $id,
		'cmp_name' => $this->input->post('cmp_name'),
		'cmp_occupation' => $this->input->post('cmp_occupation'),
		'cmp_afm' => $this->input->post('cmp_afm'),		
		'cmp_address' => $this->input->post('cmp_address'),
		'cmp_city' => $this->input->post('cmp_city'),
		'cmp_doy' => $this->input->post('cmp_doy')
		);
 	$this->db->insert('order_invoice', $data);
}
function updateUser(){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
      $id=$this->session->userdata('userid');
	  $data = array( 
		'fname' => db_clean($this->input->post('fname')),
		'lname' => db_clean($this->input->post('lname')),
		'email' => $this->input->post('email'),
		'address' => $this->input->post('address'),
		'country_id' => $this->input->post('country'),
		'city' => $this->input->post('city'),
		'postal' => $this->input->post('postal'),
		'floor' => $this->input->post('floor'),
		'name_bell' => $this->input->post('name_bell'),
		'tel' => $this->input->post('tel'),
		'mob' => $this->input->post('mob')
	);
	  $this->db->where('id',$id);
	  $this->db->update('clients',$data);	
}
function deleteUser($id){
	  $data = array( 
		'status' => 'not-active'
	);
	  $this->db->where('id',$id);
	  $this->db->update('clients',$data);	
	}

	
	function getRandPicts() {
		$data = array();
		$Q = $this->db->query("SELECT * FROM photos order by RAND() limit 3");
		if ($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
	function getRandCatPicts($cat) {
		$data = array();
		$Q = $this->db->query("SELECT * FROM photos WHERE cat_id=".$cat." order by RAND() limit 1");
		if ($Q->num_rows() > 0){
			$data = $Q->row_array();
		}
		$Q->free_result();
		return $data;
	}
	
	
	function getAllNews($lg) {
		$data=array();
		$temp=array();

		$this->db->select("id,title,shortdesc,longdesc,date");
		$this->db->where('status','active');
		$this->db->where('lang_id =',$lg);
		$this->db->orderby("date","DESC");
		
		$Q=$this->db->get('news');
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
			$data[$row['id']] =array(
					"id"=>$row['id'],
					"title"=>$row['title'],
					"shortdesc"=>$row['shortdesc'],
					"longdesc"=>$row['longdesc'],
					"date"=>$row['date']
				);
				}
		}
		
		$Q->free_result();
		return $data;
	}

function getAllPhotos(){
		$data = array();
		$this->db->select('*');
		$this->db->from('photos');
		$this->db->orderby("order","asc");
		//$this->db->join('prd_description','products.id=prd_description.products_id', 'left');
		//$this->db->where('prd_description.lang_id','2');
		$Q = $this->db->get();
		if ($Q->num_rows() > 0){
			foreach ($Q->result_array() as $row){
				$data[] = array(
					'id'=>$row['id'],
					'thumbnail'=>$row['thumbnail'],
					'image'=>$row['image']
					);
			}
		}
		$Q->free_result();
		return $data;
	}
function insertCCTransaction($ref, $order_id, $amount, $user_id){
	  date_default_timezone_set("Europe/Athens");
	  $now = date("Y-m-d G:i:s");
	  $data = array( 
		'cc_ref' => $ref,
		'cc_amount' => $amount,
		'cc_user_id' => $user_id,
		'cc_order_id' => $order_id,
		'cc_date_added' => $now,
	);
 	$this->db->insert('cc_transactions', $data);
}
 	 function getAllnewPrPrice() {
		$data=array();
		$this->db->select('*');
		$this->db->from('product_price_update');
		$Q=$this->db->get();
		if($Q->num_rows() > 0){
			foreach($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
	}
function updatePrPrice($p_id, $price){
	  $data = array( 
		'price' => $price
	);
	  $this->db->where('id',$p_id);
	  $this->db->update('products',$data);	
}


}
?>