common.inc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Library for the common formatting functions
  4. * @package chamilo.plugin.search
  5. */
  6. /**
  7. * Exit, dropping the temporary tables just before that
  8. * @param integer Whether to print an error message (passed to drop_temp_table())
  9. */
  10. function exit_local($print_err = 1) {
  11. drop_temp_table($print_err);
  12. exit;
  13. }
  14. /**
  15. * Prints the error and the template bottom, then exit
  16. * @param string The error message
  17. */
  18. function print_error_local($str){
  19. global $error;
  20. $error=$str;
  21. print_template('error');
  22. print_template('bottom');
  23. exit;
  24. }
  25. /**
  26. * Gets the number of documents from the udm object
  27. * @return mixed An integer or "Unknown" if not found
  28. */
  29. function get_doc_count() {
  30. global $udm_agent;
  31. if (udm_api_version() >= 30111) {
  32. return(udm_get_doc_count($udm_agent));
  33. } else {
  34. return "Unknown";
  35. }
  36. }
  37. /**
  38. * Format last modified date
  39. * @param integer UNIX Timestamp?
  40. * @return string Formatted date
  41. */
  42. function format_lastmod($lastmod) {
  43. $temp=$lastmod;
  44. if (!$temp) $temp = 'undefined';
  45. elseif (udm_api_version() < 30204) $temp = strftime('%a, %d %b %Y %H:%M:%S %Z',$temp);
  46. return $temp;
  47. }
  48. /**
  49. * Format date in seconds from a source in s,M,h,d,m,y
  50. * @param string Date
  51. * @return integer Date in seconds
  52. */
  53. function format_dp($dp) {
  54. $result=0;
  55. while ($dp != '') {
  56. $param = array();
  57. if (preg_match('/^([\-\+]?\d+)([sMhdmy]?)/',$dp,$param)) {
  58. switch ($param[2]) {
  59. case 's': $multiplier=1; break;
  60. case 'M': $multiplier=60; break;
  61. case 'h': $multiplier=3600; break;
  62. case 'd': $multiplier=3600*24; break;
  63. case 'm': $multiplier=3600*24*31; break;
  64. case 'y': $multiplier=3600*24*365; break;
  65. default: $multiplier=1;
  66. }
  67. $result += $param[1]*$multiplier;
  68. $dp=preg_replace("/^[\-\+]?\d+$param[2]/",'',$dp);
  69. } else {
  70. return 0;
  71. }
  72. }
  73. return $result;
  74. }
  75. /**
  76. * Formats the date using the user's settings as defined in search.html
  77. * @param string Date
  78. * @return string Formatted date
  79. */
  80. function format_userdate($date) {
  81. $result=0;
  82. $param = array();
  83. if (preg_match('/^(\d+)\/(\d+)\/(\d+)$/',$date,$param)) {
  84. $day=$param[1];
  85. $mon=$param[2];
  86. $year=$param[3];
  87. $result=mktime(0,0,0,$mon,$day,$year);
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Parses the given text for the words searched and highlight the words, the returns the string
  93. * @param string Resulting document excerpt
  94. * @return string Same string but with highlight tags (as defined in search.html)
  95. */
  96. function ParseDocText($text){
  97. global $all_words;
  98. global $hlbeg, $hlend;
  99. $str=$text;
  100. if (udm_api_version() < 30200) {
  101. for ($i=0; $i<count($all_words); $i+=1) {
  102. $word=$all_words[$i];
  103. $str = preg_replace("/([\s\t\r\n\~\!\@\#\$\%\^\&\*\(\)\-\_\=\+\\\|\{\}\[\]\;\:\'\"\<\>\?\/\,\.]+)($word)/i","\\1$hlbeg\\2$hlend",$str);
  104. $str = preg_replace("/^($word)/i","$hlbeg\\1$hlend",$str);
  105. }
  106. } else {
  107. $str = str_replace("\2",$hlbeg,$str);
  108. $str = str_replace("\3",$hlend,$str);
  109. while (substr_count($str,$hlbeg) > substr_count($str,$hlend)) {
  110. $str.=$hlend;
  111. }
  112. }
  113. return $str;
  114. }
  115. /**
  116. * Parses the global variable $QUERY_STRING
  117. */
  118. function ParseQString() {
  119. global $QUERY_STRING;
  120. global $ul, $ul_arr;
  121. global $tag, $tag_arr;
  122. global $cat, $cat_arr;
  123. global $lang, $lang_arr;
  124. global $type, $type_arr;
  125. $pairs = explode("&", $QUERY_STRING);
  126. $ul_arr = array();
  127. $tag_arr = array();
  128. $cat_arr = array();
  129. $lang_arr = array();
  130. $type_arr = array();
  131. for($i=0; $i<count($pairs); $i+=1) {
  132. $pairs[$i]=str_replace('+',' ',$pairs[$i]);
  133. list($name, $value) = explode("=",$pairs[$i]);
  134. if ($name=='ul') {
  135. $ul_arr[]=urldecode($value);
  136. if (!count($ul_arr)) $ul=urldecode($value);
  137. } elseif ($name=='tag') {
  138. $tag_arr[]=urldecode($value);
  139. if (!count($tag_arr)) $tag=urldecode($value);
  140. } elseif ($name=='cat') {
  141. $cat_arr[]=urldecode($value);
  142. if (!count($cat_arr)) $cat=urldecode($value);
  143. } elseif ($name=='lang') {
  144. $lang_arr[]=urldecode($value);
  145. if (!count($lang_arr)) $lang=urldecode($value);
  146. } elseif ($name=='type') {
  147. $type_arr[]=urldecode($value);
  148. if (!count($type_arr)) $type=urldecode($value);
  149. } else continue;
  150. }
  151. }
  152. ?>