3 * FusionForge Documentation Manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002-2003, Tim Perdue/GForge, LLC
7 * Copyright 2005, Fabio Bertagnin
8 * Copyright 2010-2011, Franck Villaume - Capgemini
9 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
10 * http://fusionforge.org
12 * This file is part of FusionForge. FusionForge is free software;
13 * you can redistribute it and/or modify it under the terms of the
14 * GNU General Public License as published by the Free Software
15 * Foundation; either version 2 of the Licence, or (at your option)
18 * FusionForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 if (!forge_check_perm('docman', $group_id, 'read')) {
33 $return_msg= _('Document Manager Access Denied');
34 session_redirect('/docman/?group_id='.$group_id.'&warning_msg='.urlencode($return_msg));
37 /* NEED A REAL REWRITE */
38 require_once $gfcommon.'docman/include/vtemplate.class.php';
40 $is_editor = forge_check_perm('docman', $g->getID(), 'approve');
43 if (empty($gfcommon)) {
44 $templates_dir = '../../common';
46 $templates_dir = $gfcommon;
48 $handle = $vtp->Open($templates_dir."/docman/templates/search.tpl.html");
49 $vtp->NewSession($handle,"MAIN");
53 if (getStringFromPost('search_type') == "one") {$onechecked = 'checked="checked"';}
54 else {$allchecked = 'checked="checked"';}
55 $vtp->AddSession($handle,"FORMSEARCH");
56 $vtp->SetVar($handle,"FORMSEARCH.TITLE",_('Search in documents'));
57 $vtp->SetVar($handle,"FORMSEARCH.GROUP_ID",$_GET["group_id"]);
58 $vtp->SetVar($handle,"FORMSEARCH.TEXTSEARCH",getStringFromPost("textsearch"));
59 $vtp->SetVar($handle,"FORMSEARCH.ALLCHECKED",$allchecked);
60 $vtp->SetVar($handle,"FORMSEARCH.ONECHECKED",$onechecked);
61 $vtp->SetVar($handle,"FORMSEARCH.SUBMIT_PROMPT",_('Search'));
62 $vtp->SetVar($handle,"FORMSEARCH.SEARCH_ALL_WORDS",_('With all the words'));
63 $vtp->SetVar($handle,"FORMSEARCH.SEARCH_ONE_WORD",_('With at least one of words'));
64 $vtp->CloseSession($handle,"FORMSEARCH");
66 if (getStringFromPost('cmd') == "search") {
67 $textsearch = getStringFromPost("textsearch");
68 $textsearch = prepare_search_text($textsearch);
69 $mots = preg_split("/[\s,]+/",$textsearch);
70 $qpa = db_construct_qpa(false, 'SELECT filename, filetype, docid, doc_data.stateid as stateid, doc_states.name as statename, title, description, createdate, updatedate, doc_group, group_id FROM doc_data JOIN doc_states ON doc_data.stateid = doc_states.stateid') ;
71 if (getStringFromPost('search_type') == "one") {
72 if (count($mots) > 0) {
73 $qpa = db_construct_qpa($qpa, ' AND (FALSE');
74 foreach ($mots as $mot) {
75 $mot = strtolower($mot);
76 $qpa = db_construct_qpa($qpa, ' OR title LIKE $1 OR description LIKE $1 OR data_words LIKE $1',
79 $qpa = db_construct_qpa($qpa, ')');
83 if (count($mots) > 0) {
84 $qpa = db_construct_qpa($qpa, ' AND (TRUE');
85 foreach ($mots as $mot) {
86 $mot = strtolower($mot);
87 $qpa = db_construct_qpa($qpa, ' AND (title LIKE $1 OR description LIKE $1 OR data_words LIKE $1)',
90 $qpa = db_construct_qpa($qpa, ')');
95 $qpa = db_construct_qpa($qpa, ' AND doc_data.stateid = 1');
97 $qpa = db_construct_qpa($qpa, ' AND doc_data.stateid != 2');
100 $qpa = db_construct_qpa($qpa, ' AND group_id = $1', array($group_id));
101 $params['group_id'] = $group_id;
102 $params['qpa'] = $qpa;
103 plugin_hook('docmansearch_has_hierarchy', $params);
105 $qpa = db_construct_qpa($qpa, 'ORDER BY updatedate, createdate');
107 $result = db_query_qpa($qpa);
109 $vtp->AddSession($handle, "MESSAGE");
110 $vtp->SetVar($handle, "MESSAGE.TEXT", _('Database query error'));
111 $vtp->CloseSession($handle, "MESSAGE");
112 } elseif (db_numrows($result) < 1) {
113 $vtp->AddSession($handle, "MESSAGE");
114 $vtp->SetVar($handle, "MESSAGE.TEXT", _('Your search did not match any documents'));
115 $vtp->CloseSession($handle, "MESSAGE");
117 while ($arr = db_fetch_array($result)) {
121 db_free_result($result);
122 // print_debug ($sql);
124 $groupsarr = array();
125 $result = db_query_params('SELECT doc_group, groupname, parent_doc_group FROM doc_groups WHERE group_id=$1',
126 array(getIntFromRequest('group_id')));
127 if ($result && db_numrows($result) > 0) {
128 while ($arr = db_fetch_array($result)) {
132 db_free_result($result);
134 $vtp->AddSession($handle, "RESULTSEARCH");
136 foreach ($resarr as $item) {
138 $vtp->AddSession($handle, "RESULT");
139 $vtp->SetVar($handle, "RESULT.N", $count);
140 $vtp->SetVar($handle, "RESULT.SEARCHTITLE", $item["title"]);
141 $vtp->SetVar($handle, "RESULT.SEARCHCOMMENT", $item["description"]);
142 $s = get_path_document($groupsarr, $item["doc_group"], "$_GET[group_id]");
143 $vtp->SetVar($handle, "RESULT.SEARCHPATH", $s);
144 if ($item['filetype'] == 'URL') {
145 $vtp->SetVar($handle, "RESULT.FILE_NAME", $item["filename"]);
147 $vtp->SetVar($handle, "RESULT.FILE_NAME", '/docman/view.php/'.$_GET["group_id"].'/'.$item["docid"].'/'.urlencode($item["filename"]));
149 if ($is_editor) $vtp->SetVar($handle, "RESULT.STATE", $item["statename"]);
150 $vtp->CloseSession($handle, "RESULT");
152 $vtp->CloseSession($handle, "RESULTSEARCH");
155 $vtp->CloseSession($handle, "MAIN");
158 // print_debug (print_r($_POST,true));
159 // print_debug (print_r($groupsarr,true));
161 function print_debug($text) {
162 echo "<pre>$text</pre>";
165 function get_path_document($groupsarr, $doc_group, $group_id) {
167 foreach ($groupsarr as $group) {
168 if ($group["doc_group"] == $doc_group) {
169 if ($group["parent_doc_group"] == 0) {
170 $href = util_make_uri("docman/?group_id=$group_id&view=listfile&dirid=$group[doc_group]");
171 $rep .= "<a href=\"$href\" style=\"color:#00610A;\">$group[groupname]</a>";
174 $s = get_path_document($groupsarr, $group["parent_doc_group"], $group_id);
175 $href = util_make_uri("docman/?group_id=$group_id&view=listfile&dirid=$group[doc_group]");
176 $rep .= "$s / <a href=\"$href\" style=\"color:#00610A;\">$group[groupname]</a>";
183 function prepare_search_text($text) {
185 $rep = utf8_decode($rep);
186 $rep = preg_replace("/é/", "/e/", $rep);
187 $rep = preg_replace("/è/", "/e/", $rep);
188 $rep = preg_replace("/ê/", "/e/", $rep);
189 $rep = preg_replace("/à/", "/a/", $rep);
190 $rep = preg_replace("/ù/", "/u/", $rep);
191 $rep = preg_replace("/ç/", "/c/", $rep);
192 $rep = preg_replace("/é/", "/e/", $rep);
193 $rep = strtolower($rep);
199 // c-file-style: "bsd"