<?php
class Courses extends Controller{
    function __construct(){
		parent::Controller();
        $this->load->model('mevents');
        //loading library for current date display
        $this->load->library('dateclass');
	}
    
    function index(){
		Header("HTTP/1.1 301 Moved Permanently");
		Header("Location: http://www.archline.hu/interior/workshop/nappali");

		
		
		
		
        $main_events = $this->mevents->getMainEvents($this->muser->user['lang'], true);
        
        if(!$main_events){
            redirect('courses/no_main_event');
        }
        
        $data['lang'] = $this->muser->user['lang'];
		$data['title'] = hun() ? 'Kurzusok - ARCHLine.XP CAD Software' : 'Courses - ARCHLine.XP CAD Software';
		$data['tartalom'] = 'courses/index';
        $data['javascript'] = array('jquery_ui');
        $data['style'] = array('jquery.ui');
		$data['csik'] = array('home', 'courses');
        $data['error'] = false;
        
        foreach($main_events as $key => $main_event){
            $results = $this->mevents->getEventDatesByDay($main_event->event_main_id, true);
            $main_events[$key]->events = $results['events'];
            $main_events[$key]->times = $results['times'];
        }
        
        $data['main_events'] = $main_events;
        
        $do = $this->input->post('do');
        
        if(!$do){
            $this->load->view('site', $data);
        } else {
            $post = array();
            
            $post['nev'] = $this->input->post('nev');
            $post['cegnev'] = $this->input->post('cegnev');
            $post['varos'] = $this->input->post('varos');
            $post['irsz'] = $this->input->post('irsz');
            $post['cim'] = $this->input->post('cim');
            $post['tel'] = $this->input->post('tel');
            $post['email'] = $this->input->post('email');
            $post['comment'] = $this->input->post('comment');
            $post['event_id'] = $this->input->post('event_id');
            $post['event_main_id'] = $this->input->post('event_main_id');
            
            if($error = $this->mevents->eventFormCheck($post, $this->muser->user['lang'])){
                $data['error'] = implode('<br />', $error);
            } else {
                //insert registration into the database
                $result = $this->mevents->EventRegistration($post);
                
                //collecting main event details
                $main_event_details = $this->mevents->getMainEventDetails($post['event_main_id']);
                
                if($result == 'success'){
                    $event_details = $this->mevents->getEventDetailsByID($post['event_id']);
                    
                    //collect the event's and user's data for the emails
                    $mail_data = array(
                        'nev' => $post['nev'],
                        'cegnev' => $post['cegnev'],
                        'varos' => $post['varos'],
                        'irsz' => $post['irsz'],
                        'cim' => $post['cim'],
                        'tel' => $post['tel'],
                        'email' => $post['email'],
                        'comment' => $post['comment'],
                        'start' => date('Y-m-d H:i',$event_details->start_ts),
                        'end' => date('Y-m-d H:i',$event_details->end_ts),
                        'event_details' => $this->mevents->getEventDetailsByID($post['event_id']),
                        'main_event_details' => $this->mevents->getMainEventDetails($post['event_main_id'])
                    );
                    
                    //send an email to cadline
                    $this->load->library('email');
					$this->email->initialize();
					$this->email->from('info@cadline.hu', 'Cadline Weboldal');
					$this->email->subject($main_event_details->name.' Registration');
					$this->email->message($this->load->view('courses/mail/reg_success_to_cadline', $mail_data, true));
					
					$this->email->send('info@archlinexp.com', 'smtp');
					$this->email->clear();
                    
                    //send an email to the registered user
                    $page = $this->mpages->get_row($main_event_details->reg_success_mail_page);
                    
                    //change the users name constans in the page body
                    $page->page_body = str_ireplace('#reg_name#', $mail_data['nev'], $page->page_body);
                    
                    $this->load->library('email');
					$this->email->initialize();
					$this->email->from('info@archlinexp.com', 'ARCHLineXP.com');
					$this->email->subject($page->page_title);
					$this->email->message($page->page_body);
					
					$this->email->send($post['email'], 'smtp');
                    
                    //get the content of the success page
                    $data['cimlap'] = $this->mpages->get_row($main_event_details->reg_success_page);
                } elseif($result == 'limit') {
                    //get the content of the limit page
                    $data['cimlap'] = hun() ? $this->mpages->get_row(489) : $this->mpages->get_row(481);
                } elseif($result == 'multiple') {
                    //get the content of the multiple registration page
                    $data['cimlap'] = hun() ? $this->mpages->get_row(488) : $this->mpages->get_row(482);
                }
                
                $data['tartalom'] = 'courses/registration_success';
            }
            
            $this->load->view('site', $data);
        }
    }
    
