<?php
/*******************************************************************************/
//                          InstantCMS v1.8                                    //
//                      http://www.instantcms.ru/                              //
//                         component "Poster"                                  //
//                       written by Marat Fatikhov                             //
//                      (nickname Марат on a site)                             //
//                       E-mail: f-marat@mail.ru                               //
//                                                                             //
//                      LICENSED BY GNU/GPL v2                                 //
//                                                                             //
/********************************************************************************/

header('Content-Type: text/html; charset=windows-1251');
header('Cache-Control: no-cache');

session_start();

define("VALID_CMS", 1);
define('PATH', $_SERVER['DOCUMENT_ROOT']);

include(PATH.'/core/cms.php');

$inCore = cmsCore::getInstance();

$inCore->loadClass('db');
$inCore->loadClass('user');
$inCore->loadClass('config');
cmsCore::loadLanguage('lang');
cmsCore::loadLanguage('components/poster');

$inDB   = cmsDatabase::getInstance();
$inUser = cmsUser::getInstance();

$inUser->update();

$inCore->loadModel('poster');
$model = new cms_model_poster();
$inCore->includeFile('components/poster/includes/postercore.php');

//проверяем входные параметры
$get_date = $inCore->request('get_date', 'str' , '');
$item_id = $inCore->request('item_id', 'int', 0);
$type = $inCore->request('type', 'str' , '');
if(!$get_date || !$item_id || !$type){ return false; }
if($type != 'event' && $type != 'objekt'){ return false; }

$item = array();
$item = $model->getItemShort($item_id, $type, $inUser->is_admin);

//берем привязки
    $binds = array();
    $binds = $model->getBinds($item_id, $type);
    $is_date = array();
    $last_time = array();
    $max_day = date('d-m-Y');
    foreach($binds as $key=>&$bind){
        if(strtotime($bind['end_date']) > strtotime($max_day)){
          $max_day = $bind['end_date'];
        }
        $bind['time_list'] = $inCore->yamlToArray($bind['time_list']);
        //обрабатываем тайм-лист
        foreach($bind['time_list'] as $day=>$sessions){
             if($day == $get_date || $bind['all_days']){
                 $is_date["$key"]["{$day}"] = true;
                 foreach($sessions as $n=>$session){
                     $start_time = $get_date.'-'.$session['start_h'].'-'.$session['start_m'];
                     if( strtotime($start_time) < strtotime(date('d-m-Y-H-i'))){
                         $last_time["$key"]["{$day}"]["{$n}"] = true;
                     }
                 }
             }
        }
    }
    //готовим ajax-пагинацию по дням для расписания
    $days = array();
    $days = makeCal(date('d-m-Y'), $max_day);
    setlocale(LC_ALL, 'ru_RU.CP1251');
    $format_days = array();
    foreach($days as $key=>$day){
         $format_day = strftime('%d %B, %a', strtotime($day));
         //сегодня- завтра
         if($day == date('d-m-Y')){
           $format_day = $_LANG['TODAY'];
         }
         if($day == date('d-m-Y', strtotime('+1 day'))){
           $format_day = $_LANG['TOMORROW'];
         }
         $format_days["{$day}"] = $format_day;
    }
    
    $smarty = $inCore->initSmarty('components', 'com_poster_schedule.tpl');
    $smarty->assign('binds', $binds);
    $smarty->assign('item', $item);
    $smarty->assign('type', $type);
    $smarty->assign('LANG', $_LANG);
    $smarty->assign('is_date', $is_date);
    $smarty->assign('last_time', $last_time);
    $smarty->assign('format_days', $format_days);
    $smarty->assign('date', $get_date);
    $smarty->assign('path', PATH);
    $smarty->display('com_poster_schedule.tpl');

?>
