search_widget.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 __DIR__.'/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.'[]">'.$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. $query = isset($_REQUEST['query']) ? Security::remove_XSS($_REQUEST['query']) : null;
  127. $form = '<form id="chamilo_search" action="'.$action.'" method="GET">
  128. <input type="text" id="query" name="query" size="40" value="' . $query.'" />
  129. <input type="hidden" name="mode" value="'. $mode.'"/>
  130. <input type="hidden" name="type" value="'. $type.'"/>
  131. <input type="hidden" name="tablename_page_nr" value="1" />
  132. '.$submit_button1.'
  133. <br /><br />';
  134. $list = get_specific_field_list();
  135. if (!empty($list)) {
  136. $form .= '<span class="search-links-box">'.$advanced_options.'&nbsp;</span>
  137. <div id="tags" class="tags" style="display:'. $display_thesaurus.';">
  138. <div class="search-help-box">'. $help.'</div>
  139. <table>
  140. <tr>';
  141. $form .= format_specific_fields_selects($sf_terms, $op);
  142. $or_checked = '';
  143. $and_checked = '';
  144. if ($op == 'or') {
  145. $or_checked = 'checked="checked"';
  146. } else if ($op == 'and') {
  147. $and_checked = 'checked="checked"';
  148. }
  149. $form .= '</tr>
  150. <tr>
  151. <td id="operator-select">
  152. '. get_lang('SearchCombineSearchWith').':<br />
  153. <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked.'>'.api_strtoupper(get_lang('Or')).'</input>
  154. <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked.'>'.api_strtoupper(get_lang('And')).'</input>
  155. </td>
  156. <td></td>
  157. <td>
  158. <br />
  159. '.$reset_button.'
  160. '. $submit_button2.'
  161. </td>
  162. </tr>
  163. </table>
  164. </div>';
  165. }
  166. $form .= '</form>
  167. <br style="clear: both;"/>';
  168. return $form;
  169. }
  170. /**
  171. * Build the prefilter form.
  172. *
  173. * This type allow filter all other multiple select terms by one term in a dinamic way
  174. */
  175. function search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix = NULL) {
  176. $thesaurus_icon = Display::return_icon('thesaurus.gif', get_lang('SearchAdvancedOptions'), array('id'=>'thesaurus-icon'));
  177. $advanced_options = '<a id="tags-toggle" href="#">'.get_lang('SearchAdvancedOptions').'</a>';
  178. $display_thesaurus = ($show_thesaurus == true ? 'block' : 'none');
  179. $help = '<h3>'.get_lang('SearchKeywordsHelpTitle').'</h3>'.get_lang('SearchKeywordsHelpComment');
  180. $mode = (!empty($_REQUEST['mode']) ? htmlentities($_REQUEST['mode']) : 'gallery');
  181. $type = (!empty($_REQUEST['type']) ? htmlentities($_REQUEST['type']) : 'normal');
  182. /**
  183. * POST avoid long urls, but we are using GET because
  184. * SortableTableFromArray pagination is done with simple links, so now we
  185. * could not send a form in pagination
  186. */
  187. if (isset($_GET['action']) && strcmp(trim($_GET['action']), 'search') === 0) {
  188. $action = 'index.php';
  189. }
  190. $form = '
  191. <form id="chamilo_search" action="'. $action.'" method="GET">
  192. <input type="text" id="query" name="query" size="40" />
  193. <input type="hidden" name="mode" value="'. $mode.'"/>
  194. <input type="hidden" name="type" value="'. $type.'"/>
  195. <input type="hidden" name="tablename_page_nr" value="1" />
  196. <input type="submit" id="submit" value="'. get_lang("Search").'" />
  197. <br /><br />';
  198. $list = get_specific_field_list();
  199. if (!empty($list)) {
  200. $form .= ' <span class="search-links-box">'.$thesaurus_icon.$advanced_options.'&nbsp;</span>
  201. <div id="tags" class="tags" style="display:'. $display_thesaurus.';">
  202. <div class="search-help-box">'. $help.'</div>
  203. <table>
  204. <tr>';
  205. if (!is_null($prefilter_prefix)) {
  206. //sorting the array of terms
  207. $temp = array();
  208. foreach ($sf_terms[$prefilter_prefix] as $key => $value) {
  209. $temp[trim(stripslashes($value['name']))] = $key;
  210. }
  211. $temp = array_flip($temp);
  212. unset($sf_term_array);
  213. natcasesort($temp);
  214. $sf_term_array = $temp;
  215. // get specific field name
  216. $sf_value = get_specific_field_list(array('code' => "'$prefilter_prefix'"));
  217. $sf_value = array_shift($sf_value);
  218. $form .= '<label class="sf-select-multiple-title" for="sf_'.$prefix.'[]">'.$icons_for_search_terms[$prefix].' '.$sf_value['name'].'</label><br />';
  219. $form .= format_one_specific_field_select($prefilter_prefix, $sf_term_array, $op, 'id="prefilter"');
  220. $form .= format_specific_fields_selects($sf_terms, $op, $prefilter_prefix);
  221. } else {
  222. $form .= format_specific_fields_selects($sf_terms, $op);
  223. }
  224. $or_checked = '';
  225. $and_checked = '';
  226. if ($op == 'or') {
  227. $or_checked = 'checked="checked"';
  228. } else if ($op == 'and') {
  229. $and_checked = 'checked="checked"';
  230. }
  231. $form .= '
  232. </tr>
  233. <tr>
  234. <td id="operator-select">
  235. '. get_lang('SearchCombineSearchWith').':<br />
  236. <input type="radio" class="search-operator" name="operator" value="or" '. $or_checked.'>'.api_strtoupper(get_lang('Or')).'</input>
  237. <input type="radio" class="search-operator" name="operator" value="and" '. $and_checked.'>'.api_strtoupper(get_lang('And')).'</input>
  238. </td>
  239. <td></td>
  240. <td>
  241. <br />
  242. <input class="lower-submit" type="submit" value="'. get_lang('Search').'" />
  243. <input type="submit" id="tags-clean" value="'. get_lang('SearchResetKeywords').'" />
  244. </td>
  245. </tr>
  246. </table>
  247. </div>';
  248. }
  249. $form .= '
  250. </form>
  251. <br style="clear: both;"/>';
  252. return $form;
  253. }
  254. /**
  255. * Show search form
  256. */
  257. function display_search_form($action, $show_thesaurus, $sf_terms, $op) {
  258. $type = (!empty($_REQUEST['type']) ? htmlentities($_REQUEST['type']) : 'normal');
  259. switch ($type) {
  260. case 'prefilter':
  261. $prefilter_prefix = api_get_setting('search_prefilter_prefix');
  262. $form = search_widget_prefilter_form($action, $show_thesaurus, $sf_terms, $op, $prefilter_prefix);
  263. break;
  264. case 'normal':
  265. default:
  266. $form = search_widget_normal_form($action, $show_thesaurus, $sf_terms, $op);
  267. }
  268. // show built form
  269. echo $form;
  270. }
  271. /**
  272. * Show the search widget
  273. *
  274. * The form will post to index.php by default, you can pass a value to
  275. * $action to use a custom action.
  276. * IMPORTANT: you have to call search_widget_prepare() before calling this
  277. * function or otherwise the form will not behave correctly.
  278. *
  279. * @param string $action Just in case your action is not
  280. * index.php
  281. */
  282. function search_widget_show($action = 'index.php')
  283. {
  284. require_once api_get_path(LIBRARY_PATH).'search/ChamiloQuery.php';
  285. // TODO: load images dinamically when they're avalaible from specific field ui to add
  286. $groupId = api_get_group_id();
  287. $sf_terms = array();
  288. $specific_fields = get_specific_field_list();
  289. $url_params = array();
  290. if (($cid = api_get_course_id()) != -1) { // with cid
  291. // get search engine terms
  292. $course_filter = chamilo_get_boolean_query(XAPIAN_PREFIX_COURSEID.$cid);
  293. $dkterms = chamilo_query_simple_query('', 0, 1000, array($course_filter));
  294. //prepare specific fields names (and also get possible URL param names)
  295. foreach ($specific_fields as $specific_field) {
  296. $temp = array();
  297. if (is_array($dkterms) && count($dkterms) > 0) {
  298. foreach ($dkterms[1] as $obj) {
  299. $temp = array_merge($obj['sf-'.$specific_field['code']], $temp);
  300. }
  301. }
  302. $sf_terms[$specific_field['code']] = $temp;
  303. $url_params[] = 'sf_'.$specific_field['code'];
  304. unset($temp);
  305. }
  306. } else { // without cid
  307. // prepare specific fields names (and also get possible URL param names)
  308. foreach ($specific_fields as $specific_field) {
  309. //get Xapian terms for a specific term prefix, in ISO, apparently
  310. $sf_terms[$specific_field['code']] = xapian_get_all_terms(1000, $specific_field['code']);
  311. $url_params[] = 'sf_'.$specific_field['code'];
  312. }
  313. }
  314. echo '<h2>'.get_lang('Search').'</h2>';
  315. // Tool introduction
  316. // TODO: Settings for the online editor to be checked (insert an image for example). Probably this is a special case here.
  317. if (api_get_course_id() !== -1)
  318. if (!empty($groupId)) {
  319. Display::display_introduction_section(TOOL_SEARCH.$groupId);
  320. } else {
  321. Display::display_introduction_section(TOOL_SEARCH);
  322. }
  323. $op = 'or';
  324. if (!empty($_REQUEST['operator']) && in_array($op, array('or', 'and'))) {
  325. $op = $_REQUEST['operator'];
  326. }
  327. //check if URL params are defined (to see if we show the thesaurus or not)
  328. $show_thesaurus = false;
  329. foreach ($url_params as $param) {
  330. if (isset($_REQUEST[$param]) && is_array($_REQUEST[$param])) {
  331. $thesaurus_decided = false;
  332. foreach ($_REQUEST[$param] as $term) {
  333. if (!empty($term)) {
  334. $show_thesaurus = true;
  335. $thesaurus_decided = true;
  336. break;
  337. }
  338. }
  339. if ($thesaurus_decided) break;
  340. }
  341. }
  342. // create the form
  343. // TODO: use FormValidator
  344. display_search_form($action, $show_thesaurus, $sf_terms, $op);
  345. }