]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/ExternalSearch.php
function run: @return mixed
[SourceForge/phpwiki.git] / lib / plugin / ExternalSearch.php
1 <?php
2
3 /**
4  * Copyright 1999,2000,2001,2002,2006 $ThePhpWikiProgrammingTeam
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * Redirect to an external web site based on form input.
25  * See http://phpwiki.sourceforge.net/phpwiki/ExternalSearchPlugin
26  *
27  * useimage sample:
28 ExternalSearch
29 url="http://www.geourl.org/near/?xsize=2048&ysize=1024&xoffset=1650&yoffset=550"
30 useimage="http://www.geourl.org/maps/au.png"
31 name="Go Godzilla All Over It"
32  */
33 if (!defined("EXTERNALSEARCH_DEFAULT_BUTTON_POSITION"))
34     define("EXTERNALSEARCH_DEFAULT_BUTTON_POSITION", "right");
35
36 class WikiPlugin_ExternalSearch
37     extends WikiPlugin
38 {
39     function getDescription()
40     {
41         return _("Redirect to an external web site based on form input.");
42         //fixme: better description
43     }
44
45     private function getInterWikiUrl()
46     {
47         $intermap = getInterwikiMap();
48         $map = $intermap->_map;
49
50         if (in_array($this->_url, array_keys($map))) {
51             if (empty($this->_name))
52                 $this->_name = $this->_url;
53             $this->_url = sprintf($map[$this->_url], '%s');
54         }
55         if (empty($this->_name))
56             $this->_name = $this->getName();
57     }
58
59     function getDefaultArguments()
60     {
61         return array('s' => false,
62             'formsize' => 30,
63             'url' => false,
64             'name' => '',
65             'useimage' => false,
66             'width' => false,
67             'height' => false,
68             'debug' => false,
69             'button_position' => EXTERNALSEARCH_DEFAULT_BUTTON_POSITION,
70             // 'left' or 'right'
71         );
72     }
73
74     /**
75      * @param WikiDB $dbi
76      * @param string $argstr
77      * @param WikiRequest $request
78      * @param string $basepage
79      * @return mixed
80      */
81     function run($dbi, $argstr, &$request, $basepage)
82     {
83         $args = $this->getArgs($argstr, $request);
84         if (empty($args['url']))
85             return '';
86
87         extract($args);
88
89         $posted = $GLOBALS['HTTP_POST_VARS'];
90         if (in_array('url', array_keys($posted))) {
91             $s = $posted['s'];
92             $this->_url = $posted['url'];
93             $this->getInterWikiUrl();
94             if (strstr($this->_url, '%s')) {
95                 $this->_url = sprintf($this->_url, $s);
96             } else
97                 $this->_url .= $s;
98             if (defined('DEBUG') && DEBUG && $debug) {
99                 trigger_error("redirect url: " . $this->_url);
100             } else {
101                 $request->redirect($this->_url); //no return!
102             }
103         }
104         $this->_name = $name;
105         $this->_s = $s;
106         if ($formsize < 1)
107             $formsize = 30;
108         $this->_url = $url;
109         $this->getInterWikiUrl();
110         $form = HTML::form(array('action' => $request->getPostURL(),
111                 'method' => 'post',
112                 //'class'  => 'class', //fixme
113                 'accept-charset' => 'UTF-8'),
114             HiddenInputs(array('pagename' => $basepage)));
115
116         $form->pushContent(HTML::input(array('type' => 'hidden',
117             'name' => 'url',
118             'value' => $this->_url)));
119         $s = HTML::input(array('type' => 'text',
120             'value' => $this->_s,
121             'name' => 's',
122             'size' => $formsize));
123         if (!empty($args["useimage"])) {
124             //FIXME: This does not work with Gecko
125             $button = HTML::img(array('src' => $useimage, 'alt' => 'imagebutton'));
126             if (!empty($width))
127                 $button->setAttr('width', $width);
128             if (!empty($height))
129                 $button->setAttr('height', $height);
130             // on button_position => none display no input form
131             if ($button_position == 'right')
132                 $form->pushContent($s);
133             $form->pushContent(HTML::button(array('type' => 'button',
134                     'class' => 'button',
135                     'value' => $this->_name,
136                 ),
137                 $button));
138             if ($button_position == 'left')
139                 $form->pushContent($s);
140         } else {
141             if ($button_position != 'left' and $button_position != 'right')
142                 return $this->error(fmt("Invalid argument: %s=%s",
143                     'button_position', $button_position));
144             $button = HTML::input(array('type' => 'submit',
145                 'class' => 'button',
146                 'value' => $this->_name));
147             if ($button_position == 'left') {
148                 $form->pushContent($button);
149                 $form->pushContent($s);
150             } elseif ($button_position == 'right') {
151                 $form->pushContent($s);
152                 $form->pushContent($button);
153             }
154         }
155         return $form;
156     }
157 }
158
159 // Local Variables:
160 // mode: php
161 // tab-width: 8
162 // c-basic-offset: 4
163 // c-hanging-comment-ender-p: nil
164 // indent-tabs-mode: nil
165 // End: