<?

class Produto_Model_EstadiosFenologicos extends Agro_Db_Table_Abstract {
	protected $_schema = 'produto';
	protected $_name = 'estadios_fenologicos';
	protected $_primary = 'id';
	public $_order = 'ds_nome_estadio_fenologico';
	
	
	public function save(array $data) {
		$Row = $this->getRow($data['id']);

		$Row->id_grupo_estadio_fenologico 	= $data['id_grupo_estadio_fenologico'];
		$Row->ds_nome_estadio_fenologico 	= $data['ds_nome_estadio_fenologico'];
		$Row->cd_estadio_fenologico 		= $data['cd_estadio_fenologico'];
		$Row->ds_chave 						= $data['ds_chave'];
		$Row->ds_descricao 					= $data['ds_descricao'];

		return $Row->save();
	}
	
	public function getData($where=false) {
		$Select = $this->select()->setIntegrityCheck(false);
		
		$Select->from(array("e"=>"produto.estadios_fenologicos"));
		$Select->joinInner(array("g"=>"produto.grupos_estadios_fenologicos"), "g.id = e.id_grupo_estadio_fenologico", array("ds_nome_grupo"));
		
		if ($this->_order) {
			$Select->order($this->_order);
		}

		$this->getWhere($Select, $where);
		
		return $Select;
	}
	
	public function getCombo($id_cultura){
		$ModelCulturas = new Produto_Model_Culturas();
		
		$Cultura = $ModelCulturas->find($id_cultura)->current();
		
		if ($Cultura->id_grupo_estadio_fenologico){
			$Rs = $this->fetchAll("fl_ativo = true and id_grupo_estadio_fenologico={$Cultura->id_grupo_estadio_fenologico}", $this->_order)->toArray();
	
			$options = array();
			if (is_array($Rs)) {
				
				$options[''] = '';
				
				foreach ($Rs as $row) {
					$options[$row['id']] = $row["ds_nome_estadio_fenologico"];
				}
	
				return $options;
			} else {
				return false;
			}
		} else {
			$options[''] = 'Configurar estadios para a cultura!';			
			return $options; 
		}
	}
}
	