Copy calendar item to log window
[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     $sql = sprintf("SELECT color FROM calendar_item_data WHERE dav_id = %d", $this->id);
107     $bgcolor = $this->fetchValue($sql);
108     if ($bgcolor) $bgcolor = sprintf(' style="background:%s;"', $bgcolor);
109
110     $color = 0;
111     $out = sprintf('<div class="caltitle" %s>%s</div>', $bgcolor, $title);
112
113     $items = array();
114     $items[] = array('dt' => substr($this->data->created,0,16),
115                      'edit' => false,
116                      'log' => 'Item created');
117
118     $sql = sprintf("SELECT id,name,url,comment,sys_edit,sys_user " .
119                    "FROM calendar_item_log WHERE dav_id = %d ORDER BY sys_edit", $this->id);
120
121     foreach ($this->fetchObjectList($sql) as $row) {
122       if (strlen($row->url))
123           $items[] = array('dt' => substr($row->sys_edit,0,16),
124                            'edit' => false,
125                            'log' => sprintf('<a href="%s" target="_blank">%s</a>',
126                                            $row->url,
127                                            $row->name ? $row->name : 'Link'));
128       if (strlen($row->comment))
129           $items[] = array('dt' => substr($row->sys_edit,0,16),
130                            'id' => $row->id,
131                            'edit' => true,
132                            'log' => $row->comment);
133     }
134
135     foreach ($items as $item) {
136         $color = !$color;
137         $out .= sprintf('<div class="row%d"><div class="logl">%s</div><div class="logr%s"%s>%s</div><div class="clear"></div></div>',
138                         $color,
139                         $item['dt'],
140                         $item['edit'] ? ' editable' : '',
141                         $item['edit'] ? sprintf(' route="Calendar_Item_Log/EditComment" item_id="%d"', $item['id']) : '',
142                         $item['log']);
143     }
144
145     return array('html' => array('log' => $out));
146   }
147
148   public function ajaxAddLog(Array $data)
149   {
150     if (!strlen($data['name']) && !strlen($data['url']) && !strlen($data['comment'])) return true;
151
152     $item = array('dav_id' => $this->id,
153                   'name' => strlen($data['name']) ? $data['name'] : NULL,
154                   'url' => strlen($data['url']) ? $data['url'] : NULL,
155                   'comment' => strlen($data['comment']) ? nl2br(trim($data['comment'])) : NULL,
156                   'sys_user' => $_SERVER['REMOTE_USER'],
157                   'sys_edit' => 'now()');
158
159     return $this->db->insertInto('calendar_item_log', $item);
160   }
161
162 }