search_widget.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Search widget. Shows the search screen contents.
  5. * @package chamilo.include.search
  6. */
  7. /**
  8. * Code
  9. */
  10. require_once dirname(__FILE__) . '/IndexableChunk.class.php';
  11. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  12. /**
  13. * Add some required CSS and JS to html's head.
  14. *
  15. * Note that $htmlHeadXtra should be passed by reference and not value,
  16. * otherwise this function will have no effect and your form will be broken.
  17. *
  18. * @param array $htmlHeadXtra A reference to the doc $htmlHeadXtra
  19. */
  20. function search_widget_prepare(&$htmlHeadXtra) {
  21. $htmlHeadXtra[] = '
  22. <script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.autocomplete.js"></script>
  23. <script type="text/javascript" src="'.api_get_path(WEB_LIBRARY_PATH).'search/search_widget.js"></script>
  24. <link rel="stylesheet" type="text/css" href="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.autocomplete.css" />
  25. <link rel="stylesheet" type="text/css" href="'.api_get_path(WEB_LIBRARY_PATH).'search/search_widget.css" />';
  26. }
  27. /**
  28. * Get one term html select
  29. */
  30. function format_one_specific_field_select($prefix, $sf_term_array, $op, $extra_select_attr='size="7" class="sf-select-multiple"') {
  31. global $charset;
  32. $multiple_select .= '<select '. $extra_select_attr .' title="'. $prefix .'" id="sf-'. $prefix .'" name="sf_'. $prefix .'[]">';
  33. $all_selected = '';
  34. if (!empty($_REQUEST['sf_'. $prefix]) ) {
  35. if (in_array('__all__', $_REQUEST['sf_'. $prefix])) {
  36. $all_selected = 'selected="selected"';
  37. }
  38. }
  39. if ($op == 'and') {
  40. $all_selected_name = get_lang('All');
  41. } else if ($op == 'or') {
  42. $all_selected_name = get_lang('Any');
  43. }
  44. $multiple_select .= '<option value="__all__" '. $all_selected .' >-- '. $all_selected_name .' --</option>';
  45. foreach ($sf_term_array as $raw_term) {
  46. $term = substr($raw_term, 1);
  47. if (empty($term)) continue;
  48. $html_term = htmlspecialchars($term, ENT_QUOTES, $charset);
  49. $selected = '';
  50. if (!empty($_REQUEST['sf_'.$prefix]) && is_array($_REQUEST['sf_'.$prefix]) && in_array($term,$_REQUEST['sf_'.$prefix])) {
  51. $selected = 'selected="selected"';
  52. }
  53. $multiple_select .= '<option value="'. $html_term .'" '.$selected.'>'. $html_term .'</option>';
  54. }
  55. $multiple_select .= '</select>';
  56. return $multiple_select;
  57. }
  58. /**
  59. * Get terms html selects
  60. */
  61. function format_specific_fields_selects($sf_terms, $op, $prefilter_prefix='') {
  62. // Process each prefix type term
  63. $i = 0;
  64. $max = count($sf_terms);
  65. $multiple_selects .='';
  66. foreach ($sf_terms as $prefix => $sf_term_array) {
  67. if ($prefix == $prefilter_prefix) continue;
  68. $multiple_select = '';
  69. if ($i>0) {
  70. //print "+" image
  71. $multiple_select .= '<td><img class="sf-select-splitter" src="../img/search-big-plus.gif" alt="plus-sign-decoration"/></td>';
  72. }
  73. //sorting the array of terms
  74. $temp = array();
  75. if (!empty($sf_term_array)) {
  76. foreach ($sf_term_array as $key => $value) {
  77. $temp[trim(stripslashes($value['name']))] = $key;
  78. }
  79. }
  80. $temp = array_flip($temp);
  81. unset($sf_term_array);
  82. natcasesort($temp);
  83. $sf_term_array = $temp;
  84. $sf_copy = $sf_term_array;
  85. // get specific field name
  86. $sf_value = get_specific_field_list(array( 'code' => "'$prefix'" ));
  87. $sf_value = array_shift($sf_value);
  88. $multiple_select .= '<td><label class="sf-select-multiple-title" for="sf_'. $prefix .'[]">'.$icons_for_search_terms[$prefix].' '.$sf_value['name'].'</label><br />';
  89. $multiple_select .= format_one_specific_field_select($prefix, $sf_term_array, $op, 'multiple="multiple" size="7" class="sf-select-multiple"');
  90. $multiple_select .= '</td>';
  91. $multiple_selects .= $multiple_select;
  92. $i++;
  93. }
  94. return $multiple_selects;
  95. }
  96. /**
  97. * Build the normal form.
  98. *
  99. * First, natural way.
  100. */
  101. function search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op) {
  102. $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
  103. $advanced_options = '<a id="tags-toggle" href="#">'. get_lang('SearchAdvancedOptions') .'</a>';
  104. $display_thesaurus = ($show_thesaurus==true? 'block': 'none');
  105. $help = '<h3>'. get_lang('SearchKeywordsHelpTitle') .'</h3>'. get_lang('SearchKeywordsHelpComment');
  106. $mode = (!empty($_REQUEST['mode'])? htmlentities($_REQUEST['mode']): 'gallery');
  107. $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
  108. /**
  109. * POST avoid long urls, but we are using GET because
  110. * SortableTableFromArray pagination is done with simple links, so now we
  111. * could not send a form in pagination
  112. */
  113. if (isset($_GET['action']) && strcmp(trim($_GET['action']),'search')===0) {
  114. $action='index.php';
  115. }
  116. $navigator_info = api_get_navigator();
  117. if ($navigator_info['name']=='Internet Explorer' && $navigator_info['version']=='6') {
  118. $submit_button1 = '<input type="submit" id="submit" value="'. get_lang('Search') .'" />';
  119. $submit_button2 = '<input class="lower-submit" type="submit" value="'. get_lang('Search') .'" />';
  120. $reset_button = '<input type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />';
  121. } else {
  122. $submit_button1 = '<button class="search" type="submit" id="submit" value="'. get_lang("Search") .'" /> '. get_lang('Search') .'</button>';
  123. $submit_button2 = '<button class="search" type="submit" value="'. get_lang('Search') .'" />'. get_lang('Search') .'</button>';
  124. $reset_button = '<button class="save" type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />'. get_lang('SearchResetKeywords') .'</button> ';
  125. }
  126. $form = '<form id="chamilo_search" action="'. $action .'" method="GET">
  127. <input type="text" id="query" name="query" size="40" value="'.stripslashes(Security::remove_XSS($_REQUEST['query'])).'" />
  128. <input type="hidden" name="mode" value="'. $mode .'"/>
  129. <input type="hidden" name="type" value="'. $type .'"/>
  130. <input type="hidden" name="tablename_page_nr" value="1" />
  131. '.$submit_button1.'
  132. <br /><br />';
  133. $list = get_specific_field_list();
  134. if(!empty($list)) {
  135. $form .= '<span class="search-links-box">'. $advanced_options .'&nbsp;</span>
  136. <div id="tags" class="tags" style="display:'. $display_thesaurus .';">
  137. <div class="search-help-box">'. $help .'</div>
  138. <table>
  139. <tr>';
  140. $form .= format_specific_fields_selects($sf_terms, $op);
  141. $or_checked = '';
  142. $and_checked = '';
  143. if ($op == 'or') {
  144. $or_checked = 'checked="checked"';
  145. } else if ($op == 'and') {
  146. $and_checked = 'checked="checked"';
  147. }
  148. $form .= '</tr>
  149. <tr>
  150. <td id="operator-select">
  151. '. get_lang('SearchCombineSearchWith') .':<br />
  152. <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked .'>'. api_strtoupper(get_lang('Or')) .'</input>
  153. <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked .'>'. api_strtoupper(get_lang('And')) .'</input>
  154. </td>
  155. <td></td>
  156. <td>
  157. <br />
  158. '.$reset_button.'
  159. '. $submit_button2.'
  160. </td>
  161. </tr>
  162. </table>
  163. </div>';
  164. }
  165. $form .='</form>
  166. <br style="clear: both;"/>';
  167. return $form;
  168. }
  169. /**
  170. * Build the prefilter form.
  171. *
  172. * This type allow filter all other multiple select terms by one term in a dinamic way
  173. */
  174. function search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix=NULL) {
  175. $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
  176. $advanced_options = '<a id="tags-toggle" href="#">'. get_lang('SearchAdvancedOptions') .'</a>';
  177. $display_thesaurus = ($show_thesaurus==true? 'block': 'none');
  178. $help = '<h3>'. get_lang('SearchKeywordsHelpTitle') .'</h3>'. get_lang('SearchKeywordsHelpComment');
  179. $mode = (!empty($_REQUEST['mode'])? htmlentities($_REQUEST['mode']): 'gallery');
  180. $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
  181. /**
  182. * POST avoid long urls, but we are using GET because
  183. * SortableTableFromArray pagination is done with simple links, so now we
  184. * could not send a form in pagination
  185. */
  186. if (isset($_GET['action']) && strcmp(trim($_GET['action']),'search')===0) {
  187. $action='index.php';
  188. }
  189. $form = '
  190. <form id="chamilo_search" action="'. $action .'" method="GET">
  191. <input type="text" id="query" name="query" size="40" />
  192. <input type="hidden" name="mode" value="'. $mode .'"/>
  193. <input type="hidden" name="type" value="'. $type .'"/>
  194. <input type="hidden" name="tablename_page_nr" value="1" />
  195. <input type="submit" id="submit" value="'. get_lang("Search") .'" />
  196. <br /><br />';
  197. $list = get_specific_field_list();
  198. if(!empty($list)) {
  199. $form .=' <span class="search-links-box">'. $thesaurus_icon . $advanced_options .'&nbsp;</span>
  200. <div id="tags" class="tags" style="display:'. $display_thesaurus .';">
  201. <div class="search-help-box">'. $help .'</div>
  202. <table>
  203. <tr>';
  204. if (!is_null($prefilter_prefix)) {
  205. //sorting the array of terms
  206. $temp = array();
  207. foreach ($sf_terms[$prefilter_prefix] as $key => $value) {
  208. $temp[trim(stripslashes($value['name']))] = $key;
  209. }
  210. $temp = array_flip($temp);
  211. unset($sf_term_array);
  212. natcasesort($temp);
  213. $sf_term_array = $temp;
  214. // get specific field name
  215. $sf_value = get_specific_field_list(array( 'code' => "'$prefilter_prefix'" ));
  216. $sf_value = array_shift($sf_value);
  217. $form .= '<label class="sf-select-multiple-title" for="sf_'. $prefix .'[]">'.$icons_for_search_terms[$prefix].' '.$sf_value['name'].'</label><br />';
  218. $form .= format_one_specific_field_select($prefilter_prefix, $sf_term_array, $op, 'id="prefilter"');
  219. $form .= format_specific_fields_selects($sf_terms, $op, $prefilter_prefix);
  220. } else {
  221. $form .= format_specific_fields_selects($sf_terms, $op);
  222. }
  223. $or_checked = '';
  224. $and_checked = '';
  225. if ($op == 'or') {
  226. $or_checked = 'checked="checked"';
  227. } else if ($op == 'and') {
  228. $and_checked = 'checked="checked"';
  229. }
  230. $form .= '
  231. </tr>
  232. <tr>
  233. <td id="operator-select">
  234. '. get_lang('SearchCombineSearchWith') .':<br />
  235. <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked .'>'. api_strtoupper(get_lang('Or')) .'</input>
  236. <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked .'>'. api_strtoupper(get_lang('And')) .'</input>
  237. </td>
  238. <td></td>
  239. <td>
  240. <br />
  241. <input class="lower-submit" type="submit" value="'. get_lang('Search') .'" />
  242. <input type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords') .'" />
  243. </td>
  244. </tr>
  245. </table>
  246. </div>';
  247. }
  248. $form .= '
  249. </form>
  250. <br style="clear: both;"/>';
  251. return $form;
  252. }
  253. /**
  254. * Show search form
  255. */
  256. function display_search_form($action, $show_thesaurus, $sf_terms, $op) {
  257. $type = (!empty($_REQUEST['type'])? htmlentities($_REQUEST['type']): 'normal');
  258. switch ($type) {
  259. case 'prefilter':
  260. $prefilter_prefix = api_get_setting('search_prefilter_prefix');
  261. $form = search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix);
  262. break;
  263. case 'normal':
  264. default:
  265. $form = search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op);
  266. }
  267. // show built form
  268. echo $form;
  269. }
  270. /**
  271. * Show the search widget
  272. *
  273. * The form will post to index.php by default, you can pass a value to
  274. * $action to use a custom action.
  275. * IMPORTANT: you have to call search_widget_prepare() before calling this
  276. * function or otherwise the form will not behave correctly.
  277. *
  278. * @param string $action Just in case your action is not
  279. * index.php
  280. */
  281. function search_widget_show($action='index.php') {
  282. global $charset;
  283. require_once api_get_path(LIBRARY_PATH).'search/ChamiloQuery.php';
  284. // TODO: load images dinamically when they're avalaible from specific field ui to add
  285. $icons_for_search_terms = array();
  286. $sf_terms = array();
  287. $specific_fields = get_specific_field_list();
  288. $url_params = array();
  289. if ( ($cid=api_get_course_id()) != -1 ) { // with cid
  290. // get search engine terms
  291. $course_filter = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID . $cid);
  292. $dkterms = chamilo_query_simple_query('', 0, 1000, array($course_filter));
  293. //prepare specific fields names (and also get possible URL param names)
  294. foreach ($specific_fields as $specific_field) {
  295. $temp = array();
  296. if (is_array($dkterms) && count($dkterms)>0) {
  297. foreach($dkterms[1] as $obj) {
  298. $temp = array_merge($obj['sf-'.$specific_field['code']], $temp);
  299. }
  300. }
  301. $sf_terms[$specific_field['code']] = $temp;
  302. $url_params[] = 'sf_'.$specific_field['code'];
  303. unset($temp);
  304. }
  305. } else { // without cid
  306. // prepare specific fields names (and also get possible URL param names)
  307. foreach ($specific_fields as $specific_field) {
  308. //get Xapian terms for a specific term prefix, in ISO, apparently
  309. $sf_terms[$specific_field['code']] = xapian_get_all_terms(1000, $specific_field['code']);
  310. $url_params[] = 'sf_'.$specific_field['code'];
  311. }
  312. }
  313. echo '<h2>'.get_lang('Search').'</h2>';
  314. // Tool introduction
  315. // TODO: Settings for the online editor to be checked (insert an image for example). Probably this is a special case here.
  316. if (api_get_course_id() !== -1)
  317. if (!empty($_SESSION['_gid'])) {
  318. Display::display_introduction_section(TOOL_SEARCH.$_SESSION['_gid']);
  319. } else {
  320. Display::display_introduction_section(TOOL_SEARCH);
  321. }
  322. $op = 'or';
  323. if (!empty($_REQUEST['operator']) && in_array($op,array('or','and'))) {
  324. $op = $_REQUEST['operator'];
  325. }
  326. //check if URL params are defined (to see if we show the thesaurus or not)
  327. $show_thesaurus = false;
  328. foreach ($url_params as $param) {
  329. if (is_array($_REQUEST[$param])) {
  330. $thesaurus_decided = FALSE;
  331. foreach ($_REQUEST[$param] as $term) {
  332. if (!empty($term)) {
  333. $show_thesaurus = true;
  334. $thesaurus_decided = TRUE;
  335. break;
  336. }
  337. }
  338. if ($thesaurus_decided) break;
  339. }
  340. }
  341. // create the form
  342. // TODO: use FormValidator
  343. display_search_form($action, $show_thesaurus, $sf_terms, $op);
  344. }