xref: /plugin/davcal/action/ajax.php (revision f69bb4491e1a8930c2218737f8703c41c7507247)
1<?php
2
3if(!defined('DOKU_INC')) die();
4
5class action_plugin_davcal_ajax extends DokuWiki_Action_Plugin {
6
7    /**
8     * @var helper_plugin_publish
9     */
10    private $hlp = null;
11
12    function __construct() {
13        $this->hlp =& plugin_load('helper','davcal');
14    }
15
16    function register(Doku_Event_Handler $controller) {
17        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
18    }
19
20    function handle_ajax_call_unknown(&$event, $param) {
21      if($event->data != 'plugin_davcal') return;
22
23      $event->preventDefault();
24      $event->stopPropagation();
25      global $INPUT;
26
27      $action = trim($INPUT->post->str('action'));
28      $id = trim($INPUT->post->str('id'));
29      $params = $INPUT->post->arr('params');
30      $user = $_SERVER['REMOTE_USER'];
31      $write = false;
32
33      $data = array();
34
35      $data['result'] = false;
36      $data['html'] = $this->getLang('unknown_error');
37
38      $acl = auth_quickaclcheck($id);
39      if($acl > AUTH_READ)
40      {
41          $write = true;
42      }
43
44      switch($action)
45      {
46          case 'newEvent':
47              if($write)
48              {
49                  $data['result'] = true;
50                  $data['html'] = $this->getLang('event_added');
51                  $this->hlp->addCalendarEntryToCalendarForPage($id, $user, $params);
52              }
53              else
54              {
55                  $data['result'] = false;
56                  $data['html'] = $this->getLang('no_permission');
57              }
58          break;
59          case 'getEvents':
60              $startDate = $INPUT->post->str('start');
61              $endDate = $INPUT->post->str('end');
62              $data = $this->hlp->getEventsWithinDateRange($id, $user, $startDate, $endDate);
63
64          break;
65          case 'editEvent':
66              if($write)
67              {
68                  $data['result'] = true;
69                  $data['html'] = $this->getLang('event_edited');
70                  $this->hlp->editCalendarEntryForPage($id, $user, $params);
71              }
72              else
73              {
74                  $data['result'] = false;
75                  $data['html'] = $this->getLang('no_permission');
76              }
77          break;
78          case 'deleteEvent':
79              if($write)
80              {
81                  $data['result'] = true;
82                  $data['html'] = $this->getLang('event_deleted');
83                  $this->hlp->deleteCalendarEntryForPage($id, $params);
84              }
85              else
86              {
87                  $data['result'] = false;
88                  $data['html'] = $this->getLang('no_permission');
89              }
90          break;
91          case 'getSettings':
92              $data['result'] = true;
93              $data['settings'] = $this->hlp->getPersonalSettings($user);
94              $data['settings']['syncurl'] = $this->hlp->getSyncUrlForPage($id, $user);
95              $data['settings']['privateurl'] = $this->hlp->getPrivateURLForPage($id);
96          break;
97          case 'saveSettings':
98              $settings = array();
99              $settings['weeknumbers'] = $params['weeknumbers'];
100              $settings['timezone'] = $params['timezone'];
101              $settings['workweek'] = $params['workweek'];
102              if($this->hlp->savePersonalSettings($settings, $user))
103              {
104                  $data['result'] = true;
105                  $data['html'] = $this->getLang('settings_saved');
106              }
107              else
108              {
109                  $data['result'] = false;
110                  $data['html'] = $this->getLang('error_saving');
111              }
112          break;
113      }
114
115
116
117
118      // If we are still here, JSON output is requested
119
120      //json library of DokuWiki
121      require_once DOKU_INC . 'inc/JSON.php';
122      $json = new JSON();
123
124      //set content type
125      header('Content-Type: application/json');
126      echo $json->encode($data);
127    }
128
129}