source: app/Http/Controllers/CrimeCaseController.php@ 3c89e27

main
Last change on this file since 3c89e27 was 3c89e27, checked in by bube-ristovska <ristovska725@…>, 4 months ago

Refactored duplicate code

  • Property mode set to 100644
File size: 5.6 KB
RevLine 
[75151c6]1<?php
2
3namespace App\Http\Controllers;
4
[768f473]5use Carbon\Carbon;
[75151c6]6use Illuminate\Http\Request;
[6dec591]7use Illuminate\Support\Facades\DB;
8use Illuminate\Support\Facades\Session;
[75151c6]9class CrimeCaseController extends Controller
10{
[3c89e27]11 private function policeStationIsPoliceman()
12 {
13 return DB::table('police_station')
14 ->where('p_id', Session::get('p_id'))
15 ->get();
16 }
17 private function policeStationIsOfficer()
18 {
19 return DB::table('police_station')
20 ->where('pe_id', Session::get('pe_id'))
21 ->get();
22 }
[75151c6]23 function cases(){
[d9c4096]24 if(Session::get('pe_id') == null) {
25 return view('login');
26 }
27
28 if(Session::get('is_policeman')){
[3c89e27]29 $police_station = $this->policeStationIsPoliceman();
[d9c4096]30 } else {
[3c89e27]31 $police_station = $this->policeStationIsOfficer();
[d9c4096]32 }
[6dec591]33
[3c89e27]34 $cases = DB::table('crime_case')
35 ->where('p_id', $police_station[0]->p_id)
36 ->get();
[d9c4096]37
[6dec591]38 return view('cases', [
[d9c4096]39 'cases' => $cases,
40 'p_address'=>$police_station[0]->p_address
[6dec591]41 ]);
42
[75151c6]43 }
[249bf91]44 function register_statement(){
45 return view('register-statement');
46 }
47 function register_statement_post()
48 {
[768f473]49 $role = request()->input('role');
50
51
52 $statement = request()->validate([
53 'embg' => 'required',
54 'description' => 'required',
55 'incident_timestamp' => 'required',
56 'incident_place'=>'required'
57 ]);
[3c89e27]58 $statement["statement_date"] = Carbon::now()->format('Y-m-d');
59 $covek = DB::select('select pe_id from people where embg=:embg;',['embg'=> $statement["embg"]]);
[768f473]60 $s_id_b = DB::select('select MAX(s_id) from statements');
61 $s_id = $s_id_b[0]->max;
[3c89e27]62 $s_id = $s_id +1 ;
63 $policaec = DB::select('select pe_id from policeman where badge_no=:badge_no;',['badge_no'=> Session::get("badge_no")]);
[768f473]64
65 if ($role === 'witness') {
66 DB::insert('INSERT INTO statements (s_id, statement_date, description, incident_timestamp, incident_place, c_id, pe_id, victim_pe_id, witness_pe_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
67 [
68 $s_id,
69 $statement["statement_date"],
70 $statement["description"],
71 $statement["incident_timestamp"],
72 $statement["incident_place"],
73 Session::get("c_id"),
74 $policaec[0]->pe_id,
75 NULL,
76 $covek[0]->pe_id
77
78 ]);
79 } elseif ($role === 'victim') {
80 DB::insert('INSERT INTO statements (s_id, statement_date, description, incident_timestamp, incident_place, c_id, pe_id, victim_pe_id, witness_pe_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)',
81 [
82 $s_id,
83 $statement["statement_date"],
84 $statement["description"],
85 $statement["incident_timestamp"],
86 $statement["incident_place"],
87 Session::get("c_id"),
88 $policaec[0]->pe_id,
89 $covek[0]->pe_id,
90 NULL
91 ]);
92 }
93 return redirect()->route('case', ['wildcard' => Session::get("c_id"),]);
94
95
[249bf91]96 }
97 function finished_cases(){
98 if(Session::get('is_policeman')){
[3c89e27]99 $police_station = $this->policeStationIsPoliceman();
[249bf91]100 } else {
[3c89e27]101 $police_station = $this->policeStationIsOfficer();
[249bf91]102 }
103
[3c89e27]104 $cases = DB::table('crime_case')
105 ->where('p_id', $police_station[0]->p_id)
106 ->where('c_status', 'Z')
107 ->get();
[249bf91]108 return view('archive', [
109 'cases' => $cases,
110 'p_address'=>$police_station[0]->p_address
111 ]);
112 }
[d9c4096]113 function case($wildcard){
[768f473]114 Session::put('c_id', $wildcard);
[3c89e27]115 $case = DB::table('crime_case')
116 ->where('c_id', $wildcard)
117 ->get();
118 $p_address = DB::table('police_station')
119 ->where('p_id', $case[0]->p_id)
120 ->value('p_address');
121 $statements = DB::table('statements')
122 ->where('c_id', $wildcard)
123 ->get();
[f7acd52]124
[2bd3041]125 $victims=[];
126 $witness=[];
[f7acd52]127 $evidence_id = [];
128 $evidence = [];
129 foreach ($statements as $statement) {
[3c89e27]130 $evidence_id = DB::table('mentions_evidence')
131 ->where('s_id', $statement->s_id)
132 ->get();
133// if (!empty($evidence_id)) { // Check if $evidence_id is not empty
134// $evidence_id[] = $evidence_id[0];
135// }
[f7acd52]136 }
137 $evidence_id=collect($evidence_id)->unique();
138 foreach ($evidence_id as $e) {
[3c89e27]139 $evidence = DB::table('evidence')
140 ->where('e_id', $e->e_id)->get();
141
[f7acd52]142 }
[2bd3041]143 foreach ($statements as $st){
[768f473]144 if (!($st->victim_pe_id)==NULL){
[3c89e27]145 $victim = DB::table('people')
146 ->where('pe_id', $st->victim_pe_id)->get();
[768f473]147 $victims[] = $victim[0];
148 }
[2bd3041]149 }
150 foreach ($statements as $st){
[768f473]151 if (!($st->witness_pe_id)==NULL) {
[3c89e27]152 $witnes = DB::table('people')
153 ->where('pe_id', $st->witness_pe_id)->get();
[768f473]154 $witness[] = $witnes[0];
155 }
[2bd3041]156 }
157
[d9c4096]158 return view('case', [
159 'case' => $case[0],
[3c89e27]160 'p_address'=>$p_address,
[d9c4096]161 'statements'=>$statements,
162 'evidence'=>$evidence,
[768f473]163 'victims' => $victims,
164 'witness' =>$witness
[d9c4096]165 ]);
[2bd3041]166
[6b10b67]167 }
[75151c6]168}
Note: See TracBrowser for help on using the repository browser.