xref: /plugin/davcal/action/ajax.php (revision 80e1ddf76ff9879b01dd835b0e45e33631480f72)
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);
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                  $data['result'] = true;
79                  $data['html'] = $this->getLang('event_added');
80                  $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
81              }
82              else
83              {
84                  $data['result'] = false;
85                  $data['html'] = $this->getLang('no_permission');
86              }
87          break;
88          // Retrieve existing Events
89          case 'getEvents':
90              $startDate = $INPUT->post->str('start');
91              $endDate = $INPUT->post->str('end');
92              $timezone = $INPUT->post->str('timezone');
93              $data = array();
94              foreach($calendarPages as $calPage)
95              {
96                  $data = array_merge($data, $this->hlp->getEventsWithinDateRange($calPage,
97                                      $user, $startDate, $endDate, $timezone));
98              }
99          break;
100          // Edit an event
101          case 'editEvent':
102              if($write)
103              {
104                  $data['result'] = true;
105                  $data['html'] = $this->getLang('event_edited');
106                  $this->hlp->editCalendarEntryForPage($id, $user, $params);
107              }
108              else
109              {
110                  $data['result'] = false;
111                  $data['html'] = $this->getLang('no_permission');
112              }
113          break;
114          // Delete an Event
115          case 'deleteEvent':
116              if($write)
117              {
118                  $data['result'] = true;
119                  $data['html'] = $this->getLang('event_deleted');
120                  $this->hlp->deleteCalendarEntryForPage($id, $params);
121              }
122              else
123              {
124                  $data['result'] = false;
125                  $data['html'] = $this->getLang('no_permission');
126              }
127          break;
128          // Get personal settings
129          case 'getSettings':
130              $data['result'] = true;
131              $data['settings'] = $this->hlp->getPersonalSettings($user);
132              $data['settings']['multi'] = $multi;
133              $data['settings']['calids'] = $this->hlp->getCalendarMapForIDs($calendarPages);
134              $data['settings']['readonly'] = !$write;
135              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($page, $user);
136              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($page);
137              $data['settings']['principalurl'] = $this->hlp->getPrincipalUrlForUser($user);
138              $data['settings']['meta'] = $this->hlp->getCalendarMetaForPage($page);
139          break;
140          // Save personal settings
141          case 'saveSettings':
142              $settings = array();
143              $settings['weeknumbers'] = $params['weeknumbers'];
144              $settings['timezone'] = $params['timezone'];
145              $settings['workweek'] = $params['workweek'];
146              $settings['monday'] = $params['monday'];
147              $settings['timeformat'] = $params['timeformat'];
148              if($this->hlp->savePersonalSettings($settings, $user))
149              {
150                  $data['result'] = true;
151                  $data['html'] = $this->getLang('settings_saved');
152              }
153              else
154              {
155                  $data['result'] = false;
156                  $data['html'] = $this->getLang('error_saving');
157              }
158          break;
159      }
160
161      // If we are still here, JSON output is requested
162
163      //json library of DokuWiki
164      require_once DOKU_INC . 'inc/JSON.php';
165      $json = new JSON();
166
167      //set content type
168      header('Content-Type: application/json');
169      echo $json->encode($data);
170    }
171
172}