source: app/Http/Controllers/PeopleController.php@ 7e9dadd

main
Last change on this file since 7e9dadd was 7e9dadd, checked in by bube-ristovska <ristovska725@…>, 11 months ago

Finalized phase 5

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[75151c6]1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
[cf84baa]6use Illuminate\Support\Facades\DB;
[d9c4096]7use Illuminate\Support\Facades\Session;
[75151c6]8
9class PeopleController extends Controller
10{
11 function filter(){
[d9c4096]12 if(Session::get('pe_id') == null) {
13 return view('login');
14 }
[cf84baa]15 $peoples = DB::select('select * from people;');
16
17 return view('filter', [
18 'peoples' => $peoples
19 ]);
20 }
21 function filter_post(){
[d9c4096]22 if(Session::get('pe_id') == null) {
23 return view('login');
24 }
[7e9dadd]25// $credentials = request()->validate([
26// 'embg' => 'required'
27// ]);
28// $embg = $credentials['embg'];
29//
30// $peoples = DB::select('SELECT * FROM people WHERE embg ~ :embg', ['embg' => '^' . $embg]);
31
[cf84baa]32 $credentials = request()->validate([
[7e9dadd]33 'embg' => 'nullable', // Assuming embg is not always required
34 'gender' => 'nullable',
35 'age' => 'nullable',
[cf84baa]36 ]);
[7e9dadd]37 $query = 'SELECT * FROM people WHERE true';
[cf84baa]38
[7e9dadd]39 $embg = '^' . $credentials['embg'];
40 if ($credentials['embg']) {
41 $query .= " AND embg LIKE '{$credentials['embg']}%'";
42 }
[cf84baa]43
[7e9dadd]44 // Check if $credentials['gender'] is an array and handle accordingly
45 if (isset($credentials['gender']) && (is_array($credentials['gender']) && count($credentials['gender']) > 0)) {
46 $genderConditions = implode(" OR ", array_map(function ($gender) {
47 return "gender = '{$gender}'";
48 }, $credentials['gender']));
49
50 $query .= " AND ({$genderConditions})";
51 } elseif (isset($credentials['gender']) && !is_array($credentials['gender'])) {
52 $query .= " AND gender = '{$credentials['gender']}'";
53 }
54
55 // Check if $credentials['age'] is an array and handle accordingly
56 if (isset($credentials['age']) && is_array($credentials['age']) && count($credentials['age']) > 0) {
57 $ageConditions = [];
58
59 foreach ($credentials['age'] as $ageRange) {
60 // Extract minimum and maximum ages from the range
61 list($minAge, $maxAge) = explode('-', $ageRange);
62
63 // Add condition for the age range
64 $ageConditions[] = "EXTRACT(YEAR FROM AGE(current_date, date_of_birth)) BETWEEN {$minAge} AND {$maxAge}";
65 }
66
67 $query .= " AND (" . implode(" OR ", $ageConditions) . ")";
68 }
69 // Use a raw SQL query with the built conditions
70
71 $peoples = DB::select($query);
72 return view('filter', ['peoples' => $peoples]);
[75151c6]73 }
[92df8cd]74 public function getPerson(Request $request)
75 {
76 $embg = $request->input('embg');
77 $person = DB::select('SELECT * FROM people WHERE embg = :embg', ['embg' => $embg]);
78
79 return response()->json($person[0] ?? null);
[d9c4096]80 }
[75151c6]81}
Note: See TracBrowser for help on using the repository browser.