xref: /plugin/davcal/helper.php (revision a1a3b6794e0e143a4a8b51d3185ce2d339be61ab)
1<?php
2/**
3  * Helper Class for the tagrevisions plugin
4  * This helper does the actual work.
5  *
6  * Configurable in DokuWiki's configuration
7  */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_davcal extends DokuWiki_Plugin {
13
14  protected $sqlite = null;
15
16  /**
17    * Constructor to load the configuration
18    */
19  public function helper_plugin_davcal() {
20    $this->sqlite =& plugin_load('helper', 'sqlite');
21    if(!$this->sqlite)
22    {
23        msg('This plugin requires the sqlite plugin. Please install it.');
24        return;
25    }
26
27    if(!$this->sqlite->init('davcal', DOKU_PLUGIN.'davcal/db/'))
28    {
29        return;
30    }
31  }
32
33  public function setCalendarNameForPage($name, $description, $id = null, $userid = null)
34  {
35      if(is_null($id))
36      {
37          global $ID;
38          $id = $ID;
39      }
40      if(is_null($userid))
41        $userid = $_SERVER['REMOTE_USER'];
42      $calid = $this->getCalendarIdForPage($id);
43      if($calid === false)
44        return $this->createCalendarForPage($name, $description, $id, $userid);
45
46      // Update the calendar name here
47
48  }
49
50  public function getCalendarIdForPage($id = null)
51  {
52      if(is_null($id))
53      {
54          global $ID;
55          $id = $ID;
56      }
57
58      $query = "SELECT calid FROM pagetocalendarmapping WHERE page=".$this->sqlite->quote_string($id);
59      $res = $this->sqlite->query($query);
60      $row = $this->sqlite->res2row($res);
61      if(isset($row['calid']))
62        return $row['calid'];
63      else
64        return false;
65  }
66
67  public function getCalendarIdToPageMapping()
68  {
69      $query = "SELECT calid, page FROM pagetocalendarmapping";
70      $res = $this->sqlite->query($query);
71      $arr = $this->sqlite->res2arr($res);
72      return $arr;
73  }
74
75  public function getCalendarIdsForUser($principalUri)
76  {
77      $user = explode('/', $principalUri);
78      $user = end($user);
79      $mapping = $this->getCalendarIdToPageMapping();
80      $calids = array();
81      foreach($mapping as $row)
82      {
83          $id = $row['calid'];
84          $page = $row['page'];
85          $acl = auth_quickaclcheck($page);
86          if($acl >= AUTH_READ)
87          {
88              $write = $acl > AUTH_READ;
89              $calids[$id] = array('readonly' => !$write);
90          }
91      }
92      return $calids;
93  }
94
95  public function createCalendarForPage($name, $description, $id = null, $userid = null)
96  {
97      if(is_null($id))
98      {
99          global $ID;
100          $id = $ID;
101      }
102      if(is_null($userid))
103          $userid = $_SERVER['REMOTE_USER'];
104      $values = array('principals/'.$userid,
105                      $name,
106                      str_replace(array('/', ' ', ':'), '_', $id),
107                      $description,
108                      'VEVENT,VTODO',
109                      0);
110      $query = "INSERT INTO calendars (principaluri, displayname, uri, description, components, transparent) VALUES (".$this->sqlite->quote_and_join($values, ',').");";
111      $res = $this->sqlite->query($query);
112      $query = "SELECT id FROM calendars WHERE principaluri=".$this->sqlite->quote_string($values[0])." AND ".
113               "displayname=".$this->sqlite->quote_string($values[1])." AND ".
114               "uri=".$this->sqlite->quote_string($values[2])." AND ".
115               "description=".$this->sqlite->quote_string($values[3]);
116      $res = $this->sqlite->query($query);
117      $row = $this->sqlite->res2row($res);
118      if(isset($row['id']))
119      {
120          $values = array($id, $row['id']);
121          $query = "INSERT INTO pagetocalendarmapping (page, calid) VALUES (".$this->sqlite->quote_and_join($values, ',').")";
122          $res = $this->sqlite->query($query);
123          return true;
124      }
125
126      return false;
127  }
128
129  public function addCalendarEntryToCalendarForPage($id, $user, $params)
130  {
131      require_once('vendor/autoload.php');
132      $vcalendar = new \Sabre\VObject\Component\VCalendar();
133      $event = $vcalendar->add('VEVENT');
134      $event->summary = $params['eventname'];
135      $dtStart = new \DateTime($params['eventfrom'], new \DateTimeZone('Europe/Vienna')); // FIXME: Timezone
136      $dtEnd = new \DateTime($params['eventto'], new \DateTimeZone('Europe/Vienna')); // FIXME: Timezone
137      $event->DTSTART = $dtStart;
138      $event->DTEND = $dtEnd;
139      $calid = $this->getCalendarIdForPage($id);
140      $uri = uniqid('dokuwiki-').'.ics';
141      $now = new DateTime();
142      $eventStr = $vcalendar->serialize();
143
144      $values = array($calid,
145                      $uri,
146                      $eventStr,
147                      $now->getTimestamp(),
148                      'VEVENT',
149                      $event->DTSTART->getDateTime()->getTimeStamp(),
150                      $event->DTEND->getDateTime()->getTimeStamp(),
151                      strlen($eventStr),
152                      md5($eventStr),
153                      uniqid()
154      );
155
156      $query = "INSERT INTO calendarobjects (calendarid, uri, calendardata, lastmodified, componenttype, firstoccurence, lastoccurence, size, etag, uid) VALUES (".$this->sqlite->quote_and_join($values, ',').")";
157      $res = $this->sqlite->query($query);
158      return true;
159  }
160
161  public function getEventsWithinDateRange($id, $user, $startDate, $endDate)
162  {
163      $data = array();
164      require_once('vendor/autoload.php');
165      $calid = $this->getCalendarIdForPage($id);
166      $startTs = new \DateTime($startDate);
167      $endTs = new \DateTime($endDate);
168      $query = "SELECT calendardata, componenttype, uid FROM calendarobjects WHERE calendarid=".
169                $this->sqlite->quote_string($calid)." AND firstoccurence > ".
170                $this->sqlite->quote_string($startTs->getTimestamp())." AND firstoccurence < ".
171                $this->sqlite->quote_string($endTs->getTimestamp());
172      $res = $this->sqlite->query($query);
173      $arr = $this->sqlite->res2arr($res);
174      foreach($arr as $row)
175      {
176          if(isset($row['calendardata']))
177          {
178              $vcal = \Sabre\VObject\Reader::read($row['calendardata']);
179              $start = $vcal->VEVENT->DTSTART->getDateTime();
180              $end = $vcal->VEVENT->DTEND->getDateTime();
181              $summary = (string)$vcal->VEVENT->summary;
182              $data[] = array("title" => $summary, "start" => $start->format(\DateTime::W3C),
183                              "end" => $end->format(\DateTime::W3C),
184                              "id" => $row['uid']);
185          }
186      }
187      return $data;
188  }
189
190  public function getEventWithUid($uid)
191  {
192      $query = "SELECT calendardata, componenttype FROM calendarobjects WHERE uid=".
193                $this->sqlite->quote_string($uid);
194      $res = $this->sqlite->query($query);
195      $row = $this->sqlite->res2row($res);
196      return $row;
197  }
198
199  public function editCalendarEntryForPage($id, $user, $params)
200  {
201      $event = $this->getEventWithUid($params['uid']);
202      require_once('vendor/autoload.php');
203      if(!isset($event['calendardata']))
204        return false;
205      $vcal = \Sabre\VObject\Reader::read($event['calendardata']);
206      $vcal->VEVENT->summary = $params['eventname'];
207      $dtStart = new \DateTime($params['eventfrom'], new \DateTimeZone('Europe/Vienna')); // FIXME: Timezone
208      $dtEnd = new \DateTime($params['eventto'], new \DateTimeZone('Europe/Vienna')); // FIXME: Timezone
209      $vcal->VEVENT->DTSTART = $dtStart;
210      $vcal->VEVENT->DTEND = $dtEnd;
211      $now = new DateTime();
212      $eventStr = $vcal->serialize();
213
214      $query = "UPDATE calendarobjects SET calendardata=".$this->sqlite->quote_string($eventStr).
215               ", lastmodified=".$this->sqlite->quote_string($now->getTimestamp()).
216               ", firstoccurence=".$this->sqlite->quote_string($dtStart->getTimestamp()).
217               ", lastoccurence=".$this->sqlite->quote_string($dtEnd->getTimestamp()).
218               ", size=".strlen($eventStr).
219               ", etag=".$this->sqlite->quote_string(md5($eventStr)).
220               " WHERE uid=".$this->sqlite->quote_string($params['uid']);
221      $res = $this->sqlite->query($query);
222      return true;
223  }
224
225  public function deleteCalendarEntryForPage($id, $params)
226  {
227      $uid = $params['uid'];
228      $query = "DELETE FROM calendarobjects WHERE uid=".$this->sqlite->quote_string($uid);
229      $res = $this->sqlite->query($query);
230      return true;
231  }
232
233}
234