1<?php 2/** 3 * DokuWiki Plugin asyncsearch (Action Component) 4 * 5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6 * @author i-net software / Gerry Weißbach <tools@inetsoftware.de> 7 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12class action_plugin_asyncsearch_asyncsearch extends DokuWiki_Action_Plugin { 13 14 /** 15 * Registers a callback function for a given event 16 * 17 * @param Doku_Event_Handler $controller DokuWiki's event controller object 18 * @return void 19 */ 20 public function register(Doku_Event_Handler $controller) { 21 $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle_action_act_preprocess'); 22 $controller->register_hook('JS_SCRIPT_LIST', 'BEFORE', $this, 'handle_js_script_list'); 23 $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'handle_action_tpl_act_renderer'); 24 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'handle_tpl_metaheader_output'); 25 } 26 27 /** 28 * [Custom event handler which performs action] 29 * 30 * @param Doku_Event $event event object by reference 31 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 32 * handler was registered] 33 * @return void 34 */ 35 36 public function handle_action_act_preprocess( Doku_Event &$event, $param) { 37 global $ACT; 38 global $QUERY; 39 40 if ( $event->data === 'search' && !empty( $QUERY ) ) { 41 $ACT = 'asyncsearch'; 42 $event->preventDefault(); 43 return false; 44 } 45 } 46 47 /** 48 * [Custom event handler which performs action] 49 * 50 * @param Doku_Event $event event object by reference 51 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 52 * handler was registered] 53 * @return void 54 */ 55 56 public function handle_action_tpl_act_renderer( Doku_Event &$event, $param) { 57 global $ACT, $QUERY, $ID; 58 global $lang; 59 60 if ( $ACT !== 'asyncsearch' ) { return; } 61 62 $intro = p_locale_xhtml('searchpage'); 63 // allow use of placeholder in search intro 64 $pagecreateinfo = (auth_quickaclcheck($ID) >= AUTH_CREATE) ? $lang['searchcreatepage'] : ''; 65 $intro = str_replace( 66 array('@QUERY@', '@SEARCH@', '@CREATEPAGEINFO@'), 67 array(hsc(rawurlencode($QUERY)), hsc($QUERY), $pagecreateinfo), 68 $intro 69 ); 70 71 echo $intro; 72 flush(); 73 echo '<div id="asyncsearch" data-term="'.hsc($QUERY).'"></div>'; 74 75 $event->preventDefault(); 76 return false; 77 } 78 79 /** 80 * Insert an extra script tag for users that have AUTH_EDIT or better 81 * 82 * @param Doku_Event $event event object by reference 83 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 84 * handler was registered] 85 * @return void 86 */ 87 public function handle_tpl_metaheader_output( Doku_Event &$event, $param) { 88 global $ACT; 89 90 // add script if user has better auth than AUTH_EDIT 91 if ( $ACT !== 'asyncsearch' ) { return; } 92 93 $event->data['script'][] = array( 94 'type'=> 'text/javascript', 'charset'=> 'utf-8', '_data'=> '', 'defer'=> 'defer', 95 'src' => DOKU_BASE.'lib/exe/js.php'.'?type=asyncsearch&tseed='.$tseed 96 ); 97 } 98 99 /** 100 * Finally, handle the JS script list. The script would be fit to do even more stuff / types 101 * but handles only admin and default currently. 102 * 103 * @param Doku_Event $event event object by reference 104 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 105 * handler was registered] 106 * @return void 107 */ 108 public function handle_js_script_list( Doku_Event &$event, $param) { 109 global $INPUT; 110 111 if ( $INPUT->str('type') === 'asyncsearch' ) { 112 $event->data = $this->js_pluginscripts(); 113 sort($event->data); 114 } 115 } 116 117 /** 118 * Returns a list of possible Plugin Scripts (no existance check here) 119 * @return array 120 */ 121 private function js_pluginscripts(){ 122 $list = array(); 123 $plugins = plugin_list(); 124 foreach ($plugins as $p){ 125 $list[] = DOKU_PLUGIN."$p/asyncsearch.js"; 126 } 127 return $list; 128 } 129} 130 131// vim:ts=4:sw=4:et: 132