<?php

class Agro_Plugin_Rs2Html {
	private $_filename, $_type, $_disposition, $_recordSet, $_string, $_titles, $_css;

	public function __construct() {
		$this->addCss("/resources/css/relatorios.css");

		echo '<html>';
		echo '<head>';
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		echo '</head>';
		echo '<body>';
		echo "<table border='0' cellpadding='2' cellspacing='1'>\n";
	}

	public function setRecordSet($RecordSet) { $this->_recordSet = $RecordSet; }
	public function setTitles($titles) { $this->_titles = $titles; }
	public function setFormat($format) { $this->_format = $format; }
	public function addCss($css) { $this->_css[] = $css; }

	public function buildCss() {
		if (is_array($this->_css)) {
			foreach ($this->_css as $link) {
				echo "<link rel='stylesheet' href='$link' type='text/css' media='screen' />\n";
			}
		}
    }

    public function buildTitles() {
    	echo "\t<thead>\n";
    	echo "\t\t<tr>\n";

    	foreach ($this->_titles as $order => $title) {
    		echo "\t\t\t<th nowrap>$title</th>\n";
    	}

    	echo "\t\t</tr>\n";
    	echo "\t</thead>\n";
    }

    public function buildString() {

    }

	public function getHtml() {
		$this->buildTitles();

		echo "\t<tbody>\n";

    	if ($this->_recordSet) {
    		$i = 0;
    		foreach ($this->_recordSet as $Row) {
    			$class = ($i % 2 == 0 ? 'odd' : 'even');
    			echo "\t\t<tr class='$class'>\n";

				foreach ($this->_titles as $column => $title) {
					switch ($this->_format[$column]['format']) {
						case 'currency':
							$Row[$column] = Agro_Util::formatDbCurrency($Row[$column]);
							break;
						case 'data':
							$Row[$column] = Agro_Util::formatDate($Row[$column], Zend_Date::DATES);
							break;
						case 'data/hora':
							$aux = explode( ' ', $Row[$column]);
							$Row[$column] = Agro_Util::formatDate($aux[0], Zend_Date::DATES) . ' ' . $aux[1];

							break;
						case 'yesno':
							if (strtoupper($Row[$column]) === 'S') {
								$Row[$column] = 'Sim';
							} elseif (strtoupper($Row[$column]) === 'N') {
								$Row[$column] = 'Não';
							}

							if ($Row[$column] === true) {
								$Row[$column] = 'Sim';
							} elseif ($Row[$column] === false) {
								$Row[$column] = 'Não';
							}
							break;
					}

					$value = $Row[$column];
					if (empty($value)) {
						$value = strlen($value) ? $value : '-';
					}

					echo "\t\t\t<td align='{$this->_format[$column]['align']}'>".$value."</td>\n";
		    	}

    			echo "\t\t</tr>\n";
    			$i++;
    		}
    	}

		echo "\t</tbody>\n";

        $this->insereLog();
	}

	public function __destruct() {
		$this->_string = "</table>\t";
		$this->_string = "</body>\t";
		$this->_string = "</html>\t";
		$this->buildCss();
	}

    /*
    * Salva um registro na tabela de log dos arquivos
    */
    private function insereLog() {

        $User = Zend_Auth::getInstance()->getIdentity();
        $data = array(
            'id_usuario_criacao' => $User->id,
            'ds_controller'      => Zend_Controller_Front::getInstance()->getRequest()->getControllerName(),
            'ds_module'          => Zend_Controller_Front::getInstance()->getRequest()->getModuleName(),
            'ds_action'          => Zend_Controller_Front::getInstance()->getRequest()->getActionName(),
            'filename'           => null
        );

        Agro_Db_Table_Abstract::getDefaultAdapter()->insert('log.log_relatorios_gerados', $data);
    }
}
