3 * FusionForge globalsearch plugin
5 * Copyright 2003-2004 GForge, LLC
6 * Copyright 2007-2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * $gwords = target words to search
29 * $gexact = 1 for search ing all words (AND), 0 - for any word (OR)
30 * $otherfreeknowledge = 1 for search in Free/Libre Knowledge Gforge Initiatives
31 * $order = "project_title" or "title" - criteria for ordering results: if empty or not allowed results are ordered by rank
35 require_once '../../env.inc.php';
36 require_once $gfcommon.'include/pre.php';
38 $otherfreeknowledge = getIntFromRequest('otherfreeknowledge') ;
39 $gwords = getStringFromRequest('gwords');
40 $order = getStringFromRequest('order', 'rank');
41 util_ensure_value_in_set ($order,
44 'project_description',
46 $offset = getIntFromRequest('offset');
47 $gexact = getStringFromRequest('gexact');
49 // Support for short aliases
50 if (!$otherfreeknowledge) {
51 $otherfreeknowledge = 0;
55 $onlysw = "AND onlysw = 'f' ";
58 function highlight_target_words($word_array,$text) {
62 $re=implode($word_array,'|');
63 return preg_replace("/($re)/i",'<span style="background-color:pink">\1</span>',$text);
66 $HTML->header(array('title'=>'Search','pagename'=>'search'));
70 $gwords = htmlspecialchars(trim($gwords));
71 $gwords = preg_replace("/\s+/", ' ', $gwords);
73 // show search box which will return results on
74 // this very page (default is to open new window)
75 $gsplugin = plugin_get_object ('globalsearch') ;
76 echo $gsplugin->search_box ();
79 Force them to enter at least three characters
82 if ($gwords && (strlen($gwords) < 3)) {
83 echo "<h2>"._("Search must be at least three characters")."</h2>";
84 $HTML->footer(array());
89 echo "<br /><b>"._("Enter Your Search Words Above")."</b></p>";
90 $HTML->footer(array());
96 if (!$offset || $offset < 0) {
101 Query to find projects
104 $array = explode(" ",$gwords);
106 $qpa = db_construct_qpa (false, 'SELECT project_title, project_link, project_description, title, link FROM plugin_globalsearch_assoc_site_project, plugin_globalsearch_assoc_site WHERE plugin_globalsearch_assoc_site_project.assoc_site_id = plugin_globalsearch_assoc_site.assoc_site_id AND enabled = $1 AND status_id = 2',
109 if ($otherfreeknowledge) {
110 $qpa = db_construct_qpa ($qpa, ' AND onlysw = $1', array ('f')) ;
113 $qpa = db_construct_qpa ($qpa, ' AND ((') ;
116 foreach ($array as $val) {
119 $qpa = db_construct_qpa ($qpa, ' AND ') ;
121 $qpa = db_construct_qpa ($qpa, ' OR ') ;
126 $qpa = db_construct_qpa ($qpa, 'lower(project_title) LIKE $1', array ("%$val%")) ;
129 $qpa = db_construct_qpa ($qpa, ') OR (') ;
132 foreach ($array as $val) {
135 $qpa = db_construct_qpa ($qpa, ' AND ') ;
137 $qpa = db_construct_qpa ($qpa, ' OR ') ;
142 $qpa = db_construct_qpa ($qpa, 'lower(project_description) LIKE $1', array ("%$val%")) ;
144 $qpa = db_construct_qpa ($qpa, ')) ORDER BY '.$order) ;
148 $result = db_query_qpa ($qpa, $limit+1, $offset, 'DB_SEARCH');
149 $rows = $rows_returned = db_numrows($result);
151 if (!$result || $rows < 1) {
153 echo "<h2>".sprintf (_('No matches found for %1$s'),
155 echo db_error('DB_SEARCH');
159 if ( $rows_returned > $limit) {
163 echo "<h3>".sprintf (_('Search results for %1$s'),
164 $gwords)."</h3><p>\n\n";
166 $title_arr = array();
167 $title_arr[] = util_make_link ('/plugins/globalsearch/?gwords='.urlencode($gwords).'&order=project_title&gexact='.$gexact,
169 $title_arr[] = util_make_link ('/plugins/globalsearch/?gwords='.urlencode($gwords).'&order=project_description&gexact='.$gexact,
171 $title_arr[] = util_make_link ('/plugins/globalsearch/?gwords='.urlencode($gwords).'&order=title&gexact='.$gexact,
174 echo $GLOBALS['HTML']->listTableTop($title_arr);
176 for ( $i = 0; $i < $rows; $i++ ) {
177 if (db_result($result, $i, 'type') == 2) {
183 print "<tr ". $HTML->boxGetAltRowStyle($i)."><td><a href=\""
184 . db_result($result, $i, 'project_link')."\" target=\"blank\">"
185 . html_image("ic/msg.png","10","12",array("border"=>"0"))." "
186 . highlight_target_words($array,db_result($result, $i, 'project_title'))."</a></td>
187 <td>".highlight_target_words($array,db_result($result,$i,'project_description'))."</td>
188 <td><center><a href=\"".db_result($result,$i,'link')."\" target=\"_blank\">"
189 . db_result($result,$i,'title')."</a></center></td></tr>\n";
192 echo $GLOBALS['HTML']->listTableBottom();
196 // This code puts the nice next/prev.
197 if ( !$no_rows && ( ($rows_returned > $rows) || ($offset != 0) ) ) {
201 echo "<table style=\"background-color:".$HTML->COLOR_LTBACK1."\" width=\"100%\" cellpadding=\"5\" cellspacing=\"0\">\n";
203 echo "\t<td align=\"left\">";
205 echo "<span style=\"font-family:arial, helvetica;text-decoration: none\">";
206 echo "<a href=\"/plugins/globalsearch/?gwords=".urlencode($gwords)."&order=".urlencode($order)."&gexact=$gexact&offset=".($offset-25);
207 echo "\"><strong>"._("Previous Results")."</strong></a></span>";
211 echo "</td>\n\t<td align=\"right\">";
212 if ( $rows_returned > $rows) {
213 echo "<span style=\"font-family:arial, helvetica;text-decoration: none\">";
214 echo "<a href=\"/plugins/globalsearch/?gwords=".urlencode($gwords)."&order=".urlencode($order)."&gexact=$gexact&offset=".($offset+25);
215 echo "\"><strong>"._("Next Results") . html_image("t.png","15","15",array("border"=>"0","align"=>"middle")) . "</strong></a></span>";
219 echo "</td>\n</tr>\n";
223 $HTML->footer(array());
227 // c-file-style: "bsd"