lk-students/app/Http/Controllers/ProfileController.php

135 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Helpers\HashHelper;
use App\Http\Requests\ProfileUpdateRequest;
use App\Models\HashRecord;
use App\Services\GetInfoOfStudent;
use App\Services\TestConnect;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;
class ProfileController extends Controller
{
public function show(): View
{
$currentWeek = json_decode(file_get_contents('https://local.mkgtu.ru/raspisnew/api.php?des=raspis_week_cur'), 1);
$user = Auth::user();
$ch = curl_init();
$groupName = 'ПИ';
$groupNumber = (string) 11;
$educationForm = 'ОФО';
$api = "https://local.mkgtu.ru/raspisnew/api.php?des=raspis_grupp&name={$groupName}&number={$groupNumber}&fo={$educationForm}";
curl_setopt($ch, CURLOPT_URL, $api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = json_decode(curl_exec($ch), 1);
curl_close($ch);
$timetable = ['err' => 0, 'msg' => ''];
if ($response['err'] !== 0) {
$timetable = ['err' => $response['err'], 'msg' => $response['msg']];
}
if (array_key_exists('raspis', $response)) {
foreach($response['raspis'] as $subject) {
$timetable[$subject['week1']][$subject['day1']][$subject['time1']] = $subject;
}
foreach($timetable as $week => $day) {
if (!is_array($day)) {
continue;
}
foreach($day as $dayNumber => $subject) {
$forSortSubjects = $timetable[$week][$dayNumber];
ksort($forSortSubjects);
$timetable[$week][$dayNumber] = $forSortSubjects;
}
}
}
// dd($timetable);
$getInfoOfStudent = new GetInfoOfStudent(new TestConnect());
$infoOfStudent = $getInfoOfStudent->getInfo($user);
$apiHash = HashHelper::getApiHash($infoOfStudent);
$recordHash = HashHelper::getRecordHash($user);
if ($apiHash === $recordHash) {
return view('dashboard', compact('user', 'currentWeek', 'timetable'));
}
$user->guid = $infoOfStudent['guid'];
$user->guid_faculty = $infoOfStudent['guid_faculty'];
$user->guid_group = $infoOfStudent['guid_group'];
$user->name = $infoOfStudent['name'];
$user->first_name = $infoOfStudent['first_name'];
$user->last_name = $infoOfStudent['last_name'];
$user->middle_name = $infoOfStudent['middle_name'];
$user->number_record_book = $infoOfStudent['number_record_book'];
$user->email = $infoOfStudent['email'];
$user->faculty = $infoOfStudent['faculty'];
$user->course = $infoOfStudent['course'];
$user->group = $infoOfStudent['group'];
$user->code = $infoOfStudent['code'];
$user->form_education = $infoOfStudent['form_education'];
$user->save();
$hashRecord = HashRecord::firstOrCreate(['user_id' => $user->id]);
$hashRecord->hash = $apiHash;
$hashRecord->save();
Cache::set($user->guid, json_encode($infoOfStudent));
return view('dashboard', compact('user', 'currentWeek', 'timetable'));
}
public function edit(Request $request): View
{
return view('profile.edit', [
'user' => $request->user(),
]);
}
/**
* Update the user's profile information.
*/
public function update(ProfileUpdateRequest $request): RedirectResponse
{
$request->user()->fill($request->validated());
if ($request->user()->isDirty('email')) {
$request->user()->email_verified_at = null;
}
$request->user()->save();
return Redirect::route('profile.edit')->with('status', 'profile-updated');
}
/**
* Delete the user's account.
*/
public function destroy(Request $request): RedirectResponse
{
$request->validateWithBag('userDeletion', [
'password' => ['required', 'current_password'],
]);
$user = $request->user();
Auth::logout();
$user->delete();
$request->session()->invalidate();
$request->session()->regenerateToken();
return Redirect::to('/');
}
}