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