<?php
App::uses('AppController', 'Controller');
/**
 * Blacklists Controller
 *
 * @property Blacklist $Blacklist
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class BlacklistsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Session');

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		
		$this->FilterResults->addFilters(
			array(
				'filter_phone' => array(
					'Blacklist.phone' => array(
						'operator' => 'LIKE',
						'value' => array(
							'before' => '%',
							'after' => '%'
						)
					)
				),
			)
		);
		
		// Define conditions
		$this->FilterResults->setPaginate('order', 'Blacklist.id DESC');
		$this->FilterResults->setPaginate('limit', 50);
		$this->FilterResults->setPaginate('conditions', $this->FilterResults->getConditions());
		
		$this->Blacklist->recursive = 0;
		$this->set('blacklists', $this->paginate());
	}

/**
 * Importacao de dados pelo Model
 * @param unknown_type $export
 */
	public function admin_import($export = null) {
		if ($this->request->is('post')) {
	
			$filename = $this->request->data['Blacklist']['document']['name'];
			$tmpname = $this->request->data['Blacklist']['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->Blacklist->import($filename, $this->Auth->user('id'));			 
    			$this->Session->setFlash('Arquivo Importado com Sucesso');			
			}
			$this->redirect(array(
					'plugin' => 'mailings',
					'admin' => true,
					'controller' => 'blacklists',
					'action' => 'index'));
		}
	}	
	
/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Blacklist->exists($id)) {
			throw new NotFoundException(__('Invalid blacklist'));
		}
		$options = array('conditions' => array('Blacklist.' . $this->Blacklist->primaryKey => $id));
		$this->set('blacklist', $this->Blacklist->find('first', $options));
		$this->set('currentkey', $id);
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Blacklist->create();			
			if ($this->Blacklist->save($this->request->data)) {
				$this->Session->setFlash(__('The blacklist has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The blacklist could not be saved. Please, try again.'));
			}
		}
		$users = $this->Blacklist->User->find('list');
		$this->set('user_id', $this->Auth->user('id'));
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Blacklist->exists($id)) {
			throw new NotFoundException(__('Invalid blacklist'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Blacklist->save($this->request->data)) {
				$this->Session->setFlash(__('The blacklist has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The blacklist could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Blacklist.' . $this->Blacklist->primaryKey => $id));
			$this->request->data = $this->Blacklist->find('first', $options);
		}
		$users = $this->Blacklist->User->find('list');
		$this->set(compact('users'));
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Blacklist->id = $id;
		if (!$this->Blacklist->exists()) {
			throw new NotFoundException(__('Invalid blacklist'));
		}
		if ($this->request->is('post') || $this->request->is('delete')) {
			if ($this->Blacklist->delete()) {
				$this->Session->setFlash(__('The blacklist has been deleted.'));
			} else {
				$this->Session->setFlash(__('The blacklist could not be deleted. Please, try again.'));
			}
		}
		return $this->redirect(array('action' => 'index'));
	}
}
