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