<?php
App::uses('AppController', 'Controller');
/**
 * Contatos Controller
 *
 * @property Contato $Contato
 */
class ContatosController extends AppController {


/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Contato->recursive = 0;		
		$this->set('contatos', $this->paginate());
	}

/**
 * admin_view method
 *
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {		
		$this->Contato->id = $id;
		if (!$this->Contato->exists()) {
			throw new NotFoundException(__('Invalid contato'));
		}
		$this->set('contato', $this->Contato->read(null, $id));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add_no() {
		if ($this->request->is('post')) {
			$this->Contato->create();
			if ($this->Contato->save($this->request->data)) {
				$this->Session->setFlash(__('The contato has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The contato could not be saved. Please, try again.'));
			}
		}
		$clientes = $this->Contato->Cliente->find('list');
		$clientes = $this->Contato->Cliente->find('list');
		$telefones = $this->Contato->Telefone->find('list');
		$this->set(compact('clientes', 'clientes', 'telefones'));
	}

/**
 * admin_edit method
 *
 * @param string $id
 * @return void
 */
	public function admin_edit_no($id = null) {
		$this->Contato->id = $id;
		if (!$this->Contato->exists()) {
			throw new NotFoundException(__('Invalid contato'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Contato->save($this->request->data)) {
				$this->Session->setFlash(__('The contato has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The contato could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Contato->read(null, $id);
		}
		$clientes = $this->Contato->Cliente->find('list');
		$clientes = $this->Contato->Cliente->find('list');
		$telefones = $this->Contato->Telefone->find('list');
		$this->set(compact('clientes', 'clientes', 'telefones'));
	}

/**
 * admin_delete method
 *
 * @param string $id
 * @return void
 */
	public function admin_delete_no($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Contato->id = $id;
		if (!$this->Contato->exists()) {
			throw new NotFoundException(__('Invalid contato'));
		}
		if ($this->Contato->delete()) {
			$this->Session->setFlash(__('Contato deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Contato was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
	
/**
 * Admin import
 *
 * @return void
 * @access public
 */
	public function admin_import() {
		$this->set('title_for_layout', 'Importar Contatos');
	
		if ($this->request->is('post')) {

			$filename = $this->request->data['Contato']['document']['name'];
			$tmpname = $this->request->data['Contato']['document']['tmp_name'];
	
			if (($filename == null) || (trim($filename) == "")) {
				$this->Session->setFlash("Selecione um arquivo válido");
			}
			else {
				$filedata = WWW_ROOT. 'uploads' .DS. $filename;
				move_uploaded_file($tmpname, $filedata);
				
// 				$this->loadModel('Registromailing');
// 				$this->Registromailing->create();
// 				$this->Registromailing->importContact($filename);
				
				$this->Contato->import($filename);
				$this->Session->setFlash('Arquivo Importado com Sucesso');
			}
			$this->redirect(array(
					'plugin' => false,
					'admin' => true,
					'controller' => 'contatos',
					'action' => 'index'));
		}
	}	
}
