xref: /plugin/davcal/action/ajax.php (revision 185e2535914616e98630b0777145a3e6d5c8d81b)
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      $params = $INPUT->post->arr('params');
34      $user = $_SERVER['REMOTE_USER'];
35      $write = false;
36      $multi = false;
37
38      $data = array();
39
40      $data['result'] = false;
41      $data['html'] = $this->getLang('unknown_error');
42
43      // Check if we have access to the calendar ($id is given by parameters,
44      // that's not necessarily the page we come from)
45      $acl = auth_quickaclcheck($id);
46      if($acl > AUTH_READ)
47      {
48          $write = true;
49      }
50
51      // Retrieve the calendar pages based on the meta data
52      $calendarPages = $this->hlp->getCalendarPagesByMeta($id);
53      if($calendarPages === false)
54      {
55          $calendarPages = array($id);
56      }
57      if(count($calendarPages) > 1)
58        $multi = true;
59
60      // Parse the requested action
61      switch($action)
62      {
63          // Add a new Event
64          case 'newEvent':
65              if($write)
66              {
67                  $data['result'] = true;
68                  $data['html'] = $this->getLang('event_added');
69                  $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
70              }
71              else
72              {
73                  $data['result'] = false;
74                  $data['html'] = $this->getLang('no_permission');
75              }
76          break;
77          // Retrieve existing Events
78          case 'getEvents':
79              $startDate = $INPUT->post->str('start');
80              $endDate = $INPUT->post->str('end');
81              $data = array();
82              foreach($calendarPages as $page)
83              {
84                  $data = array_merge($data, $this->hlp->getEventsWithinDateRange($page,
85                                      $user, $startDate, $endDate));
86              }
87          break;
88          // Edit an event
89          case 'editEvent':
90              if($write)
91              {
92                  $data['result'] = true;
93                  $data['html'] = $this->getLang('event_edited');
94                  $this->hlp->editCalendarEntryForPage($id, $user, $params);
95              }
96              else
97              {
98                  $data['result'] = false;
99                  $data['html'] = $this->getLang('no_permission');
100              }
101          break;
102          // Delete an Event
103          case 'deleteEvent':
104              if($write)
105              {
106                  $data['result'] = true;
107                  $data['html'] = $this->getLang('event_deleted');
108                  $this->hlp->deleteCalendarEntryForPage($id, $params);
109              }
110              else
111              {
112                  $data['result'] = false;
113                  $data['html'] = $this->getLang('no_permission');
114              }
115          break;
116          // Get personal settings
117          case 'getSettings':
118              $data['result'] = true;
119              $data['settings'] = $this->hlp->getPersonalSettings($user);
120              $data['settings']['multi'] = $multi;
121              $data['settings']['calids'] = $this->hlp->getCalendarMapForIDs($calendarPages);
122              $data['settings']['readonly'] = !$write;
123              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($id, $user);
124              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($id);
125          break;
126          // Save personal settings
127          case 'saveSettings':
128              $settings = array();
129              $settings['weeknumbers'] = $params['weeknumbers'];
130              $settings['timezone'] = $params['timezone'];
131              $settings['workweek'] = $params['workweek'];
132              $settings['monday'] = $params['monday'];
133              if($this->hlp->savePersonalSettings($settings, $user))
134              {
135                  $data['result'] = true;
136                  $data['html'] = $this->getLang('settings_saved');
137              }
138              else
139              {
140                  $data['result'] = false;
141                  $data['html'] = $this->getLang('error_saving');
142              }
143          break;
144      }
145
146      // If we are still here, JSON output is requested
147
148      //json library of DokuWiki
149      require_once DOKU_INC . 'inc/JSON.php';
150      $json = new JSON();
151
152      //set content type
153      header('Content-Type: application/json');
154      echo $json->encode($data);
155    }
156
157}