    function no_main_event(){
        $data['lang'] = $this->muser->user['lang'];
		$data['title'] = hun() ? 'Bemutatók - ARCHLine.XP CAD Software' : 'Events - ARCHLine.XP CAD Software';
		$data['tartalom'] = 'courses/no_main_event';
        $data['page'] = hun() ? $this->mpages->get_row(493) : $this->mpages->get_row(494);
		if(hun())
		    $data['csik'] = array('Főoldal', 'Bemutatók');
        else
		   $data['csik'] = array('home', 'events');
        $this->load->view('site', $data);
    }
    
    function registrate(){
        redirect('courses');
        /*
        $event_main_id = $this->uri->segment(3);
        
        if(!$event_main_id || !$main_event_details = $this->mevents->getMainEventDetails($event_main_id)){
            redirect('events');
        }
        
        $data['lang'] = $this->muser->user['lang'];
		$data['title'] = hun() ? $main_event_details->name.' - Events - ARCHLine.XP CAD Software' : $main_event_details->name.' - Events - ARCHLine.XP CAD Software';
		$data['tartalom'] = 'events/registrate';
		$data['csik'] = array('home', 'events', $main_event_details->name);
        $data['error'] = false;
        
        $data['main_event'] = $main_event_details;
        
        $results = $this->mevents->getEventDatesByDay($event_main_id);
        
        $data['events'] = $results['events'];
        $data['times'] = $results['times'];
                
        $do = $this->input->post('do');
        
        if(!$do){
            $this->load->view('site', $data);
        } else {
            $post = array();
            
            $post['nev'] = $this->input->post('nev');
            $post['cegnev'] = $this->input->post('cegnev');
            $post['varos'] = $this->input->post('varos');
            $post['irsz'] = $this->input->post('irsz');
            $post['cim'] = $this->input->post('cim');
            $post['tel'] = $this->input->post('tel');
            $post['email'] = $this->input->post('email');
            $post['comment'] = $this->input->post('comment');
            $post['event_id'] = $this->input->post('event_id');
            $post['event_main_id'] = $event_main_id;
            
            if($error = $this->mevents->eventFormCheck($post, $this->muser->user['lang'])){
                $data['error'] = implode('<br />', $error);
            } else {
                //insert registration into the database
                $result = $this->mevents->EventRegistration($post);
                
                if($result == 'success'){
                    //get the content of the success page
                    $data['cimlap'] = $this->mpages->get_row(480);
                    
                    $event_details = $this->mevents->getEventDetailsByID($post['event_id']);
                    
                    //collect the event's and user's data for the emails
                    $mail_data = array(
                        'nev' => $post['nev'],
                        'cegnev' => $post['cegnev'],
                        'varos' => $post['varos'],
                        'irsz' => $post['irsz'],
                        'cim' => $post['cim'],
                        'tel' => $post['tel'],
                        'email' => $post['email'],
                        'comment' => $post['comment'],
                        'start' => date('Y-m-d H:i',$event_details->start_ts),
                        'end' => date('Y-m-d H:i',$event_details->end_ts),
                        'event_details' => $this->mevents->getEventDetailsByID($post['event_id'])
                    );
                    
                    //send an email to cadline
                    $this->load->library('email');
					$this->email->initialize();
					$this->email->from('info@cadline.hu', 'Cadline Weboldal');
					$this->email->subject($main_event_details->name.' Registration');
					$this->email->message($this->load->view('events/mail/reg_success_to_cadline', $mail_data, true));
					
					$this->email->send('info@archlinexp.com', 'smtp');
					//$this->email->send('sztikee@gmail.com', 'smtp');
                    //$this->email->send('atikenny@gmail.com', 'smtp');
					$this->email->clear();
                    
                    //send an email to the registered user
                    $page = $this->mpages->get_row($main_event_details->reg_success_mail_page);
                    
                    //change the users name constans in the page body
                    $page->page_body = str_ireplace('#reg_name#', $mail_data['nev'], $page->page_body);
                    
                    $this->load->library('email');
					$this->email->initialize();
					$this->email->from('info@archlinexp.com', 'ARCHLineXP.com');
					$this->email->subject($page->page_title);
					$this->email->message($page->page_body);
					
					$this->email->send($post['email'], 'smtp');
                } elseif($result == 'limit') {
                    //get the content of the limit page
                    $data['cimlap'] = $this->mpages->get_row(481);
                } elseif($result == 'multiple') {
                    //get the content of the multiple registration page
                    $data['cimlap'] = $this->mpages->get_row(482);
                }
                
                $data['tartalom'] = 'events/registration_success';
            }
            
            $this->load->view('site', $data);
        }
        */
    }
}