<?php
error_reporting(0);
 extract($_REQUEST);
#pagination
$sestype=$_SESSION['ses_atype'];

function valoperator($feat)
{
$sel_operator=mysql_query("select * from tbl_splituser where split_id= '".$_SESSION['ses_op_id']."'") or die(mysql_error());
$res_operator=mysql_fetch_array($sel_operator);
$menu_accessmenu=explode(",",$res_operator['feat_id']);
if(!in_array($feat, $menu_accessmenu))
{
  echo'<script language=javascript>alert("Access Denied");</script>';
  echo("<script language='javascript'>window.location.href='home.php'</script>");
exit;
}
}


function valoperatoraccess($feataccess)
{
$sel_operatoraccess=mysql_query("select * from tbl_splituser where split_id= '".$_SESSION['ses_op_id']."'") or die(mysql_error());
$res_operatoraccess=mysql_fetch_array($sel_operatoraccess);
$menu_accessmenuaccess=explode(",",$res_operatoraccess['feat_permission']);
if(!in_array($feataccess, $menu_accessmenuaccess))
{
echo'<script language=javascript>alert("Access Denied");</script>';
echo("<script language='javascript'>window.location.href='home.php'</script>");
  exit;
}
}
function isselected($val1, $val2)
{
	
	if($val1==$val2)
	{
		return 'selected="selected"';
	}
	else
	{
		return '';
	}
}	
function DateTimeout($str)
{
list($tdat, $ttim) = split(' ', trim($str));
list($year,$month,$day) = split('[/.-]', trim($tdat));
return (strlen($str)>5)?($day."-".$month."-".$year." ".$ttim):"";
}
function select_query($table_name, $field_name, $condition, $limitations='',$print='')
{
	if($field_name == "")
	{
		$field_name = "*";
	}
	
	$selectQ = "select $field_name from $table_name";
	
	if($condition!="")
	{
		$selectQ .= " where $condition";
	}
	if($limitations!="")
	{
		$selectQ .= " limit $limitations";
	}
	if($print){echo $selectQ."<br>";}
   
	$resQuery    =    mysql_query($selectQ);
	
	if(($_SERVER['HTTP_HOST']=="localhost:81" || $_SERVER['HTTP_HOST']=="localhost") && mysql_error())
	{
		echo $sql,"<br>",mysql_error();
	}
   
	$NumRows    =    mysql_num_rows($resQuery);
   
	if($limitations == 1)
	{
		$fetchArray    =    mysql_fetch_assoc($resQuery);
	   
		$fetchArray['nr']    =    $NumRows;
	   
		return $fetchArray;
	}
	else
	{
		while($res = mysql_fetch_assoc($resQuery))
		{
			$resultArr[] = $res;
		}
	
		return array(Query => $resQuery,nr => $NumRows,result=>$resultArr);
	}
	
}

