<?php

class ScheduleTask extends Shell {
   
	var $uses = array('Site', 'Schedule', 'Criterium');
	var $components = array('Alexa', 'GooglePagerank', 'W3c');
	
   
	function execute() 
	{
		$this->out('Starting scheduler');
		
		$this->Site->recursive = 1;
		$sites = $this->Site->find('list');

		$this->Criterium->recursive = 0;
		$criteria = $this->Criterium->find('all');
		
		
		$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))
				{
					$this->out("Pridavam $site, kriterium {$criterium['Criterium']['id']}");
					
					$data = array(
						'Schedule' => array(
							'criterium' => $criterium['Criterium']['id'],
							'site' => $siteId,
						)
					);
					$this->Schedule->create();					
					//$this->Schedule->save($data);
				}
			}
		}
	}
}

?>