Add lots of JS magic to make calendar usable
[infodrom.org/service.infodrom.org] / class / calendar_item.class.php
diff --git a/class/calendar_item.class.php b/class/calendar_item.class.php
new file mode 100644 (file)
index 0000000..99f0cf1
--- /dev/null
@@ -0,0 +1,139 @@
+<?php
+
+class Calendar_Item extends DatabaseTable {
+  private $isRegular;
+  private $timespan;
+  private $timedays = array();
+
+  public function __construct($id=false)
+  {
+    $this->idcolumn = 'dav_id';
+    $db = new Database(DBDRIVER, DBHOST, DAV_DBNAME, DBUSER, DBPASS);
+
+    parent::__construct('calendar_item', $id, $db);
+  }
+
+  protected function postLoad()
+  {
+    $start = explode(' ', $this->data->dtstart);
+    $starttime = explode(':', $start[1]);
+    $end = explode(' ', $this->data->dtend);
+    $endtime = explode(':', $end[1]);
+
+    $this->isRegular = $start[0] == $end[0];
+
+    if ($this->isRegular) {
+      if ($starttime[1] == '00' && $endtime[1] == '00') {
+       if ($starttime[0] == $endtime[0])
+         $this->timespan = $starttime[0];
+       else
+         $this->timespan = $starttime[0] .'-'. $endtime[0];
+      } else {
+       $this->timespan = sprintf('%d:%02d-%d:%02d',
+                                 $starttime[0], $starttime[1],
+                                 $endtime[0], $endtime[1]);
+      }
+    } else {
+      $startTimeStamp = strtotime($start[0]) + (3*60*60);
+      $endTimeStamp = strtotime($end[0]);
+
+      while ($startTimeStamp < $endTimeStamp) {
+       $this->timeDays[] = date('Y-m-d', $startTimeStamp);
+       $startTimeStamp += (24*60*60);
+      }
+    }
+
+  }
+
+  public function isRegular()
+  {
+    return $this->isRegular;
+  }
+
+  public function timespan()
+  {
+    return $this->timespan;
+  }
+
+  public function getDays()
+  {
+    return $this->timeDays;
+  }
+
+  public function toSpan()
+  {
+    $tooltip = '';
+    if ($this->data->description)
+      $tooltip .= (strlen($tooltip)?', ':'').$this->data->description;
+    if ($this->data->location)
+      $tooltip .= (strlen($tooltip)?', ':'').'Ort: '.$this->data->location;
+
+    if (strlen($tooltip))
+      $tooltip = sprintf(' title="%s"', $tooltip);
+
+    $sql = sprintf("SELECT color FROM calendar_item_data WHERE dav_id = %d", $this->id);
+    $bgcolor = $this->fetchValue($sql);
+    if ($bgcolor) $bgcolor = sprintf(' style="background:%s;"', $bgcolor);
+
+    return sprintf('<span dav_id="%d"%s%s>%s %s</span>',
+                  $this->id,
+                  $tooltip, $bgcolor,
+                  $this->timespan(),
+                  $this->data->summary);
+  }
+
+  public function ajaxSetColor(Array $data)
+  {
+    $sql = sprintf("SELECT id FROM calendar_item_data WHERE dav_id = %d", $this->id);
+    $id = $this->fetchValue($sql);
+
+    if ($id)
+      $sql = sprintf("UPDATE calendar_item_data SET color = %s WHERE id = %d",
+                    $this->quote($data['color']), $id);
+    else
+      $sql = sprintf("INSERT INTO calendar_item_data (dav_id, color) VALUES (%d,%s)",
+                    $this->id, $this->quote($data['color']));
+
+    return $this->execute($sql);
+  }
+
+  public function ajaxLog(Array $data)
+  {
+    $title = $this->data->summary;
+    if ($this->data->location)
+      $title .= ', ' . $this->data->location;
+
+    $out = sprintf('<div class="caltitle">%s</div>', $title);
+    $out .= sprintf('<div>%s: Item created</div>', substr($this->data->created,0,16));
+    # $out .= sprintf('<div>Last modified %s</div>', $this->data->last_modified);
+
+    $sql = sprintf("SELECT name,url,comment,sys_edit,sys_user " .
+                  "FROM calendar_item_log WHERE dav_id = %d ORDER BY sys_edit", $this->id);
+    foreach ($this->fetchObjectList($sql) as $row) {
+      if (strlen($row->url))
+       $out .= sprintf('<div>%s: <a href="%s" target="_blank">%s</a></div>',
+                       substr($row->sys_edit,0,16),
+                       $row->url,
+                       $row->name ? $row->name : 'Link');
+      if (strlen($row->comment))
+       $out .= sprintf('<div>%s: %s</div>', substr($row->sys_edit,0,16), $row->comment);
+    }
+
+    return array('html' => array('log' => $out));
+  }
+
+  public function ajaxAddLog(Array $data)
+  {
+    if (!strlen($data['name']) && !strlen($data['url']) && !strlen($data['comment'])) return true;
+
+    $item = array('dav_id' => $this->id,
+                 'name' => strlen($data['name']) ? $data['name'] : NULL,
+                 'url' => strlen($data['url']) ? $data['url'] : NULL,
+                 'comment' => strlen($data['comment']) ? $data['comment'] : NULL,
+                 'sys_user' => $_SERVER['REMOTE_USER'],
+                 'sys_edit' => 'now()');
+
+    return $this->db->insertInto('calendar_item_log', $item);
+  }
+
+}