<?php
class Grid
{
	public $em;
	
	public function standart($set, $gridName)
	{
		$this->em = $set->em;
		$sel = $set->sql;
		$sql = $this->em->addSelect("*");
		$sql = $this->em->getFrom("($sel)", 'a');
		$sql = $this->em->getMultiSort($set->sort);
		$eks = $this->em->queryEks($sql);
		$tmp = $this->em->getFetchObject($eks);
		$ctr = $set->ctrl;
		$_pk = $set->dBasePk[0];

		$data = '<table id="'.$gridName.'">';
		$data.= '<thead>';
		$data.= '<th width="30" align="center">No</th>';
		
		foreach($set->grid as $r){
			$data.= '<th width="'.$r['width'].'" align="'.$r['align'].'">'.$r['nama'].'</th>';
		}

		$data.= '</thead>';
		$data.= '<tbody>';

		$no = 1;
		foreach($tmp as $r){ 
			$data.= '<tr>';
			$data.= '<td>'.$no.'</td>';
			foreach($set->grid as $r2){
				$id = $r2['id'];
				$data.= '<td>';
				if($r2['type'] == 'select'){
					$data.= "<select id='".$id."_".$no."'>";
					foreach($r2['select'] as $r3){
						$data.= "<option value='$r3'>".$r3;
					}
					$data.= "</select>";
				}else
				if($r2['type'] == 'button'){
					$data.= "<input no='".$no."' kode='".$r->$_pk."' type='button' class='_".$id." _iconAct ".$r2['class']." ".$ctr."_".$r2['class']."' alt='".$id."'>";
				}else{
					$data.= $r->$id;
				}
				$data.= '</td>';
			}
			$data.= '</tr>';
			$no++;
		} 
		$data.= '</tbody>';
		$data.= '</table>';
		
		$width 	= 800;
		$height = 250;
		if(!empty($set->width)){
			$width = $set->width;
		}
		if(!empty($set->height)){
			$height = $set->height;
		}
		
		$data.= "<script type='text/javascript'>";
		$data.= "$(document).ready(function(){";
		$data.= "$('#".$gridName."').flexigrid({
						width: ".$width.",
						height: ".$height.",
						title: '".$set->modul."',
					});
					$('.meioMask').setMask();
				})
				";
		$data.= "</script>";
		return $data;
	}
	
	public function getGridStartXml()
	{
		echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
		echo "<rows>";
	}
	
	public function getGridPage($page)
	{
		echo "<page>$page</page>";
	}
	
	public function getGridTotal($total)
	{
		$total = $total - 1;
		if($total <= 0){
			$total = 1;
		}
		echo "<total>$total</total>";
	}
	
	public function getGridStartRows($kode)
	{
		$kode = str_ireplace("&", " &amp; ", $kode);
		echo "<row id='$kode'>";
	}
	
	public function getGridCell($val)
	{
		echo "<cell><![CDATA[$val]]></cell>";
	}
	
	public function getGridEndRows()
	{
		echo "</row>";
	}
	
	public function getGridEndXml()
	{
		echo "</rows>";
	}
}