]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/ExternalSearch.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / ExternalSearch.php
1 <?php // -*-php-*-
2 // $Id$
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  * Redirects 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 getName () {
40         return _("ExternalSearch");
41     }
42
43     function getDescription () {
44         return _("Redirects to an external web site based on form input");
45         //fixme: better description
46     }
47
48     function _getInterWikiUrl(&$request) {
49         $intermap = getInterwikiMap();
50         $map = $intermap->_map;
51
52         if (in_array($this->_url, array_keys($map))) {
53             if (empty($this->_name))
54                 $this->_name = $this->_url;
55             $this->_url = sprintf($map[$this->_url], '%s');
56         }
57         if (empty($this->_name))
58             $this->_name = $this->getName();
59     }
60
61     function getDefaultArguments() {
62         return array('s'        => false,
63                      'formsize' => 30,
64                      'url'      => false,
65                      'name'     => '',
66                      'useimage' => false,
67                      'width'    => false,
68                      'height'   => false,
69                      'debug'    => false,
70                      'button_position' => EXTERNALSEARCH_DEFAULT_BUTTON_POSITION,
71                      // 'left' or 'right'
72                      );
73     }
74
75     function run($dbi, $argstr, &$request, $basepage) {
76         $args = $this->getArgs($argstr, $request);
77         if (empty($args['url']))
78             return '';
79
80         extract($args);
81
82         $posted = $GLOBALS['HTTP_POST_VARS'];
83         if (in_array('url', array_keys($posted))) {
84             $s = $posted['s'];
85             $this->_url = $posted['url'];
86             $this->_getInterWikiUrl($request);
87             if (strstr($this->_url, '%s')) {
88                 $this->_url = sprintf($this->_url, $s);
89             } else
90                 $this->_url .= $s;
91             if (defined('DEBUG') && DEBUG && $debug) {
92                 trigger_error("redirect url: " . $this->_url);
93             } else {
94                 $request->redirect($this->_url); //no return!
95             }
96         }
97         $this->_name = $name;
98         $this->_s = $s;
99         if ($formsize < 1)
100             $formsize = 30;
101         $this->_url = $url;
102         $this->_getInterWikiUrl($request);
103         $form = HTML::form(array('action' => $request->getPostURL(),
104                                  'method' => 'post',
105                                  //'class'  => 'class', //fixme
106                                  'accept-charset' => $GLOBALS['charset']),
107                            HiddenInputs(array('pagename' => $basepage)));
108
109         $form->pushContent(HTML::input(array('type' => 'hidden',
110                                              'name'  => 'url',
111                                              'value' => $this->_url)));
112         $s = HTML::input(array('type' => 'text',
113                                'value' => $this->_s,
114                                'name'  => 's',
115                                'size'  => $formsize));
116         if (!empty($args["useimage"])) {
117             //FIXME: This does not work with Gecko
118             $button = HTML::img(array('src' => $useimage, 'alt' => 'imagebutton'));
119             if (!empty($width))
120                 $button->setAttr('width',$width);
121             if (!empty($height))
122                 $button->setAttr('height',$height);
123             // on button_position => none display no input form
124             if ($button_position == 'right')
125                 $form->pushContent($s);
126             $form->pushContent(HTML::button(array('type' => 'button',
127                                                   'class' => 'button',
128                                                   'value' => $this->_name,
129                                                   ),
130                                             $button));
131             if ($button_position == 'left')
132                 $form->pushContent($s);
133         } else {
134             if ($button_position != 'left' and $button_position != 'right')
135                 return $this->error(fmt("Invalid argument: %s=%s",
136                                         'button_position', $button_position));
137             $button = HTML::input(array('type' => 'submit',
138                                         'class' => 'button',
139                                         'value' => $this->_name));
140             if ($button_position == 'left') {
141                 $form->pushContent($button);
142                 $form->pushContent($s);
143             } elseif ($button_position == 'right') {
144                 $form->pushContent($s);
145                 $form->pushContent($button);
146             }
147         }
148         return $form;
149     }
150 };
151
152 // Local Variables:
153 // mode: php
154 // tab-width: 8
155 // c-basic-offset: 4
156 // c-hanging-comment-ender-p: nil
157 // indent-tabs-mode: nil
158 // End:
159 ?>