<?php 
// delete folders and sub folders of a directory
function delTree($dir) {
    $files = glob( $dir . '*', GLOB_MARK );
    foreach( $files as $file ){
        if( substr( $file, -1 ) == '/' )
            delTree( $file );
        else
            unlink( $file );
    }
    //rmdir( $dir );
}


/// creating zip of a dir
function createZipFromDir($dir, $zip_file) {
    $zip = new ZipArchive;
    if (true !== $zip->open($zip_file, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE)) {
        return false;
    }
    zipDir($dir, $zip);
    return $zip;
}

function zipDir($dir, $zip, $relative_path = DIRECTORY_SEPARATOR) {
    $dir = rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
    if ($handle = opendir($dir)) {
       while (false !== ($file = readdir($handle))) {
            if (file === '.' || $file === '..') {
                continue;
            }
            if (is_file($dir . $file)) {
                $zip->addFile($dir . $file, $file);
            } elseif (is_dir($dir . $file)) {
                zipDir($dir . $file, $zip, $relative_path . $file);
            }
        }
    }
    closedir($handle);
}

// zip files and download
function zipFilesAndDownload($file_names,$archive_file_name,$file_path)
{
  //create the object
  $zip = new ZipArchive();
  //create the file and throw the error if unsuccessful
  if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    exit("cannot open <$archive_file_name>\n");
  }

  //add each files of $file_name array to archive
  foreach($file_names as $files)
  {
//  	echo $file_path.$files.".csv<br>";
	$file1="/".$files.".csv";
    $zip->addFile($file_path.$file1,$file1);
//	echo $zip->addFile($file_path.$file1,$file1);
  }
//  print_r($zip);
  $zip->close();

  //then send the headers to foce download the zip file
  header("Content-type: application/zip");
  header("Content-Disposition: attachment; filename=$archive_file_name");
  header("Pragma: no-cache");
  header("Expires: 0");
  readfile("$archive_file_name");
  exit;
}

// creating zip of a folder
function create_zip($files = array(),$ndir,$destination = '',$overwrite = true) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
	  $file1=$ndir."/".$file.".csv";
      if(file_exists($file1)) {
        $valid_files[] = $file;
      }
    }
  }
//  echo "des=".$destination;
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
	  $file1=$ndir."/".$file.".csv";
      $zip->addFile($file1,$file.".csv");
    }
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//    print_r($zip);
    //close the zip -- done!
    $zip->close();
    
    //check to make sure the file exists
    return file_exists($destination);
  }
  else
  {
    return false;
  }
}

// get max of a filed value in a table *** 1 ****
function max_id($table,$field,$condition)
{
	$qr='';
	if($condition!='')
	$qr=" where ".$condition;
	$selmax=executework("select max(".$field.") from ".$table.$qr);
	$rows=@mysql_fetch_array($selmax);
	if($rows[0]!="")
	$maxid=$rows[0]+1;
	else
	$maxid=1;
	return $maxid;
}

// get max of a filed value in a table *** 101 ****
function max_id1($table,$field)
{
	$selmax=executework("select max(".$field.") from ".$table);
	$rows=@mysql_fetch_array($selmax);
	if($rows[0]!="")
	$maxid=$rows[0]+1;
	else
	$maxid=101;
	return $maxid;
}

// get max of a filed value in a table *** 1001 ****
function max_id2($table,$field)
{
	$admn=array();
	
	$selmax=executework("select max(".$field.") from ".$table);
	$rows=@mysql_fetch_array($selmax);
	if($rows[0]!="")
	$maxid=$rows[0]+1;
	else
	$maxid=1001;
	return $maxid;
}

// round the number to n no of decimals
function numround($st,$n)
{
	if($st!="")
	{
		$n1=pow(10 ,$n);
		$num=round($st*$n1)/($n1);
	}
	return $num;
}

// get no of days between 2 days
function GetDays($sStartDate, $sEndDate){  
  // Firstly, format the provided dates.  
  // This function works best with YYYY-MM-DD  
  // but other date formats will work thanks  
  // to strtotime().  
  $sStartDate = gmdate("Y-m-d", strtotime($sStartDate));  
  $sEndDate = gmdate("Y-m-d", strtotime($sEndDate));  
  
  // Start the variable off with the start date  
  $aDays[] = datepattrn($sStartDate);  
  
  // Set a 'temp' variable, sCurrentDate, with  
  // the start date - before beginning the loop  
  $sCurrentDate = $sStartDate;
  
  // While the current date is less than the end date
  while($sCurrentDate < $sEndDate){
    // Add a day to the current date  
    $sCurrentDate = gmdate("Y-m-d", strtotime("+1 day", strtotime($sCurrentDate)));  
  
    // Add this new day to the aDays array  
    $aDays[] = datepattrn($sCurrentDate);  
  }
  
  // Once the loop has finished, return the  
  // array of days.  
  return $aDays;  
}

