<?php
App::uses('AppController', 'Controller');
/**
 * Analytics Controller
 *
 * @property Analytic $Analytic
 */
class AnalyticsController extends AppController {
	
	public function beforeFilter() {
		parent::beforeFilter();		
		$this->Auth->allow('send');
	}

	public $components = array(
			'Highcharts.Highcharts',
	);

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Analytic->recursive = 0;
		$this->paginate['order'] = 'Analytic.created DESC';
		$this->paginate['limit'] = '7';
		$this->set('analytics', $this->paginate());
	}	
	
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_analysis() {
		$this->Analytic->recursive = -1;		
		
		$public = $this->Analytic->find('all', array(
				'group' => array(
						'Analytic.page_title',						
				),
				'fields' => array(
						'count(Analytic.page_title) AS total',
						'Analytic.page_title',						
				),
				'conditions' => array(
						'Analytic.page_slug NOT LIKE' => '%admin%',
						'Analytic.page_title NOT LIKE' => '%nalytics%'
				),
				'order' => 'total DESC, Analytic.page_title',
				'limit' => 7
		));
		
		
		$ips = $this->Analytic->find('all', array(
				'group' => array(
						'Analytic.ip',
						'Analytic.country_name',
						'Analytic.region_name',
						'Analytic.city',
				),
				'fields' => array(
						'count(Analytic.ip) AS total',
						'Analytic.ip',
						'Analytic.country_name',
						'Analytic.region_name',
						'Analytic.city',
				),
				'conditions' => array(
						'Analytic.page_title NOT LIKE' => '%nalytics%'
				),
				'order' => 'total DESC, Analytic.ip',
				'limit' => 5
		));
		$this->set(compact('public', 'ips'));
	}
	
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_ranking_page() {
		$this->Analytic->recursive = -1;
		
		$this->paginate['group'] = 'Analytic.page_title';
		$this->paginate['fields'] = array(
				'count(Analytic.page_title) AS total',
				'Analytic.page_title'
			);		
		$this->paginate['order'] = 'total DESC, Analytic.page_title';
		$this->paginate['limit'] = '12';
		$this->set('analytics', $this->paginate());
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_ranking_protected_page() {
		$this->Analytic->recursive = -1;
		
		$this->paginate['group'] = 'Analytic.page_title';
		$this->paginate['fields'] = array(
				'count(Analytic.page_title) AS total',
				'Analytic.page_title'
		);
		$this->paginate['conditions'] = array(
				'Analytic.page_slug LIKE' => '%admin%',
		);
		$this->paginate['order'] = 'total DESC, Analytic.page_title';
		$this->paginate['limit'] = '12';
		$this->set('analytics', $this->paginate());
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_ranking_public_page() {
		$this->Analytic->recursive = -1;
		
		$this->paginate['group'] = 'Analytic.page_title';
		$this->paginate['fields'] = array(
				'count(Analytic.page_title) AS total',
				'Analytic.page_title'
		);
		$this->paginate['conditions'] = array(
				'Analytic.page_slug NOT LIKE' => '%admin%',
		);
		$this->paginate['order'] = 'total DESC, Analytic.page_title';
		$this->paginate['limit'] = '12';
		$this->set('analytics', $this->paginate());
	}
	
/**
 * admin_index method
 *
 * @return void
 */
	public function admin_ranking_ip() {
		$this->Analytic->recursive = -1;
			
		$this->paginate['group'] = array(
				'Analytic.ip',
				'Analytic.country_name',
				'Analytic.region_name',
				'Analytic.city',
			);
		$this->paginate['fields'] = array(
				'count(Analytic.ip) AS total',
				'Analytic.ip',
				'Analytic.country_name',
				'Analytic.region_name',
				'Analytic.city',
			);
		$this->paginate['conditions'] = array(
				'Analytic.page_slug NOT LIKE' => '%admin%',
				'Analytic.page_title NOT LIKE' => '%nalytics%'
			);
		$this->paginate['order'] = 'total DESC, Analytic.ip';
		$this->paginate['limit'] = '12';
		$this->set('analytics', $this->paginate());
	}
	
/**
 * Envia os dados estatisticos do site para o banco via javascript
 */	
	public function send() {
		$this->autoRender = false;
		if($this->request->is('ajax')) {
			$query = $this->request->query;			
			
			$this->autoRender = false;
			
			if (strpos($query['page_title'], 'Analytics') === false) {
			
				$this->Analytic->create();
				$this->Analytic->set('page_title',$query['page_title']);
				$this->Analytic->set('page_slug',$query['page_slug']);
				$this->Analytic->set('user_id',$this->Auth->User('id'));
				$this->Analytic->set('created', date('Y-m-d H:i:s'));
				$this->Analytic->set('ip', $_SERVER['REMOTE_ADDR']);
				$this->Analytic->save();
				
				$id = $this->Analytic->id;
				$ipinfo = $this->getIpInfo($_SERVER['REMOTE_ADDR']);
				
				$this->Analytic->set('country_code', $ipinfo->country_code);
				$this->Analytic->set('country_name', $ipinfo->country_name);
				$this->Analytic->set('region_code', $ipinfo->region_code);
				$this->Analytic->set('region_name', $ipinfo->region_name);
				$this->Analytic->set('city', $ipinfo->city);
				$this->Analytic = $this->Analytic->save();
			}
		}
	}	
		
/**
 * Outras URLS
 * 
 * http://ipinfo.io/179.184.242.189/json
 * http://ip-api.com/json/187.58.1.179
 * 
 */	
	public function getIpInfo($ip) {
		$url = 'http://freegeoip.net/json/'.$ip;
		
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$response = curl_exec($ch);
		curl_close($ch);
		
		return JSON_DECODE($response);
	}
	
/**
 * valida todos os ips cadastrados no sistema
 */
	public function admin_validate() {
		
		$this->Analytic->recursive = -1;
		$Analytics = $this->Analytic->find('all', array(
				'conditions' => array(
						'OR' => array(
							'country_code' => '',
							'country_code IS NULL'
						)		
				)	
		));
		
		foreach($Analytics as $a) {			
			$ipinfo = $this->getIpInfo($a['Analytic']['ip']);
 			$a['Analytic']['country_code'] = $ipinfo->country_code;
 			$a['Analytic']['country_name'] = $ipinfo->country_name;
 			$a['Analytic']['region_code'] = $ipinfo->region_code;
 			$a['Analytic']['region_name'] = $ipinfo->region_name;
 			$a['Analytic']['city'] = $ipinfo->city;
 			$this->Analytic->save($a);		
		}		
		$this->Session->setFlash('Os dados foram atualizados',
				'default', array(), 'good');
		$this->redirect(array('action' => 'index'));
	}
}
