Open log window on click, improve alterating background
[infodrom.org/service.infodrom.org] / class / calendar_item.class.php
1 <?php
2
3 class Calendar_Item extends DatabaseTable {
4   private $isRegular;
5   private $timespan;
6   private $timedays = array();
7
8   public function __construct($id=false)
9   {
10     $this->idcolumn = 'dav_id';
11     $db = new Database(DBDRIVER, DBHOST, DAV_DBNAME, DBUSER, DBPASS);
12
13     parent::__construct('calendar_item', $id, $db);
14   }
15
16   protected function postLoad()
17   {
18     $start = explode(' ', $this->data->dtstart);
19     $starttime = explode(':', $start[1]);
20     $end = explode(' ', $this->data->dtend);
21     $endtime = explode(':', $end[1]);
22
23     $this->isRegular = $start[0] == $end[0];
24
25     if ($this->isRegular) {
26       if ($starttime[1] == '00' && $endtime[1] == '00') {
27         if ($starttime[0] == $endtime[0])
28           $this->timespan = $starttime[0];
29         else
30           $this->timespan = $starttime[0] .'-'. $endtime[0];
31       } else {
32         $this->timespan = sprintf('%d:%02d-%d:%02d',
33                                   $starttime[0], $starttime[1],
34                                   $endtime[0], $endtime[1]);
35       }
36     } else {
37       $startTimeStamp = strtotime($start[0]) + (3*60*60);
38       $endTimeStamp = strtotime($end[0]);
39
40       while ($startTimeStamp < $endTimeStamp) {
41         $this->timeDays[] = date('Y-m-d', $startTimeStamp);
42         $startTimeStamp += (24*60*60);
43       }
44     }
45
46   }
47
48   public function isRegular()
49   {
50     return $this->isRegular;
51   }
52
53   public function timespan()
54   {
55     return $this->timespan;
56   }
57
58   public function getDays()
59   {
60     return $this->timeDays;
61   }
62
63   public function toSpan()
64   {
65     $tooltip = '';
66     if ($this->data->description)
67       $tooltip .= (strlen($tooltip)?', ':'').$this->data->description;
68     if ($this->data->location)
69       $tooltip .= (strlen($tooltip)?', ':'').'Ort: '.$this->data->location;
70
71     if (strlen($tooltip))
72       $tooltip = sprintf(' title="%s"', $tooltip);
73
74     $sql = sprintf("SELECT color FROM calendar_item_data WHERE dav_id = %d", $this->id);
75     $bgcolor = $this->fetchValue($sql);
76     if ($bgcolor) $bgcolor = sprintf(' style="background:%s;"', $bgcolor);
77
78     return sprintf('<span dav_id="%d"%s%s>%s %s</span>',
79                    $this->id,
80                    $tooltip, $bgcolor,
81                    $this->timespan(),
82                    $this->data->summary);
83   }
84
85   public function ajaxSetColor(Array $data)
86   {
87     $sql = sprintf("SELECT id FROM calendar_item_data WHERE dav_id = %d", $this->id);
88     $id = $this->fetchValue($sql);
89
90     if ($id)
91       $sql = sprintf("UPDATE calendar_item_data SET color = %s WHERE id = %d",
92                      $this->quote($data['color']), $id);
93     else
94       $sql = sprintf("INSERT INTO calendar_item_data (dav_id, color) VALUES (%d,%s)",
95                      $this->id, $this->quote($data['color']));
96
97     return $this->execute($sql);
98   }
99
100   public function ajaxLog(Array $data)
101   {
102     $title = $this->data->summary;
103     if ($this->data->location)
104       $title .= ', ' . $this->data->location;
105
106     $color = 0;
107     $out = sprintf('<div class="caltitle">%s</div>', $title);
108     $out .= sprintf('<div class="row%d">%s: Item created</div>', $color, substr($this->data->created,0,16));
109     # $out .= sprintf('<div>Last modified %s</div>', $this->data->last_modified);
110
111     $sql = sprintf("SELECT name,url,comment,sys_edit,sys_user " .
112                    "FROM calendar_item_log WHERE dav_id = %d ORDER BY sys_edit", $this->id);
113     foreach ($this->fetchObjectList($sql) as $row) {
114       $color = !$color;
115       if (strlen($row->url))
116         $out .= sprintf('<div class="row%d">%s: <a href="%s" target="_blank">%s</a></div>',
117                         $color,
118                         substr($row->sys_edit,0,16),
119                         $row->url,
120                         $row->name ? $row->name : 'Link');
121       if (strlen($row->comment))
122         $out .= sprintf('<div class="row%d">%s: %s</div>', $color, substr($row->sys_edit,0,16), $row->comment);
123     }
124
125     return array('html' => array('log' => $out));
126   }
127
128   public function ajaxAddLog(Array $data)
129   {
130     if (!strlen($data['name']) && !strlen($data['url']) && !strlen($data['comment'])) return true;
131
132     $item = array('dav_id' => $this->id,
133                   'name' => strlen($data['name']) ? $data['name'] : NULL,
134                   'url' => strlen($data['url']) ? $data['url'] : NULL,
135                   'comment' => strlen($data['comment']) ? $data['comment'] : NULL,
136                   'sys_user' => $_SERVER['REMOTE_USER'],
137                   'sys_edit' => 'now()');
138
139     return $this->db->insertInto('calendar_item_log', $item);
140   }
141
142 }