<?php
/**
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @copyright   Copyright (C) 2016-2024 CMExension
 * @license     GNU General Public License version 2 or later
 */

define('_JEXEC', 1);

defined('_JEXEC') or die;

// Make sure we're being called from the command line, not a web interface.
if (array_key_exists('REQUEST_METHOD', $_SERVER))
	die();

/**
 * This is a CRON script which should be called from the command-line, not the
 * web. For example something like:
 * /usr/bin/php /path/to/site/cli/com_cmgiftcard.php
 */

// Set flag that this is a parent file.
define('DS', DIRECTORY_SEPARATOR);

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

define('JPATH_BASE', dirname(dirname(dirname(dirname(__FILE__)))));
require_once JPATH_BASE . '/includes/defines.php';

// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';

// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';

// Load the configuration.
require_once JPATH_CONFIGURATION . '/configuration.php';

// Load required classes from the framework and the component.
jimport('joomla.database.table');
jimport('joomla.application.component.modellist');
require_once JPATH_BASE . '/administrator/components/com_cmgiftcard/helpers/cmgiftcard.php';
JModelLegacy::addIncludePath(JPATH_BASE . '/administrator/components/com_cmgiftcard/models/', 'CMGiftCardModel');

// JTable::addIncludePath(JPATH_BASE . '/administrator/components/com_cmdealaggregator/tables/');

/**
 * This script is a part of CM Gift Card component.
 * It helps send electronic gift cards to recipients.
 *
 * @package     CMGiftCard
 * @subpackage  com_cmgiftcard
 * @since       1.0.0
 */
class CMGiftCardCron extends JApplicationCli
{
	/**
	 * Entry point for the script
	 *
	 * @return  void
	 *
	 * @since   1.0.0
	 */
	public function doExecute()
	{
		$this->out('Script started');

		$cards = JModelLegacy::getInstance('Cards', 'CMGiftCardModel')->getElectronicCards();

		if (count($cards))
		{
			foreach ($cards as $card)
			{
				CMGiftCardHelper::sendGiftCardEmail($card);
			}
		}

		$this->out('Script stopped');
	}
}

JApplicationCli::getInstance('CMGiftCardCron')->execute();
