| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Crocodicstudio\Crudbooster\Controllers\CBController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Crocodicstudio\Crudbooster\CBCoreModule\Search; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Facades\Cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | trait IndexAjax | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public function getDataTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $table = request('table'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $label = request('label'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         $datatableWhere = urldecode(request('datatable_where')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 		$datatableFormat = urldecode(Request::get('datatable_format')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $foreignKeyName = request('fk_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $foreignKeyValue = request('fk_value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         if (! $table || ! $label || ! $foreignKeyName || ! $foreignKeyValue) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             return response()->json([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $query = DB::table($table); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         if ($datatableWhere) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $query->whereRaw($datatableWhere); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		if($datatableFormat) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			$label = $datatableFormat; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 			$query->select(DB::raw('id as select_value, CONCAT('.$label.') as select_label')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			$query->select('id as select_value',$label.' as select_label'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $query->where($foreignKeyName, $foreignKeyValue); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $query->orderby('select_label', 'asc'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return response()->json($query->get()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getDataModalDatatable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $data = base64_decode(json_decode(request('data'), true)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $columns = explode(',', $data['columns']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $result = DB::table($data['table']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->applyQ($result, $columns); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if ($data['sql_where']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $result->whereraw($data['sql_where']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->applyOrderBy($data, $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $limit = ($data['limit']) ?: 6; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return view('crudbooster::form.type_components.datamodal.browser', ['result' => $result->paginate($limit), 'data' => $data]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function getDataQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $key = request('query'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (! Cache::has($key)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             return response()->json(['items' => []]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $query = Cache::get($key); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $fk_name = request('fk_name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $fk_value = request('fk_value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $condition = ' where '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         if (strpos(strtolower($query), 'where') !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $condition = ' and '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (strpos(strtolower($query), 'order by')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             $query = str_replace('ORDER BY', 'order by', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             $qraw = explode('order by', $query); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $query = $qraw[0].$condition.$fk_name." = '$fk_value' $qraw[1]"; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $query .= $condition.$fk_name." = '$fk_value'"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $query = DB::select(DB::raw($query)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return response()->json(['items' => $query]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function postFindData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $items = app(Search::class)->searchData(request('data'), request('q'), request('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return response()->json(['items' => $items]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @param $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     private function applyQ($result, $columns) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         if (request('q')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $result->where(function ($where) use ($columns) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             foreach ($columns as $c => $col) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 if ($c == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |                     $where->where($col, 'like', '%'.request('q').'%'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |                     $where->orWhere($col, 'like', '%'.request('q').'%'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * @param $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * @param $result | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 116 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     private function applyOrderBy($data, $result) | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if ($data['sql_orderby']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             $result->orderByRaw($data['sql_orderby']); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |             $result->orderBy($data['column_value'], 'desc'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | } |