source: app/Http/Controllers/PeopleController.php@ 2bd3041

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

Finalized phase 5

  • Property mode set to 100644
File size: 2.8 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use Illuminate\Http\Request;
6use Illuminate\Support\Facades\DB;
7use Illuminate\Support\Facades\Session;
8
9class PeopleController extends Controller
10{
11 function filter(){
12 if(Session::get('pe_id') == null) {
13 return view('login');
14 }
15 $peoples = DB::select('select * from people;');
16
17 return view('filter', [
18 'peoples' => $peoples
19 ]);
20 }
21 function filter_post(){
22 if(Session::get('pe_id') == null) {
23 return view('login');
24 }
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
32 $credentials = request()->validate([
33 'embg' => 'nullable', // Assuming embg is not always required
34 'gender' => 'nullable',
35 'age' => 'nullable',
36 ]);
37 $query = 'SELECT * FROM people WHERE true';
38
39 $embg = '^' . $credentials['embg'];
40 if ($credentials['embg']) {
41 $query .= " AND embg LIKE '{$credentials['embg']}%'";
42 }
43
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]);
73 }
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);
80 }
81}
Note: See TracBrowser for help on using the repository browser.