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

	public function getWeeksOptions(){
		$this->autoRender = false ;
		$result['error'] = 1;
		$result['message'] = 'error occured!';
		//pr($this->request);
		
		$weeks = [];
		
		if(!empty($this->request->data['eoption']) && $this->request->data['eoption'] == 'setsel'){
            $eoption = array();
        }else{
			$eoption = array('0'=>__('All Weeks'));
		}
		
		$data = $this->request->data;
		if (!empty($data['period'])) {
			$year = date('Y', strtotime($data['period']));
			$month = date('n', strtotime($data['period']));
			
			$monthlyCalendar = $this->getMonthlyCalendar($year, $month);
			
			if(!empty($monthlyCalendar)){
				$weekrange = getWeekrange($monthlyCalendar);
			
				if(!empty($weekrange)){
					foreach($weekrange as $wk => $range){
						$weeks[$wk] = 'Week '.$wk.' ('. date('d M', strtotime($range['start'])).' to '.date('d M', strtotime($range['end'])).')';
					}
					$result['weeks'] = $eoption + $weeks;
					$result['error'] = 0;
					$result['message'] = '';
				}
			}else{
				$result['message'] = 'Calendar is not created.';
			}
		} 
		
		echo json_encode($result);
        exit;
	}
}