<?php 
define("CMS", "korCMS");
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED ^ E_STRICT); 
include("../../config/odyssey.ini.php");	// access from calendar module folder
require_once("../../library/db.php");
require_once("../../library/messages.php");
require_once("../../library/language.php");
require_once("../../library/content.php");
require_once("../../library/parameters.php");
require_once("../../library/system.php");
require_once("../../library/sef.php");
$db = new db();
$content = new content();
$parameters = new parameters();
$language = new language();
$system = new system();
$sef = new sef();

function getBase(){
	global $domain_name;
	if($_SERVER['SERVER_NAME'] == "localhost") return str_replace("localhost", NULL, $domain_name)."/";	
	global $domainPath;
	$www = preg_match('/^www/', $_SERVER['SERVER_NAME']) ? 'www.' : null;
	$parts = explode("/", $_SERVER["PHP_SELF"]);
	array_shift($parts); // the first element will be empty to we get rid of it
	array_pop($parts); // get rid of script name
	$fPath =implode("/", $parts);
	$newPath = $domainPath.$fPath;
	if(substr($newPath, -1) != "/") $newPath.="/";
	return $newPath; // for sef urls, must be a base tag in html HEAD with this value
}

function getFPath(){
	$parts = explode("/", $_SERVER["PHP_SELF"]);
	array_shift($parts); // the first element will be empty to we get rid of it
	array_pop($parts); // get rid of script name
	$fPath =implode("/", $parts);
	if(substr($fPath, -1) != "/") $fPath.="/";
	$fPath = ($fPath == "/") ? null : $fPath;	
	return $fPath;	
}
$db->query("SET NAMES 'utf8mb4'");
if(!$_REQUEST['langID']){
  $language = new language();
  $langID = $language->getCurrentLanguage();
}else{
  $langID = $_REQUEST['langID'];
}
if(!$_REQUEST["startframe"]){ 
	echo("done=1");
	exit; 
}
header('Content-type: text/html; charset=utf-8');



if($_SESSION["calendar_password"]) $loged=($_SESSION["calendar_password"]) ? 1 : 0;



class Calendar{
	//Constructor
	function Calendar()    {    }
	
	function getDaysInMonth($month, $year){
		if($month < 1 || $month > 12) return 0;
		$d = $this->daysInMonth[$month - 1];
		if($month == 2){
			if($year%4 == 0){
				if($year%100 == 0){
					if($year%400 == 0) $d = 29;
				}else{
					$d = 29;
				}
			}
		}
		return $d;
	}
	
	
	function adjustDate($month, $year){
		$a = array();  
		$a[0] = $month;
		$a[1] = $year;
		while ($a[0] > 12){
			$a[0] -= 12;
			$a[1]++;
		}
		while ($a[0] <= 0){
			$a[0] += 12;
			$a[1]--;
		}
		return $a;
	}
	
	
	function getMonthData($m, $y, $showYear = 1){
		$s = "";
		$a = $this->adjustDate($m, $y);
		$month = $a[0];
		$year = $a[1];        
		$monthName = $this->monthNames[$month - 1];
		$daysInMonth = $this->getDaysInMonth($month, $year);
		$date = getdate(gmmktime(12, 0, 0, $month, 1, $year));
		$first = $date["wday"];
		$firstIndex = ($first) ? $first : 7;
		$rest = $daysInMonth - (7-($firstIndex-1));
		$lastIndex = $rest % 7;
		$lastIndex = ($lastIndex)?$lastIndex : 7;
		$firstrowdays = (7-$firstIndex)+1;
		$rows =  ceil(($daysInMonth - $firstrowdays)/7) +1;
		$pmonth =$month-1;
		$pyear = $year;
		if($pmonth==0){
			$pmonth = 12;
			$pyear -=1;
		}
		$previousMonth = $this->getDaysInMonth($pmonth, $pyear);
		$queryString="firstIndex=".$firstIndex."&lastIndex=".$lastIndex."&firstrowdays=".$firstrowdays."&rows=".$rows."&daysInMonth=".$daysInMonth."&previousMonth=".$previousMonth;
		return $queryString;  
	}
	
