2019-12-01 23:36:12 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2020-03-06 18:33:03 +01:00
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
2020-03-01 23:30:37 +01:00
|
|
|
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;
|
2020-03-03 15:24:09 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2019-12-01 23:36:12 +01:00
|
|
|
|
|
|
|
class UserSubjectsController extends Controller
|
|
|
|
{
|
2020-03-01 23:30:37 +01:00
|
|
|
protected function validator(array $data)
|
|
|
|
{
|
|
|
|
$messages = [
|
|
|
|
'name.required' => 'Nazwa przedmiotu jest wymagana.',
|
|
|
|
'name.unique' => 'Podana nazwa przedmiotu już istnieje w systemie.',
|
2020-03-03 15:24:09 +01:00
|
|
|
'weekday.in' => 'Dzień tygodnia jest niepoprawny.',
|
|
|
|
'time.date_format' => 'Podana godzina jest nieprawidłowa.',
|
2020-03-01 23:30:37 +01:00
|
|
|
'room_id.required' => 'Sala jest wymagana.',
|
|
|
|
'room_id.exists' => 'Niepoprawny numer sali.'
|
|
|
|
];
|
|
|
|
|
|
|
|
$rules = [
|
|
|
|
'name' => ['required', 'unique:subjects,name'],
|
2020-03-03 15:24:09 +01:00
|
|
|
'weekday' => Rule::in([null, 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela']),
|
|
|
|
'time' => ['date_format:H:i'],
|
2020-03-01 23:30:37 +01:00
|
|
|
'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.',
|
2020-03-03 15:24:09 +01:00
|
|
|
'weekday_e.in' => 'Dzień tygodnia jest niepoprawny.',
|
|
|
|
'time_e.date_format' => 'Podana godzina jest nieprawidłowa.',
|
2020-03-01 23:30:37 +01:00
|
|
|
'room_id_e.required' => 'Sala jest wymagana.',
|
|
|
|
'room_id_e.exists' => 'Niepoprawny numer sali.'
|
|
|
|
];
|
|
|
|
|
|
|
|
$rules = [
|
2020-03-03 15:24:09 +01:00
|
|
|
'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'],
|
2020-03-01 23:30:37 +01:00
|
|
|
'room_id_e' => ['required', 'exists:rooms,id'],
|
2020-03-03 15:24:09 +01:00
|
|
|
'subject_id' => ['sometimes']
|
2020-03-01 23:30:37 +01:00
|
|
|
];
|
|
|
|
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();
|
2020-03-06 18:33:03 +01:00
|
|
|
if(!$user_id) {
|
|
|
|
abort(401);
|
|
|
|
}
|
2019-12-19 00:10:39 +01:00
|
|
|
$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();
|
2019-12-06 22:34:25 +01:00
|
|
|
$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 {
|
2019-12-20 14:09:16 +01:00
|
|
|
$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) {
|
2020-03-01 23:30:37 +01:00
|
|
|
|
|
|
|
$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');
|
2020-03-01 23:30:37 +01:00
|
|
|
$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,
|
2020-03-01 23:30:37 +01:00
|
|
|
'user_id' => $user_id
|
2019-12-01 23:36:12 +01:00
|
|
|
]);
|
2019-12-19 00:10:39 +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
|
|
|
{
|
2020-03-06 18:33:03 +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
|
|
|
}
|
|
|
|
|
2020-03-01 23:30:37 +01:00
|
|
|
public function edit_subject(Request $request)
|
2019-12-01 23:36:12 +01:00
|
|
|
{
|
2020-03-01 23:30:37 +01:00
|
|
|
$subject_id = $request->input('subject_id');
|
2020-03-06 18:33:03 +01:00
|
|
|
try {
|
|
|
|
$subject = Subject::findOrFail($subject_id);
|
|
|
|
if ($request->input('name_e') == $subject->name) {
|
2020-03-03 15:24:09 +01:00
|
|
|
$validator = $this->edited_validator($request->except(['name_e']));
|
|
|
|
} else {
|
|
|
|
$validator = $this->edited_validator($request->all());
|
|
|
|
}
|
2020-03-01 23:30:37 +01:00
|
|
|
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();
|
2020-03-06 18:33:03 +01:00
|
|
|
return redirect()->back();
|
|
|
|
} catch(ModelNotFoundException $exception) {
|
|
|
|
return redirect()->back()->withErrors(['Taki przedmiot nie istnieje w bazie danych, zatem nie można go edytować.']);
|
2020-03-01 23:30:37 +01:00
|
|
|
}
|
2020-03-06 18:33:03 +01:00
|
|
|
|
|
|
|
}
|
2019-12-01 23:36:12 +01:00
|
|
|
}
|