<?php

class Cron extends Controller {

	function Cron()
	{
		parent::Controller();
		if($_SERVER['REMOTE_ADDR'] != '81.31.32.70'){echo 'NO!';exit;}
    $this->load->model('Dbmysql');
    $this->load->model('Functions');
    $this->load->model('Kategorie');
    $this->load->model('Lokality');
    $this->load->model('Emaily');
    $this->load->model('Timer');
		$this->load->helper('url');
		$this->config->load('katalog_config');
		$this->data = $this->config->item('data');
		$this->Timer->start();
		$config['charset'] = 'utf-8';
    $config['wordwrap'] = TRUE;
    $config['mailtype'] = 'html';
    $this->email->initialize($config);
		$this->data['actions'] = '';
	}

	function index()
	{
		if(date('H') == '00' and date('i') < 5)$this->deleteCache();
		if(date('H') == '23' and date('i') < 5)$this->indexovanieLok();
		if(date('H') == '23' and date('i') < 5)$this->indexovanieKat();
		if(date('H') == '02' and date('i') < 5)$this->deleteNotVerificated();
		if(date('i') < 15)$this->povoljednu();
		//if(date('H') == '22' and date('i') < 35)$this->povolsmthg();
		//if(date('H') == '15' and date('i') < 30)$this->odpovol();
		else echo date('H');
	}
	
	function odpovol()
	{
    $this->Dbmysql->db->query('UPDATE firma SET status = 0 WHERE status = 1');
    $this->data['actions'] .= '<hr>Odpovolenie';
  }
	
	function povoljednu()
	{
    $this->Dbmysql->db->query('UPDATE firma SET status = 1 WHERE status = 0 and reg_date = 0 ORDER BY LENGTH(popis) DESC LIMIT 1');
    $query = $this->Dbmysql->db->query('SELECT COUNT(*) as pocet FROM firma WHERE status = 1');
    $res = $query->result_array();
    $this->data['actions'] .= '<hr>Povolena 1, spolu povolenych: '.$res[0]['pocet'];
  }
	
	function povolsmthg()
  {
    $limit = $this->uri->segment(3);
    $limit = rand(8, 20);
    $celkLimit = 0;
    if($limit > 0)
    {
      $query = $this->Dbmysql->db->query('SELECT * FROM okecStrom3 WHERE id NOT IN (SELECT parent_id FROM okecStrom3)');
  		foreach($query->result_array() as $v)
  		{
  			$this->Dbmysql->db->query('UPDATE firma SET status = 1 WHERE id IN (SELECT firma_id FROM firmakategoria WHERE kategoria_id = '.$v['id'].') AND status = 0 ORDER BY LENGTH(popis) DESC LIMIT '.$limit);
  			$celkLimit += $limit;
  		}
		}
		$this->data['actions'] .= '<hr>Povolenych bolo '.$celkLimit.' novych firiem';
  }
	
	function deleteCache()
	{
		$dirpath = getcwd().'/system/cache/';
		$handle=opendir($dirpath);
		while (($file = readdir($handle)) !== false) {
			if(is_file($dirpath.$file) and $file != 'index.html')
			{
				unlink($dirpath.$file);
			}
		}
		closedir($handle);
		$this->data['actions'] .= '<hr>Cache empty!';
	}
	
	function indexovanieLok()
	{
		$query = $this->Dbmysql->db->query('SELECT id FROM lokality');
		$r1 = $query->result_array();
		foreach($r1 as $v1)
		{
			$lokality = $this->Lokality->getAllPodlokalityIds($v1['id']);
			$lokality[] = $v1['id'];
			$query = $this->Dbmysql->db->query('SELECT firma_id FROM firmalokalita WHERE lokalita_id IN ('.implode($lokality, ', ').')');
			$r2 = $query->result_array();
			$index = '';
			foreach($r2 as $v2)
			{
				$index .= $v2['firma_id'].',';
			}
			$this->Dbmysql->db->query('DELETE FROM katalogindex WHERE kat_lok = "0_'.$v1['id'].'"');
			$this->Dbmysql->db->query('INSERT INTO katalogindex SET kat_lok = "0_'.$v1['id'].'", ids = "'.substr($index, 0, -1).'"');
		}
		$this->data['actions'] .= '<hr>Indexovanie lokalit prebehlo uspesne';
	}
	
	function indexovanieKat()
	{
		$query = $this->Dbmysql->db->query('SELECT id FROM okecStrom3');
		$r1 = $query->result_array();
		foreach($r1 as $v1)
		{
			$kategorie = $this->Kategorie->getAllPodkategorieIds($v1['id']);
			$kategorie[] = $v1['id'];	
			$query = $this->Dbmysql->db->query('SELECT firma_id FROM firmakategoria WHERE kategoria_id IN ('.implode($kategorie, ', ').')');
			$r2 = $query->result_array();
			$index = '';
			foreach($r2 as $v2)
			{
				$index .= $v2['firma_id'].',';
			}
			$this->Dbmysql->db->query('DELETE FROM katalogindex WHERE kat_lok = "'.$v1['id'].'_0"');
			$this->Dbmysql->db->query('INSERT INTO katalogindex SET kat_lok = "'.$v1['id'].'_0", ids = "'.substr($index, 0, -1).'"');
		}		
		$this->data['actions'] .= '<hr>Indexovanie kategorii prebehlo uspesne';
	}
	
	function deleteNotVerificated()
	{
		$deadtime = 172800;//2 dni
		$this->Dbmysql->db->query('DELETE FROM firmahodnoceni WHERE verificated = 0 AND datum < '.(time() - $deadtime));
		$this->Dbmysql->db->query('DELETE FROM diskuze WHERE verificated = 0 AND datum < '.(time() - $deadtime));
		$this->data['actions'] .= '<hr>Zmazane neverifikovane data';
	}
	
	function __destruct()
	{
		if(!empty($this->data['actions']))
		{
      $this->data['actions'] .= '<hr>Script time: <b>'.round($this->Timer->stop(),4).' secs</b>';
      $this->Emaily->sendmail('Cron', 'mario.palka@gmail.com', 'Cron '.date("d.m.Y",time()), $this->data['actions']);
    }
		
	}
}
?>

