<?php

class Agro_List_Table {
	private $_recordset;
	private $_columns;
	private $_actions;
	private $_view;
	
	public function __construct(array $columns, $Recordset, $paginator, $request) {
		$this->_view = Zend_Layout::getMvcInstance()->getView();
		$this->_recordset = $Recordset;
		$this->_columns = $columns;
		$this->_paginator = $paginator;
		$this->_request = $request;
		
		$Acoes = new Sistema_Model_Acoes();
		$this->_actions = $Acoes->getActions($this->_view->moduleName, $this->_view->controllerName);
	}
	
	public function toString() {
		$table  = $this->_getTableHeader();
		$table .= $this->_getTableBody();
		$table .= $this->_getTableFooter();
		
		return $table;
	}
	
	private function _getTableHeader() {
		ob_start();
		
		?>
		<table class='grid' cellspacing='1' cellpadding='0' border='0' id='tablePagination'>
			<thead>
				<tr>
					<? if (is_array($this->_columns)): ?>
						<? foreach ($this->_columns as $column => $header): ?>
							<th id="<?=$column?>" nowrap="nowrap">
								<a href="<?=$this->_mountOrder($column)?>">
									<? if (is_array($header)): ?>
										<?=$header['label']?>
									<? else: ?>
										<?=$header?>
									<? endif; ?>
									
									<img src="/resources/imagens/<?=$this->_img?>" border="0" />
								</a>
							</th>
						<? endforeach; ?>
					<? endif; ?>
					
					<? if (count($this->_actions)): ?>
						<th nowrap="nowrap" width="1%">Ações</th>
					<? endif; ?>
				</tr>
			</thead>
		
		<?
		$html = ob_get_contents();
		ob_end_clean();
		
		return $html;
	}
	
	private function _getTableBody() {
		$View = Zend_Layout::getMvcInstance()->getView();
		ob_start();
		
		?>
		<tbody>
			<? if ($this->_recordset->count()): ?>
				<? foreach ($this->_recordset as $Row): ?>
					<tr>
						<? if (is_array($this->_columns)): ?>
							<? foreach ($this->_columns as $field => $options): ?>
								<? if ($options['type'] == 'data'): ?>
									<?$Row[$field] = Agro_Util::formatDate($Row[$field],Zend_Date::DATES); ?>
								<? endif; ?>
								<? if ($options['type'] == 'currency'): ?>
									<?$Row[$field] = Agro_Util::formatDbCurrency($Row[$field]); ?>
								<? endif; ?>
								<td style="cursor:default" nowrap="nowrap" align="<?=$options['align']?>" width="<?=$options['width']?>"><?=$View->escape((is_array($Row) ? Agro_Util::cortar($Row[$field], 150) : Agro_Util::cortar($Row->{$field}, 150)))?></td>
							<? endforeach; ?>
						<? endif; ?>
						
						<? if (count($this->_actions)): ?>
							<td nowrap="nowrap" align="center">
								<? if(!is_array($Row)) $Row = $Row->toArray();?>
								<?=$this->_getActions($Row); ?>
							</td>
						<? endif; ?>
					</tr>
				<? endforeach; ?>
			<? endif; ?>
		</tbody>
		<?
		
		$html = ob_get_contents();
		ob_end_clean();
		
		return $html;
	}
	
	private function _getTableFooter() {
		ob_start();
		?>
		</table>
		
		<? if ($this->_paginator): ?>
			<table width="100%">
				<tr>
					<td><?=$this->_view->paginationControl($this->_recordset, 'Sliding');?></td>
				</tr>
			</table>
		
		<?
		endif;
		
		$html = ob_get_contents();
		ob_end_clean();
		
		return $html;
	}
	
	private function _getActions($item) {
		ob_start();
		
		$User = Zend_Auth::getInstance()->getIdentity();
		
		$icon = array();
		
		if ($this->_actions) {
			foreach ($this->_actions as $Action) {
				if (Agro_Util::checkPermission($Action)) {
					if (array_key_exists($Action->ds_param, $item)) {
						?>
						<a id="<?=$item[$Action->ds_param]?>" href="/<?=$Action->ds_module?>/<?=$Action->ds_controller?>/<?=$Action->ds_action?>/id/<?=$item[$Action->ds_param]?>" title="<?=$Action->ds_label?>" class="icon_<?=strtolower(str_replace(' ','',$Action->ds_action)); ?>">
							<?
							if ($Action->ds_action == 'active'):
								if ($item['fl_ativo']):
									$Action->ds_image = 'active.png';
								else:
									$Action->ds_image = 'inactive.png';
								endif;
							endif;
							?>
							
							<img class="<?=$Action->ds_action?>_<?=$item[$Action->ds_param]?>" src="/resources/imagens/mini/<?=$Action->ds_image?>" border="0" />
						</a>
						<?
					} else {
						?><img src="/resources/imagens/mini/transp.png" border="0" width="20" height="20" title="O parametro passado não existe." /><?
					}
				}
			}
		}
		
		$html = ob_get_contents();
		ob_end_clean();
		
		return $html;
	}
	
	private function _mountOrder($name) {
		if ($this->_request->getParam('o') == $name) {
			$type = ($this->_request->getParam('s') == 'asc' ? 'desc' : 'asc');
			$this->_img = "{$this->_request->getParam('s')}.gif";
		} else {
			$this->_img = "transp.png";
			$type = 'asc';
		}
		
		$link = "/{$this->_view->moduleName}/{$this->_view->controllerName}/{$this->_view->actionName}/o/$name/s/$type";
		return $link;
	}
}