<?php
App::uses('AppController', 'Controller');
class CalendarsController extends AppController {

/**
 * Controller name
 *
 * @var string
 */
	var $events;

	public function index(){
          
			$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
			"August", "September", "October", "November", "December");    
		    
			if (!isset($this->request->params['named']["m"])) $this->request->params['named']["m"] = date("n");
			if (!isset($this->request->params['named']["y"])) $this->request->params['named']["y"] = date("Y");

			$currentMonth = $this->request->params['named']["m"];
			$currentYear = $this->request->params['named']["y"];

			$p_year = $currentYear;
			$n_year = $currentYear;
			$p_month = $currentMonth-1;
			$n_month = $currentMonth+1;

			if ($p_month == 0 ) {
				$p_month = 12;
				$p_year = $currentYear - 1;
			}

			if ($n_month == 13 ) {
				$n_month = 1;
				$n_year = $currentYear + 1;
			}
			$days=array('1'=>"Sunday",'2'=>"Monday",'3'=>"Tuesday",'4'=>"Wednesday",'5'=>"Thursday",'6'=>"Friday",'7'=>"Saturday");

			$timestamp = mktime(0,0,0,$currentMonth,1,$currentYear);
		    $maxday = date("t",$timestamp);
			$thismonth = getdate ($timestamp);
			$startday = $thismonth['wday'];
			if($currentMonth<10)
                 $currentMonth="0".$currentMonth;
			$conditions['date_format(Calendar.eventopendate,"%Y-%m")'] = $currentYear."-".$currentMonth;
			$uid=$this->Auth->user('id');
			$roleid=$this->Auth->user('role_id');
		   // $data= $this->Calendar->query("SELECT * FROM events where date_format(eventopendate,'%Y-%m')='".$currentYear."-".$currentMonth."'");
			$eventdata=$this->Calendar->find('list',array('fields'=>array('Calendar.id','Calendar.eventopendate'),'conditions'=>array('date_format(Calendar.eventopendate,"%Y-%m")'=>$currentYear."-".$currentMonth, 'OR'=>array('Calendar.uid'=>$uid, 'Calendar.role LIKE "%'.$roleid.'%"') ) ));
		
			$this->set(compact('eventdata','days','monthNames','p_year','n_year','p_month','n_month','currentMonth','currentYear','timestamp','maxday','thismonth','startday','roles'));
	}
    
	public function getrole($roleid){
		$resourcedata= array();
           switch($roleid) {

				case "1":
					$resourcedata['source']=1;
                    $resourcedata['destination']=11;
				    break;
				case "2":
					$resourcedata['source']=2;
                    $resourcedata['destination']=6;
				    break;
				
				case "3":
					$resourcedata['source']=3;
					$resourcedata['destination']=6;
					break;
				case "4":
					$resourcedata['source']=4;
					$resourcedata['destination']=6;
					break;
				case "5":
					$resourcedata['source']=5;
					$resourcedata['destination']=6;
					break;
				case "6":
						$resourcedata['source']=6;
						$resourcedata['destination']=6;
						break;
				case "7":
					$resourcedata['source']=7;
					$resourcedata['destination']=8;
					break;
				
				case "8":
					$resourcedata['source']=8;
					$resourcedata['destination']=8;
					break;
				
				case "9":
					$resourcedata['source']=9;
					$resourcedata['destination']=11;
					break;
				
				case "10":
					$resourcedata['source']=10;
					$resourcedata['destination']=11;
					break;
				case "11":
					$resourcedata['source']=11;
					$resourcedata['destination']=11;
					break;
			    default:
					break;
		   }
			
               return $resourcedata;
		   
	}


	public function viewevent(){
     
	  if($this->params['named']['data']){
			$roleid=$this->Auth->user('role_id');
			$conditions['md5(Calendar.eventopendate)'] = $this->params['named']['data'];
			$eventdata=$this->Calendar->find('all',array('conditions'=>array('md5(Calendar.eventopendate)'=>$this->params['named']['data'],'OR'=>array('Calendar.uid'=>$roleid,' find_in_set ('.$roleid.',Calendar.role) ' ))));

			$this->set(compact('eventdata'));
	  } else {
		   $this->Session->setFlash(__('No event found'), 'default', array());
	  }
	 
	}

	public function getdata(){
		//print_r($this->request->data);
		/*	$timestamp = mktime(0,0,0,$currentMonth,1,$currentYear);
			$maxday = date("t",$timestamp);*/
		 return new CakeResponse(array('body'=> json_encode($this->request->data['startyear']),'status'=>200));
		 exit;
	}

	public function add(){	
		$roleid=$this->Auth->user('role_id');
		$resourcedata = $this->getrole($roleid);
		$this->loadModel('Role');
		$roles = $this->Role->find('all',array('conditions'=>array('Role.id >='=>$resourcedata['source'], 'Role.id <='=>$resourcedata['destination'])));

	    if($this->request->data){
	
			 $this->request->data['Calendar']=$this->request->data['Calendar'];
		
			$eventstartdate = new DateTime($this->request->data["Calendar"]["eventopendate"]);
			$eventclosedate = new DateTime($this->request->data["Calendar"]["eventclosedate"]);

			if($eventstartdate > $eventclosedate) {
				echo 'Event start date should be less than event close date';
			     $this->Session->setFlash(__('Event start date should be less than event close date'), 'default', array());
			}
			else
			{   
				$this->request->data['Calendar']['role']=implode(",",$this->request->data["role"]);
				$stdate=explode(" ",$this->request->data["Calendar"]["eventopendate"]);
				$enddate=explode(" ",$this->request->data["Calendar"]["eventclosedate"]);
				$this->request->data['Calendar']['starttime']=$stdate['1'];
				$this->request->data['Calendar']['eventopendate']=$stdate['0'];
				$this->request->data['Calendar']['eventclosedate']=$enddate['0'];
				$this->request->data['Calendar']['endtime']=$enddate['1'];
				$this->request->data['Calendar']['status']='1';
				$this->request->data['Calendar']['uid']=$this->Auth->user('role_id');		   
				if($this->Calendar->save($this->request->data)) {
						 $this->Session->setFlash(__('Event has been submitted successfully'), 'default', array());
						
				} else {
					    echo 'Problem to save event';
						 $this->Session->setFlash(__('Problem to save event'), 'default', array());
				}
			}
		}
        $years=$this->genertaehourminute(2014,2030);
        $hours=$this->genertaehourminute(1,24);
		$minutes=$this->genertaehourminute(1,60);
		$monthNames = Array(1=>"January", 2=>"February", 3=>"March", 4=>"April", 5=>"May", 6=>"June", 7=>"July",
			8=>"August", 9=>"September", 10=>"October", 11=>"November", 12=>"December");         
		$this->set(compact('hours','minutes','years','monthNames','roles'));
	}

    public function genertaehourminute($starthm,$hm){
		$hmtime=array();
		for($i=$starthm;$i<=$hm;$i++){
          if($i<10)
             $hmtime[$i]="0".$i;
		  else
			  $hmtime[$i]=$i;
		}
		return $hmtime;
	}
 }