3 * GForge Doc Search engine
5 * Fabio Bertagnin November 2005
16 require_once('../env.inc.php');
17 require_once $gfwww.'include/pre.php';
18 require_once $gfwww.'docman/include/vtemplate.class.php';
19 require_once $gfwww.'docman/include/doc_utils.php';
20 require_once $gfcommon.'docman/DocumentFactory.class.php';
21 require_once $gfcommon.'docman/DocumentGroupFactory.class.php';
23 $group_id = getIntFromRequest('group_id');
24 $language_id = getIntFromRequest('language_id');
29 $g =& group_get_object($group_id);
30 if (!$g || !is_object($g) || $g->isError()) {
34 $df = new DocumentFactory($g);
36 exit_error(_('Error'),$df->getErrorMessage());
39 $dgf = new DocumentGroupFactory($g);
40 if ($dgf->isError()) {
41 exit_error(_('Error'),$dgf->getErrorMessage());
44 // the "selected language" variable will be used in the links to navigate the
45 // document groups tree
48 if (session_loggedin()) {
49 $language_id = $LUSER->getLanguage();
54 $selected_language = $language_id;
55 } else if ($language_id == "*") {
57 $selected_language = "*";
59 $selected_language = $language_id;
61 $df->setLanguageID($language_id);
64 // check if the user is docman's admin
65 $perm =& $g->getPermission( session_get_user() );
66 if (!$perm || $perm->isError() || !$perm->isDocEditor())
75 docman_header(_('Document Manager: Display Document'),_('Project: %1$s'),'docman','',$g->getPublicName());
78 $handle = $vtp->Open("search.tpl.html");
79 $vtp->NewSession($handle,"MAIN");
81 $allchecked = ""; $onechecked = "";
82 if (getStringFromPost('search_type') == "one") {$onechecked = "checked";}
83 else {$allchecked = "checked";}
84 $vtp->AddSession($handle,"FORMSEARCH");
85 $vtp->SetVar($handle,"FORMSEARCH.TITLE",_('Search in documents'));
86 $vtp->SetVar($handle,"FORMSEARCH.GROUP_ID",$_GET["group_id"]);
87 $vtp->SetVar($handle,"FORMSEARCH.TEXTSEARCH",getStringFromPost("textsearch"));
88 $vtp->SetVar($handle,"FORMSEARCH.ALLCHECKED",$allchecked);
89 $vtp->SetVar($handle,"FORMSEARCH.ONECHECKED",$onechecked);
90 $vtp->SetVar($handle,"FORMSEARCH.SUBMIT_PROMPT",_('Search'));
91 $vtp->SetVar($handle,"FORMSEARCH.SEARCH_ALL_WORDS",_('With all the words'));
92 $vtp->SetVar($handle,"FORMSEARCH.SEARCH_ONE_WORD",_('With at least one of words'));
93 $vtp->CloseSession($handle,"FORMSEARCH");
95 if (getStringFromPost('cmd') == "search")
98 $textsearch = getStringFromPost("textsearch");
99 $textsearch = prepare_search_text ($textsearch);
100 $mots = preg_split("/[\s,]+/",$textsearch);
101 $qpa = db_construct_qpa (false, 'SELECT filename, docid, doc_data.stateid as stateid, doc_states.name as statename, title, description, createdate, updatedate, doc_group, language_id, group_id FROM doc_data JOIN doc_states ON doc_data.stateid = doc_states.stateid') ;
103 if (getStringFromPost('search_type') == "one")
105 if (count($mots) > 0)
107 $qpa = db_construct_qpa ($qpa, ' AND (FALSE') ;
108 foreach ($mots as $mot)
110 $mot = strtolower($mot);
111 $qpa = db_construct_qpa ($qpa, ' OR title LIKE $1 OR description LIKE $1 OR data_words LIKE $1',
114 $qpa = db_construct_qpa ($qpa, ')') ;
120 if (count($mots) > 0)
122 $qpa = db_construct_qpa ($qpa, ' AND (TRUE') ;
123 foreach ($mots as $mot)
125 $mot = strtolower($mot);
126 $qpa = db_construct_qpa ($qpa, ' AND (title LIKE $1 OR description LIKE $1 OR data_words LIKE $1)',
129 $qpa = db_construct_qpa ($qpa, ')') ;
134 $qpa = db_construct_qpa ($qpa, ' AND doc_data.stateid = 1') ;
137 $qpa = db_construct_qpa ($qpa, 'AND group_id = $1',
138 array (getIntFromRequest ('group_id'))) ;
139 $qpa = db_construct_qpa ($qpa, 'ORDER BY updatedate, createdate') ;
141 $result = db_query_qpa ($qpa);
144 $vtp->AddSession($handle,"MESSAGE");
145 $vtp->SetVar($handle,"MESSAGE.TEXT",_('Database query error'));
146 $vtp->CloseSession($handle,"MESSAGE");
148 elseif (db_numrows($result) < 1)
150 $vtp->AddSession($handle,"MESSAGE");
151 $vtp->SetVar($handle,"MESSAGE.TEXT",_('Your search did not match any documents'));
152 $vtp->CloseSession($handle,"MESSAGE");
156 while ($arr = db_fetch_array($result))
161 db_free_result($result);
162 // print_debug ($sql);
164 $groupsarr = array();
165 $result = db_query_params ('SELECT doc_group, groupname, parent_doc_group FROM doc_groups WHERE group_id=$1',
166 array (getIntFromRequest ('group_id')));
167 if ($result && db_numrows($result) > 0)
169 while ($arr = db_fetch_array($result))
174 db_free_result($result);
176 $vtp->AddSession($handle,"RESULTSEARCH");
178 foreach ($resarr as $item)
181 $vtp->AddSession($handle,"RESULT");
182 $vtp->SetVar($handle,"RESULT.N",$count);
183 $vtp->SetVar($handle,"RESULT.SEARCHTITLE",$item["title"]);
184 $vtp->SetVar($handle,"RESULT.SEARCHCOMMENT",$item["description"]);
185 $s = get_path_document ($groupsarr, $item["doc_group"], "$_GET[group_id]", "$item[language_id]");
186 $vtp->SetVar($handle,"RESULT.SEARCHPATH",$s);
187 $vtp->SetVar($handle,"RESULT.GROUP_ID",$_GET["group_id"]);
188 $vtp->SetVar($handle,"RESULT.DOC_ID",$item["docid"]);
189 $vtp->SetVar($handle,"RESULT.FILE_NAME",urlencode($item["filename"]));
190 if ($is_editor) $vtp->SetVar($handle,"RESULT.STATE",$item["statename"]);
191 $vtp->CloseSession($handle,"RESULT");
193 $vtp->CloseSession($handle,"RESULTSEARCH");
196 $vtp->CloseSession($handle,"MAIN");
199 // print_debug (print_r($_POST,true));
200 // print_debug (print_r($groupsarr,true));
202 docman_footer(array());
204 function print_debug ($text)
206 echo "<pre>$text</pre>";
209 function get_path_document ($groupsarr, $doc_group, $group_id, $language_id="1")
212 foreach ($groupsarr as $group)
214 if ($group["doc_group"] == $doc_group)
216 if ($group["parent_doc_group"] == 0)
218 $href = util_make_url ("/docman/index.php?group_id=$group_id&selected_doc_group_id=$group[doc_group]&language_id=$language_id");
219 $rep .= "<a href=\"$href\" style=\"color:#00610A;\">$group[groupname]</a>";
222 $s = get_path_document ($groupsarr, $group["parent_doc_group"], $group_id, $language_id);
223 $href = util_make_url ("/docman/index.php?group_id=$group_id&selected_doc_group_id=$group[doc_group]&language_id=$language_id");
224 $rep .= "$s / <a href=\"$href\" style=\"color:#00610A;\">$group[groupname]</a>";
231 function prepare_search_text ($text)
235 $rep = utf8_decode($rep);
236 $rep = ereg_replace ("é", "e", $rep);
237 $rep = ereg_replace ("è", "e", $rep);
238 $rep = ereg_replace ("ê", "e", $rep);
239 $rep = ereg_replace ("à", "a", $rep);
240 $rep = ereg_replace ("ù", "u", $rep);
241 $rep = ereg_replace ("ç", "c", $rep);
242 $rep = ereg_replace ("é", "e", $rep);
243 $rep = strtolower ($rep);
249 // c-file-style: "bsd"