<?php

/**
* @version 2.8.14
* @package Joomla 2.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 DtregisterControllerEvent extends DtrController {

	var $name ='event';

	function __construct($config = array()){

		 parent::__construct($config);

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

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

		 $this->view->setModel($this->getModel( 'category' ),false);

		 $this->view->setModel($this->getModel( 'location' ),false);

		 $this->view->setModel($this->getModel( 'section' ),false);

		 $this->view->setModel($this->getModel( 'discountcode' ),false);

		 $this->view->setModel($this->getModel( 'file' ),false);

		 $this->view->setModel($this->getModel( 'field' ),false);
		 $this->view->setModel($this->getModel( 'user' ),false);
         
		 $this->view->setModel($this->getModel( 'payoption' ),false);
		 $this->registerTask( 'new', 'add' );

		 $this->registerDefaultTask("events");

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

	}

  function getcategory(){

	$database = &JFactory::getDBO();

	$sectionId = $_REQUEST['sectionId'];

	$options=array();

				$options[]=JHTML::_('select.option',"",JText::_('DT_SELECT_CATEGORY'));

				$query = "Select * from #__categories where section='".$sectionId ."'";

				$database->setQuery($query);

				$sections = $database->loadObjectList();

				if (is_array($sections)) 
				foreach($sections as $section){

					$options[]=JHTML::_('select.option',$section->id,$section->title);

				}

			echo $section_html = JHTML::_('select.genericlist', $options,"articlecategory","","value","text");

			?>

			<?php

			exit;

}

function getarticle(){

	$database = &JFactory::getDBO();

	$categoryId = $_REQUEST['categoryId'];

	$options=array();
               $user = JFactory::getUser();
				$options[]=JHTML::_('select.option',"","Select Article");
				$con = "";
                if(!$user->authorise('core.admin')) {
					$groups	= implode(',', $user->getAuthorisedViewLevels());
			         
				  $con = " and ".'c.access IN ('.$groups.')';
				}
				$query = "Select c.* from #__content c  where c.catid='".$categoryId ."' and `state` = '1' ".$con;

				$database->setQuery($query);

				$sections = $database->loadObjectList();
				
				if (is_array($sections)) 
				foreach($sections as $section){
					
                    if($user->authorise('core.edit', 'com_content.article.' . $section->id) != true) {
					 
					} else {
						 $options[]=JHTML::_('select.option',$section->id,$section->title);
					}

				}

			echo $section_html = JHTML::_('select.genericlist', $options,"data[event][article_id]","","value","text");

			?>

			<?php

			exit;

}

	function copy(){

      $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  JRequest::setVar('slabId',$cid[0]); 

	  $this->view->assign('copy',$cid[0]);

	  $this->add();

   }

  function publish(){

      $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  $event = $this->getModel('event')->table;

	   if (is_array($cid)) 
	   foreach($cid as $id){

	      $publish = 1;
		  
		  $query = "update #__dtregister_group_event set publish = ".$publish." where parent_id=".$id." or slabId=".$id; 
		  $event->rawquery($query);    

	  }

	  $this->events();	   

   }

   function unpublish(){

      $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  $event = $this->getModel('event')->table;

	   if (is_array($cid)) 
	   foreach($cid as $id){

	     $publish = 0;

		 $query = "update #__dtregister_group_event set publish = ".$publish." where parent_id=".$id." or slabId=".$id; 
		 $event->rawquery($query);    

	  }

	  $this->events();

   }

    function archive(){

      $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  $event = $this->getModel('event')->table;

	   if (is_array($cid)) 
	   foreach($cid as $id){

	     $archive = 1;

		 $query = "update #__dtregister_group_event set archive = ".$archive." where parent_id=".$id." or slabId=".$id; 
		 $event->rawquery($query);   

	  }
	  
	  $this->events();

   }

   function remove(){

      global $mainframe;

	  $event = $this->getModel('event')->table;

	  $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );
	 
	  if (is_array($cid)) 
	  foreach($cid as $id){

	      $event->load($id);

		  $event->delete($id);      

	  }

	 $mainframe->redirect( "index.php?option=".DTR_COM_COMPONENT."&controller=event" );

   }

   function unarchive(){

      $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  $event = $this->getModel('event')->table;

	   if (is_array($cid)) 
	   foreach($cid as $id){

		  $archive = 0;
 
		  $query = "update #__dtregister_group_event set archive = ".$archive." where parent_id=".$id." or slabId=".$id; 
		  $event->rawquery($query);   

	  }

	  $this->events();

   }

	function events(){

       $archive = JRequest::getVar('archive',-1);

		if($archive==1){

		   JToolBarHelper::unarchiveList('unarchive');

		}else{

		  JToolBarHelper::archiveList('archive');

		}

        JToolBarHelper::divider();

        JToolBarHelper::publishList('publish');

    	JToolBarHelper::divider();

		JToolBarHelper::unpublishList('unpublish');

        JToolBarHelper::divider();

        JToolBarHelper::editList();

	    JToolBarHelper::divider();
         if($this->view->checkpermission_create_event()) {
        JToolBarHelper::addNew();

        JToolBarHelper::divider();

	    JToolBarHelper::editList('copy', JText::_( 'DT_COPY_EVENT') );
		 }

	    JToolBarHelper::divider();

	    JToolBarHelper::divider();

        JToolBarHelper::deleteList();
		
		JToolBarHelper::divider();
		
       // JToolBarHelper::custom('doexport','upload','upload',JText::_( 'DT_GOOGLE_EXPORT'),false);
		
	    $this->view->setLayout('list');

		$this->view->display();

	}

	function edit(){

	   JToolBarHelper::apply('apply');
       JToolBarHelper::divider();
       JToolBarHelper::save();
       JToolBarHelper::divider();
       JToolBarHelper::cancel('cancel');
       $row = $this->getModel('event')->table;
       $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );
       JRequest::setVar('slabId',$cid[0]); 
       $slabId = JRequest::getVar('slabId',0);
	   
       $row->load($slabId);
	  
	   $layout = 'add';
       $this->view->assign('row',$row);
       $this->view->setLayout($layout);
       $this->view->display();
       
	}

	function add(){
        JToolBarHelper::apply('apply');
        JToolBarHelper::divider();
        JToolBarHelper::save();
	    JToolBarHelper::divider();
        JToolBarHelper::cancel('cancel');
        $row = $this->getModel('event')->table;

	    $slabId = JRequest::getVar('slabId',0);  

	    $row->load($slabId);

		$row->slabId = "";

	    $this->view->assign('row',$row);

	    $this->view->setLayout('add');

		$this->view->display();

	}

	function apply(){

	  global $mainframe;

	  $row = $this->getModel('event')->table;

	  if(false !== $row->save(JRequest::getVar('data'))){
		  
		  global $eventListOrder , $eventListOrder_sec;
		   $conf = DtrModel::getInstance('config','DtregisterModel');
		  
		     $conf->updateEventorder($eventListOrder,$eventListOrder_sec);
	  }else{
//prd("nothing");
		  $mainframe->redirect("index.php?option=com_dtregister&controller=event&task=edit&cid[]=".$row->slabId,$row->error);

	  }
//prd("nothing stop"); 
	  $mainframe->redirect("index.php?option=com_dtregister&controller=event&task=edit&cid[]=".$row->slabId);

	}

	function editconfirm(){
	    global $mainframe;
	   	$data = DT_Session::get('event.data');
		$files = DT_Session::get('event.event_files');
		JRequest::setVar('event_files', $files, 'files');
		DT_Session::clear('event');
		$row = $this->getModel('event')->table;
		if($row->save($data) !== false){
			
		}else{
			 $mainframe->redirect("index.php?option=com_dtregister&controller=event&task=edit&cid[]=".$row->slabId,$row->error);
		}
		$mainframe->redirect("index.php?option=com_dtregister&controller=event");
	}
	
	function editcancel(){
	   global $mainframe;
	   DT_Session::clear('event');
	   $mainframe->redirect("index.php?option=com_dtregister&controller=event");
	}

	function save(){
      
	  global $mainframe;

	  $row = $this->getModel('event')->table;

	  $data = JRequest::getVar('data');
	  
	  $error = false;
	  $error = false;
	  
	  if($data['event']['slabId']!=""){
		  
		   $row->load($data['event']['slabId']);
		  
		   if(!$row->validDateChange($data)){		

		   }
		   
		   if(!$row->comparerepeat($data) && $row->repetition !== false){

			   $this->error = JText::_("DT_REPETITIONS_NOT_VALID");

			   $error = true;

		   }
		  
	  }
	  
	  if($error){
		   DT_Session::set('event.data',$data);
		   $files = JRequest::getVar('event_files', null, 'files', 'array');
		   DT_Session::set('event.event_files',$files);
		   $this->view->setLayout('warning');
		   $this->view->assign('message',JText::_("DT_REPEAT_DELETE_WARNING"));
		   $this->view->display();
	  }else{
        
		if($row->save($data) !== false){
		   
           global $eventListOrder,$eventListOrder_sec;
		   $conf = DtrModel::getInstance('config','DtregisterModel');
		  
		     $conf->updateEventorder($eventListOrder,$eventListOrder_sec);
		  
		}else{
			$mainframe->redirect("index.php?option=com_dtregister&controller=event&task=edit&cid[]=".$row->slabId,$row->error);

		}
  
		 $mainframe->redirect("index.php?option=com_dtregister&controller=event");

	  }

	}

	function getjevent(){
	   global $now;
	   $jevt = $this->getModel('event')->tableJevt;

	   $data = $jevt->find(' evdet_id='.JRequest::getVar('eventId',0));

	   $rep = $jevt->parserule(JRequest::getVar('eventId',0));
       $params = JComponentHelper::getParams("com_jevents");
       $tz = $params->get("icaltimezonelive", "");
	   if ($tz != "" && is_callable("date_default_timezone_set"))
		{
			$timezone = date_default_timezone_get();
			//date_default_timezone_set($tz);
			$this->jeventstimezone = $timezone;
		}
	  ob_clean();

	 // prd(json_encode($repjson));

	   if($data){

	      $data = $data[0];
		  
		  $data->dtstart_date = strftime("%Y-%m-%d", $data->dtstart); 
		  $data->dtstart_time = strftime("%H:%M", $data->dtstart); 
		  $data->dtend_date = strftime("%Y-%m-%d", $data->dtend); 
		  $data->dtend_time = strftime("%H:%M", $data->dtend); 

	   if($rep)

	      $data->reprules = $jevt->getObjData();

	   echo json_encode($data);

	   }else{

	      $array = array('error'=>'No event');

		  echo json_encode($array);

	   }
if ($tz && is_callable("date_default_timezone_set"))
		{
			date_default_timezone_set($timezone);
		}
	   die;

	}

	  function orderup(){
       global $mainframe;
	   $row = $this->getModel('event')->table;

       $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	   $uid = $cid[0];

	   $row->load( (int)$uid );
	   $row->reorder(' parent_id=0');
	   $row->move( -1, 'parent_id=0' );
	  $mainframe->redirect( "index.php?option=".DTR_COM_COMPONENT."&controller=event" ); 

   }

     function orderdown(){
      global $mainframe;
	  $row = $this->getModel('event')->table;

      $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  $uid = $cid[0];

	  $row->load( (int)$uid );
	  $row->reorder(' parent_id=0');

	  $row->move( 1, 'parent_id=0' );
	  $mainframe->redirect( "index.php?option=".DTR_COM_COMPONENT."&controller=event" );
   }

	 function order($inc){

       global $mainframe;

	   $row = $this->getModel('event')->table;

       $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	   $uid = $cid[0];

	   $row->load( (int)$uid );

	   $row->reorder(' parent_id=0');

	   $row->move( $inc, 'parent_id=0' );
	   
	  // $row->updateOrder();

	//  $mainframe->redirect( "index.php?option=".DTR_COM_COMPONENT."&controller=event" );

   }

     function saveorder(){

      global $mainframe;

	  $row = $this->getModel('event')->table;

	  $cid = JRequest::getVar( 'cid', array(0), 'request', 'array' );

	  $order = JRequest::getVar( 'order', array(0), '', 'array' );

	  $total = count( $cid );
      // pr($order);
	  
	  if (is_array($order)) 
	  foreach( $order as $slabId => $ordering ) {

		$row->load( $slabId );

       if ($row->ordering != $ordering && $row->parent_id==0) {

			$row->ordering = $ordering;

           // prd($row->store());
           unset($row->repetition);
		   unset($row->timezone_show);
		   $row->store();
           
			if ($row->getError() !="") {

               JError::raiseError(500, $row->getError() );

				// prd($row);

				exit();

			}

		}

	 }

   $mainframe->redirect( "index.php?option=".DTR_COM_COMPONENT."&controller=event" );

   }
   
   function removeimage(){
	   
	    $event = $this->getModel('event')->table;
		
		$slabId = JRequest::getVar('eventId',0);
		
		$event->slabId = $slabId;
		$event->removeImage();	
	   
   }
     function loadfield() {
      
	  $type = JRequest::getVar('type');
      $row = $this->getModel('event')->table;
       
      $slabId = JRequest::getVar('slabId',0);
	   
      $row->load($slabId);
	   
      $this->view->assign('row',$row);
	  $this->view->setLayout($type);

	  $this->view->display();
   
   }
   function showtags() {
   
   		 $this->view->setLayout('showtags');

		$this->view->display();
   
   }

}