<?
if(!defined("B_PROLOG_INCLUDED")||B_PROLOG_INCLUDED!==true)die();

define('WORK_DIR',dirname(__FILE__));
require_once(WORK_DIR. '/calendar.class.php');

$arParams["USE_PERMISSIONS"] = $arParams["USE_PERMISSIONS"]=="Y";
if(!is_array($arParams["GROUP_PERMISSIONS"]))
	$arParams["GROUP_PERMISSIONS"] = array(1);
	
if (trim($arParams["NO_CNAGE_DAY"]) !="Y") {$arParams["NO_CNAGE_DAY"]="N";}

$bUSER_HAVE_ACCESS = !$arParams["USE_PERMISSIONS"];
if($arParams["USE_PERMISSIONS"] && isset($GLOBALS["USER"]) && is_object($GLOBALS["USER"]))
{
	$arUserGroupArray = $GLOBALS["USER"]->GetUserGroupArray();
	foreach($arParams["GROUP_PERMISSIONS"] as $PERM)
	{
		if(in_array($PERM, $arUserGroupArray))
		{
			$bUSER_HAVE_ACCESS = true;
			break;
		}
	}
}
if(!$bUSER_HAVE_ACCESS)
{
	ShowError(GetMessage("T_NEWS_DETAIL_PERM_DEN"));
	return 0;
}

if($arParams["SHOW_WORKFLOW"] || $this->StartResultCache(false, array(($arParams["CACHE_GROUPS"]==="N"? false: $USER->GetGroups()),$bUSER_HAVE_ACCESS, $arParams["CALENDAR_TYPE"],date ('d.m.Y'))))
{
	if(!CModule::IncludeModule("iblock"))
	{
		$this->AbortResultCache();
		ShowError(GetMessage("IBLOCK_MODULE_NOT_INSTALLED"));
		return;
	}
	if($arParams["CALENDAR_TYPE"]=="year")
	{
		$startMonth=1;
		$endMonth=12;
	}	
	else
	{
		$startMonth=(int)date("m");
		$endMonth=$startMonth;
	}
	$arResult['MONTHS']=array();
	$c = new FranchiseCalendar();
	$c->lng = LANGUAGE_ID;
	if($arParams["SHOW_TITLE"]=="Y")
		$c->showTitle=true;
	else
		$c->showTitle=false;	
	if($arParams["SHOW_YEAR"]=="Y")
		$c->showYear=true; 
	else
		$c->showYear=false;
	if($arParams["ADD_MONTH_CHANGE"]=="Y" && $arParams["CALENDAR_TYPE"]=="year")
		$c->addMonthChange=true; 
	else
		$c->addMonthChange=false;
		
	$c->change_days=$arParams["NO_CNAGE_DAY"];
	for($i=$startMonth;$i<=$endMonth;$i++)
	{		
		$date=explode("-",date("d-m-Y",mktime(0,0,0,$i,1,date("Y"))));
		
		$arFilter = Array(
		   "IBLOCK_ID"=>$arParams["IBLOCK_ID"], 
		   "ACTIVE"=>"Y",   
		    array(
		        "LOGIC" => "OR",
		        array(
		        		">=DATE_ACTIVE_FROM"=>date($DB->DateFormatToPHP(CLang::GetDateFormat("SHORT")), mktime(0,0,0,$i,1,date("Y"))),
		        		"<DATE_ACTIVE_FROM"=>date($DB->DateFormatToPHP(CLang::GetDateFormat("SHORT")), mktime(0,0,0,$i+1,1,date("Y"))),
		        	),
		        array("PROPERTY_YEARLY_VALUE" => GetMessage("YES")),
		    ),
		);		
		$res = CIBlockElement::GetList(Array("DATE_ACTIVE_FROM"=>"ASC"), $arFilter,false,false,Array("ID","NAME","PREVIEW_PICTURE","DATE_ACTIVE_FROM","DATE_ACTIVE_TO","PROPERTY_HOLIDAY","PROPERTY_SOKR","PROPERTY_WORK","PROPERTY_YEARLY"));
		while($ar=$res->GetNext())
		{			
			$day=explode(".",$ar['DATE_ACTIVE_FROM']);
			
			if (IntVal($day[1])!=IntVal($date[1]))
				continue;
					
			if ($ar['PROPERTY_HOLIDAY_VALUE']!='')
				$c->addDateHoliday($day[0], $day[1], $date[2], $ar["NAME"]);
				
			if ($ar['PROPERTY_SOKR_VALUE']!='')
				$c->addDatePreHoliday($day[0], $day[1], $date[2], $ar["NAME"]);
				
			if ($ar['PROPERTY_WORK_VALUE']!='')
				$c->addDateWorkday($day[0], $day[1], $date[2], $ar["NAME"]);
		}
		$c->addToday(IntVal(date('d')), IntVal(date("m")),IntVal(date("Y")), GetMessage("TODAY"));		
		$c->genMonth(false, $date[1], $date[2]);		
	}
	$arResult['MONTHS']=$c->getMonthsRes();
	$this->IncludeComponentTemplate();
}

?>