<?php

/**
* @version 2.7.18
* @package Joomla 1.5
* @subpackage DT Register
* @copyright Copyright (C) 2006 DTH Development
* @copyright contact dthdev@dthdevelopment.com
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/

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

class DtregisterControllerExport extends DtrController {

   var $name ='export';

   function __construct($config = array()){
		
		 $config = array('default_task'=>'eventlist');
		 parent::__construct($config);

		 $this->view = & $this->getView( 'export', 'html' );  

		 $this->view->setModel($this->getModel('export'),true);

		 $this->view->setModel($this->getModel('event'));
		 $this->view->setModel($this->getModel('field'));
		 $this->view->setModel($this->getModel('user'));

		 $this->registerDefaultTask("eventlist");

         JToolBarHelper::title(  JText::_( 'DT_CSV_EXPORT_UTILITY'), 'dtregister' );

	}
	
	function downloadfile(){
    	$tExport = $this->getModel('export')->table;
		
		$tExport->filename = JRequest::getVar('filename');
		
		$tExport->output();
	}
	
	function fieldlist(){
	   global $mainframe;
	   $tExport = $this->getModel('export')->table;
	   if(isset($_POST['formsubmit'])){
		  
		  $general_export_fields = JRequest::getVar('general_export_fields',array(),null,'array');
		  $individual_export_fields = JRequest::getVar('individual_export_fields',array(),null,'array');
		  $group_export_fields = JRequest::getVar('group_export_fields',array(),null,'array');

 $tExport->saveFields($general_export_fields,$individual_export_fields,$group_export_fields,JRequest::getVar('group_reg',0));
		  
		  $tExport->doexport($_REQUEST['datefrom'],$_REQUEST['dateto'],JRequest::getVar('page',0));
		  
	   }
	    JToolBarHelper::addNew('eventlist',JText::_( 'DT_BACK'));
        JToolBarHelper::spacer();
        JToolBarHelper::addNew('fieldlist',JText::_( 'DT_CREATE_EXPORT'));

	   $tExport->loadfirstRow();
	    
	   $this->view->assign('group_export_fields',$tExport->group_export_fields);
	   $this->view->assign('individual_export_fields',$tExport->individual_export_fields );
	   $this->view->assign('general_export_fields',$tExport->general_export_fields );
	   $this->view->assign('tExport',$tExport);
	   $this->display();	
	}
	
	function eventlist(){
		global $mainframe;
		$tExport = $this->getModel('export')->table;
		$my = &JFactory::getUser();
		if(isset($_POST['formsubmit']) && isset($_POST['export_events'])){
		   
			$events = JRequest::getVar('export_events',array(),null,'array');
			
			$tExport->saveEvents($events);
			$mainframe->redirect('index.php?option=com_dtregister&controller=export&task=fieldlist');
		}
		$tExport->loadfirstRow();
		JToolBarHelper::addNew('eventlist',JText::_( 'DT_NEXT'));

		$tEvent = $this->getModel('event')->table;
		$user = &JFactory::getUser();
		 $aros = $this->getModel( 'aro' )->table->findaroByUser($user);
		//	prd($aros);
		$aros = array($aros->id);
		$editaco = $this->getModel( 'aco' )->table->find(' controller="event" and task="edit" and `alias` = "DT_EDIT_OWN_EVENT" ');
		$user_condtion = "";
		
		if($editaco && isset($editaco[0])){
			if($aros) {
				foreach($aros as $key => $aro) {
					if($aro == 1){ // public group
					  unset($aros[$key]);
					}
				}
				
			}
			$editaco = $editaco[0];
			$permission = $this->getModel( 'permission' )->table->find('aro_id in('.implode(",",$aros).') and aco_id = '.$editaco->id);
			
			if($permission) {
				$user_condtion = " and user_id = ".$user->id;
			}
		}
		//$events = $tEvent->find('archive = 0 and user_id ='.$my->id , ' dtstart ');
		$events = $tEvent->find('archive = 0 '.$user_condtion , ' dtstart ');
		
		$this->view->assign('events',$events);
		
		$this->view->assign('expevents',$tExport->events);
		
		$this->display();
	}

}

?>