%PDF- %PDF-
Direktori : /var/www/html/camillo/camillo-api-master/app/Observers/ |
Current File : /var/www/html/camillo/camillo-api-master/app/Observers/UserPrincipalObserver.php |
<?php namespace App\Observers; use App\Enums\UsertypeEnum; use App\Models\Classroom; use App\Models\Individual; use App\Models\Institute; use App\Models\Schoolyear; use App\User; use Carbon\Carbon; use Illuminate\Support\Facades\DB; class UserPrincipalObserver { /** * Handle the user "created" event. * * @param \App\User $user * @return void * @throws \Throwable */ public function created(User $user) { $current_user = auth()->user() ? auth()->user() : auth('api')->user(); if ($current_user->usertype->role === UsertypeEnum::PRINCIPAL){ $this->associateInstitute($user, $current_user); } if ($user->usertype->role === UsertypeEnum::PRINCIPAL) { $this->generateBasicInstituteTree($user); } } /** * @param User $user * @throws \Throwable */ private function generateBasicInstituteTree(User $user) { DB::transaction(function () use ($user) { $currentYear = date("Y"); $individual = new Individual([ 'user_id'=> $user->id, 'name' => "Il mio nome", 'surname' => "Il mio Cognome", // 'birth_date' => "1981-02-01", 'email' => 'principal@camillo.online', 'fiscal_code' => str_random(16), 'mobile' => "3470000000", 'phone' => "3470000000", 'show_contacts' => 1, ]); $individual->saveOrFail(); $individual->refresh(); $institute = new Institute([ 'code' => strtoupper(str_random(4)), 'name' => "Istituto $user->id", 'category' => "SAMPLE", 'typology' => "SAMPLE", 'city' => "FakeTown", 'province' => "CZ", 'zip_code' => "88046", 'address' => "Via tale", 'phone' => "0000000000", 'email' => $user->individual->email, 'active' => true ]); $institute->saveOrFail(); $institute->refresh(); $user->institutes()->attach($institute); $schoolYear = new Schoolyear([ 'institute_id' => $institute->id, 'start_date' => Carbon::create($currentYear, 9, 1)->format('Y-m-d'), 'end_date' => Carbon::create($currentYear + 1, 5, 31)->format('Y-m-d'), 'description' => $currentYear . '/' . (date("y") + 1) ]); $schoolYear->saveOrFail(); $schoolYear->refresh(); $classroom = new Classroom([ 'institute_id' => $institute->id, 'schoolyear_id' => $schoolYear->id, 'class_name' => "1A", 'arrival_time' => "09:00:00", 'leaving_time' => "16:00:00" ]); $classroom->saveOrFail(); }); } private function associateInstitute(User $user, $current_user){ $institutes = $current_user->institutes; foreach ($institutes as $institute){ $user->institutes()->attach($institute); } } }