<?php

	class Grid
	{
		var $fields			= Array();
		var $labels			= Array();
		var $columnsSize	= Array();
		var $masks			= Array();
		var $paramsSend		= Array();
		var $columnsFooter  = Array();
		var $data			= null;
		var $key			= "";
		var $url_defaul		= "";
		var $url_new		= "";
		var $url_alter		= "";
		var $url_del		= "";
		var $first			= 0 ;
		var $nav			= 20 ;
		var $browser  		= false;
		var $navigate		= true;
		var $selectRow		= false;
		var $selectRowTag   = "";
		var $showFooters	= false;
		var $fieldLink		= "";
			
		function set($field, $label, $size=0, $mask="")
		{
			$this->fields[$field] = $field;
			$this->labels[$field] = $label;
			$this->columnsSize[$field] = $size;
			$this->masks[$field] = $mask;
		}

		function setParamsSend($param, $value)
		{
			$this->paramsSend[$param] = $value;
		}

		public function formatDate($value)
		{
			$tmp = substr($value, 8, 2) ;
			$tmp .= "/" .substr($value, 5, 2) ;
			$tmp .= "/" .substr($value, 0, 4);
			return $tmp;
		}

		public function Make()
		{
			global $PATH_IMAGES;

			$nav	= $this->nav;
			$first	= $this->first;  
			
			if (($first == "") or ($first == 0))
				$first = 1;
			
			$next = $first + $nav; 
			
			$tot = 0 ;
			if (isset($this->data))
				$tot  = count($this->data) / $nav;

			if (empty($this->url_new))
				$this->url_new = $this->url_defaul;

			if (empty($this->url_alter))
				$this->url_alter = $this->url_defaul;

			if (empty($this->url_del))
				$this->url_del = $this->url_defaul;

			$conteudo = "

				<script>

					function remove(id) 
					{
						if (confirm('Deseja excluir?')) 
							window.location = '". $this->url_del ."?action=del&key=' + id +'';
					}

				</script>";

			$conteudo .= "

				<table class=\"table table-striped table-bordered\" id=\"listing\">
					<thead>
					<tr>";

			if ($this->selectRow)
				$conteudo .= "<th width=20></th>";		
		
			foreach($this->fields as $field)
				$conteudo .= "<th width=". $this->columnsSize[$field] .">". $this->labels[$field] ."</th>";

			if (!$this->browser)
				$conteudo .= "<th width=40></th>";

			$conteudo .= "</tr>
				</thead>";

			if (isset($this->data))
			{
				$i=0;
				foreach($this->data as $row)
				{
					if (($i >= (($first * $nav) - $nav)) && ($i < (($first * $nav))))
					{
						$conteudo .= "<tr>";

						if ($this->selectRow)
						{	
							$selectRowTag = "";
							if (!empty($this->selectRowTag))
								$selectRowTag = "selectRowTag=\"". $row[$this->selectRowTag] ."\"";

							$conteudo .="
							<td align=\"center\">
									<input type=\"checkbox\" name=\"row_$i\" $selectRowTag value=\"". $row[$this->key] ."\" />
							</td>";
						}

						foreach($this->fields as $field)
						{
							$style = "";

							if ($this->masks[$field] == "dateFormat")
							{
								if (!empty($row[$field]))
									$value = $this->formatDate($row[$field]);
								else
									$value = "";
							}
							else if ($this->masks[$field] == "numberFormat")
							{
								$style = "style='text-align: right;'";
								if (!empty($row[$field]))
									$value = number_format($row[$field], 2, '.', ''); 
								else
									$value = number_format(0, 2, '.', '');
							}
							else if ($this->masks[$field] == "moneyFormat")
							{
								$style = "style='text-align: right;'";
								if (!empty($row[$field]))
									$value = number_format($row[$field], 2, '.', ','); 
								else
									$value = number_format(0, 2, '.', ',');
							}
							else
								$value = $row[$field];

							if ($this->fieldLink == "")
								$conteudo .= "<td $style>$value</td>";
							else
								$conteudo .= "<td><a href=\"uploads/". $row[$this->fieldLink] ."\">$value</a></td>";
						}

						if (!$this->browser)
						{
						$conteudo .="
							<td align=\"center\">
								<a href=\"#\" onclick=\"location.href='". $this->url_alter ."?action=alter&key=". $row[$this->key] ."';\"><img src=$PATH_IMAGES/b_edit.png border=0 title=\"Alterar\"></a>	
								<a href=\"#\" onclick=\"remove(". $row[$this->key] .");\"><img src=$PATH_IMAGES/b_drop.png border=0 title=\"Delete\"></a>	
							</td>";
						}

						$conteudo .= "</tr>";
					}
					$i++;
				}
			}

			$conteudo .= "
				</table><br>";

			if (!$this->browser)
				$conteudo .="<input type=button onclick=\"location.href='". $this->url_new ."?action=new';\" value=\"Novo\">";

			if ($this->navigate)
			{
				$ant = 1 ;
				$tmp = 1 ;

				if ($first > 1) 
					$ant = ($first - 1);
				
				$prox = $first;
				if ($first < $tot)
					$prox = ($first + 1);

				$linkparams = "";
				foreach($this->paramsSend as $key => $value)
					$linkparams .= $key ."=". $value ."&";

				$conteudoNav = "
				<br clear=\"all\">
				<div id=\"guardaPaginacao\">
					<ul>
						<li class=\"proxAnt\"><a href=\"". $this->url_defaul ."?". $linkparams ."first=". $ant ."\">&lt; Anterior</a></li>
						<li class=\"separador\">|</li>";
				
				for($i=0;$i<$tot;$i++)
				{
					$tmp = $i + 1;	
					$marcado = "";
					if ($i == ($first-1))
						$marcado = "class=\"pagMarcada\"";
					$conteudoNav .= "<li><a $marcado href=\"". $this->url_defaul ."?". $linkparams ."first=". $tmp ."\">". $tmp ."</a></li>";
				}

				$conteudoNav .= "
						<li class=\"separador\">|</li>
						<li class=\"proxAnt\"><a href=\"". $this->url_defaul ."?". $linkparams ."first=". $tmp ."\">Pr&oacute;xima &gt;</a></li>
					</ul>
				</div>";

				$conteudo .= $conteudoNav;
			}

			return $conteudo ;
		}
	}

?>