<?php

/*------------------------------------------------------------------------
# com_guru
# ------------------------------------------------------------------------
# author    iJoomla
# copyright Copyright (C) 2013 ijoomla.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
# Websites: http://www.ijoomla.com
# Technical Support:  Forum - http://www.ijoomla.com.com/forum/index/
-------------------------------------------------------------------------*/

defined('_JEXEC') or die('Restricted access');

class plgSystemGuruCron extends JPlugin
{
    function onAfterRoute()
    {
        $app = JFactory::getApplication();
        $guruCron = $app->input->get('gurucron');
        if ($app->isClient('site') && $guruCron) {
            require_once JPATH_ROOT . '/components/com_guru/helpers/cronjobs.php';
            guru_cronjobs();
            die('done');
        }

        if ($app->isClient('site')) {
            $timezone = new DateTimeZone(JFactory::getConfig()->get('offset'));
            $jnow = new JDate('now');
            $jnow->setTimezone($timezone);
            $date_today = $jnow->toSQL(true);
            $date_today_int = strtotime($date_today);
            $day_today = date("Y-m-d", $date_today_int);

            $db = JFactory::getDBO();
            $sql = "select last_check_date from #__guru_config where id=1";
            $db->setQuery($sql);
            $last_check_date = $db->loadResult();
            $int_last_check = strtotime($last_check_date);
            $day_last_check = date("Y-m-d", $int_last_check);

            if ($day_today != $day_last_check) {
                $cronUrl = JUri::root() . '?gurucron=1';
                $doc = JFactory::getDocument();
                $doc->addScriptDeclaration(";fetch('$cronUrl', {method: 'post'});");
            }
        }
    }
}

