<?php

class Sistema_Model_Roles extends Agro_Db_Table_Abstract {
    const ID_ATENDIMENTO  = 4;
    const ID_VISTORIADOR  = 18;
    const ID_VISTORIADOR_RESP  = 30;

	protected $_primary = array('id');
	protected $_schema = 'sistema';
	protected $_name = 'roles';

	public $_columnExcel = array(
		'id' => 'Cod',
		'ds_nome_role' => 'Nome',
	);

  public function save(array $data) {
    	$Row = $this->getRow($data['id']);

		$Row->ds_nome_role = $data['ds_nome_role'];
		$Row->ds_chave = $data['ds_chave'];
		$Row->id_parent = $data['id_parent'];

		return $Row->save();
	}

	public function getRoles($id='') {
		if($id == '') {
			$sql = "select * from sistema.roles where id_parent is null";
			$Rs = $this->_db->fetchAll($sql);
		} else {
			$sql = "select * from sistema.roles where id_parent = $id";
			$Rs = $this->_db->fetchAll($sql);
		}
		$arr = array();
		foreach($Rs as $Row) {
			$arr[$Row['ds_chave']] = $this->getRoles($Row['id']);
		}
		return $arr;
	}

	public function checkParent($idMenu) {
		$select = $this->select()->distinct(true)->setIntegrityCheck(false);
		$select->from(array('a'=>'sistema.acao'));
		$select->where('id_parent = ?', $idMenu);

		return $this->fetchAll($select)->count();
	}

	public function addActions() {
		$sql = "select id from sistema.acao where id not in (select id_acao from sistema.roles_acao where id_role = ".PERFIL_ADMIN.")";
		$Rs = $this->_db->query($sql);
		foreach($Rs as $key => $val) {
			$sql = "insert into sistema.roles_acao (id_acao, id_role, fl_permitido) values ({$val['id']}, ".PERFIL_ADMIN.", true)";
			$this->_db->query($sql);
		}
	}

	public function getIdChave($chave) {
		$Row = $this->fetchRow("ds_chave = '$chave'");
		return $Row->id;
	}

	public function fetchRoles($parent=false) {
		$Select = $this->select()->setIntegrityCheck(false);
		$Select->from(array('roles' => 'sistema.roles'), array('ds_chave as role', 'id'));
		$Select->joinLeft(array('parent' => 'sistema.roles'), 'roles.id_parent = parent.id', 'ds_chave as parent');
		$Select->order('roles.ds_chave');

		if ($parent) {
			$Select->where('roles.id_parent = ?', $parent);
		} else {
			$Select->where('roles.id_parent is null');
		}

		return $this->fetchAll($Select);
	}

	public function getCombo() {
		if (Zend_Auth::getInstance()->getIdentity()->ds_chave == "ADMIN") {
			return $this->getDefaultAdapter()->fetchPairs("select id, ds_nome_role from sistema.roles order by ds_nome_role");
		} else {
			return $this->getDefaultAdapter()->fetchPairs("select id, ds_nome_role from sistema.roles where ds_chave != 'ADMIN' order by ds_nome_role");
		}
	}

	public function getComboPreposto() {
	    return $this->getDefaultAdapter()->fetchPairs("select id, ds_nome_role from sistema.roles where ds_chave in ('CORRETOR','PREP_SEM_CHAMADO','PREP_SEM_CHAMADO_SIN','PREP_COM_CHAMADO_SIN') order by ds_nome_role");
	}

	public function getComboVistoriador() {
		return $this->getDefaultAdapter()->fetchPairs("select id, ds_nome_role from sistema.roles where ds_chave in
			('VISTORIADOR','VISTORIADOR_RESP') order by ds_nome_role");
	}

	public function getComboAuxiliar() {
		return $this->getDefaultAdapter()->fetchPairs("select id, ds_nome_role from sistema.roles where ds_chave in
			('AUXILIAR_CAMPO') order by ds_nome_role");
	}

    public function getComboFuncionario()
    {
        return $this->getDefaultAdapter()->fetchPairs("select id, ds_nome_role from sistema.roles where ds_chave not in	('VISTORIADOR','VISTORIADOR_RESP', 'AUXILIAR_CAMPO') order by ds_nome_role");
    }
}
