<?php

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


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

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);
global $mainframe ;
class Climainframe {
    
	function isAdmin() {
		 
		 return true ;
	}
}
$mainframe = new Climainframe();
chdir(dirname(__FILE__));
// Load system defines
chdir("../../");

if (file_exists(dirname(dirname(dirname(__FILE__))) . '/defines.php'))
{
	require_once dirname(dirname(dirname(__FILE__))) . '/defines.php';
}

if (!defined('_JDEFINES'))
{
	
	define('JPATH_BASE', getcwd());
	require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_BASE.'/includes/framework.php';
require_once JPATH_BASE.'/includes/application.php';
require_once JPATH_LIBRARIES . '/import.php';
require_once JPATH_LIBRARIES . '/cms.php';

// Force library to be in JError legacy mode
JError::$legacy = true;

// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
// Get the framework.
require_once JPATH_LIBRARIES . '/import.php';

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

// Force library to be in JError legacy mode
JError::$legacy = true;

// Import necessary classes not handled by the autoloaders
jimport('joomla.application.menu');
jimport('joomla.environment.uri');
jimport('joomla.event.dispatcher');
jimport('joomla.utilities.utility');
jimport('joomla.utilities.arrayhelper');

if (!defined('DTR_COM_COMPONENT')){

	define('DTR_COM_COMPONENT',"com_dtregister");

	define("DTR_COMPONENT",str_replace("com_","",'DT_COM_COMPONENT'));

}
define('JPATH_COMPONENT_ADMINISTRATOR',JPATH_ADMINISTRATOR."/components/com_dtregister") ;
		
//global $mainframe;
//$mainframe = JFactory::getApplication('site');
//$mainframe->initialise();
//include_once(JPATH_COMPONENT_ADMINISTRATOR.DS.'lib'.DS.'defines.php');
JLoader::register('DtrModel' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtmodel.php');

JLoader::register('DtrModeladmin' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dtmodeladmin.php');
JLoader::register('DtrTable' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'dttable.php');
JLoader::register('DT_Tagparser' , JPATH_SITE.DS.'administrator'.DS.'components'.DS.'com_dtregister'.DS.'lib'.DS.'tagparser.php');
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/user.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/config.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/reminder.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/paymentmethod.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/fieldtype.php");
include_once(JPATH_SITE."/administrator/components/com_dtregister/models/export.php");
$config = new DtregisterModelConfig(array());


		$config->setGlobal();
jimport('joomla.application.component.helper');
DtrTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.DTR_COM_COMPONENT.DS.'models');
class Updatecron extends JApplicationCli
{
	/**
	 * Entry point for the script
	 *
	 * @return  void
	 *
	 * @since   2.5
	 */
	public function execute()
	{
		
		$lang = JFactory::getLanguage();
		$extension = 'com_dtregister';
		$base_dir = JPATH_SITE;
		$language_tag = 'en-GB';
		$reload = true;
		$lang->load($extension, $base_dir, $language_tag, $reload);
		$this->_db = $db = JFactory::getDBO();
		$export = &  DtrTable::getInstance('Export','Table');
		$export->loadfirstRow();
		$export_events = $export->events ;
		$this->event_files = array();
		
		$export->config = $this->config;
		foreach($export_events as $event) {
		  $export->events = array($event);
		  $export->doexport_all();
		  $this->event_files[$event] = $export->filename ;
		 
		  $this->send($event);
		}
	    
		

		
	}
	
	function send($event_id) {
		global $DT_mailfrom,$DT_fromname,$thanksmsg,$admin_registrationemail,$admin_notification,$subject_admin_registrationemail, $admin_email_from_user;
		
		$event = & DtrTable::getInstance('event','Table');
		
	    $event->load($event_id);
		
		
		
		$adminemails=$event->email;
      $adminemails = explode(";",$adminemails);
	  $tmpPath = $this->config->get('tmp_path');
      $attachments = array($tmpPath."/".$this->event_files[$event_id]);
	  
	  $mailer = JFactory::getMailer();
	  $sender = array( 
		  $DT_mailfrom,
		  strip_tags(html_entity_decode($DT_fromname)) );
	   
	  $mailer->setSender($sender);
	  $mailer->setSubject("CSV EXPORT");
	  $mailer->setBody("CSV IN ATTACHMENT");
	  $mailer->addAttachment($tmpPath."/".$this->event_files[$event_id],'records.csv');
	   foreach($adminemails as $email) {
		    $mailer->ClearAddresses();
		    $this->out($email);
			
			$mailer->addRecipient($email);
			
			$mailer->Send();

// Optional file attached

	    // JUTility::sendMail( $DT_mailfrom, strip_tags(html_entity_decode($DT_fromname)),$email,"CSV EXPORT","CSV IN ATTACHMENT",1,null,null,$attachments);
		
	  }
	
	 
	 
		
	}
	
	
}

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