search.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. class search {
  6. private static $_search;
  7. public static function getInstance() {
  8. if (!self::$_search) {
  9. $c = __CLASS__;
  10. self::$_search = new $c;
  11. }
  12. return self::$_search;
  13. }
  14. public function getDisplaySearch($model_name, $ids = "") {
  15. $rs = null;
  16. if (!empty($ids)) {
  17. $rs = $ids;
  18. } else {
  19. $sql = "select ids from public.ra_online_search_config where lower(login_name)='" . strtolower(_getLoginName()) . "' and lower(model_name) = '" . strtolower($model_name) . "'";
  20. $rs = common::excuteOneSql($sql);
  21. if (empty($rs)) {
  22. $sql = "select array_to_string(array(select id from public.ra_online_search where active = true and default_display = true and lower(model_name) = '" . strtolower($model_name) . "'";
  23. if (_isCustomerLogin())
  24. $sql .= " and customer_display = true";
  25. $sql .= " order by order_by asc, search_name asc), ',')";
  26. $rs = common::excuteOneSql($sql);
  27. }
  28. }
  29. $result = "";
  30. $sql = $this->_getSql($rs, "html_value, value_name, search_condition, value_name");
  31. $rss = common::excuteListSql($sql);
  32. foreach ($rss as $key => $value) {
  33. if ($value['search_condition'] == "year") {
  34. $result .= '<div class="modules3">Year : <input name="' . $value['value_name'] . '_start" id="' . $value['value_name'] . '_start" type="text" oninput = "value=value.replace(/[^\d]/g,\'\')" value=""><div class="line"> - </div>
  35. <input name="' . $value['value_name'] . '_end" id="' . $value['value_name'] . '_end" type="text" oninput = "value=value.replace(/[^\d]/g,\'\')" value=""></div>';
  36. } else {
  37. $result .= $value['html_value'];
  38. }
  39. }
  40. return $result;
  41. }
  42. public function _getSql($ids, $str) {
  43. $ids_arr = explode(',', $ids);
  44. $sql = "";
  45. foreach ($ids_arr as $value) {
  46. if (!empty($value)) {
  47. if (empty($sql)) {
  48. $sql .= "SELECT $str FROM public.ra_online_search_cso where id = " . $value;
  49. if (_isCustomerLogin())
  50. $sql .= ' and customer_display = true';
  51. } else {
  52. $sql .= " union all SELECT $str FROM public.ra_online_search_cso where id = " . $value;
  53. if (_isCustomerLogin())
  54. $sql .= ' and customer_display = true';
  55. }
  56. }
  57. }
  58. return $sql;
  59. }
  60. public function saveSettingDisplay($newid, $model_name) {
  61. $login_name = _getLoginName();
  62. if ($this->checkUserDisplaySetting($model_name) === TRUE) {
  63. $sql = "UPDATE public.ra_online_search_config SET ids = '" . common::check_input($newid) . "' WHERE lower(model_name) = '" . common::check_input(strtolower($model_name)) . "' AND lower(login_name) = '" . common::check_input(strtolower($login_name)) . "'";
  64. } else {
  65. $sql = "INSERT INTO public.ra_online_search_config (login_name, model_name, ids) VALUES ('" . common::check_input($login_name) . "', '" . common::check_input($model_name) . "', '" . common::check_input($newid) . "')";
  66. }
  67. common::excuteUpdateSql($sql);
  68. }
  69. /*
  70. * check user had setting display
  71. */
  72. public function checkUserDisplaySetting($model_name) {
  73. $login_name = _getLoginName();
  74. $sql = "select ids from public.ra_online_search_config where lower(login_name)='" . strtolower($login_name) . "' and lower(model_name) = '" . strtolower($model_name) . "'"; //check login_name is setting
  75. $rs = common::excuteObjectSql($sql);
  76. if (!empty($rs)) {
  77. return TRUE;
  78. }
  79. return FALSE;
  80. }
  81. public function getSearchSQL($model_name) {
  82. $sql = "select ids from public.ra_online_search_config_cso where lower(login_name)='" . strtolower(_getLoginName()) . "' and lower(model_name) = '" . strtolower($model_name) . "'";
  83. $rs = common::excuteOneSql($sql);
  84. if (empty($rs)) {
  85. $sql = "select array_to_string(array(select id from public.ra_online_search_cso where active = true and lower(model_name) = '" . strtolower($model_name) . "'";
  86. if (_isCustomerLogin())
  87. $sql .= " and customer_display = true";
  88. $sql .= " order by order_by), ',')";
  89. $rs = common::excuteOneSql($sql);
  90. }
  91. $result = " and 1=1";
  92. $sql = $this->_getSql($rs, "value_name, search_condition, is_int");
  93. $rss = common::excuteListSql($sql);
  94. foreach ($rss as $v) {
  95. if ((isset($_POST[$v['value_name']]) && !empty($_POST[$v['value_name']])) || (isset($_POST[$v['value_name'] . '_start']) && !empty($_POST[$v['value_name'] . '_start'])) || (isset($_POST[$v['value_name'] . '_end']) && !empty($_POST[$v['value_name'] . '_end']))) {
  96. if (strtolower($model_name) == "booking_search" && (strtolower($v['value_name']) == "transport_mode")) {
  97. //transport_mode todo
  98. } elseif (strtolower($model_name) == "ocean_search" && (strtolower($v['value_name']) == "from_station"
  99. || strtolower($v['value_name']) == "incoterms")){
  100. //这两块特殊处理,可能以后会移除
  101. } else {
  102. if ($v['search_condition'] == '>=,<=') {
  103. $s = $v['value_name'] . '_start';
  104. $e = $v['value_name'] . '_end';
  105. if (isset($_POST[$s]) && !empty($_POST[$s])) {
  106. $result .= " and " . $v['value_name'] . " >= '" . common::check_input(common::usDate2sqlDate($_POST[$s]) . ' 00:00:00') . "'";
  107. }
  108. if (isset($_POST[$e]) && !empty($_POST[$e])) {
  109. $result .= " and " . $v['value_name'] . " <= '" . common::check_input(common::usDate2sqlDate($_POST[$e]) . ' 23:59:59') . "'";
  110. }
  111. } elseif ($v['search_condition'] == 'year') {
  112. $s = $v['value_name'] . '_start';
  113. $e = $v['value_name'] . '_end';
  114. if (isset($_POST[$s]) && !empty($_POST[$s])) {
  115. $result .= " and " . $v['value_name'] . " >= " . common::check_input($_POST[$s]);
  116. }
  117. if (isset($_POST[$e]) && !empty($_POST[$e])) {
  118. $result .= " and " . $v['value_name'] . " <= " . common::check_input($_POST[$e]);
  119. }
  120. } elseif ($v['search_condition'] == "in") {
  121. $result .= " and lower(" . $v['value_name'] . ") " . common::getInNotInSql($_POST[$v['value_name']]);
  122. } else {
  123. //处理多个输入查询情况包含一个 数组类型,也有可能单个字符串
  124. $value_arr = $_POST[$v['value_name']];
  125. if(!is_array($value_arr)){
  126. $value_arr = array($value_arr);
  127. }
  128. $more_sql = "1<>1";
  129. foreach($value_arr as $value){
  130. //处理合并查询 包含一个
  131. $value_name_arr = explode("/", $v['value_name']);
  132. foreach($value_name_arr as $value_name){
  133. if ($v['is_int'] == 't') {
  134. $more_sql .= " or " . " " . $value_name . " " . $v['search_condition'] . " " . trim($value);
  135. } elseif ($v['search_condition'] == 'ilike') {
  136. $more_sql .= " or " . $value_name . " " . $v['search_condition'] . " '" . common::check_input(trim($value)) . "%'";
  137. } elseif ($v['search_condition'] == 'like') {
  138. $more_sql .= " or " . $value_name . " " . $v['search_condition'] . " '%" . common::check_input(trim($value)) . "%'";
  139. } else {
  140. $more_sql .= " or " . $value_name . " " . $v['search_condition'] . " '" . common::check_input(trim($value)) . "'";
  141. }
  142. }
  143. }
  144. if ($more_sql <> "1<>1"){
  145. $result .= " and ($more_sql)";
  146. }
  147. }
  148. }
  149. }
  150. }
  151. return $result;
  152. }
  153. public function getDisplayColumn($model_name, $isin = true) {
  154. $rs = null;
  155. $sql = "select ids from public.ra_online_search_config where lower(login_name)='" . strtolower(_getLoginName()) . "' and lower(model_name) = '" . strtolower($model_name) . "'";
  156. $rs = common::excuteOneSql($sql);
  157. if (empty($rs)) {
  158. $sql = "select array_to_string(array(select id from public.ra_online_search where active = true and default_display = true and lower(model_name) = '" . strtolower($model_name) . "'";
  159. if (_isCustomerLogin())
  160. $sql .= " and customer_display = true";
  161. $sql .= " order by order_by asc, search_name asc), ',')";
  162. $rs = common::excuteOneSql($sql);
  163. }
  164. if ($isin) {
  165. $sql = $this->_getSql($rs, "id, search_name as name");
  166. $rss = common::excuteListSql($sql);
  167. } else {
  168. $sql = "SELECT id, search_name as name FROM public.ra_online_search WHERE active = true and lower(model_name) = '" . strtolower($model_name) . "' and id not in ($rs)";
  169. if (_isCustomerLogin())
  170. $sql .= ' and customer_display=true';
  171. $rss = common::excuteListSql($sql);
  172. }
  173. return $rss;
  174. }
  175. }
  176. ?>