aboutsummaryrefslogtreecommitdiff
path: root/list.php
blob: 90e6084d38ebb910f200909d1bc67fe22b45d9d7 (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
<!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);
}

$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);
foreach ($events as $event){
	echo '<tr>';
	echo '<td>'.date ('D j M Y',strtotime ($event['start_date']));
	if ($event['end_date']!=$event['start_date'])
		echo ' to '.date ('j M',strtotime ($event['end_date']));
	echo '</td>';
	echo '<td>'.htmlspecialchars ($event['location']).'</td>';
	echo '<td><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>