<?php
//DB wrapper functions
function executeQuery($query){
    return mysqli_query($_SESSION['db_link'],$query);   
}
function totalRecords($result){
   return mysqli_num_rows($result);
}
function realEscape($result){
   return mysqli_real_escape_string($_SESSION['db_link'],$result);
}
function getRecordId(){
   return mysqli_insert_id($_SESSION['db_link']);
}
function getAssoc($result){
   return mysqli_fetch_assoc($result);
}
function getRow($result){
   return mysqli_fetch_row($result);
}
function showError($query){
   die(mysqli_error()."<br /> Query: ".$query);
}
function dbClose(){
   mysqli_close();
}
function getRecord($key,$table,$value,$att){
  $sel="select * from $table where $key='$value' order by id desc limit 0,1";
  $res=executeQuery($sel);
  $rec= getAssoc($res);
  return $rec[$att];
}
function getRecordCond($table,$cond,$att){
  $sel="select * from $table where ".$cond;
  $res=executeQuery($sel);
  $rec= getAssoc($res);
  return $rec[$att];
}
function getRecordCondCount($table,$cond,$att){
  $sel="select * from $table where ".$cond;
  $res=executeQuery($sel);
  $cnt=totalRecords($res);
  return $cnt;
}
function getRecordCondTotal($table,$cond,$att){
  $sel="select * from $table where ".$cond;
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}
function getArabic($key){
  $sel="select * from library where english='$key'";
  $res=executeQuery($sel);
  if(mysqli_num_rows($res)!=0 && LANG=='ar'){
  $rec= getAssoc($res);
  return $rec['arabic'];
  }else{
  return $key;
  }
}
function getArabicAdmin($key){
  $sel="select * from library where english='$key'";
  $res=executeQuery($sel);
  if(mysqli_num_rows($res)!=0){
  $rec= getAssoc($res);
  return $rec['arabic'];
  }else{
  return $key;
  }
}
function getRecordTotal($key,$table,$value,$att){
  $sel="select * from $table where $key='$value'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}
function getRecordTotalCon($key,$table,$value,$att,$key1,$value1){
  $sel="select * from $table where $key='$value' and $key1='$value1'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}
function getRecordTotalConSp($key,$table,$value,$att,$key1,$value1){
  $sel="select * from $table where $key='$value' and $key1 < '$value1'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}
function getRecordTotalConSp2($key,$table,$value,$att,$key1,$value1,$key2,$value2){
  $sel="select * from $table where $key='$value' and $key1 = '$value1'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}
function getRecordTotalConSp3($key,$table,$value,$att,$key1,$value1,$value2){
  $sel="select * from $table where $key='$value' and $key1 between '$value1' and '$value2'";
  $res=executeQuery($sel);
  $tot=0;
  while($rec= getAssoc($res)){
  $tot+= $rec[$att];
  }
  return $tot;
}
function getRecordCount($key,$table,$value){
  $sel="select * from $table where $key='$value'";
  $res=executeQuery($sel);
    return mysqli_num_rows($res);
}
function getArabicApp($key,$lang){
  $sel="select * from library where english='$key'";
  $res=executeQuery($sel);
  if(totalRecords($res)!=0){
	 
  $rec= getAssoc($res);
   if($lang=='ar'){
  return $rec['arabic'];
   }else if($lang=='fr'){
	  return $rec['arabic'];  
	   }else{
	  return $rec['arabic'];  
	   }
  }else{
  return $key;
  }
}
function getArabicApp2($key,$lang){
   $sel="select * from library where english='$key'";
  $res=executeQuery($sel);
  if(totalRecords($res)!=0){
	
  $rec= getAssoc($res);
   if($lang=='ar'){
	   // echo  $rec['arabic'];
  return $rec['arabic'];
   }else if($lang=='fr'){
	  return $rec['arabic'];  
	   }else{
	  return $rec['arabic'];  
	   }
  }else{
  return $key;
  }
}
function valuecount($table,$val,$cond){
if($val!='' || $val!='All'){
$date=date('Y-m-d', strtotime('today - '.$val.' days'));
$dt=executeQuery("select id from ".$table." where now > '$date%' and ".$cond);
$dat=getAssoc($dt);
if($dat['id']!=''){
$sel=executeQuery("select id from ".$table." where id >= '$dat[id]' and ".$cond);
  return totalRecords($sel);
}else{
  return 0;
}
}else{
  $sel=executeQuery("select id from ".$table);
  return totalRecords($sel);
}
}
function valuecountrange($table,$val1,$val2,$cond){
if($val1!='' && $val2!=''){
$val1=$val1.' 00:00:00'; $val2=$val2.' 23:59:59';
$dt=executeQuery("select id from ".$table." where now >= '$val1' and now <= '$val2' and ".$cond);
  return totalRecords($dt);
}else{
  $sel=executeQuery("select id from ".$table." where ".$cond);
  return totalRecords($sel);
}
}

function pricecount($table,$val,$src,$cond){
if($val!='' && $val!='All'){
$date=date('Y-m-d', strtotime('today - '.$val.' days'));
$dt=executeQuery("select id from ".$table." where ".$cond." and now > '$date%'");
$dat=getAssoc($dt);
$id = $dat['id'];
if($id!=''){
	$tot=0;
$sel=executeQuery("select $src from ".$table." where ".$cond." and id >= '$id'");
while($res=getAssoc($sel)){
  $tot+=floatval($res[$src]);
}
return $tot;
}else{
  return 0;
}
}else{
  $tot=0;
  $sel=executeQuery("select $src from ".$table." where ".$cond);
  while($res=getAssoc($sel)){
  $tot+=floatval($res[$src]);
}
return $tot;
}
}
function totalPriceCust($table,$val1,$val2,$src,$cond){
$date1=$val1.' 00:00:00';
$date2=$val2.' 23:59:59';
if($cond!=''){$cond = $cond.' and';}
$tot=0;
$sel=executeQuery("select $src from ".$table." where ".$cond." now >= '$date1' and  now <= '$date2'");
while($res=getAssoc($sel)){
  $tot+=floatval($res[$src]);
}
return $tot;
}
function time_elapsed_string($datetime,$lang="ar", $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
			
			/*if($lang=="ar"){
			
			 $vv=$v . ($diff->$k > 1 ? 's' : '');
			 $vv=getArabicApp(trim($vv),$lang);
			 
			 $ago=getArabicApp('ago',$lang);
			 
			
            $v =$ago.' '. $diff->$k . ' ' . $vv;
			}else{*/
			
			 $vv=$v . ($diff->$k > 1 ? 's' : '') . ' ago';
			 //$vv=getArabicApptime(trim($vv),$lang);
			 
			
            $v = getArabicApptime($diff->$k . ' ' . $vv,"ar");	
			/*	}*/
        } else {
            unset($string[$k]);
        }
    }
    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string)  : 'الآن';
}
function getArabicApptime($key,$lang){
	 $kk=explode(" ",$key);
	if($kk[1]=="second" || $kk[1]=="seconds"){
	return getArabicApp('Now',$lang);
	exit;
	}
if($kk[1]=="minute" || $kk[1]=="minutes"){
		 if($kk[0]==1){
			 $key="قبل دقيقة";
			 }else if($kk[0]==2){
				 $key=" قبل دقيقتين"; 
				 }else if($kk[0]>=3 && $kk[0]<11){
			$ago=getArabicApp('ago',$lang);
			$str="دقائق";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 
					 }else{
					$ago=getArabicApp('ago',$lang);
			$str="دقيقة";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 	 
					 }
		 }else if($kk[1]=="day" || $kk[1]=="days"){
		 if($kk[0]==1){
			 $key="منذ يوم";
			 }else if($kk[0]==2){
				 $key="منذ يومين"; 
				 }else if($kk[0]>=3 && $kk[0]<11){
			$ago=getArabicApp('ago',$lang);
			$str="أيام";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 
					 }else{
					$ago=getArabicApp('ago',$lang);
			$str="دقيقة";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 	 
					 }
		 }else if($kk[1]=="hour" || $kk[1]=="hours"){
			 
			 
			 
			  
		 if($kk[0]==1){
			 $key=" قبل ساعة";
			 }else if($kk[0]==2){
				 $key=" قبل ساعتين"; 
				 }else if($kk[0]>=3 && $kk[0]<=10){
			$ago=getArabicApp('ago',"ar");
			$str="ساعات";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 
					 }else{
					$ago=getArabicApp('ago',$lang);
			$str="ساعة";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 	 
					 }
			 
			 }else if($kk[1]=="month" || $kk[1]=="months"){
			 
			 
			 
			  
		 if($kk[0]==1){
			 $key=" قبل شهر";
			 }else if($kk[0]==2){
				 $key=" قبل شهرين"; 
				 }else if($kk[0]>=3 && $kk[0]<=10){
			$ago=getArabicApp('ago',"ar");
			$str="أشهر";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 
					 }else{
					$ago=getArabicApp('ago',$lang);
			$str="أشهر";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 	 
					 }
			 }else if($kk[1]=="week" || $kk[1]=="weeks"){
			 
			 
			 
			  
		 if($kk[0]==1){
			 $key=" قبل أسبوع";
			 }else if($kk[0]==2){
				 $key="قبل أسبوعين"; 
				 }else if($kk[0]>=3 && $kk[0]<=10){
			$ago=getArabicApp('ago',"ar");
			$str="أسابيع";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 
					 }else{
					$ago=getArabicApp('ago',$lang);
			$str="أسابيع";
			 
            $key =$ago.' '. $kk[0] . ' '.$str; 
					 	 
					 }
			 }
		 return $key;
 
}
function getOptions($key,$table,$value,$action,$parent,$i,$par){?>
	<option <?php if($action=='edit'){if($par==$value){echo 'selected';}}?> value="<?php echo $value;?>"><span style="font-size:<?php echo 26;?>px;"><?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo getRecord('id','ven_categories',$value,'name') ?></span></option>
    
    <?php 
		$pid=$value;
									$s="select * from ven_categories where parent='$pid'";
									$su=executeQuery($s);
									$cnt=totalRecords($su);
									if($cnt!=0){
										$i++;
									}else{?>
										
										<?php }
									while($sub=getAssoc($su)){
										
									
										getOptions('id','ven_categories',$sub['id'],$action,$sub['parent'],$i,$par);
										}
											
	 }
	 function getOptionsprod($key,$table,$value,$action,$parent,$i,$par){
		  $cnt=getRecordCount('parent','ven_categories',$value);
		  if($cnt!=0){
		 ?>
     <optgroup label="<?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo getRecord('id','ven_categories',$value,'name') ?>"></optgroup>
    <?php }else{?>
	<option <?php if($action=='edit'){if($par==$value){echo 'selected';}}?> value="<?php echo $value;?>"><span style="font-size:<?php echo 26;?>px;"><?php 
	$j=$i;
	while($j>0){echo " ->";$j--;}echo getRecord('id','ven_categories',$value,'name') ?></span></option>
    
    <?php }
		$pid=$value;
		$s="select * from ven_categories where parent='$pid'";
		$su=executeQuery($s);
		$cnt=totalRecords($su);
		if($cnt!=0){
			$i++;
		}else{?>
			
			<?php }
		while($sub=getAssoc($su)){
			getOptionsprod('id','ven_categories',$sub['id'],$action,$sub['parent'],$i,$par);
			}
											
	 }
?>
