#error_reporting(E_ALL);
require_once('./global.php');
# show to authenticated users only
if($vbulletin->userinfo['userid'] ==0 ) {
print_no_permission();
}
// Make Nav Bar
$navbits = array('tv.php' //. $vbulletin->session->vars['sessionurl_q']
=> 'Телепрограмма');
# preferences save mode
if (!is_null($_GET['do']) && $_GET['do'] == 'saveprefs') {
#save preferences to db
$chan_pref_array = array();
foreach ($_POST as $key=>$val) {
if (preg_match('/^c_[0-9]+_([0-9]+)$/', $key, $matches) && $val == '1') {
$chan_pref_array["$matches[1]"] = 1;
}
}
$chan_pref_list='';
foreach($chan_pref_array as $key => $val) {
if ($chan_pref_list != '') {
$chan_pref_list .= ',';
}
$chan_pref_list .= $key;
}
$db->query_write("UPDATE " . TABLE_PREFIX . "userfield SET field11='$chan_pref_list' WHERE userid=".$vbulletin->userinfo['userid'] );
#redirect to TV program
$vbulletin->url = 'tv.php'; # . $vbulletin->session->vars['sessionurl'] . "u=$userinfo[userid]";
eval(print_standard_redirect('Настройки телепрограммы сохранены'));
exit;
}
# preferences mode
if(!is_null($_GET['do']) && $_GET['do'] == 'preferences') {
$sth = $db->query_read_slave("SELECT field11 FROM userfield WHERE userid=".$vbulletin->userinfo['userid'] );
$userpref = $db->fetch_array($sth);
if (!is_null($userpref)) {
$chan_list = sanitize_chan_list($userpref['field11']);
} else {
$chan_list = '';
}
$user_chan_list_array = split(',', $chan_list);
$tvgroups = make_group_chan_list ('tvprefchanbit', 'tvprefgroupbit', $user_chan_list_array);
$navbits['tv.php?do=preferences'] = 'Настройки телепрограммы';
$navbits = construct_navbits($navbits);
eval('$navbar = "' . fetch_template('navbar') . '";');
## apply template and output the result
eval('print_output("' . fetch_template('TVpref') . '");');
exit;
}
# TV program output mode
# parse input data
# show type: integer, 0 - today, 1 - tomorrow, 7 - for a week
$w = 2; # default
if(!is_null($_REQUEST['w'])) {
$w = intval($_REQUEST['w']);
}
if($w != 0 && $w != 1 && $w !=2 && $w != 7) {
$w = 0;
}
# channel list
$chan_list = '';
# if passed as form parameter - use this
if(!is_null($_REQUEST['chanid']) && intval($_REQUEST['chanid']) > 0) {
$chanid = intval($_REQUEST['chanid']);
$chan_list .= $chanid;
}
# if not use preferences
if($chan_list == '') {
$sth = $db->query_read_slave("SELECT field11 FROM userfield WHERE userid=".$vbulletin->userinfo['userid'] );
$userpref = $db->fetch_array($sth);
if (!is_null($userpref)) {
$chan_list = sanitize_chan_list($userpref['field11']);
}
}
# if none - show nothing
if($chan_list == '') {
$chan_list = '3';
}
# show types
$what_option_list =
"На сегодня "
."На завтра "
."На сегодня и завтра "
."На неделю "
;
# $tvprog is displayed by the TV template
$tvprog = '';
# loop selecting appropriate guides
# ['timezoneoffset'] contains user selected timezone
# while ['tzoffset'] contains offset already adjusted for DST
$user_tz_offset = is_null($vbulletin->userinfo['tzoffset']) ? 0 : $vbulletin->userinfo['tzoffset'];
if ($user_tz_offset == '') {
$user_tz_offset = 0;
}
if($chan_list != '') {
$sql =
"SELECT e.channel_id as channel_id, c.channel_name as channel_name,"
." DATE_FORMAT(e.start + INTERVAL $user_tz_offset HOUR,'%Y-%m-%d') AS local_date,"
." DATE_FORMAT(e.start + INTERVAL $user_tz_offset HOUR,'%H:%i') AS local_time,"
." e.event AS event"
." FROM tv.events e, tv.channels c"
." WHERE e.channel_id = c.channel_id"
." AND c.channel_id IN ($chan_list)";
if($w == 1) {
$sql .= " AND e.start >= CURDATE() - INTERVAL $user_tz_offset HOUR + INTERVAL 1 DAY"
." AND e.start < CURDATE() - INTERVAL $user_tz_offset HOUR + INTERVAL 2 DAY";
} elseif ($w ==2) {
$sql .= " AND e.start >= CURDATE() - INTERVAL $user_tz_offset HOUR"
." AND e.start < CURDATE() - INTERVAL $user_tz_offset HOUR + INTERVAL 2 DAY";
} elseif ($w ==7) {
$sql .= " AND e.start >= CURDATE() - INTERVAL $user_tz_offset HOUR"
." AND e.start < CURDATE() - INTERVAL $user_tz_offset HOUR + INTERVAL 7 DAY";
} else {
$sql .= " AND e.start >= CURDATE() - INTERVAL $user_tz_offset HOUR"
." AND e.start < CURDATE() - INTERVAL $user_tz_offset HOUR + INTERVAL 1 DAY";
}
$sql .= " ORDER BY e.channel_id, e.start";
$sth = $db->query_read_slave($sql);
$pending_data = 0;
$prev_chan_id = '';
$prev_chan_name = '';
$prev_date = '';
$event_data = array();
while ($evdata = $db->fetch_array($sth)) {
if( ($prev_chan_id != '' && $prev_date != '')
&& ($prev_chan_id != $evdata['channel_id'] || $prev_date != $evdata['local_date'])) {
# flush previous events
$tv_channel_name = $prev_chan_name;
$tv_date = $prev_date;
$col_len = ceil(count($event_data)/2.0);
$tv_col1 = implode("\n", array_slice($event_data, 0, $col_len));
$tv_col2 = implode("\n", array_slice($event_data, $col_len, $col_len));
// $tv_col3 = implode("\n", array_slice($event_data, $col_len*2, $col_len));
eval('$tvprog .= "' . fetch_template('tvlistbit') . '";');
$event_data = array();
$pending_data = 0;
}
$ev_time = $evdata['local_time'];
$ev_name = $evdata['event'];
# apply tveventbit template to format event time and name
eval('$event_data[] = "' . fetch_template('tveventbit') . '";');
$prev_chan_id = $evdata['channel_id'];
$prev_chan_name = $evdata['channel_name'];
$prev_date = $evdata['local_date'];
$pending_data++;
}
if($pending_data) {
# flush previous events
$tv_channel_name = $prev_chan_name;
$tv_date = $prev_date;
$col_len = ceil(count($event_data)/2.0);
$tv_col1 = implode("\n", array_slice($event_data, 0, $col_len));
$tv_col2 = implode("\n", array_slice($event_data, $col_len, $col_len));
// $tv_col3 = implode("\n", array_slice($event_data, $col_len*2, $col_len));
eval('$tvprog .= "' . fetch_template('tvlistbit') . '";');
}
unset($event_data);
unset($sth);
}
// Make list of grouped channels links
$tvgroups = make_group_chan_list ('tvchanbit', 'tvgroupbit', array());
// Make Nav Bar
$navbits = construct_navbits($navbits);
eval('$navbar = "' . fetch_template('navbar') . '";');
## apply TV template and output the result
eval('print_output("' . fetch_template('TV') . '");');
function sanitize_chan_list($l) {
$s = split(',',$l);
$l = '';
foreach ($s as $key=>$val) {
$f = intval($val);
if ($f > 0) {
if ($l != '') {
$l .= ',';
}
$l .= $f;
}
}
unset($s);
return($l);
}
function make_group_chan_list ($chan_template, $group_template, $user_prefs_array) {
global $db;
global $w;
# this is the return value
$tvgroups='';
$grouparray = array();
$sth = $db->query_read_slave("SELECT group_id, group_name FROM tv.groups ORDER BY group_name");
while ($groupdata = $db->fetch_array($sth)) {
$group_name = $groupdata['group_name'];
$group_id = $groupdata['group_id'];
$grouparray[] = array($group_id, $group_name);
}
unset($sth);
$grouparray[] = array(0, 'Остальные');
foreach($grouparray as $key=>$val) {
$group_id = $val[0];
$group_name = $val[1];
$tv_rows = '';
if ($group_id == 0) {
$sth = $db->query_read_slave(
"SELECT channel_id, channel_name, channel_comment"
." FROM tv.channels"
." WHERE channel_id not in (SELECT channel_id FROM tv.channel_group)"
." AND active = 1"
." ORDER BY channel_name" );
} else {
$sth = $db->query_read_slave(
"SELECT c.channel_id AS channel_id, c.channel_name AS channel_name, c.channel_comment as channel_comment"
." FROM tv.channels c, tv.channel_group cg"
." WHERE c.channel_id = cg.channel_id"
." AND cg.group_id = $group_id AND c.active = 1"
." ORDER BY c.channel_name" );
}
while($chandata = $db->fetch_array($sth)) {
$channel_id = $chandata['channel_id'];
$channel_name = $chandata['channel_name'];
$channel_comment = $chandata['channel_comment'];
$control_num = $group_id . '_' . $channel_id;
$is_checked = in_array($channel_id, $user_prefs_array) ? 'checked' : '';
# apply tvprefchanbit template to format channel name and id
eval('$tv_rows .= "' . fetch_template($chan_template) . '";');
}
unset($sth);
if($tv_rows != '') { // if there are channels in the group
# apply tvprefgroupbit and add to $tvgroups
eval('$tvgroups .= "' . fetch_template($group_template) . '";');
}
unset($chan_pref_list);
}
return $tvgroups;
}
?>