/
class.tx_akgoogleavail_pi1.php
286 lines (265 loc) · 9.47 KB
/
class.tx_akgoogleavail_pi1.php
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/***************************************************************
* Copyright notice
*
* (c) 2013 Alexander Kruth <kruth@bfpi.de>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
// require_once(PATH_tslib . 'class.tslib_pibase.php');
require_once('class.iCalReader.php');
require_once('class.daystatus.php');
/**
* Plugin 'Availability calendar' for the 'ak_google_avail' extension.
*
* @author Alexander Kruth <kruth@bfpi.de>
* @package TYPO3
* @subpackage tx_akgoogleavail
*/
class tx_akgoogleavail_pi1 extends tslib_pibase {
public $prefixId = 'tx_akgoogleavail_pi1'; // Same as class name
public $scriptRelPath = 'pi1/class.tx_akgoogleavail_pi1.php'; // Path to this script relative to the extension dir.
public $extKey = 'ak_google_avail'; // The extension key.
public $pi_checkCHash = TRUE;
/**
* The main method of the Plugin.
*
* @param string $content The Plugin content
* @param array $conf The Plugin configuration
* @return string The content that is displayed on the website
*/
public function main($content, array $conf) {
$this->conf = $conf;
$this->pi_setPiVarDefaults();
$this->pi_loadLL();
$this->pi_initPIflexForm();
$content = "";
switch($this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'mode')) {
case "single":
$content = $this->singleMode();
break;
case "multiple":
$content = $this->multipleMode();
break;
}
return $this->pi_wrapInBaseClass($content);
}
function singleMode() {
$url = $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'url');
$bookedDays = $this->getBookedDays($url);
$now = new DateTime("first day of this month");
$now->setTime(0, 0, 0);
$content = $this->legend();
$content .= '<br class="clear"/>';
for($i = 1; $i <= 12; $i++) {
$content .= $this->drawMonth($now, $bookedDays);
$now->modify("+1 month");
}
$content .= '<br class="clear"/>';
return $content;
}
function multipleMode() {
$params = t3lib_div::_GET($this->prefixId);
$month = new DateTime("first day of this month");
if(isset($params) && isset($params['month'])) {
list($m, $y) = explode("-", $params['month']);
$month = new DateTime("$y-$m-1");
}
$month->setTime(0, 0, 0);
$content = '';
$content .= $this->legend();
$content .= $this->multipleTable($month);
return $content;
}
function multipleSelectMonth($month) {
$first_month = new DateTime("first day of this month");
$last_month = new DateTime("next year");
$month_period = new DatePeriod($first_month, new DateInterval('P1M'), $last_month);
$content = '<form action="' . $this->pi_getPageLink($GLOBALS['TSFE']->id) . '" method="GET">';
$content .= '<select name="' . $this->prefixId . '[month]" onchange="this.form.submit();">';
$current_month_year = $month->format('m-Y');
foreach($month_period as $month_date) {
$month_year = $month_date->format('m-Y');
$content .= '<option value="'. $month_year .'"';
if($month_year == $current_month_year) {
$content .= ' selected="selected"';
}
$content .= '>'. $this->formatFullMonth($month_date) .'</option>';
}
$content .= '</select>';
$content .= '</form>';
return $content;
}
function multipleTable($month) {
$start = clone $month;
$start->sub(new DateInterval('P3D'));
$end = clone $month;
$end->add(new DateInterval('P1M3D'));
$content = '<table><tr><th></th>'.
'<th colspan="3">'. $this->formatShortMonth($start) .'</th>'.
'<th colspan="'. $month->format('t') .'">'. $this->multipleSelectMonth($month) .'</th>'.
'<th colspan="3">'. $this->formatShortMonth($end) .'</th></tr>';
$names = explode("\n", $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'names'));
$urls = explode("\n", $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'urls'));
$pages = explode(",", $this->pi_getFFvalue($this->cObj->data['pi_flexform'], 'pages'));
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
$content .= '<tr><td></td>';
foreach($period as $date) {
$content .= '<td class="wday">'. $this->pi_getLL('wday_'. $date->format('N')) .'</td>';
}
$content .= '</tr>';
for($i = 0; $i < count($names); $i++) {
$content .= '<tr><td class="name">' .
'<a href="'. $this->pi_getPageLink($pages[$i]) .'">'.
$names[$i] .'</a></td>';
$bookedDays = $this->getBookedDays($urls[$i]);
foreach($period as $date) {
$content .= $this->drawDay($date, $bookedDays);
}
}
$content .= '</tr></tbody></table>';
return $content;
}
function legend() {
return <<<EOT
<div class="legend">
<table>
<tr>
<td class="vacant day"> </td>
<th>{$this->pi_getLL('legend_vacant')}</th>
<td class="booked day"> </td>
<th>{$this->pi_getLL('legend_booked')}</th>
</tr>
</table>
</div>
EOT;
}
function formatShortMonth($date) {
return $this->pi_getLL("shortmonth_". $date->format('n')) .' '. $date->format('Y');
}
function formatFullMonth($date) {
return $this->pi_getLL("month_". $date->format('n')) .' '. $date->format('Y');
}
function drawMonth($start, $bookedDays) {
$date = clone $start;
$ret = '<div class="month">
<h3 class="monthname">' . $this->formatFullMonth($date) .'</h3>
<table>
<tr>';
for($i = 1; $i <= 7; $i++) {
$ret .= '<th>'. $this->pi_getLL('wday_'. $i) .'</th>';
}
$ret .= '</tr>';
for($week = 1; $week <= 6; $week++) {
$ret .= "<tr>";
for($wday = 1; $wday <= 7; $wday++) {
// Wochentag stimmt mit Spalte überein und Monat ist der gewünschte
if(intval($date->format('N')) == $wday &&
$date->format('n') == $start->format('n')) {
$ret .= $this->drawDay($date, $bookedDays);
$date->modify("+1 day");
}
else {
$ret .= "<td></td>";
}
}
$ret .= "</tr>";
}
$ret .= '</table></div>';
return $ret;
}
function drawDay(&$date, &$bookedDays) {
$class = "day ";
$title = $this->pi_getLL('title_vacant');
$dateStr = $date->format("Y-m-d");
if(array_key_exists($dateStr, $bookedDays)) {
switch($bookedDays[$dateStr]) {
case DayStatus::BOOKED:
$class .= "booked";
$title = $this->pi_getLL('title_booked');
break;
case DayStatus::ARRIVAL:
$class .= "arrival";
$title = $this->pi_getLL('title_arrival');
break;
case DayStatus::DEPARTURE:
$class .= "departure";
$title = $this->pi_getLL('title_departure');
break;
}
}
else {
$class .= "vacant";
}
return '<td class="'. $class .'" title="'. $title .'">'. $date->format('j') ."</td>";
}
function getBookedDays($url) {
$ical = new ICal($url);
$bookedDays = array();
$events = $ical->events();
if(!isset($events)) {
return $bookedDays;
}
// Nach Anreise sortieren
usort($events, create_function('$a, $b', 'return strcmp($a[\'DTSTART\'], $b[\'DTSTART\']);'));
foreach($events as $event) {
$period = new DatePeriod(
new DateTime($event['DTSTART']),
new DateInterval('P1D'),
new DateTime($event['DTEND'])
);
$days = iterator_to_array($period);
// $days in Strings umwandeln, damit sie als Array-Keys benutzt werden können
array_walk($days, create_function('&$date', '$date = $date->format("Y-m-d");'));
reset($days);
$arrivalDay = current($days);
$departureDay = end($days);
$this->setArrivalDate($bookedDays, $arrivalDay);
// Alle Tage außer An- und Abreise durchgehen
for($i = 1; $i < count($days) - 1; $i++) {
$bookedDays[$days[$i]] = DayStatus::BOOKED;
}
if($departureDay != $arrivalDay)
$this->setDepartureDate($bookedDays, $departureDay);
}
return $bookedDays;
}
function setArrivalDate(&$bookedDays, &$arrivalDay) {
if(array_key_exists($arrivalDay, $bookedDays)) {
switch($bookedDays[$arrivalDay]) {
case DayStatus::ARRIVAL:
case DayStatus::BOOKED:
break;
case DayStatus::DEPARTURE:
$bookedDays[$arrivalDay] = DayStatus::BOOKED;
}
}
else {
$bookedDays[$arrivalDay] = DayStatus::ARRIVAL;
}
}
function setDepartureDate(&$bookedDays, &$departureDay) {
if(!array_key_exists($departureDay, $bookedDays)) {
$bookedDays[$departureDay] = DayStatus::DEPARTURE;
}
}
}
if(defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/ak_google_avail/pi1/class.tx_akgoogleavail_pi1.php'])) {
include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/ak_google_avail/pi1/class.tx_akgoogleavail_pi1.php']);
}
?>