Samson Mutunga 1 tahun lalu
induk
melakukan
f30aa36e78

+ 19 - 2
app/Http/Controllers/AdminController.php

@@ -833,6 +833,7 @@ class AdminController extends Controller
 
     public function surveys(Request $request)
     {
+        $filters = $request->all();
         $entityTypes = Survey::ALLOWED_ENTITIES;
         $surveyFormsPath = resource_path(Survey::FORM_PATH);
         $filesInFolder = File::allFiles($surveyFormsPath);
@@ -846,8 +847,24 @@ class AdminController extends Controller
             $forms[] = $internalName;
         }
         
-        $records = Survey::paginate(5);
-        return view('app.admin.surveys.list', compact('forms', 'records', 'entityTypes'));
+        $records = Survey::query();
+        $searchString = $request->get('string');
+        if($searchString){
+            $searchString = strtolower($searchString);
+            $records = $records->where(function ($q) use ($searchString) {
+                return $q->orWhereRaw('LOWER(title::text) ILIKE ?', ['%' . $searchString . '%'])
+                    ->orWhereRaw('LOWER(internal_name::text) ILIKE ?', ['%' . $searchString . '%'])
+                    ->orWhereRaw('survey_data ILIKE ?', ['%' . $searchString . '%']);
+            });
+        }
+
+        $entityType = $request->get('entity_type');
+        if($entityType){
+            $records = $records->where('entity_type', $entityType);
+        }
+
+        $records = $records->orderBy('created_at', 'DESC')->paginate(5);
+        return view('app.admin.surveys.list', compact('forms', 'records', 'entityTypes', 'filters'));
     }
 
     public function getEntityRecords(Request $request)

+ 3 - 3
resources/views/app/admin/surveys/list.blade.php

@@ -20,9 +20,9 @@
                 </div>
 
             </div>
-            {{-- <div class="p-3">
-			filter blade
-		</div> --}}
+            <div class="p-3">
+			@include('app.admin.surveys.partials.filters')
+		</div>
             <table class="table table-hover bg-white p-0 m-0 table-bordered table-sm border-top border-bottom ">
                 <thead class="bg-light">
                     <tr>

+ 99 - 0
resources/views/app/admin/surveys/partials/filters.blade.php

@@ -0,0 +1,99 @@
+<?php
+    $url = route('admin.surveys');
+?>
+<style>
+	#admin-surveys-filters label {
+		font-weight: bold;
+	}
+
+	#admin-surveys-filters .mw-100px {
+		min-width: 100px;
+	}
+
+	.filter-container {
+		display: flex;
+		align-items: flex-start;
+		flex-wrap: wrap;
+	}
+
+	.filter-container>div {
+		width: 165px;
+	}
+
+	.filter-container>div:not(:last-child) {
+		margin-right: 10px;
+	}
+
+	.sm-section {
+		width: auto !important;
+	}
+</style>
+<form id="admin-surveys-filters" method="GET" action="{{ $url }}" class="filter-container" v-cloak>
+	<div class="sm-section">
+		<div class="">
+			<label>Filter String:</label>
+			<input name="string" class="form-control input-sm" v-model="filters.string">
+		</div>
+	</div>
+    <div class="sm-section">
+		<div class="">
+			<label>Entity Type:</label>
+			<select name="entity_type" class="form-control input-sm" v-model="filters.entity_type">
+                @foreach ($entityTypes as $entityType)
+                    <option value="{{ $entityType }}">{{ formatAsTitle($entityType) }}</option>
+                @endforeach
+            </select>
+		</div>
+	</div>
+
+	<div>
+		<div class="">
+			<label>&nbsp;</label>
+			<div class=" d-flex">
+				<button type="button" v-on:click.prevent="doSubmit()" class="btn btn-primary btn-sm mr-2"><i class="fas fa-filter"></i> Filter</button>
+				<a href="#" v-on:click.prevent="fastLoad('{{ $url }}')" class="btn btn-link btn-sm text-danger">Clear</a>
+			</div>
+		</div>
+	</div>
+</form>
+
+<?php
+$loadedFilters = @$filters;
+$allFilterKeys = [
+	'string',
+    'entity_type'
+];
+for ($i = 0; $i < count($allFilterKeys); $i++) {
+	if (!isset($loadedFilters[$allFilterKeys[$i]]) || !$loadedFilters[$allFilterKeys[$i]]) {
+		$loadedFilters[$allFilterKeys[$i]] = '';
+	}
+}
+?>
+<script>
+	(function() {
+		function init() {
+			new Vue({
+				el: '#admin-surveys-filters',
+				delimiters: ['@{{', '}}'],
+				data: {
+					filters: <?= json_encode($loadedFilters) ?>
+				},
+				methods: {
+					init: function() {
+
+					},
+					doSubmit: function() {
+						fastLoad("{{ $url }}?" + $('#admin-surveys-filters').serialize());
+						return false;
+					}
+				},
+				mounted: function() {
+					this.init();
+				},
+			});
+
+
+		}
+		addMCInitializer('admin-surveys-filters', init, '#admin-surveys-filters');
+	})();
+</script>