<?php

App::import('Controller', array('Webval'));
App::import('Vendor', 'cURL', array ( 'file' => 'curl.php'));

class TestsController extends WebvalController
{
	var $uses = array('Site', 'Schedule', 'Criterium');
	var $components = array('Auth', 'Alexa', 'GooglePagerank', 'W3c');
	
	function index()
	{		
		$url = 'http://joj.sk';
		$fp = FirePHP::getInstance(true);
		
		$curl = new cURL();
		
		/*$GooglePageRankURL = 'http://www.google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:'.$url;
		$content = $curl->get($GooglePageRankURL);
		preg_match('/[0-9]+$/', $content, $matches);
		$googlePageRank = $matches[0];
		$fp->log($googlePageRank, 'Google Pagerank');*/
		
		// alexa
		/*$alexaURL = 'http://www.alexa.com/siteinfo/'.urlencode($url);
		$content = $curl->get($alexaURL);
		$doc = new DOMDocument();
		@$doc->loadHTML($content);
		$xpath = new DOMXPath($doc);
		$xquery = '//div[@class="data down"]';		
		$entries = $xpath->query($xquery);
		foreach ($entries as $entry)
		{
			$alexaRank = $entry->nodeValue;
			break;
		}
		$fp->log($alexaRank,'Alexa');*/
		
		/*
		
		$w3curl = 'http://validator.w3.org/check?uri='.urlencode($url);
		$content = $curl->get($w3curl);
		//$fp->log($content);
		
		$doc = new DOMDocument();
		@$doc->loadHTML($content);
		$xpath = new DOMXPath($doc);
		$xquery =  '//td[@class="invalid"]';
		$entries = $xpath->query($xquery);
		foreach ($entries as $entry)
		{
			$w3cRankString = $entry->nodeValue;
			break;
		}
		preg_match('/([0-9]+) Error./', $w3cRankString, $matches);
		//$fp->log($matches);
		$w3cRank = intval($matches[1]);
		$fp->log($w3cRank,'W3C');*/
		
		/*$this->Site->recursive = 1;
		$sites = $this->Site->find('list');
		$fp->info($sites);

		$this->Criterium->recursive = 0;
		$criteria = $this->Criterium->find('all');
		$fp->info($criteria);
		
		
		$values = $this->Site->CriteriumValue->find('first',
			array('conditions' => array(
				'CriteriumValue.site' => 1,
				'CriteriumValue.time > ' => date('Y-m-d', strtotime($criteria[1]['Criterium']['periodicity'])), 
			))
		);
		
		foreach ($sites as $siteId => $site)
		{
			foreach ($criteria as $criterium)
			{
				$value = $this->Site->CriteriumValue->find('first',
					array('conditions' => array(
						'CriteriumValue.site' => $siteId,
						'CriteriumValue.criterium' => $criterium['Criterium']['id'],
						'CriteriumValue.time > ' => date('Y-m-d', strtotime($criterium['Criterium']['periodicity'])), 
					))
				);
				if (empty($value))
				{
					$fp->info("Pridavam $site, kriterium {$criterium['Criterium']['id']}");
					
					$data = array(
						'Schedule' => array(
							'criterium' => $criterium['Criterium']['id'],
							'site' => $siteId,
						)
					);
					$this->Schedule->create();					
					$this->Schedule->save($data);
				}
			}
		}
		
		$fp->info($values, 'values');*/
		
		for ($i = 0; $i < 5; $i++)
		{
			$schedule = $this->Schedule->find('first');
			
            
			if (!empty($schedule))
			{
				switch ($schedule['Schedule']['criterium'])
				{
					case CriteriumValue::GOOGLE_PAGERANK :
						$rank = $this->GooglePagerank->getRank($schedule['Site']['url']);
						break;
					case CriteriumValue::ALEXA_RANK :
						$rank = $this->Alexa->getRank($schedule['Site']['url']);
						break;
					case CriteriumValue::W3C_VALIDITY :
						$rank = $this->W3c->getRank($schedule['Site']['url']);
						break;
				}
				
				$data = array(
	           		'criterium' => $schedule['Schedule']['criterium'],
	           		'site' => $schedule['Schedule']['site'],
	           		'value' => $rank,
	           		'time' => date('Y-m-d h:i:s'),
	            );
	            $this->Criterium->CriteriumValue->create();
	            $this->Criterium->CriteriumValue->save($data);
	            $this->Schedule->remove($schedule['Schedule']['id']);
			}
			
		}
		
	}
	
}

?>