search_widget.php 16 KB

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