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