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_pagelookup 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('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call_unknown');
22    }
23
24    /**
25     * [Custom event handler which performs action]
26     *
27     * @param Doku_Event $event  event object by reference
28     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
29     *                           handler was registered]
30     * @return void
31     */
32    public function handle_ajax_call_unknown( Doku_Event &$event, $param) {
33        global $ACT, $INPUT;
34
35        if ( $event->data === 'asyncsearch' && $INPUT->str('pluginID') == 'pagelookup' ) {
36            $this->handle_ft_pageLookup( $INPUT->str('term') );
37        } else
38        if ( $event->data === 'asyncsearch' && $INPUT->str('pluginID') == 'pagesearch' ) {
39            $this->handle_ft_pageSearch( $INPUT->str('term') );
40        } else {
41            return true;
42        }
43
44        $event->preventDefault();
45        return false;
46    }
47
48    /**
49     * [Custom event handler which performs action]
50     *
51     * @param Doku_Event $event  event object by reference
52     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
53     *                           handler was registered]
54     * @return void
55     */
56    private function handle_ft_pageLookup( $QUERY ) {
57        global $lang;
58
59        //do quick pagesearch
60        $data = ft_pageLookup($QUERY,true,useHeading('navigation'));
61        if(count($data)){
62            print '<div class="search_quickresult">';
63            print '<h3>'.$lang['quickhits'].':</h3>';
64            print '<ul class="search_quickhits">';
65            foreach($data as $id => $title){
66                print '<li> ';
67                if (useHeading('navigation')) {
68                    $name = $title;
69                }else{
70                    $ns = getNS($id);
71                    if($ns){
72                        $name = shorten(noNS($id), ' ('.$ns.')',30);
73                    }else{
74                        $name = $id;
75                    }
76                }
77                print html_wikilink(':'.$id,$name);
78                print '</li> ';
79            }
80            print '</ul> ';
81            //clear float (see http://www.complexspiral.com/publications/containing-floats/)
82            print '<div class="clearer"></div>';
83            print '</div>';
84        }
85    }
86
87    /**
88     * [Custom event handler which performs action]
89     *
90     * @param Doku_Event $event  event object by reference
91     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
92     *                           handler was registered]
93     * @return void
94     */
95    private function handle_ft_pageSearch( $QUERY ) {
96        global $lang;
97
98        //do fulltext search
99        $data = ft_pageSearch($QUERY,$regex);
100        if(count($data)){
101            print '<dl class="search_results">';
102            $num = 1;
103            foreach($data as $id => $cnt){
104                print '<dt>';
105                print html_wikilink(':'.$id,useHeading('navigation')?null:$id,$regex);
106                if($cnt !== 0){
107                    print ': '.$cnt.' '.$lang['hits'].'';
108                }
109                print '</dt>';
110                if($cnt !== 0){
111                    if($num < FT_SNIPPET_NUMBER){ // create snippets for the first number of matches only
112                        print '<dd>'.ft_snippet($id,$regex).'</dd>';
113                    }
114                    $num++;
115                }
116                flush();
117            }
118            print '</dl>';
119        }else{
120            print '<div class="nothing">'.$lang['nothingfound'].'</div>';
121        }
122    }
123
124}
125
126// vim:ts=4:sw=4:et:
127