function pagination($tbl_name,$fields,$where='',$orderby='',$page_url,$pagesize,$page_no)
{
	if($where)$where=" WHERE ".$where;
	if($orderby)$orderby=" ORDER BY ".$orderby;
	$sql1="SELECT ".$fields." FROM ".$tbl_name.$where.$orderby;
	$rowsperpage = $pagesize;
	$website = $page_url; 
	$pagination = new CSSPagination($sql1, $rowsperpage, $website); 
	$pagination->setPage($page_no); 
	$qsql1 = @mysql_query($sql1) or die("failed");
	$RecordCount = mysql_num_rows($qsql1);
	$sql2 = "select ".$fields." FROM ".$tbl_name.$where.$orderby." LIMIT ".$pagination->getLimit() . ", ". $rowsperpage; 
	$result_query = @mysql_query($sql2);
	$page = $pagination->showPage();
	$resultArr = array();
	if($RecordCount>1)
	{
		while($res = mysql_fetch_assoc($result_query))
		{
			$resultArr[] = $res;
		}
	}
	else if($RecordCount==1)
	{
		$res = mysql_fetch_assoc($result_query);
		$resultArr[] = $res;
	}
	$array_res	=	array('Query'=>$resultArr,'page'=>$page,'nr'=>$RecordCount);
	return $array_res;
}
function paginationQry($sql1,$page_url,$pagesize,$page_no)
{
	$rowsperpage = $pagesize;
	$website = $page_url; 
	$pagination = new CSSPagination($sql1, $rowsperpage, $website); 
	$pagination->setPage($page_no); 
	$qsql1 = @mysql_query($sql1) or die("failed");
	$RecordCount = mysql_num_rows($qsql1);
	$sql2 = $sql1." LIMIT ".$pagination->getLimit() . ", ". $rowsperpage; 
	$result_query = @mysql_query($sql2);
	$page = $pagination->showPage();
	$resultArr = array();
	if($RecordCount>1)
	{
		while($res = mysql_fetch_assoc($result_query))
		{
			$resultArr[] = $res;
		}
	}
	else if($RecordCount==1)
	{
		$res = mysql_fetch_assoc($result_query);
		$resultArr[] = $res;
	}
	$array_res	=	array('Query'=>$resultArr,'page'=>$page,'nr'=>$RecordCount);
	return $array_res;
}
#pagination for array
function pagination_array($array, $page = 1, $link_prefix = false, $link_suffix = false, $limit_page = 20, $limit_number = 10)
{
	if (empty($page) or !$limit_page) $page = 1;
	$num_rows = count($array);
	if (!$num_rows or $limit_page >= $num_rows) return false;
	$num_pages = ceil($num_rows / $limit_page);
	$page_offset = ($page - 1) * $limit_page;
	//Calculating the first number to show.
	if ($limit_number)
	{
		$limit_number_start = $page - ceil($limit_number / 2);
		$limit_number_end = ceil($page + $limit_number / 2) - 1;
		if ($limit_number_start < 1) $limit_number_start = 1;
		//In case if the current page is at the beginning.
		$dif = ($limit_number_end - $limit_number_start);
		if ($dif < $limit_number) $limit_number_end = $limit_number_end + ($limit_number - ($dif + 1));
		if ($limit_number_end > $num_pages) $limit_number_end = $num_pages;
		//In case if the current page is at the ending.
		$dif = ($limit_number_end - $limit_number_start);
		if ($limit_number_start < 1) $limit_number_start = 1;
	}
	else
	{
		$limit_number_start = 1;
		$limit_number_end = $num_pages;
	}
	//Generating page links.
	for ($i = $limit_number_start; $i <= $limit_number_end; $i++)
	{
		$page_cur = "<a href='$link_prefix$i$link_suffix'>$i</a>";
		if ($page == $i) $page_cur = "<b>$i</b>";
		else $page_cur = "<a href='$link_prefix$i$link_suffix'>$i</a>";
		$panel .= " <span>$page_cur</span>";
	}
	$panel = trim($panel);
	//Navigation arrows.
	if ($limit_number_start > 1) $panel = "<b><a href='$link_prefix".(1)."$link_suffix'>&lt;&lt;</a>  <a href='$link_prefix".($page - 1)."$link_suffix'>&lt;</a></b> $panel";
	if ($limit_number_end < $num_pages) $panel = "$panel <b><a href='$link_prefix".($page + 1)."$link_suffix'>&gt;</a> <a href='$link_prefix$num_pages$link_suffix'>&gt;&gt;</a></b>";
	$output['panel'] = $panel; //Panel HTML source.
	$output['offset'] = $page_offset; //Current page number.
	$output['limit'] = $limit_page; //Number of resuts per page.
	$output['array'] = array_slice($array, $page_offset, $limit_page, true); //Array of current page results.
	
	return $output;
}