function generate_droplist_bydata($qr,$label,$val)
{
	while($row=@mysqli_fetch_array($qr))
	{
		$drp=$drp."<option value=";
		$drp.=$row[$val]." />";
		$drp.=$row[$label];
		$drp.="</option>";
	}
	return $drp;
}

function generate_droplist_byarray($array,$label,$val)
{
	$drp='';
	for($i=0;$i<count($array);$i++)
	{
		$drp=$drp."<option value=";
		$drp.=$array[$i][$val]." />";
		$drp.=$array[$i][$label];
		$drp.="</option>";
	}
	return $drp;
}

// --- random no generation
function str_rand($length, $seeds)
{
    // Possible seeds
    $seedings['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
    $seedings['numeric'] = '0123456789';
    $seedings['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
    $seedings['hexidec'] = '0123456789abcdef';
    
    // Choose seed
    if (isset($seedings[$seeds]))
    {
        $seeds = $seedings[$seeds];
    }
    
    // Seed generator
    list($usec, $sec) = explode(' ', microtime());
    $seed = (float) $sec + ((float) $usec * 100000);
    mt_srand($seed);
    
    // Generate
    $str = '';
    $seeds_count = strlen($seeds);
    
    for ($i = 0; $length > $i; $i++)
    {
        $str .= $seeds[mt_rand(0, $seeds_count - 1)];
    }
    
    return $str;
}


/// to get sub categories list from a multi level category table
function get_gensubcat($table,$fld,$pre,$fid,$act,$is,$cid)
{
	$arr[0]=$cid;
	$array[0][0]=$cid;
	if($act==1 && $is!="")
	$qr=" and ".$is."=1";
	$j=1;
	$m=1;
	for($i=0;$i<count($arr);$i++)
	{
		$n=0;
		$sel=executework("select * from ".$table." where ".$pre."=".$arr[$i].$qr);
		$cnt=@mysql_num_rows($sel);
		if($cnt>0)
		{
			while($row=@mysql_fetch_array($sel))
			{
				$arr[$j]=$row[$fid];
				$array[$m][$n]=$row[$fid];
				$j++;
				$n++;
			}
			$m++;
		}
	}
	$arry[0]=$arr;
	$arry[1]=$array;
	return $arry;
}

//get_gensubcat('hrms_privileges','id','parent_id','id','status','1','1');

function update_status($table,$field,$rec_id,$st_field,$status,$condition)
{
	$qr='';
	if($condition!='')
	$qr=" and ".$condition;
	$sel=executework("select * from ".$table." where ".$field."=".$rec_id.$qr);
	$cnt=@mysqli_num_rows($sel);
	if($cnt>0)
	{
		$upd=executework("update ".$table." set ".$st_field."='".$status."' where ".$field."=".$rec_id.$qr);
		return "success";
	}
	else
	return "not exist";
}

function delete_record($table,$field,$rec_id,$condition)
{
	$qr='';
	if($condition!='')
	$qr=" and ".$condition;
	$sel=executework("select * from ".$table." where ".$field."=".$rec_id.$qr);
	$cnt=@mysqli_num_rows($sel);
	if($cnt>0)
	{
		$upd=executework("delete from ".$table." where ".$field."=".$rec_id.$qr);
		return "deleted";
	}
	else
	return "not exist";
}

function get_reccount($table,$cntfld,$condition)
{
	$qr='';
	if($condition!='')
	$qr=" and ".$condition;
	$sel=executework("select count(*) as cnt from ".$table." where ".$cntfld."<>''".$qr);
	$cnt=@mysqli_num_rows($sel);
	$count=0;
	if($cnt>0)
	{
		$row=@mysqli_fetch_array($sel);
		$count=$row['cnt'];
	}
	return $count;
}

function get_state_value($id)
{
	$sel=executework("select * from haindavasakthi_states where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['state_name']))
	$val=$row['state_name'];
	return $val;
}

function get_dt_value($id)
{
	$sel=executework("select * from haindavasakthi_district where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['dt_name']))
	$val=$row['dt_name'];
	return $val;
}

function get_mandal_value($id)
{
	$sel=executework("select * from haindavasakthi_mandal where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['mandal_name']))
	$val=$row['mandal_name'];
	return $val;
}

function get_village_value($id)
{
	$sel=executework("select * from haindavasakthi_village where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['village_name']))
	$val=$row['village_name'];
	return $val;
}

function get_catg_value($id)
{
	$sel=executework("select * from haindavasakthi_catg where id='".$id."'");
	$cnt=@mysqli_num_rows($sel);
	$val='';
	$row=@mysqli_fetch_array($sel);
	if(!empty($row['catg_name']))
	$val=$row['catg_name'];
	return $val;
}

?>