暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

FetchController.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Purchase_request\ListPr;
  4. use Illuminate\Support\Facades\Auth;
  5. use Illuminate\Http\Request;
  6. use App\Models\User;
  7. use Validator;
  8. use Helper;
  9. use DB;
  10. use Hashids;
  11. use Session;
  12. class FetchController extends Controller
  13. {
  14. private $pagetitle = 'Fetch Controller';
  15. private $route = 'fetch';
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. $data['pagetitle'] = $this->pagetitle;
  24. $data['breadcrumb'] = [$this->pagetitle => route($this->route.'.create')];
  25. $data['route'] = $this->route;
  26. return view($this->route.'.index', $data);
  27. }
  28. //security yang telah di kembangkan
  29. public function globalfetch(Request $param )
  30. {
  31. DB::enableQueryLog();
  32. $q = $param->post('q');
  33. $parameter = $param->post('parameter');
  34. $query = DB::table(decrypt($parameter['t']));
  35. $except = $param->post('except');
  36. $onlyin = $param->post('onlyin');
  37. $select = explode(',', decrypt($parameter['s']));
  38. // dd($parameter);
  39. $query->select($select);
  40. if (@$parameter['j'] != '' && @$parameter['j'] != NULL) {
  41. foreach ($parameter['j'] as $key => $value) {
  42. if ($value['type'] == 'inner') {
  43. $query->join($value['t'] ,$value['fieldA'], $value['operator'], $value['fieldB']);
  44. }elseif ($value['type'] == 'left') {
  45. $query->leftJoin($value['t'] ,$value['fieldA'], $value['operator'], $value['fieldB']);
  46. }elseif ($value['type'] == 'right') {
  47. $query->rightJoin($value['t'] ,$value['fieldA'], $value['operator'], $value['fieldB']);
  48. }
  49. }
  50. }
  51. if (@$parameter['w'] != '' && @$parameter['w'] != NULL) {
  52. foreach ($parameter['w'] as $key => $value) {
  53. // print_r($value['value']);exit();
  54. $likeornot = '';
  55. $thefield = '';
  56. if ($value['operator'] == 'LIKE' || $value['operator'] == 'Like' || $value['operator'] == 'like' ||
  57. $value['operator'] == 'ILIKE' || $value['operator'] == 'Ilike' || $value['operator'] == 'ilike') {
  58. $likeornot = '%';
  59. }
  60. if (strpos($value['field'], 'select-index-') !== false) {
  61. $thefield = $select[str_replace('select-index-', '', $value['field'])];
  62. }else{
  63. $thefield = decrypt($value['field']);
  64. }
  65. $query->where($thefield, $value['operator'], $likeornot.($value['value'] == '-NMSearch-' ? $q : $value['value']).$likeornot);
  66. }
  67. }
  68. $id = explode(',', decrypt($parameter['s']) );
  69. $exceptarr = explode(',', $except);
  70. if ($except != '') {
  71. $query->whereNOTIn($id[0],array_filter($exceptarr));
  72. }
  73. $onlyinarr = explode(',', $onlyin);
  74. $onlyinarr = array_filter($onlyinarr);
  75. foreach ($onlyinarr as $key => $value) {
  76. $onlyinarr[$key] = Hashids::decode($value)[0];
  77. }
  78. if ($onlyin != '') {
  79. $query->whereIn($id[0],array_filter($onlyinarr));
  80. }
  81. // $query->where(decrypt($value['field']), $value['operator'], $value);
  82. $query->offset((@$parameter['field'] != '' && @$parameter['field'] != NULL ? $parameter['field'] : 0));
  83. $query->limit((@$parameter['field'] != '' && @$parameter['field'] != NULL ? $parameter['field'] : 5));
  84. $result = $query->get();
  85. // print_r(
  86. // DB::getQueryLog()
  87. // );
  88. // print_r($result);exit();
  89. $data = array();
  90. $i = 0;
  91. foreach ($result as $keyres => $valueres) {
  92. $j = 0;
  93. foreach (explode(',', decrypt($parameter['s']) ) as $key => $value) {
  94. if ($j == 0) {
  95. $data[$i]['id'] = $valueres->{$value};
  96. }elseif($j == 1){
  97. $data[$i]['text'] = $valueres->{$value};
  98. }else{
  99. $data[$i][$value] = $valueres->{$value};
  100. }
  101. $j++;
  102. }
  103. $i++;
  104. }
  105. // echo "<pre>";
  106. // print_r($data);exit();
  107. echo json_encode( ['item' => $data] );
  108. }
  109. }