atcheck/app/Http/Controllers/User/UserSubjectsController.php

138 lines
5.2 KiB
PHP
Raw Normal View History

2019-12-01 23:36:12 +01:00
<?php
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\Validator;
2019-12-01 23:36:12 +01:00
use Illuminate\Http\Request;
2019-12-04 10:21:58 +01:00
use Illuminate\Support\Facades\Auth;
2019-12-01 23:36:12 +01:00
use App\Subject;
use App\Room;
use Illuminate\Validation\Rule;
2019-12-01 23:36:12 +01:00
class UserSubjectsController extends Controller
{
protected function validator(array $data)
{
$messages = [
'name.required' => 'Nazwa przedmiotu jest wymagana.',
'name.unique' => 'Podana nazwa przedmiotu już istnieje w systemie.',
'weekday.in' => 'Dzień tygodnia jest niepoprawny.',
'time.date_format' => 'Podana godzina jest nieprawidłowa.',
'room_id.required' => 'Sala jest wymagana.',
'room_id.exists' => 'Niepoprawny numer sali.'
];
$rules = [
'name' => ['required', 'unique:subjects,name'],
'weekday' => Rule::in([null, 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela']),
'time' => ['date_format:H:i'],
'room_id' => ['required', 'exists:rooms,id'],
];
return Validator::make($data, $rules, $messages);
}
protected function edited_validator(array $data)
{
$messages = [
'name_e.required' => 'Nazwa przedmiotu jest wymagana.',
'name_e.unique' => 'Podana nazwa przedmiotu już istnieje w systemie.',
'weekday_e.in' => 'Dzień tygodnia jest niepoprawny.',
'time_e.date_format' => 'Podana godzina jest nieprawidłowa.',
'room_id_e.required' => 'Sala jest wymagana.',
'room_id_e.exists' => 'Niepoprawny numer sali.'
];
$rules = [
'name_e' => ['sometimes', 'required', 'unique:subjects,name'],
'weekday_e' => Rule::in([null, 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela']),
'time_e' => ['date_format:H:i'],
'room_id_e' => ['required', 'exists:rooms,id'],
'subject_id' => ['sometimes']
];
return Validator::make($data, $rules, $messages);
}
2019-12-16 15:45:28 +01:00
public function index($groupBy = 'weekday') {
2019-12-04 10:21:58 +01:00
$user_id = Auth::id();
if(!$user_id) {
abort(401);
}
$subjects = Subject::where('user_id', $user_id)->orderBy('name','ASC')->get();
2019-12-01 23:36:12 +01:00
$subjects_grouped = $subjects->groupBy($groupBy);
2019-12-09 12:17:23 +01:00
$rooms = Room::all();
$weekdays = ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela'];
$defaultTime = date("H:i");
2019-12-15 12:43:40 +01:00
if(date('w')-1 < 0 ) {
$defaultWeekday = $weekdays[6];
} else {
$defaultWeekday = $weekdays[date('w') - 1];
2019-12-15 12:43:40 +01:00
}
2019-12-16 15:45:28 +01:00
return view('user.user_subjects', ['subjects' => $subjects, 'weekdays' => $weekdays, 'subjects_grouped' => $subjects_grouped, 'grouped_by' => $groupBy, 'defaultTime' => $defaultTime, 'defaultWeekday' => $defaultWeekday, 'rooms' => $rooms]);
2019-12-01 23:36:12 +01:00
}
2019-12-16 15:45:28 +01:00
public function add_subject(Request $request) {
$validator = $this->validator($request->all());
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput();
}
2019-12-01 23:36:12 +01:00
$name = $request->input('name');
$weekday = $request->input('weekday');
$time = $request->input('time');
2019-12-09 12:17:23 +01:00
$room_id = $request->input('room_id');
$user_id = Auth::id();
2019-12-01 23:36:12 +01:00
Subject::create([
'name' => $name,
'weekday'=> $weekday,
'time' => $time,
2019-12-09 12:17:23 +01:00
'room_id' => $room_id,
'user_id' => $user_id
2019-12-01 23:36:12 +01:00
]);
return redirect()->back();
2019-12-01 23:36:12 +01:00
}
2019-12-04 10:21:58 +01:00
public function delete_subject($subject_id)
2019-12-01 23:36:12 +01:00
{
try {
Subject::findOrFail($subject_id)->delete();
return redirect()->back();
} catch(ModelNotFoundException $exception) {
return redirect()->back()->withErrors(['Taki przedmiot nie istnieje w bazie danych, zatem nie można go usunąć.']);
}
2019-12-01 23:36:12 +01:00
}
public function edit_subject(Request $request)
2019-12-01 23:36:12 +01:00
{
$subject_id = $request->input('subject_id');
try {
$subject = Subject::findOrFail($subject_id);
if ($request->input('name_e') == $subject->name) {
$validator = $this->edited_validator($request->except(['name_e']));
} else {
$validator = $this->edited_validator($request->all());
}
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator)
->withInput()
->with('subject_id_redirected', $subject_id);
}
$subject->name = $request->input('name_e');
$subject->weekday = $request->input('weekday_e');
$subject->time = $request->input('time_e');
$subject->room_id = $request->input('room_id_e');
$subject->save();
$subject->refresh();
return redirect()->back();
} catch(ModelNotFoundException $exception) {
return redirect()->back()->withErrors(['Taki przedmiot nie istnieje w bazie danych, zatem nie można go edytować.']);
}
}
2019-12-01 23:36:12 +01:00
}