aboutsummaryrefslogtreecommitdiff
path: root/list.php
blob: e3831a4ef9cbe166213d3eeb719128bc5d188494 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html>
<head>
	<title>Events</title>
	<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h1>HebrewTools Events List</h1>
Select <a href="#" onclick="check_all_keywords(true);">all</a> / <a href="#" onclick="check_all_keywords(false);">no</a> filters.
<div id="keyword-filters">
<?php
require_once ('db.php');

function htmlid ($str)
{
	return preg_replace ('/\W/','-',$str);
}

function format_date_range ($start,$end)
{
	$dates=date ('D j',$start);
	if (date ('Y',$start) != date ('Y',$end))
		$dates.=date (' M Y',$start).' to '.date ('j M',$end);
	else if (date ('M',$start) != date ('M',$end))
		$dates.=date (' M',$start).' to '.date ('j M',$end);
	else if (date ('j',$start) != date ('j',$end))
		$dates.='&ndash;'.date ('j M',$end);
	else
		$dates.=date (' M',$start);
	if (date ('Y') != date ('Y',$end))
		$dates.=date (' Y',$end);
	return $dates;
}

$selected_keywords=isset ($_GET['keywords']) ? explode(',',$_GET['keywords']) : null;

$keywords=get_keywords();
foreach ($keywords as $record){
	$keyword=$record['keyword'];
	$checked=is_null ($selected_keywords) || in_array($keyword,$selected_keywords);
	$checked=$checked ? 'checked="checked"' : '';
	echo '<label for="kw-'.htmlid ($keyword).'">';
	echo '<input type="checkbox" class="keyword" value="'.$keyword.'" id="kw-'.htmlid ($keyword).'" '.$checked.'/>';
	echo ' '.$keyword.' ('.$record['count'].')</label><br/>';
}

$ical_url='https://'.$_SERVER['HTTP_HOST'].'/ical.php';
if (isset ($_GET['keywords']) && count ($_GET['keywords']) != count ($keywords))
	$ical_url.='?keywords='.$_GET['keywords'];
?>
</div>
<br/>
<form action="list.php" method="get">
<input type="hidden" name="keywords" value=""/>
<input type="submit" value="Update filters"/>
</form>
<p>Export this calendar in <a href="<?=$ical_url?>">iCal</a> format.</p>
<table>
<tr>
	<th>Date(s)</th>
	<th>Location</th>
	<th>Title</th>
	<th>Keywords</th>
</tr>
<?php
$events=is_null ($selected_keywords) ? get_events() : get_events ($selected_keywords);
$last_month=null;
foreach ($events as $event){
	$start_date=strtotime ($event['start_date']);
	$end_date=strtotime ($event['end_date']);

	$classes=[];

	$month=date ('m',$start_date);
	if ($month!=$last_month)
		$classes[]='new-month';
	$last_month=$month;

	$time=time();
	if ($end_date+3600*24<$time)
		$classes[]='expired';
	elseif ($start_date<=$time && $time<$end_date+3600*24)
		$classes[]='current';

	if (count ($classes))
		echo '<tr class="'.implode (' ',$classes).'">';
	else
		echo '<tr>';

	echo '<td>'.format_date_range ($start_date,$end_date).'</td>';
	echo '<td>'.htmlspecialchars ($event['location']).'</td>';
	echo '<td class="large"><span class="title-link" title="Click for details" onclick="expand_or_collapse(this,'.$event['id'].');">'.htmlspecialchars ($event['title']).'</span></td>';

	foreach ($event['keywords'] as $i => $kw){
		if (!is_null ($selected_keywords) && !in_array ($kw,$selected_keywords))
			$event['keywords'][$i]='<span class="filtered-keyword">'.htmlspecialchars ($kw).'</span>';
		else
			$event['keywords'][$i]=htmlspecialchars ($kw);
	}
	echo '<td>'.implode (', ',$event['keywords']).'</td>';

	echo '</tr>';
}
?>
</table>
<script type="text/javascript">
let checkboxes=Array.from (document.getElementsByClassName ('keyword'));

function update_keywords_field ()
{
	var keywords=checkboxes.filter (cb => cb.checked).map (cb => cb.value);
	var elem=document.getElementsByName ('keywords')[0];
	elem.value=keywords.join (',');
}

function check_all_keywords (check)
{
	checkboxes.map (cb => cb.checked=check);
	update_keywords_field();
	return false;
}

function expand_or_collapse (elem,id)
{
	if (elem.parentNode.childNodes.length > 1){
		let p=elem.parentNode.childNodes[1];
		p.style.display=p.style.display=='none' ? 'block' : 'none';
		return;
	}

	var xhttp=new XMLHttpRequest();
	xhttp.onreadystatechange=function(){
		if (this.readyState!=4)
			return;
		if (this.status!=200){
			window.alert ('Failed to fetch event information ('+this.status+')');
			return;
		}

		let ev=JSON.parse (xhttp.responseText);
		let p=document.createElement ('p');
		p.classList.add ('description');
		p.innerHTML=ev.description;
		elem.parentNode.appendChild (p);
	};
	xhttp.open ('GET','event.php?id='+id,true);
	xhttp.send();
}

update_keywords_field();
checkboxes.map (cb => cb.onchange=update_keywords_field);
</script>
</body>
</html>