<?php
App::uses('PortabilityAppController', 'Portability.Controller');
/**
 * Cash Controller
 *
 * @property Cash $Cash
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class PortabilityController extends AppController {

	
/**
 * Pega um item, decrementando 1 do total e salvando novamente
 */	
	public function userExists($user_key) {	
		
		if (empty($user_key)) {
			return false;
		}
		
		/**
		 * Le o usuario requisitado
		 */
		$this->loadModel('User');
		$this->User->create();
		$this->User->recursive = 0;
		$_User = $this->User->findByPortabilityKey($user_key);
		
		if ($this->User->exists($_User['User']['id']))
			return true; 
		return false;
	}

/**
 * Pega um item, decrementando 1 do total e salvando novamente
 */
	public function getByKey($user_key) {
	
		if (empty($user_key)) {
			return null;
		}
	
		/**
		 * Le o usuario requisitado
		 */
		$this->loadModel('User');
		$this->User->create();
		$this->User->recursive = 0;
		return $this->User->findByPortabilityKey($user_key);
	}
	
	
/**
 * Registra uma consulta portabilidade
 * $_data = array(
 * 		'phone' => '',
 * 		'user_id' => '',
 * 		'operator' => '',
 * 		'label' => '',
 * ) 
 * @param unknown $_data
 */	
	public function registerHistory($_data = array()) {
		$this->loadModel('PortHistory');
		$this->PortHistory->create();
		$this->PortHistory->set('phone', $_data['phone']);
		$this->PortHistory->set('user_id', $_data['user_id']);
		$this->PortHistory->set('operator', $_data['operator']);
		$this->PortHistory->set('label', $_data['label']);
		$this->PortHistory->save();
	}
	
}