	var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	function getDayInfo($day, $month, $year){
		global $langID;
		global $db;
		global $content;
		global $sef;
		$query = "select events.id as id, events.day as day,  content_events.title as title, content_events.url as url 
		from events, content_events 
		where events.active=1 
		and events.day='$day' 
		and events.month='$month' 
		and events.year='$year' 
		and content_events.mainID=events.id 
		and content_events.langID='$langID' ";
		
		$results = $db->query($query);
		
		$events = $db->getNumRows($results);
		if($events){
			$ret="";
			$counter=0;
			$eventsData = $db->getRecords($results, false);
			foreach($eventsData as $row){
				$a = explode("index.php?section=", $row['url']);
				$b= explode("&view=", $a[1]);
				$section = $b[0];
				$a = explode("&id=", $row['url']);
				$b = explode("&langID=", $a[1]);
				$id = $b[0];
				if($content->isActiveRecord($section, $id)){
					$counter++;
					$ret.="&id".$counter."=".$row[id]."&title".$counter."=".urlencode($row[title])."&url".$counter."=".urlencode($sef->url($row[url], false));
				}else{
					$events--;
				}
			}
			$ret.="&numOfEvents=".$events;
			return $ret;
		}else{
			$ret.="&numOfEvents=0";
			return $ret;
		}
	}
	
	function getMonthEvents($month, $year){
		global $db;
		global $langID;
		global $content;
		$query = "select distinct events.day, content_events.url 
							from events, content_events where 
							events.month='$month' 
							and events.year='$year' 
							and content_events.mainID=events.id 
							and content_events.langID='$langID' 
							and events.active=1";
		$results = $db->query($query);
		$eventsDates = "";
		$temp="";
		
		while($row=$db->getRecord($results, false)){
			$eventsDates[]= $row['day'];
			$urls[] = $row['url'];
		}
		for($n=1; $n <=31; $n++){
			$temp[$n-1] = 0;
			for($i=0; $i <= count($eventsDates); $i++){
				if($eventsDates[$i] == $n){
					$a = explode("index.php?section=", $urls[$i]);
					$b= explode("&view=", $a[1]);
					$section = $b[0];
					$a = explode("&id=", $urls[$i]);
					$b = explode("&langID=", $a[1]);
					$id = $b[0];
					if($content->isActiveRecord($section, $id)){	
						$temp[$n-1] = $eventsDates[$i];
					}else{
						$temp[$n-1] = 0;	
					}
				}
			}
		}
		$eventsDates = implode("|", $temp);
		$pmonth =$month-1;
		$pyear = $year;
		if($pmonth==0){
			$pmonth = 12;
			$pyear -=1;
		}
		$query = "select distinct day from events where month='$pmonth' and year='$pyear' and active=1";
		$results = $db->query($query);
		$peventsDates = "";
		$temp="";
		while($row=$db->getRecord($results, false)){
			$peventsDates[]= $row[day];
		}
		for($n=1; $n <=31; $n++){
			$temp[$n-1] = 0;
			for($i=0; $i <= count($peventsDates); $i++){
				if($peventsDates[$i] == $n) $temp[$n-1] = $peventsDates[$i];
			}
		}
		$peventsDates = implode("|", $temp);
		
		
		$nmonth =$month+1;
		$nyear = $year;
		if($nmonth==13){
			$nmonth = 1;
			$nyear +=1;
		}
		$query = "select distinct day from events where month='$nmonth' and year='$nyear' and active=1";
		$results = $db->query($query);
		$neventsDates = "";
		$temp="";
		while($row=$db->getRecord($results, false)){
			$neventsDates[]= $row[day];
		}
		for($n=1; $n <=31; $n++){
			$temp[$n-1] = 0;
			for($i=0; $i <= count($neventsDates); $i++){
				if($neventsDates[$i] == $n) $temp[$n-1] = $neventsDates[$i];
			}
		}
		$neventsDates = implode("|", $temp);
		$ret="&eventsDates=".$eventsDates."&peventsDates=".$peventsDates."&neventsDates=".$neventsDates."&done=1";
		return $ret;
	}
	
} // end of class
$day = (int)($_REQUEST['selectedDay']) ? $_REQUEST['selectedDay'] : date("j");
$month = (int)$_REQUEST['currentMonth'];
$year = (int)$_REQUEST['currentYear'];
$d = getdate(time()); 
if($month == "") $month = $d["mon"];
if($year == "") $year = $d["year"];
$cal = new Calendar; 
if($_REQUEST["mode"]!="events") echo $cal->getMonthData($month, $year);
echo $cal->getDayInfo($day, $month, $year);
echo $cal->getMonthEvents($month, $year);
echo("&loged=".$loged);
?>