<?php

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

class SitesController extends WebvalController {

	var $scaffold = true;
	var $components = array('Auth', 'Alexa', 'GooglePagerank');
	
	function index()
	{
		$this->Site->recursive = 1;
		$fp = FirePHP::getInstance(true);
		$fp->log($this->paginate(), 'sites');
		$this->set('sites', $this->paginate());
	}
	
	function add() {
	
	}
	
	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);
		}
		
	}
	
}

?>