add calendar events
This commit is contained in:
104
src/calend-block.php
Normal file
104
src/calend-block.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
function calend_block($month, $year, $script=false){
|
||||
|
||||
$args = array(
|
||||
'numberposts' => -1,
|
||||
'category' => 61, // 61 - мероприятия
|
||||
'orderby' => 'date',
|
||||
'order' => 'ASC',
|
||||
|
||||
'date_query' => array(
|
||||
array(
|
||||
'after' => array( // после этой даты
|
||||
'year' => $year,
|
||||
'month' => $month,
|
||||
'day' => 1,
|
||||
),
|
||||
'before' => array( // до этой даты
|
||||
'year' => $year,
|
||||
'month' => $month,
|
||||
'day' => 31,
|
||||
),
|
||||
// 'inclusive'=> true
|
||||
)
|
||||
),
|
||||
|
||||
'post_type' => 'post',
|
||||
'post_status' => 'publish, future',
|
||||
'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
|
||||
);
|
||||
|
||||
$posts = get_posts($args);
|
||||
|
||||
$events = [];
|
||||
|
||||
foreach ($posts as $post){
|
||||
$key = date('d.m.Y', strtotime($post->post_date));
|
||||
$events[$key]['text'] = $post->post_title;
|
||||
$events[$key]['link'] = get_permalink($post->ID);
|
||||
|
||||
}?>
|
||||
|
||||
<div id="event-calendar">
|
||||
<?=Calendar::getMonth(date($month), date($year), $events);?>
|
||||
</div>
|
||||
|
||||
<?php if ($script){?>
|
||||
<script>
|
||||
|
||||
var calend_link_base = "<?=get_site_url()?>/wp-content/themes/vij/src/eventcalendar.php";
|
||||
var calend_curr_year = "<?=$year?>";
|
||||
var calend_curr_month = "<?=$month?>";
|
||||
|
||||
|
||||
function calend_prev_month(){
|
||||
load_calendar_month(type = 'prev');
|
||||
}
|
||||
|
||||
function calend_next_month(){
|
||||
load_calendar_month(type = 'next');
|
||||
}
|
||||
|
||||
|
||||
function load_calendar_month(type = 'next'){
|
||||
|
||||
if (type == 'next'){
|
||||
|
||||
if (calend_curr_month == 12 ){
|
||||
calend_curr_month = 1;
|
||||
calend_curr_year = parseInt(calend_curr_year) + 1;
|
||||
}
|
||||
else {
|
||||
calend_curr_month = parseInt(calend_curr_month) + 1;
|
||||
}
|
||||
|
||||
}
|
||||
else if (type == 'prev'){
|
||||
|
||||
if (calend_curr_month == 1 ){
|
||||
calend_curr_month = 12;
|
||||
calend_curr_year = parseInt(calend_curr_year) - 1;
|
||||
}
|
||||
else {
|
||||
calend_curr_month = parseInt(calend_curr_month) - 1;
|
||||
}
|
||||
}
|
||||
|
||||
$currlink = calend_link_base + '?year=' + calend_curr_year + '&month=' + calend_curr_month;
|
||||
|
||||
fetch($currlink).then(function (response) {
|
||||
return response.text();
|
||||
}).then(function (html) {
|
||||
document.getElementById('event-calendar').innerHTML = html;
|
||||
}).catch(function (err) {
|
||||
console.warn('Something went wrong.', err);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<?}
|
||||
|
||||
}
|
||||
158
src/calendar.php
Normal file
158
src/calendar.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
|
||||
class Calendar
|
||||
{
|
||||
|
||||
/**
|
||||
* Вывод календаря на один месяц.
|
||||
*/
|
||||
public static function getMonth($month, $year, $events = array())
|
||||
{
|
||||
$months = array(
|
||||
1 => 'Январь',
|
||||
2 => 'Февраль',
|
||||
3 => 'Март',
|
||||
4 => 'Апрель',
|
||||
5 => 'Май',
|
||||
6 => 'Июнь',
|
||||
7 => 'Июль',
|
||||
8 => 'Август',
|
||||
9 => 'Сентябрь',
|
||||
10 => 'Октябрь',
|
||||
11 => 'Ноябрь',
|
||||
12 => 'Декабрь'
|
||||
);
|
||||
|
||||
$month = intval($month);
|
||||
|
||||
|
||||
$out = '
|
||||
<div id="calendar-item">
|
||||
<div class="calendar-title">Календарь событий</div>
|
||||
<div class="calendar-head">
|
||||
<div class="calendar-prev"><a id="calendar-arrow-prev" onclick="load_calendar_month('."'prev'".');"><</a></div>
|
||||
<div class="calendar-head-text">' . $months[$month] . ' ' . $year . '</div>
|
||||
<div class="calendar-next"><a id="calendar-arrow-next" onclick="load_calendar_month('."'next'".');">></a></div>
|
||||
</div>
|
||||
<table>
|
||||
<tr>
|
||||
<th>Пн</th>
|
||||
<th>Вт</th>
|
||||
<th>Ср</th>
|
||||
<th>Чт</th>
|
||||
<th>Пт</th>
|
||||
<th>Сб</th>
|
||||
<th>Вс</th>
|
||||
</tr>';
|
||||
|
||||
$day_week = date('N', mktime(0, 0, 0, $month, 1, $year));
|
||||
$day_week--;
|
||||
|
||||
$out.= '<tr>';
|
||||
|
||||
for ($x = 0; $x < $day_week; $x++) {
|
||||
$out.= '<td></td>';
|
||||
}
|
||||
|
||||
$days_counter = 0;
|
||||
$days_month = date('t', mktime(0, 0, 0, $month, 1, $year));
|
||||
|
||||
for ($day = 1; $day <= $days_month; $day++) {
|
||||
if (date('j.n.Y') == $day . '.' . $month . '.' . $year) {
|
||||
$class = 'today';
|
||||
} elseif (time() > strtotime($day . '.' . $month . '.' . $year)) {
|
||||
$class = 'last';
|
||||
} else {
|
||||
$class = '';
|
||||
}
|
||||
|
||||
$event_show = false;
|
||||
$event_text = '';
|
||||
$event_link = '';
|
||||
if (!empty($events)) {
|
||||
foreach ($events as $date => $text) {
|
||||
$date = explode('.', $date);
|
||||
if (count($date) == 3) {
|
||||
$y = explode(' ', $date[2]);
|
||||
if (count($y) == 2) {
|
||||
$date[2] = $y[0];
|
||||
}
|
||||
|
||||
if ($day == intval($date[0]) && $month == intval($date[1]) && $year == $date[2]) {
|
||||
$event_show = true;
|
||||
$event_text = $text['text'];
|
||||
$event_link = $text['link'];
|
||||
}
|
||||
} elseif (count($date) == 2) {
|
||||
if ($day == intval($date[0]) && $month == intval($date[1])) {
|
||||
$event_show = true;
|
||||
$event_text = $text['text'];
|
||||
$event_link = $text['link'];
|
||||
}
|
||||
} elseif ($day == intval($date[0])) {
|
||||
$event_show = true;
|
||||
$event_text = $text['text'];
|
||||
$event_link = $text['link'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($event_show) {
|
||||
|
||||
$out.= '<td class="calendar-day ' . $class . ' calendar-event">';
|
||||
if (!empty($event_text)) {
|
||||
$out.= '<div class="calendar-popup" ><a href="'.$event_link.'" title="'.$event_text.'">'.$day.'</a></div>';
|
||||
}
|
||||
$out.= '</td>';
|
||||
} else {
|
||||
$out.= '<td class="calendar-day ' . $class . '">' . $day . '</td>';
|
||||
}
|
||||
|
||||
if ($day_week == 6) {
|
||||
$out.= '</tr>';
|
||||
if (($days_counter + 1) != $days_month) {
|
||||
$out.= '<tr>';
|
||||
}
|
||||
$day_week = -1;
|
||||
}
|
||||
|
||||
$day_week++;
|
||||
$days_counter++;
|
||||
}
|
||||
|
||||
$out .= '</tr></table></div>';
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Вывод календаря на несколько месяцев.
|
||||
*/
|
||||
public static function getInterval($start, $end, $events = array())
|
||||
{
|
||||
$curent = explode('.', $start);
|
||||
$curent[0] = intval($curent[0]);
|
||||
|
||||
$end = explode('.', $end);
|
||||
$end[0] = intval($end[0]);
|
||||
|
||||
$begin = true;
|
||||
$out = '<div class="calendar-wrp">';
|
||||
do {
|
||||
$out .= self::getMonth($curent[0], $curent[1], $events);
|
||||
|
||||
if ($curent[0] == $end[0] && $curent[1] == $end[1]) {
|
||||
$begin = false;
|
||||
}
|
||||
|
||||
$curent[0]++;
|
||||
if ($curent[0] == 13) {
|
||||
$curent[0] = 1;
|
||||
$curent[1]++;
|
||||
}
|
||||
} while ($begin == true);
|
||||
|
||||
$out .= '</div>';
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
14
src/eventcalendar.php
Normal file
14
src/eventcalendar.php
Normal file
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
ini_set('display_errors','on');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
require '/vhosts/beta/wp-load.php';
|
||||
//require '/thosts/vij/wp/wp-load.php';
|
||||
|
||||
|
||||
|
||||
if (isset($_GET['month']) and $_GET['year']){
|
||||
calend_block($_GET['month'], $_GET['year']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user