<?php

App::import('Controller', array('Webval'));

class SitesController extends WebvalController {

	var $scaffold = true;
	var $components = array('Auth', 'Alexa', 'GooglePagerank', 'FusionCharts.FusionCharts', 'W3c');
	var $helpers = array('FusionCharts.FusionCharts');
	
	function index()
	{
		$this->Site->recursive = 1;
		$this->set('sites', $this->paginate());
	}
	
	function view($id = null)
	{
		$this->Site->recursive = 2;
		if (!$id) {
			$this->Session->setFlash('Neexistujúca stránka');
			$this->redirect(array('action'=>'index'));
		}
		$site = $this->Site->read(null, $id);
		$this->set('site', $site);
		
		$this->FusionCharts->create
			(
				'Criteria',
				array
				(
					'type' => 'Column2D',
					'width' => 600,
					'height' => 350,
					'id' => ''
				)
			);

		$this->FusionCharts->setChartParams
			(
				'Criteria',
				array
				(
					'caption' => 'Hodnotenia v kritériách',
					'xAxisName' => 'Kritérium',
					'yAxisName' => 'Body',
					'decimalPrecision' => '0',
					'formatNumberScale' => '0'
				)
			);

		$chartData = array();
		foreach ($site['CriteriumValue'] as $criterium_value) 
		{
			
			$chartData[] = array('value' => $criterium_value['value'], 'params' => array('name' => $criterium_value['Criterium']['name']));
		}
			
		$this->FusionCharts->addChartData('Criteria', $chartData );
	}
	
	function add() 
	{
		if (!empty($this->data)) 
		{
            $user = $this->Auth->user();			
			$this->data['Site']['created_by'] = $user['User']['id']; 
			
			if ($this->data['Site']['checkNow'])
            {
            	$alexaRank = $this->Alexa->getRank($this->data['Site']['url']);
            	$this->data['CriteriumValue'][] = array(
            		'criterium' => $this->Alexa->criteriumId,
            		'site' => $id,
            		'value' => $alexaRank,
            		'time' => date('Y-m-d h:i:s'),
            	);
            }
			
	        if($this->Site->saveAll($this->data)) 
	        {				
				$this->Session->setFlash("Stránka uložená");
	            $this->redirect(array('action'=>'index'));
	        }
		}
		
	}
	
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash('Neexistujúca stránka');
			$this->redirect(array('action'=>'index'));
		}
		
		if (!empty($this->data)) 
		{
            $fp = FirePHP::getInstance(true);
            $fp->log($this->data);
            if ($this->data['Site']['checkNow'])
            {
            	$alexaRank = $this->Alexa->getRank($this->data['Site']['url']);
            	$this->data['CriteriumValue'][] = array(
            		'criterium' => $this->Alexa->criteriumId,
            		'site' => $id,
            		'value' => $alexaRank,
            		'time' => date('Y-m-d h:i:s'),
            	);
            }
			
	        if($this->Site->saveAll($this->data)) {
				
				$this->Session->setFlash("Stránka uložená");
	            $this->redirect(array('action'=>'index'));
	        }
		}
		
		if (empty($this->data)) {
			$this->data = $this->Site->read(null, $id);
		}
		$categories = $this->Site->Category->find('list',array('fields'=>array('id','name')));
        $this->set(compact('categories'));		
	}
	
	function comment_add() {
		if (!empty($this->data)) {
			$this->Site->Comment->create();			
			$user = $this->Auth->user();			
			$this->data['Comment']['user'] = $user['User']['id']; 
			/*$fp = FirePHP::getInstance(true);
			$fp->log($this->data, 'data');*/
						
			if ($this->Site->Comment->save($this->data)) {
				$this->Session->setFlash('Komentár uložený');
				$this->redirect(array('action'=>'view', $this->data['Comment']['site']));
			} else {
				$this->Session->setFlash('Chyba pri ukladaní komentára');
			}
		}	
	}
	
}

?>