<?php

class Solicitacao_TarefaController extends Agro_Controller_Action {
	
	public function init() {
		$this->view->title = 'Solicitações';
	}
	
	public function indexAction() {
		$this->view->title .= ' » Minhas Solicitações';
	}
	
	public function getForm(){
		return new Solicitacao_Form_Tarefa();
	}
	
	public function setBack() {
		$this->view->back = '/solicitacao/tarefa';
	}
	
	public function viewAction() {
		$this->view->title .= ' » Minhas Solicitações » Visualizar';
		
		if ($this->getRequest()->isPost()) {
			$Andamentos = new Solicitacao_Model_TarefaAndamento();
			$Andamentos->getDefaultAdapter()->beginTransaction();
			
			try {
				//$file = Agro_File::upload('solicitacoes');
				$file = $this->upload('solicitacoes');

				if ($Andamentos->add($this->getRequest()->getParams(), $file)) {
					$Andamentos->getDefaultAdapter()->commit();
					
					$this->view->showMessage(MSG_SUCCESS, 'success');
				} else {
					$Andamentos->getDefaultAdapter()->rollBack();
					$this->view->showMessage(MSG_ERROR, 'alert');
				}
				
			} catch (Exception $e) {
				// Volta as alteracoes do banco
				$Andamentos->getDefaultAdapter()->rollBack();
				
				// Apaga o arquivo se existir
				Agro_File::delete('solicitacoes', $file['new_name']);
				
				// Mostra msg de erro
				$this->view->showMessage($e->getMessage(), 'error');
			}
		}
		
		if ($this->getRequest()->getParam('id')) {
			$Tarefas = new Solicitacao_Model_Tarefa();
			$this->view->tarefa = $Tarefas->find($this->getRequest()->getParam('id'))->current();
			$this->view->tarefa->fl_lido = 1;
			$this->view->tarefa->dados();
			$this->view->tarefa->save();
		}

		$Form = $this->getForm();
		$Form->id_status->setValue($this->view->tarefa->id_status);
		$this->view->form = $Form;
	}
	
	public function addAction() {
		$this->view->title .= ' » Criar Solicitação';
		$this->view->back = '/solicitacao/tarefa';
	
		$this->view->form = new Solicitacao_Form_Tarefa();
		
		if ($this->getRequest()->isPost()) {
			if ($this->view->form->isValid($this->getRequest()->getParams())) {
				$Tarefas = new Solicitacao_Model_Tarefa();
				
				//if ($Tarefas->save($this->getRequest()->getParams(), Agro_File::upload('solicitacoes'))) {
				if ($Tarefas->save($this->getRequest()->getParams(), $this->upload('solicitacoes'))) {
					$this->view->showMessage(MSG_SUCCESS, 'success');
									
					$this->view->form->reset();
					
				} else {
					$this->view->showMessage(MSG_ERROR, 'error');
				}
			} else {
				$this->view->showMessage($this->getHtmlFormError($this->view->form->getMessages()), 'alert');
				$this->view->form->populate($this->getRequest()->getParams());
			}
		}
	}	
	
	public function departamentoAction() {
		$this->view->title .= 'Solicitações » Solicitações do Departamento';
		$Tarefas = new Solicitacao_Model_Tarefa();
		
		$this->view->combo = $Tarefas->combo_departamento();
		$this->view->records = $Tarefas->departamento();
	}
        
	private function upload($directory) {
		$return = array();
                $i = 1;
		if (is_array($_FILES) && count($_FILES)) {
			$path = Agro_File::getPath($directory);
			
			foreach ($_FILES as $file) {
				try {
					if (!$file['error']) {
						$newName = $i++.Agro_File::name(Agro_File::getExtension($file['name']));
						
						if (copy($file['tmp_name'], $path.$newName)) {
							$return[] = array(
								'old_name' => $file['name'],
								'new_name' => $newName,
								'status' => true
							);
						}
					}
				} catch (Exception $e) {
					die($e);
				}
			}
			
			return $return;
		}
	}        
}