<?php
require_once __DIR__ . '/../vendor/autoload.php';

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

	protected function _initSession() {
		Zend_Session::start();
	}

	protected function _initDefaultLoader() {
		$loader = Zend_Loader_Autoloader::getInstance();
		$loader->suppressNotFoundWarnings(false);
		$loader->setFallbackAutoloader(false);
	}

	public function _initTranslate() {
		// Create the object and add a language
		$translate = new Zend_Translate('Array', LIBRARY . '/languages/pt_BR/Zend_Validate.php', 'pt_BR');
		Zend_Registry::set('Zend_Translate', $translate);
	}

	protected function _initDoctype() {
		@$release = file_get_contents(APPLICATION."/../application/release.txt");
		Zend_Registry::set('Zend_Currency', new Zend_Currency('pt_BR'));

		Zend_Registry::set('config', $this->getApplication()->getOptions());
		$this->bootstrap('view');

		$View = $this->getResource('view');
		$View->headTitle('AgroNet 2.0');

		switch (APPLICATION_ENV) {
			case 'production':
				$View->headTitle($release);
				break;
			case 'homolog':
				$View->headTitle(APPLICATION_ENV);
				$View->headTitle($release);
				break;
			default:
				$View->headTitle(APPLICATION_ENV);
				break;
		}

		$View->headTitle()->setSeparator(' - ');
		$View->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
		$View->addHelperPath('Agro/View/Helper', 'Agro_View_Helper');
		$View->user = Zend_Auth::getInstance()->getIdentity();

		// COMENTADO PARA ATIVAR HTTS EM TODO O AGRONET NÃO APENAS NO LOGIN
		//Zend_Controller_Action_HelperBroker::addPath('Agro/Controller/Helper','Agro_Controller_Action_Helper');

		return $View;
	}

	protected function setConstants($constants) {
		foreach ($constants as $key => $value) {
			if (!defined($key)) {
				define($key, $value);
			}
		}
	}

        /* Essa função faz o registro da classe Log
         * que, com a função zf_debug na classe Agro_Util
         * É necessário as extensões Firebug (getfirebug.com) e
         * FirePHP (www.firephp.org) instaladas no Firefox (ou em arquivo)
         */
	protected function _initLogging() {
 		$logger = new Zend_Log();
		//$writer = new Zend_Log_Writer_Firebug();

		//verifica o sistema operacional
		$os = strtoupper(substr(PHP_OS, 0, 3));
		if ($os == 'LIN' || $os == 'DAR') {
			$path = !defined('LOG_PATH') ? '/tmp' : LOG_PATH;
			$stream = @fopen($path.'/log_'.APPLICATION_ENV.'_'.date('Ymd').'.log', 'a', false);
		} else if ($os == 'WIN') {
			$stream = @fopen('c:\Windows\temp\log_'.APPLICATION_ENV.'_'.date('Ymd').'.log', 'a', false);
		}

		if (!$stream) {
			throw new Exception('Erro ao gerar arquivo de log.
			Se estiver usando /tmp/ (ou c:\tmp no Windows) certifique que, no arquivo /etc/systemd/system/multi-user.target.wants/apache2.service, o parâmetro
			PrivateTmp esteja FALSE. Após execute: systemctl daemon-reload && systemctl restart apache2');
		}

        $writer = new Zend_Log_Writer_Stream($stream);
		$logger->addWriter($writer);
		Zend_Registry::set('logger',$logger);
	}

	protected function _initZFDebug() {

		if (APPLICATION_ENV == 'tonismars') { //coloque aqui seu ambiente pra ativar a barra de debug.
			$autoloader = Zend_Loader_Autoloader::getInstance();
			$autoloader->registerNamespace('ZFDebug');

			//Database
			$this->bootstrap('db');
			$db = $this->getPluginResource('db')->getDbAdapter();

			if ($this->hasPluginResource('cache')) {
				$this->bootstrap('cache');
				$cache = $this->getPluginResource('cache')->getDbAdapter();
			}

			$options = array(
				'plugins' => array('Variables',
								   'Database' => array('adapter' => $db),
								   'File' => array('basePath' => APPLICATION_PATH),
								   'Memory',
								   'Time',
						           'Registry',
						           //'Cache' => array('backend' => $cache->getBackend()),
						           'Exception',
								   //'jquery_path' => 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js',
								   'Html'
								)
			);
			$debug = new ZFDebug_Controller_Plugin_Debug($options);

			$this->bootstrap('frontController');
			$frontController = $this->getResource('frontController');
			$frontController->registerPlugin($debug);
		}
	}
}
