1346619c6SAndreas Gohr<?php 2346619c6SAndreas Gohr/** 3346619c6SAndreas Gohr * DokuWiki Plugin quicksubscribe (Action Component) 4346619c6SAndreas Gohr * 5346619c6SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6346619c6SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 7346619c6SAndreas Gohr */ 8346619c6SAndreas Gohr 9*e02bd35eSAndreas Gohrclass action_plugin_quicksubscribe extends DokuWiki_Action_Plugin 10*e02bd35eSAndreas Gohr{ 11*e02bd35eSAndreas Gohr /** @inheritdoc */ 12*e02bd35eSAndreas Gohr function register(Doku_Event_Handler $controller) 13*e02bd35eSAndreas Gohr { 14346619c6SAndreas Gohr $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown'); 15346619c6SAndreas Gohr } 16346619c6SAndreas Gohr 17*e02bd35eSAndreas Gohr /** 18*e02bd35eSAndreas Gohr * Handle subscription/unsubscription AJAX events 19*e02bd35eSAndreas Gohr * 20*e02bd35eSAndreas Gohr * @param Doku_Event $event 21*e02bd35eSAndreas Gohr * @param $param 22*e02bd35eSAndreas Gohr */ 23*e02bd35eSAndreas Gohr function handle_ajax_call_unknown(Doku_Event $event, $param) 24*e02bd35eSAndreas Gohr { 25346619c6SAndreas Gohr if ($event->data != 'plugin_quicksubscribe') return; 26346619c6SAndreas Gohr $event->preventDefault(); 27346619c6SAndreas Gohr $event->stopPropagation(); 28346619c6SAndreas Gohr 29346619c6SAndreas Gohr global $INPUT; 30346619c6SAndreas Gohr 31346619c6SAndreas Gohr $ns = cleanID($INPUT->str('ns')) . ':'; // we only handle namespaces 32346619c6SAndreas Gohr $do = $INPUT->str('do'); 33346619c6SAndreas Gohr 34346619c6SAndreas Gohr $ok = false; 35346619c6SAndreas Gohr $msg = ''; 36346619c6SAndreas Gohr 37346619c6SAndreas Gohr $sub = new Subscription(); 38346619c6SAndreas Gohr 39346619c6SAndreas Gohr if ($do == 'subscribe') { 40346619c6SAndreas Gohr // new subscriptions 41*e02bd35eSAndreas Gohr try { 42346619c6SAndreas Gohr $ok = $sub->add($ns, $_SERVER['REMOTE_USER'], 'list'); 43*e02bd35eSAndreas Gohr } catch (\Exception $ignored) { 44*e02bd35eSAndreas Gohr $ok = false; 45*e02bd35eSAndreas Gohr } 46346619c6SAndreas Gohr if ($ok) { 47*e02bd35eSAndreas Gohr $msg = sprintf($this->getLang('sub_succ'), prettyprint_id($ns)); 48346619c6SAndreas Gohr } else { 49*e02bd35eSAndreas Gohr $msg = sprintf($this->getLang('sub_fail'), prettyprint_id($ns)); 50346619c6SAndreas Gohr } 51346619c6SAndreas Gohr } elseif ($do == 'unsubscribe') { 52346619c6SAndreas Gohr // subscription removal 53346619c6SAndreas Gohr $ok = $sub->remove($ns, $_SERVER['REMOTE_USER']); 54346619c6SAndreas Gohr if ($ok) { 55*e02bd35eSAndreas Gohr $msg = sprintf($this->getLang('unsub_succ'), prettyprint_id($ns)); 56346619c6SAndreas Gohr } else { 57*e02bd35eSAndreas Gohr $msg = sprintf($this->getLang('unsub_fail'), prettyprint_id($ns)); 58346619c6SAndreas Gohr } 59346619c6SAndreas Gohr } 60346619c6SAndreas Gohr 61346619c6SAndreas Gohr if (!$ok) http_status(400); 62346619c6SAndreas Gohr echo '<p>' . $msg . '</p>'; 63346619c6SAndreas Gohr } 64346619c6SAndreas Gohr} 65