function get_cat($table_name, $field_name, $condition, $name='',$print='')
{
	if($field_name == "")
	{
		$field_name = "*";
	}
	
	$selectQ = "select $field_name from $table_name";
	
	if($condition!="")
	{
		$selectQ .= " where $condition limit 1";
	}

	if($print){echo $selectQ."<br>";}
   
	$resQuery    =    mysql_query($selectQ);
	
	if(($_SERVER['HTTP_HOST']=="localhost:81" || $_SERVER['HTTP_HOST']=="localhost") && mysql_error())
	{
		echo $sql,"<br>",mysql_error();
	}
   
	$NumRows    =    mysql_num_rows($resQuery);
	if($NumRows==0)
	{
	echo 'NOT IN';
	}
	else
	{
	$res = mysql_fetch_assoc($resQuery);
	echo ($res[$name])?$res[$name]:'Nill';
	}
}
function show_list($table_name,$field_name,$where='',$order,$key='',$name='',$select='')
{
	if($where)$where=" WHERE ".$where;
	$squery = mysql_query("SELECT ".$field_name." FROM ".$table_name." ".$where." ORDER BY ".$order." ASC");
	if(mysql_num_rows($squery)!=0)
	{
		while($res=mysql_fetch_object($squery))
		{
			$sel=($select)?isselected($res->$key,$select):'';
			echo '<option value="'.$res->$key.'" '.$sel.'>'.$res->$name.'</option>';	
		}
	}
}
function pos_add($table_name,$field_name,$catpos,$id,$where='')
{
	if($where)$where = "WHERE ".$where;
	$selectSql = mysql_query("SELECT * FROM ".$table_name." ".$where." ORDER BY ".$field_name." ASC");
	$rows = mysql_num_rows($selectSql);
	
	if($catpos!="Last")
	{
		$limit=$catpos-1;//0
		
		$squery = mysql_query("SELECT * FROM ".$table_name." ".$where." ORDER BY ".$field_name." ASC LIMIT $limit,$rows");
		while($resval=mysql_fetch_object($squery))
		{
			$inc = $resval->$field_name+1;
			$update=mysql_query("UPDATE ".$table_name." set ".$field_name."='$inc' WHERE ".$id."='".$resval->$id."'");
		}
		
		$fabric_pos = $catpos;
	}
	else
	{
		$fabric_pos=$rows+1;
	}
	return $fabric_pos;
}
#position update
function pos_update($table_name,$field_name,$catpos,$resultpos,$id,$where='')
{
	if($where)$where = " WHERE ".$where;
	if($resultpos<$catpos)
	{
		if($resultpos==1)
		{
			$limit=0;
			$max=($catpos-$resultpos)+1;
		}
		else
		{
			$limit=$resultpos;//4
			$max=($catpos-$resultpos);//5
		}
		
		$squery = mysql_query("SELECT * FROM ".$table_name.$where." ORDER BY ".$field_name." LIMIT $limit,$max");
		while($res=mysql_fetch_assoc($squery))
		{
			$inc=$res[$field_name]-1;
			$update = mysql_query("UPDATE ".$table_name." SET ".$field_name."='$inc' WHERE ".$id."='".$res[$id]."'");
		}
		
	}
	else
	{
		$limit=$catpos-1;//7
		$max=($resultpos-$catpos);//1
		$squery = mysql_query("SELECT * FROM ".$table_name.$where." ORDER BY ".$field_name." LIMIT $limit,$max");
		//echo "SELECT * FROM ".$table_name.$where." ORDER BY ".$field_name." LIMIT $limit,$max"."<br>";
		while($res=mysql_fetch_assoc($squery))
		{
			$inc=$res[$field_name]+1;
			//echo "UPDATE ".$table_name." SET ".$field_name."='$inc' WHERE ".$id."='".$res[$id]."'"."<br>";
			$update =  mysql_query("UPDATE ".$table_name." SET ".$field_name."='$inc' WHERE ".$id."='".$res[$id]."'");
		}
	}
	return $catpos;
}
function insert($table,$exist='',$new_field='',$up_folder='')
{
        if($up_folder=="")
        {
            $folder = "upload/";
        }
        else
        {
            $folder = "$up_folder/";
        }
   
   
        $selfield = mysql_query("show columns from $table");
        while($rowfield=@mysql_fetch_assoc($selfield))
        {
            $field[]=$rowfield[Field];
        }
        //print_r($field);
       
        if($exist)
        {
       
            $expexist=explode(",",$exist);
            foreach($expexist as $existvar)
            {
                $existcond.= "$existvar = '".variable($_POST[$existvar])."' and  ";
            }
           
        }
        else
        {
       
            foreach($_POST as $key => $value)
            {
                if(@in_array($key,$field))
                {
                    $existcond.= "$key = '".variable($_POST[$key])."' and ";
                }
           
            }
       
        }
       
	   //	echo $existcond."<br>";
		
        $existcond = substr($existcond,0,strlen($existcond)-5);
		
        //echo $existcond."<br>";
   
        $selexist=mysql_query("select * from $table where $existcond");
		
		//echo "select * from $table where $existcond";
		
        $checkexist = mysql_affected_rows();
       
        if($checkexist <= 0)
        {
       
            foreach($_POST as $key => $value)
            {
                if(@in_array($key,$field))
                {
                    $insertfield.= "$key, ";
                    $insertvalue.= "'".variable($_POST[$key])."', ";
                }
           
            }
           
            if($new_field)
            {
                foreach($new_field as $key => $value)
                {
                    if(@in_array($key,$field))
                    {
                        $insertfield.= "$key, ";
                        $insertvalue.= "'".variable($new_field[$key])."', ";
                    }
               
                }
            }
           
            if($_FILES)
            {
           
                foreach($_FILES as $key => $value)
                {
                    if(@in_array($key,$field) && $_FILES[$key][name])
                    {
                        $insertfield.= "$key, ";
                        $file_name = rand(1,99999).$_FILES[$key][name];
                        $strFileType = strtolower($_FILES[$key]["type"]);
                        $insertvalue.= "'".$file_name."', ";
                        move_uploaded_file($_FILES[$key][tmp_name],$folder.$file_name);
                       
                    }
               
                }
           
            }
            if(@in_array("date",$field))
            {
                $insert_field = $insertfield."date";
                $insert_value = $insertvalue."curdate()";
            }
            else
            {
                $insert_field = substr($insertfield,0,strlen($insertfield)-2);
                $insert_value = substr($insertvalue,0,strlen($insertvalue)-2);
            }
           
            //echo $insert_field,"<br>";
            //echo $insert_value;
            $ins_query = "insert into $table($insert_field) values($insert_value)";
            //echo $ins_query;
            $result = mysql_query($ins_query);
           
            if(mysql_error())
            {
                echo mysql_error(),"<br>",$ins_query;
            }
           
            $ar = mysql_affected_rows();
            $insert_id = mysql_insert_id();
           
            return array(ar=>$ar,id=>$insert_id);
       
        }
        else
        {
            //echo "Already Exist";
            return array(exist=>'1');
        }
   
}
function update($table,$upcond,$new_field='',$up_folder='',$checkbox='')
{
   
        if($up_folder=="")
        {
            $folder = "upload/";
        }
        else
        {
            $folder = "$up_folder/";
        }
   
   
        $selfield = mysql_query("show columns from $table");
		
        while($rowfield=@mysql_fetch_assoc($selfield))
        {
            $field[]=$rowfield[Field];
        }
        //print_r($field);
      
            foreach($_POST as $key => $value)
            {
                if(@in_array($key,$field))
                {
                    $updatefield.= "$key = "."'".variable($_POST[$key])."', ";
                }
           
            }
           
            if($new_field)
            {
                foreach($new_field as $key => $value)
                {
                    if(@in_array($key,$field))
                    {
                        $updatefield.= "$key = "."'".variable($new_field[$key])."', ";
                    }
               
                }
            }
           
            if($_FILES)
            {
           
                foreach($_FILES as $key => $value)
                {
                    if(@in_array($key,$field) && $_FILES[$key][name])
                    {
                        $file_name = rand(1,99999).$_FILES[$key][name];
                        $updatefield.= "$key = "."'".$file_name."', ";
                        move_uploaded_file($_FILES[$key][tmp_name],$folder.$file_name);
                    }
               
                }
           
            }
           
            if($checkbox)
            {
           
                $expcheckbox=explode(",",$checkbox);
                foreach($expcheckbox as $checkboxvar)
                {
                    if(@in_array($checkboxvar,$field) && $_POST[$checkboxvar]=="")
                    {
                        $updatefield.= "$checkboxvar = "."'".$_POST[$checkboxvar]."', ";
                    }
                }
               
            }
           
            $insert_field = substr($updatefield,0,strlen($updatefield)-2);
           
            //echo $insert_field,"<br>";
            $up_query = "update $table set $insert_field where $upcond";
            $result = mysql_query($up_query);
            //echo $up_query."<br>";
           
            if(mysql_error())
            {
                echo mysql_error(),"<br>",$ins_query;
            }
           
            $ar = mysql_affected_rows();
           
            return array(ar=>$ar);
   
}

?>