Add view to davical data
authorJoey Schulze <joey@infodrom.org>
Sat, 6 May 2017 17:16:18 +0000 (19:16 +0200)
committerJoey Schulze <joey@infodrom.org>
Sat, 6 May 2017 17:16:18 +0000 (19:16 +0200)
class/calendar_item.class.php [new file with mode: 0644]
src/Infodrom/calendar/index.wml [new file with mode: 0644]
src/Infodrom/calendar/submenu.inc [new file with mode: 0644]
src/Infodrom/functions.inc

diff --git a/class/calendar_item.class.php b/class/calendar_item.class.php
new file mode 100644 (file)
index 0000000..ffac6bb
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+
+class Calendar_Item extends DatabaseTable {
+
+  public function __construct($id=false)
+  {
+    $db = new Database(DBDRIVER, DBHOST, DAV_DBNAME, DBUSER, DBPASS);
+
+    parent::__construct('calendar_item', $id, $db, 'dav_id');
+  }
+
+  public static function pgTimestamp($timestamp)
+  {
+    if (is_string($timestamp))
+      return $timestamp;
+    elseif (is_int($timestamp))
+      return date('Y-m-d H:i:s', $timestamp);
+    elseif (is_object($timestamp) && is_a($timestamp, 'DateTime'))
+      return $timestamp->format('Y-m-d H:i:s');
+    else
+      return NULL;
+  }
+
+  public static function formatTimespan($dtstart, $dtend=false)
+  {
+    $start = new DateTime($dtstart);
+
+    if ($dtend !== false) {
+      $end = new DateTime($dtend);
+
+      if ($start->format('Y-m-d') == $end->format('Y-m-d')) {
+       if ($start->format('i') == '00' && $end->format('i') == '00')
+         return $start->format('d.m. H') . '-' . $end->format('H') . ' Uhr';
+       else
+         return $start->format('d.m. H:i') . '-' . $end->format('H:i');
+      }
+
+      if ($start->format('H:i:s') == '00:00:00' && $end->format('H:i:s') == '00:00:00') {
+       $end->sub(new DateInterval('P1D'));
+       if ($start->format('Y-m-d') == $end->format('Y-m-d'))
+         return $start->format('d.m.');
+       else
+         return $start->format('d.m.') . '-' . $end->format('d.m.');
+      }
+
+      if ($start->format('Y-m-') == $end->format('Y-m-')) {
+       return $start->format('d.m. H:i') . '-' . $end->format('d.m. H:i');
+      }
+    }
+
+    return 'timespan';
+  }
+
+  public function getYears()
+  {
+    $sql = sprintf("SELECT DISTINCT extract(year from dtstart) AS year FROM calendar_item WHERE user_no = %d ORDER BY year",
+                  DAV_USER_NO);
+    return $this->db->fetchObjectList($sql);
+  }
+
+  public function getItems($from=false, $to=false)
+  {
+    $sql = sprintf("SELECT dav_id,dtstart,dtend,EXTRACT(WEEK FROM dtstart) AS kw,summary,location,description FROM calendar_item WHERE user_no = %d",
+                  DAV_USER_NO);
+    if ($from)
+      $sql .= sprintf(" AND dtstart >= %s", $this->db->quote(self::pgTimestamp($from)));
+    if ($to)
+      $sql .= sprintf(" AND dtstart <= %s", $this->db->quote(self::pgTimestamp($to)));
+
+    $sql .= " ORDER BY dtstart";
+
+    return $this->db->fetchObjectList($sql);
+  }
+
+}
diff --git a/src/Infodrom/calendar/index.wml b/src/Infodrom/calendar/index.wml
new file mode 100644 (file)
index 0000000..62590d2
--- /dev/null
@@ -0,0 +1,86 @@
+#include <infodrom.style>
+
+<future>
+<page func="Infodrom Oldenburg" title="Termine">
+
+<style type="text/css">
+table#calendar tr.row:hover {
+    background: yellow;
+}
+table#calendar tr.newkw td {
+    border-top: 1px solid #999;
+}
+table#calendar tr.month {
+    background: #a7ddff;
+}
+table#calendar tr.current {
+    background: orange;
+}
+</style>
+
+<table id="calendar" width="100%" class="smallfont border" cellspacing="0" summary="">
+<tr class="head">
+<th width="5%">KW</th>
+<th width="20%">Datum</th>
+<th width="55%">Beschreibung</th>
+<th width="20%">Location</th>
+</tr>
+
+<?php
+  setlocale('LC_TIME', 'de_DE');
+  $now = date('Y-m-d H:i:s');
+  $pivot = date('Y-m-d H:i:s', time()+(7*24*60*60));
+
+  $item = new Calendar_Item();
+  $month = '';
+
+  if (empty($_GET['year'])) {
+    $from = time();
+    $to = false;
+  } else {
+    $from = mktime(0,0,0,1,1,intval($_GET['year']));
+    $to = mktime(0,0,0,1,1,intval($_GET['year'])+1) - 1;
+  }
+  $kw = '';
+  foreach ($item->getItems($from, $to) as $row) {
+    $start = new DateTime($row->dtstart);
+    $newmonth = $start->format('F Y');
+    if ($month != $newmonth) {
+      $month = $newmonth;
+      printf('<tr class="month" month="%s"><td colspan="4">%s</td></tr>',
+            $start->format('Y-m'), $month);
+      $kw = '';
+    }
+
+    $tooltip = '';
+    if (strlen($row->description))
+      $tooltip = sprintf(' title="%s"', htmlspecialchars($row->description));
+
+    $class = 'row';
+    if ($row->dtstart >= $now && $row->dtstart < $pivot)
+      $class .= ' current';
+    if (strlen($kw) && $kw != $row->kw)
+      $class .= ' newkw';
+      
+    printf('<tr id="%d" class="%s"><td>%s</td><td>%s</td><td%s>%s</td><td>%s</td></tr>',
+          $row->dav_id,
+          $class,
+          $kw != $row->kw ? $row->kw : '&nbsp;',
+          Calendar_Item::formatTimespan($row->dtstart, $row->dtend),
+          utf8_decode($tooltip),
+          utf8_decode($row->summary),
+          utf8_decode($row->location));
+    $kw = $row->kw;
+  }
+
+?>
+
+</table>
+
+</page>
+
+# Local variables:
+# mode: text
+# mode: auto-fill
+# mode: iso-accents
+# end:
diff --git a/src/Infodrom/calendar/submenu.inc b/src/Infodrom/calendar/submenu.inc
new file mode 100644 (file)
index 0000000..20a7503
--- /dev/null
@@ -0,0 +1,13 @@
+<strong>Menu</strong><br>
+
+<?php
+  printf('&nbsp;<a href="index.php">Aktuell</a><br>');
+  $item = new Calendar_Item();
+  foreach ($item->getYears() as $row) {
+    printf('&nbsp;&nbsp;<a href="index.php?year=%d">%d</a><br>', $row->year, $row->year);
+  }
+?>
+
+<menu-rule>
+
+#include "../functions.inc" prefix="../"
index a5b93df..38aeb43 100644 (file)
@@ -10,3 +10,4 @@
 &nbsp;<a href="$(prefix)todo/">Todo</a><br>
 &nbsp;<a href="$(prefix)zeitungen/">Zeitungen</a><br>
 &nbsp;<a href="$(prefix)ticker/">Ticker</a><br>
+&nbsp;<a href="$(prefix)calendar/">Termine</a><br>