<?php

class Produto_Model_Validacoesportaenxerto extends Agro_Db_Table_Abstract {
	protected $_schema = 'produto';
	protected $_name = 'validacoes_grupo_portaenxerto';
	protected $_primary = 'id';
	public $_paginator = false;
	public $_order = 'ds_nome_grupo_portaenxerto';

	public function getDataMultiChecks($where) {
		$select = $this->getData($where);
		$Rs = $this->fetchRow($select);
		return $Rs;
	}

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

        $Row->id_validacao = $data['id_validacao'];
        $Row->ds_nome_grupo_portaenxerto = $data['ds_nome_grupo_portaenxerto'];

        $Row->save();

        return $Row->save();
    }
	
	public function setAtivo($id) {
		$Row = $this->getRow($id);
		
		if($Row['fl_ativo'] == 0) 
			$Row['fl_ativo'] = 'true';
		else 
			$Row['fl_ativo'] = 'false';
		if($Row->save()) {
			return true;
		} else {
			return false;
		}
	}
	
	public function getCombo($first=true,$field=null, $id_validacao=null) {
		$where = 'fl_ativo = true';
		if($id_validacao != null) {
			$where .= " and id_validacao = $id_validacao";
		}
		$Rs = $this->fetchAll($where, $this->_order)->toArray();
		
		$options = array();
		if (is_array($Rs)) {
			if ($first) {
				$options[''] = '';
			}
			
			$field = $field ? $field : $this->_order;
			foreach ($Rs as $row) {
				$options[$row['id']] = $row[$field];
			}
			
			return $options;
		} else {
			return false;
		}
	}

	public function getGrupos($idValidacao) {
		return $this->fetchPairs('id', 'ds_nome_grupo_portaenxerto', false, false, 1, 'id_validacao = '.$idValidacao, true, 'ASC');
	}
	
	public function getGrupoPortaenxerto($idPortaenxerto) {
		$Select = $this->select()->distinct(true);
		$Select->setIntegrityCheck(false);
		$Select->from(array('vgpp'=>'produto.validacoes_grupo_portaenxerto_portaenxertos'), 'id_grupo_portaenxerto');
		$Select->join(array('vgp' => 'produto.validacoes_grupo_portaenxerto'), 'vgpp.id_grupo_portaenxerto = vgp.id', null);
		$Select->where('vgpp.id_portaenxerto = ?', $idPortaenxerto);
		
		return $this->getAdapter()->fetchOne($Select);
	}
}
