xref: /plugin/davcal/action/ajax.php (revision b0440c91c34b966f16a3247f6ee110053f678a98)
1<?php
2
3/**
4 * DokuWiki DAVCal PlugIn - Ajax component
5 */
6
7if(!defined('DOKU_INC')) die();
8
9class action_plugin_davcal_ajax extends DokuWiki_Action_Plugin {
10
11    /**
12     * @var helper_plugin_davcal
13     */
14    private $hlp = null;
15
16    function __construct() {
17        $this->hlp =& plugin_load('helper','davcal');
18    }
19
20    function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
22    }
23
24    function handle_ajax_call_unknown(&$event, $param) {
25      if($event->data != 'plugin_davcal') return;
26
27      $event->preventDefault();
28      $event->stopPropagation();
29      global $INPUT;
30
31      $action = trim($INPUT->post->str('action'));
32      $id = trim($INPUT->post->str('id'));
33      $page = trim($INPUT->post->str('page'));
34      $params = $INPUT->post->arr('params');
35      if(isset($_SERVER['REMOTE_USER']) && !is_null($_SERVER['REMOTE_USER']))
36        $user = $_SERVER['REMOTE_USER'];
37      else
38        $user = null;
39      $write = false;
40      $multi = false;
41
42      $data = array();
43
44      $data['result'] = false;
45      $data['html'] = $this->getLang('unknown_error');
46
47      // Check if we have access to the calendar ($id is given by parameters,
48      // that's not necessarily the page we come from)
49      $acl = auth_quickaclcheck($id);
50      if($acl > AUTH_READ)
51      {
52          $write = true;
53      }
54      elseif($acl < AUTH_READ)
55      {
56          $data['result'] = false;
57          $data['html'] = $this->getLang('no_permission');
58          // Set to an invalid action in order to just return the result
59          $action = 'invalid';
60      }
61
62      // Retrieve the calendar pages based on the meta data
63      $calendarPages = $this->hlp->getCalendarPagesByMeta($page);
64      if($calendarPages === false)
65      {
66          $calendarPages = array($page => null);
67      }
68      if(count($calendarPages) > 1)
69        $multi = true;
70
71      // Parse the requested action
72      switch($action)
73      {
74          // Add a new Event
75          case 'newEvent':
76              if($write)
77              {
78                  $res = $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
79                  if($res === true)
80                  {
81                    $data['result'] = true;
82                    $data['html'] = $this->getLang('event_added');
83                  }
84                  else
85                  {
86                    $data['result'] = false;
87                    $data['html'] = $this->getLang('unknown_error');
88                  }
89              }
90              else
91              {
92                  $data['result'] = false;
93                  $data['html'] = $this->getLang('no_permission');
94              }
95          break;
96          // Retrieve existing Events
97          case 'getEvents':
98              $startDate = $INPUT->post->str('start');
99              $endDate = $INPUT->post->str('end');
100              $timezone = $INPUT->post->str('timezone');
101              $data = array();
102              foreach($calendarPages as $calPage => $color)
103              {
104                  $data = array_merge($data, $this->hlp->getEventsWithinDateRange($calPage,
105                                      $user, $startDate, $endDate, $timezone, $color));
106              }
107          break;
108          // Edit an event
109          case 'editEvent':
110              if($write)
111              {
112                  $res = $this->hlp->editCalendarEntryForPage($id, $user, $params);
113                  if($res === true)
114                  {
115                    $data['result'] = true;
116                    $data['html'] = $this->getLang('event_edited');
117                  }
118                  else
119                  {
120                    $data['result'] = false;
121                    $data['html'] = $this->getLang('unknown_error');
122                  }
123              }
124              else
125              {
126                  $data['result'] = false;
127                  $data['html'] = $this->getLang('no_permission');
128              }
129          break;
130          // Delete an Event
131          case 'deleteEvent':
132              if($write)
133              {
134                  $res = $this->hlp->deleteCalendarEntryForPage($id, $params);
135                  if($res === true)
136                  {
137                    $data['result'] = true;
138                    $data['html'] = $this->getLang('event_deleted');
139                  }
140                  else
141                  {
142                    $data['result'] = false;
143                    $data['html'] = $this->getLang('unknown_error');
144                  }
145              }
146              else
147              {
148                  $data['result'] = false;
149                  $data['html'] = $this->getLang('no_permission');
150              }
151          break;
152          // Get personal settings
153          case 'getSettings':
154              $data['result'] = true;
155              $data['settings'] = $this->hlp->getPersonalSettings($user);
156              $data['settings']['multi'] = $multi;
157              $data['settings']['calids'] = $this->hlp->getCalendarMapForIDs($calendarPages);
158              $data['settings']['readonly'] = !$write;
159              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($page, $user);
160              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($page);
161              $data['settings']['principalurl'] = $this->hlp->getPrincipalUrlForUser($user);
162              $data['settings']['meta'] = $this->hlp->getCalendarMetaForPage($page);
163          break;
164          // Save personal settings
165          case 'saveSettings':
166              $settings = array();
167              $settings['weeknumbers'] = $params['weeknumbers'];
168              $settings['timezone'] = $params['timezone'];
169              $settings['workweek'] = $params['workweek'];
170              $settings['monday'] = $params['monday'];
171              $settings['timeformat'] = $params['timeformat'];
172              if($this->hlp->savePersonalSettings($settings, $user))
173              {
174                  $data['result'] = true;
175                  $data['html'] = $this->getLang('settings_saved');
176              }
177              else
178              {
179                  $data['result'] = false;
180                  $data['html'] = $this->getLang('error_saving');
181              }
182          break;
183      }
184
185      // If we are still here, JSON output is requested
186
187      //json library of DokuWiki
188      require_once DOKU_INC . 'inc/JSON.php';
189      $json = new JSON();
190
191      //set content type
192      header('Content-Type: application/json');
193      echo $json->encode($data);
194    }
195
196}
197