Skip to content
This repository was archived by the owner on Mar 30, 2024. It is now read-only.

Commit aeb4a21

Browse files
committed
Optimize ics file
1 parent da22628 commit aeb4a21

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

VERSION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
latest
2-
0.5.0
3-
0.4
2+
0.5.1
3+
0.5
44
0

php/core/Calendar.php

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@
1313

1414
class Calendar {
1515

16+
private const CAL_EOL = "\r\n";
17+
private string $timezone = "";
18+
1619
public function __construct( Login $login ) {
1720
$this->login = $login;
21+
$this->timezone = Config::getTimezone()->getName();
1822
}
1923

2024
public function generateICS(DataAccess $da) : string {
@@ -23,30 +27,45 @@ public function generateICS(DataAccess $da) : string {
2327
}
2428

2529
private function toICS(array $plainData) : string {
26-
$cal = '';
27-
$eol = "\r\n";
28-
29-
$cal .= 'BEGIN:VCALENDAR' . $eol;
30-
$cal .= 'VERSION:2.0' . $eol;
31-
$cal .= 'PRODID:https://github.com/KIMB-technologies/TaskTimeTerminateServer' . $eol;
32-
$cal .= 'CALSCALE:GREGORIAN' . $eol;
33-
$cal .= 'METHOD:PUBLISH' . $eol;
34-
35-
$tz = date_default_timezone_get();
36-
37-
foreach($plainData as $dat ){
38-
$cal .= 'BEGIN:VEVENT' . $eol;
39-
$cal .= 'UID:' . uniqid() . '@ttt-server' . $eol;
30+
$cal = 'BEGIN:VCALENDAR' . self::CAL_EOL;
31+
$cal .= 'VERSION:2.0' . self::CAL_EOL;
32+
$cal .= 'PRODID:https://github.com/KIMB-technologies/TaskTimeTerminateServer' . self::CAL_EOL;
33+
$cal .= 'CALSCALE:GREGORIAN' . self::CAL_EOL;
34+
$cal .= 'METHOD:PUBLISH' . self::CAL_EOL;
4035

41-
$cal .= 'SUMMARY:' . $dat['category'] . ' - ' . $dat['name'] . $eol;
42-
$cal .= 'DTSTART;TZID=' . $tz . ':'. date('Ymd', $dat['begin']) .'T'. date('His', $dat['begin']) .'Z' . $eol;
43-
$cal .= 'DTEND;TZID=' . $tz . ':'. date('Ymd', $dat['end']) .'T'. date('His', $dat['end']) .'Z' . $eol;
44-
45-
$cal .= 'END:VEVENT' . $eol;
36+
// name, category, begin, end
37+
$current = array("","", 0, 0);
38+
foreach( $plainData as $dat ){ // will be in order current to past (sees sorting in TTTStats::printDataset())
39+
if(
40+
$dat['name'] === $current[0] && $dat['category'] === $current[1] &&
41+
abs( $current[2] - $dat['end'] ) < 120
42+
){ // group to one "event"?
43+
$current[2] = $dat['begin'];
44+
continue;
45+
}
46+
if(!empty($current[0]) && !empty($current[1])){
47+
$cal .= $this->calEvent(...$current);
48+
}
49+
$current = array($dat['name'], $dat['category'], $dat['begin'], $dat['end']);
50+
}
51+
if(!empty($current[0]) && !empty($current[1])){
52+
$cal .= $this->calEvent(...$current);
4653
}
4754

48-
$cal .= 'END:VCALENDAR' . $eol;
49-
return $cal;
55+
return $cal . 'END:VCALENDAR' . self::CAL_EOL;
56+
}
57+
58+
private function calEvent(string $name, string $category, int $begin, int $end) : string {
59+
return 'BEGIN:VEVENT' . self::CAL_EOL
60+
. 'UID:' . sha1($name . $category . $begin . $end) . '@ttt-server' . self::CAL_EOL
61+
. 'SUMMARY:' . mb_strcut($name . ' - ' . $category, 0, 75 - 8) . self::CAL_EOL // maximal 75 octets per line
62+
. $this->calTimeRow($begin, 'DTSTART')
63+
. $this->calTimeRow($end, 'DTEND')
64+
.'END:VEVENT' . self::CAL_EOL;
65+
}
66+
67+
private function calTimeRow(int $time, string $rowname) : string {
68+
return $rowname . ';TZID=' . $this->timezone . ':'. date('Ymd\THis', $time ) .'Z' . self::CAL_EOL;
5069
}
5170

5271
public function getLink(DataAccess $da) : string {

php/core/templates/edit_en.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ <h3>Add task</h3>
2727
<input type="number" placeholder="dd" name="begin_d" size=2 max=31 min=1 value="%%DAY%%" class="form-control">
2828
<button type="button" class="btn btn-light" id="beginDialog">&#x1F4C5;</button>
2929
&nbsp;&nbsp; &nbsp;&nbsp;
30-
<input type="number" placeholder="hh" name="begin_H" size=2 max=23 min=0 value="%%HOUR%%" class="form-control">
31-
<input type="number" placeholder="mm" name="begin_i" size=2 max=59 min=0 value="%%MIN%%" class="form-control">
30+
<input type="number" placeholder="hh" name="begin_H" size=2 max=23 min=0 class="form-control">
31+
<input type="number" placeholder="mm" name="begin_i" size=2 max=59 min=0 class="form-control">
3232
</div>
3333
</div>
3434
<div class="form-group row">
@@ -41,8 +41,8 @@ <h3>Add task</h3>
4141
<input type="number" placeholder="dd" name="end_d" size=2 max=31 min=1 value="%%DAY%%" class="form-control">
4242
<button type="button" class="btn btn-light" id="endDialog">&#x1F4C5;</button>
4343
&nbsp;&nbsp; &nbsp;&nbsp;
44-
<input type="number" placeholder="hh" name="end_H" size=2 max=23 min=0 class="form-control">
45-
<input type="number" placeholder="mm" name="end_i" size=2 max=59 min=0 class="form-control">
44+
<input type="number" placeholder="hh" name="end_H" size=2 max=23 min=0 value="%%HOUR%%" class="form-control">
45+
<input type="number" placeholder="mm" name="end_i" size=2 max=59 min=0 value="%%MIN%%" class="form-control">
4646
</div>
4747
</div>
4848
<div class="form-group row">

0 commit comments

Comments
 (0)