<?php
App::uses('AppController', 'Controller');
/**
 * ActiveCustomers Controller
 *
 * @property ActiveCustomer $ActiveCustomer
 */
class ActiveCustomersController extends AppController {


	public function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allow(
				'isactive',
				'notify'
		 	);
	}
	
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->ActiveCustomer->recursive = 0;
		$this->paginate['order'] = 'ActiveCustomer.active ASC, ActiveCustomer.name';
		$this->set('activeCustomers', $this->paginate());
	}

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

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->ActiveCustomer->create();
			$this->ActiveCustomer->set('activekey', uniqid());
			if ($this->ActiveCustomer->save($this->request->data)) {				
				$this->Session->setFlash(__('The active customer has been saved', 
						'default', array(), 'good'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The active customer could not be saved. Please, try again.',
						'default', array(), 'bad'));
			}
		}
	}

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

/**
 * admin_active_inactive
 */
	public function admin_active_inactive($id = null, $active = null) {
		$this->ActiveCustomer->id = $id;
		if (!$this->ActiveCustomer->exists()) {
			throw new NotFoundException(__('Invalid active customer'));
		}
		if ($this->request->is('post') || $this->request->is('get')) {
			
			$this->ActiveCustomer->read(null, $id);
			$this->ActiveCustomer->set('active', !$active);
			$this->ActiveCustomer->set('lastadjust', date('Y-m-d H:i:s', strtotime("now")));
			
			if ($this->ActiveCustomer->save()) {
				$this->Session->setFlash(__('The active customer has been saved'),
						'default', array(), 'good');
			} else {
				$this->Session->setFlash(__('The active customer could not be saved. Please, try again.'),
						'default', array(), 'bad');
			}
		}
		$this->redirect(array('action' => 'index'));
	}

/**
 * admin_delete method
 *
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->ActiveCustomer->id = $id;
		if (!$this->ActiveCustomer->exists()) {
			throw new NotFoundException(__('Invalid active customer'));
		}
		if ($this->ActiveCustomer->delete()) {
			$this->Session->setFlash(__('Active customer deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Active customer was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
	
/**
 * validate
 */
	public function isactive($activekey = null) {
		$this->autoLayout = false;
		
		if ($activekey != null) {
			$customer = $this->ActiveCustomer->findByActivekey($activekey);
			if ($customer != null) {
				return $this->redirect(array(
						'action' => 'notify', boobin($customer['ActiveCustomer']['active'])					
				));
			}
		}
		return $this->redirect(array('action' => 'notify', 'Active Key Not Found'));
	}

/**
 * notify envia para a view
 */
	public function notify($message) {
		$this->autoLayout = false;
		$this->set('title_for_layout', 'BmConnector');
		$this->set('notify', $message);
	}
	
}
