<?   
   class vtable
   {      
	  private $colAction, $maxRow, $unitField, $nl2brFields, $htmlDecode, $thumbsize;
	  protected $dbHandle;
	  protected $sqlField, $sql, $headerClass, $attrCol, $attrTH;
	  protected $col, $cmdCol, $startNumber, $useNumber, $pictPath, $pictDoc, $isPicture=array("Foto", "Photo", "Picture", "Image", "Cover", "Gambar");
	  public $fieldID;
	  public $fieldKeys; //array to fields....
	  private $controller, $MOD;
	  public  $RID;
	  private $scientific;
	  
	  function __construct($col, &$db="", $pictPath="", $pictDoc="", $size=48)
	  {
	     $this->dbHandle= $db;//new dbConnection();		 
		 $this->col= $col;
		 $this->pictPath= $pictPath;		
		 $this->pictDoc= $pictDoc;		
		 $this->thumbsize= $size;
		 $this->RID= uniqid();		 		 
		 $controller= current(explode("/",$_SERVER['QUERY_STRING']));
		 
		 if(isset($_SESSION['SES_MOD']))
		 $this->MOD= deflatter($_SESSION['SES_MOD']);		
	  }
	  
	  public function getRID(){ return $this->RID; }
	  protected function setColumn()
	  {	     
		 $headerClass= $this->headerClass;
		 echo "<thead><tr>";
		 if($this->useNumber) 
		 {
			 $attr= (isset($this->attrTH['No']))? $this->attrTH['No']:"";
			 echo "<th width=2% $attr>No</th>";
		 }
		 foreach($this->col as $key=>$field)
		 {
			if($this->attrTH && array_key_exists($key, $this->attrTH))$attr= $this->attrTH[$key];  
		    echo "<th $attr>$key</th>";		 
		 }
		 if($this->colAction){			
			$attr=$this->attrTH['Act'];   			  
		    echo "<th colspan={$this->colAction} width=4% $attr></th>";
		 }
		 echo "</tr></thead>";
		 $this->getDataField();
	  }	  	  
	  
	  public function setTHAttribute($attrTH) //array field
	  {
	      $this->attrTH= $attrTH;  
	  }
	  
	  public function setColAttribute($attrCol) //array field
	  {
	      $this->attrCol= $attrCol;  
	  }
	  
	  public function setNL2BR($fields) //array field
	  {
	      $this->nl2brFields= $fields;  
	  }
	  
	  public function htmlspecialcharsDecode($fields) //array field
	  {
		  $this->htmlDecode= $fields;  
	  }
	  
	  public function setController($c){$this->controller=$c;}
	  
	  public function setColAction($act='', $useNo=true)
	  {  
	  	  $action=array();
		  if(isset($_SESSION['SES_LEVEL']) && ($_SESSION['SES_LEVEL']!=1))
		  {
		  	$controller= (!empty($this->controller))? $this->controller:current(explode("/",$_SERVER['QUERY_STRING']));
			$pattern='/[#]'.$controller.'\(\d\)/';
			preg_match($pattern,$this->MOD,$match);					
			if(count($match)) $acc= substr($match[0], strlen($match[0])-2,1);										

			if(count($act))
		  	if($acc==1) unset($act); //read only
			else // 2= entry form, 3= edit, 4= delete, optional combine value of item
			{				
			//print_r($act);
				foreach($act as $key=>$val)
				{
					if($key==$acc) array_push($action, $val);
					else if(($acc==5)||($acc==6)||($acc==7)||($acc==9))
					{
						$arr=array();
						if($acc==5) $arr=array(3);
						else if($acc==6) $arr=array(4);
						else if($acc==7 || $acc==9) $arr=array(3,4);						
						if(in_array($key, $arr)) array_push($action, $val);															
					}
					if($key>9) array_push($action, $val);																							
				}
			}						
		  }
		  else if($act)		  
		  	foreach($act as $key=>$val) array_push($action, $val);										
		  $this->cmdCol= $action; 
		  $this->colAction=($action!='')? count($action):0; 
		  $this->useNumber= $useNo;   
	  }
	  
	  public function startColNumber($no)
	  {
	     $this->startNumber= $no;
		 $this->useNumber=true;
	  }
	  public function useNo(){$this->useNumber=true;}
	  
	  protected function getColAction()
	  {   return $this->cmdCol;	  }
	  
	  protected function setDataField($field)
	  {  $this->sqlField= $field; }
	  
	  public function setHeaderClass($class)
	  { $this->headerClass= $class; }
	  
	  public function setSQL($sql)
	  { $this->sql= $sql; }
	  
	  public function getSQL() { return $this->sql; }	  
	  
	  protected function getDataField() 
	  { 
	     $temp= array(); $i=0;
		 foreach($this->col as $key=>$field)
		 {
		   $temp[$key]= $field; $i++;
		 }
		 return $this->sqlField= $temp; 	  
	  }
	  
	  private function thumbnail($image, $const)
      {	
	     if(@getimagesize($image))
	  	 {
		    list($width, $height) = getimagesize($image);
	
		    if($width>$height)
		    {
	  		   $w=$const;
      		   $h=ceil($height * ($const/$width));
		    }   
		    else
		    {
	  		   $h=$const;
      		   $w=ceil($width * ($const/$height));
		    }
		    $img= end(explode("/", $image));
		 return "<img src=$this->pictPath/$img width=$w height=$h border=0 class=foto />";
		 }

		 else return "";
  	  }
	  
	  protected function isPicture($key, $value)
	  {
	     if((in_array($key, $this->isPicture))&&(!empty($value)))
		 {
		    return $this->thumbnail($this->pictDoc."/$value", $this->thumbsize);
		 }
		 else return $value;
	  }	  
	  
	  public function formatMoney($cols)
	  {
	     $this->moneyCols= $cols;
	  }
	  
	  private function moneyFormat($value)
      {    
	     if(empty($value))
	        return;
  
		 $length=strlen($value);	
		 while($length>3)
		 {	  
	   		$start= ($length)-3;
	   		$temp.=substr($value, $start, 3).".";	 
	   		$length-= 3;	   
		 }
	
		 $temp=array_reverse(explode(".", $temp)); $temp=implode(".", $temp);
		 $val=substr($value, 0, $length).$temp;
		return $val;
  	  }
	  
	  public function itemUnit($arrData)
	  {
	     $this->unitField= $arrData;
	  }

       public function scientific($data){
           $this->scientific= $data;
       }

       public static function scientificFormat($val){
           if($val) {
               $exp = floor(log($val, 10));
               return sprintf('%.2fE%+03d', $val / pow(10, $exp), $exp);
           }
           else return 0;
       }

	  public function viewData($attr, $hideact=array(), &$returnkeys="")
	  {
	     //echo $this->RID;
		 echo "<input type=hidden id=$this->RID /><table $attr>";
		    $this->setColumn();
			$no= $this->startNumber;
			
		    $qry=$this->dbHandle->dbQuery($this->sql);
			$row=$this->dbHandle->dbQNumRow($qry); $r0=1;
						
			while($val=$this->dbHandle->dbFetchArray($qry))
			{			   
			   /*if($row1 && $row2)
			   {
				   $cl=($cl==$row1)? $row2:$row1;
				   $bgclr="bgcolor=$cl";
			   }*/
			   $id= $val[$this->fieldID].$this->RID; 
			   echo "<tr id=$id>";
			   if($this->useNumber)echo "<td>$no</td>";
			   foreach($this->sqlField as $key=>$field)
			   {
			      $value= $val[$field];
				  //if($returnkeys[0]==$field) $returnkeys[1]= $value;
				  //$type= mysql_field_type($qry, $key);
				  $tdAttr="";
				  if(!empty($this->attrCol))
				     $tdAttr=(array_key_exists($field, $this->attrCol)) ? $this->attrCol[$field]: '';
				  if(!empty($this->nl2brFields))
				     $value=(in_array($field, $this->nl2brFields)) ? nl2br($value):$value ; 
				  if(!empty($this->htmlDecode))	 
				     $value=(in_array($field, $this->htmlDecode)) ? htmlspecialchars_decode($value):$value ; 
				  $value= $this->isPicture($key, $value);
				  
				  if(!empty($this->moneyCols))
				     $value=in_array($field, $this->moneyCols) ? $this->moneyFormat($value): $value;
                   if(!empty($this->scientific))
                       $value=in_array($field, $this->scientific) ? vtable::scientificFormat($value): $value;
				   
				  echo "<td $tdAttr>$value</td>"; 
				  
				  $unit="";
			   }  
			   if(($hideact)&&($val[key($hideact)]==$hideact[key($hideact)])&& count($hideact))
			   {
				   echo "<td align=center valign=middle></td>";
			   }
			   else if($this->colAction)
			   {			      
				  $act= $this->getColAction();
				  for($i=0; $i<$this->colAction; $i++)
				  {				    
					 $cmd= $act[$i]; 
					 if($this->fieldKeys&&count($this->fieldKeys))
					 {					    
					    $asterix="*";
						
						foreach($this->fieldKeys as $fKey)
					    {						   
						   //$cmd= str_replace("[$asterix]", str_replace(" ","%20",$val[$fKey]), $cmd);
						   $cmd= str_replace("[$asterix]", str_replace(" ","&nbsp;",$val[$fKey]), $cmd);
						   $asterix.="*";
					    }
					 }					 
					    
					 $cmd= str_replace("*", $val[$this->fieldID], $cmd);
					 $cbName= $val[$this->fieldID];
					 //echo "<td><input type=checkbox name='$cbName'/></td>";
					 //if(($i+1)==$this->colAction) $colAttr= $this->attrTH[$i];
					 //echo "<td align=center valign=top $colAttr>$cmd</td>";
					 //if(($i+1)==$this->colAction) $bd= "border-right:none";
					 echo "<td align=center >$cmd</td>";
					 //$bd=$colAttr="";
				  } 	
			   }			    	  
			   $no++; $r0++;
			   echo "</tr>";				     
			}
			$this->dbHandle->dbFreeResult($qry);
		 echo "</table>";
	  }
	  
	  public function paging($sql, $max, $act="", $data="", $pg="", $pgid="")
	  {
	     global $CONFIG;
		 
		 $mcol=5;
		 $this->maxRow= $max; 
		 $pgID= (empty($pgid))? "PageID":$pgid;
		 $qry=$this->dbHandle->dbQuery($sql); 
		 $totrow= $this->dbHandle->dbQNumRow($qry); 
		 if($totrow)
		 {		
		 	if($this->maxRow==1) $row= $totrow;
			else $row=ceil($totrow/$this->maxRow); 
			
			if($row>1){
			 
				$i= (($pg/$mcol)&&($pg>1))? ($mcol*floor($pg/$mcol))+1:1;		 
				$x=1;
				echo "<div class='clearfix'></div><nav aria-label='Page Navigation' class='float-right'><ul class=pagination>";
			 
				if($i>$mcol) echo "<li class='page-item'><a class='page-link' href=# onclick='$(\"#$data\").load(\"$act\"+($i-$mcol)); return false;'>Prev</a></li>";	 
		
				 while(($i<=$row)&&($x<=5)) 	 
				 {
					 if($i==$pg) $active="active";
					 else $active="";
					 echo "<li class='page-item $active'><a class='page-link' href=# onclick='$(\"#$data\").load(\"$act$i\");'>$i</a></li>";
					 $i++; $x++;
				 }
				 if($i<$row) echo "<li class='page-item'><a class='page-link' href=# onclick='$(\"#$data\").load(\"$act$i\"); return false;'>Next</a></li>";		 
				 
				 echo "</ul></nav>";					 		 
			}		 
		 }
	  }	  	  
   }
?>