jquery-editable: Automatically call backend if route and id attributes are present
[infodrom.org/service.infodrom.org] / class / calendar_item.class.php
1 <?php
2
3 class Calendar_Item extends DatabaseTable {
4
5   public function __construct($id=false)
6   {
7     $db = new Database(DBDRIVER, DBHOST, DAV_DBNAME, DBUSER, DBPASS);
8
9     parent::__construct('calendar_item', $id, $db, 'dav_id');
10   }
11
12   public static function pgTimestamp($timestamp)
13   {
14     if (is_string($timestamp))
15       return $timestamp;
16     elseif (is_int($timestamp))
17       return date('Y-m-d H:i:s', $timestamp);
18     elseif (is_object($timestamp) && is_a($timestamp, 'DateTime'))
19       return $timestamp->format('Y-m-d H:i:s');
20     else
21       return NULL;
22   }
23
24   public static function formatTimespan($dtstart, $dtend=false)
25   {
26     $start = new DateTime($dtstart);
27
28     if ($dtend !== false) {
29       $end = new DateTime($dtend);
30
31       if ($start->format('Y-m-d') == $end->format('Y-m-d')) {
32         if ($start->format('i') == '00' && $end->format('i') == '00')
33           return $start->format('d.m. H') . '-' . $end->format('H') . ' Uhr';
34         else
35           return $start->format('d.m. H:i') . '-' . $end->format('H:i');
36       }
37
38       if ($start->format('H:i:s') == '00:00:00' && $end->format('H:i:s') == '00:00:00') {
39         $end->sub(new DateInterval('P1D'));
40         if ($start->format('Y-m-d') == $end->format('Y-m-d'))
41           return $start->format('d.m.');
42         else
43           return $start->format('d.m.') . '-' . $end->format('d.m.');
44       }
45
46       if ($start->format('Y-m-') == $end->format('Y-m-')) {
47         return $start->format('d.m. H:i') . '-' . $end->format('d.m. H:i');
48       }
49     }
50
51     return 'timespan';
52   }
53
54   public function getYears()
55   {
56     $sql = sprintf("SELECT DISTINCT extract(year from dtstart) AS year FROM calendar_item WHERE user_no = %d ORDER BY year",
57                    DAV_USER_NO);
58     return $this->db->fetchObjectList($sql);
59   }
60
61   public function getItems($from=false, $to=false)
62   {
63     $sql = sprintf("SELECT dav_id,dtstart,dtend,EXTRACT(WEEK FROM dtstart) AS kw,summary,location,description FROM calendar_item WHERE user_no = %d",
64                    DAV_USER_NO);
65     if ($from)
66       $sql .= sprintf(" AND dtstart >= %s", $this->db->quote(self::pgTimestamp($from)));
67     if ($to)
68       $sql .= sprintf(" AND dtstart <= %s", $this->db->quote(self::pgTimestamp($to)));
69
70     $sql .= " ORDER BY dtstart";
71
72     return $this->db->fetchObjectList($sql);
73   }
74
75 }