Open log window on click, improve alterating background
[infodrom.org/service.infodrom.org] / class / calendar_item.class.php
index ffac6bb..2eeb005 100644 (file)
 <?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, 'dav_id');
+    parent::__construct('calendar_item', $id, $db);
   }
 
-  public static function pgTimestamp($timestamp)
+  protected function postLoad()
   {
-    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);
+    $start = explode(' ', $this->data->dtstart);
+    $starttime = explode(':', $start[1]);
+    $end = explode(' ', $this->data->dtend);
+    $endtime = explode(':', $end[1]);
 
-    if ($dtend !== false) {
-      $end = new DateTime($dtend);
+    $this->isRegular = $start[0] == $end[0];
 
-      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';
+    if ($this->isRegular) {
+      if ($starttime[1] == '00' && $endtime[1] == '00') {
+       if ($starttime[0] == $endtime[0])
+         $this->timespan = $starttime[0];
        else
-         return $start->format('d.m. H:i') . '-' . $end->format('H:i');
+         $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]);
 
-      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');
+      while ($startTimeStamp < $endTimeStamp) {
+       $this->timeDays[] = date('Y-m-d', $startTimeStamp);
+       $startTimeStamp += (24*60*60);
       }
     }
 
-    return 'timespan';
   }
 
-  public function getYears()
+  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)
   {
-    $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);
+    $title = $this->data->summary;
+    if ($this->data->location)
+      $title .= ', ' . $this->data->location;
+
+    $color = 0;
+    $out = sprintf('<div class="caltitle">%s</div>', $title);
+    $out .= sprintf('<div class="row%d">%s: Item created</div>', $color, 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) {
+      $color = !$color;
+      if (strlen($row->url))
+       $out .= sprintf('<div class="row%d">%s: <a href="%s" target="_blank">%s</a></div>',
+                       $color,
+                       substr($row->sys_edit,0,16),
+                       $row->url,
+                       $row->name ? $row->name : 'Link');
+      if (strlen($row->comment))
+       $out .= sprintf('<div class="row%d">%s: %s</div>', $color, substr($row->sys_edit,0,16), $row->comment);
+    }
+
+    return array('html' => array('log' => $out));
   }
 
-  public function getItems($from=false, $to=false)
+  public function ajaxAddLog(Array $data)
   {
-    $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)));
+    if (!strlen($data['name']) && !strlen($data['url']) && !strlen($data['comment'])) return true;
 
-    $sql .= " ORDER BY dtstart";
+    $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->fetchObjectList($sql);
+    return $this->db->insertInto('calendar_item_log', $item);
   }
 
 }