search_widget.php 16 KB

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