Server IP : / Your IP : 10.244.4.16 [ Web Server : nginx/1.25.3 System : Linux escuela-portal-app-54f56585bc-kst6g 5.15.0-1084-azure #93-Ubuntu SMP Sat Mar 15 14:12:29 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.2.13 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals, Domains : 0 Domains MySQL : OFF | cURL : ON | WGET : OFF | Perl : ON | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/app/app/Models/ |
Upload File : |
<?php namespace App\Models; use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; class Cycle extends Model { use HasFactory; protected $fillable = [ 'id', 'name', 'description', 'certification', 'modality_type_id', 'duration', 'imagen', 'requirement', 'slug' ]; protected static function boot() { parent::boot(); // Asignar el slug antes de crear el registro static::creating(function ($cycle) { $originalSlug = $proposedSlug = Str::slug($cycle->name); $count = 1; while (static::where('slug', $proposedSlug)->exists()) { // Si el slug ya existe, agregar un sufijo numérico $proposedSlug = $originalSlug . '-' . $count; $count++; } $cycle->slug = $proposedSlug; }); } public function courses() { return $this->hasMany(Course::class, 'cycle_id'); } public function getOption($id) { try { $response = Option::find($id)->name; } catch (\Throwable $th) { $response = ''; } return $response; } public function requimentsInfo() { return [ 'duration' => $this->duration, 'modality' => $this->getOption($this->modality_type_id), 'alliances' => $this->nameAlliances(), 'certification' => $this->certification, 'requiments' => $this->requirement ]; } public function saveFile($file = null, $url, $column, $ant) { try { Storage::disk('files_base64')->delete(str_replace('/storage/', '', $ant)); } catch (\Throwable $th) { } if ($file) { /** Almacena archivo */ $file = $file[0] ?? $file; $file_base64 = base64_decode(substr($file['content'], strpos($file['content'], ",") + 1)); Storage::disk('files_base64')->put($url, $file_base64); /** Actualiza url */ $this[$column] = '/storage/' . $url; $this->save(); } } public function alliances() { return AssignedAlliance::where('model', 'cycles')->where('model_id', $this->id)->pluck('alliance_id'); } public function nameAlliances() { $alliances = Alliance::whereIn('id', $this->alliances())->get(); if ($alliances->count() >= 1) { $names = $alliances->pluck('name')->implode(', '); } return $names ?? ''; } public function saveAlliances(array $alliances) { $valoresActuales = $this->alliances()->toArray(); $valoresParaEliminar = array_diff($valoresActuales, $alliances); $valoresParaAgregar = array_diff($alliances, $valoresActuales); //Eliminar valores AssignedAlliance::where('model', 'cycles')->where('model_id', $this->id)->whereIn('alliance_id', $valoresParaEliminar)->delete(); //Añadir valores foreach ($valoresParaAgregar as $value) { AssignedAlliance::create([ 'model' => 'cycles', 'model_id' => $this->id, 'alliance_id' => $value ]); } } public function getFile($ext, $letter, $url) { $path = ''; if (isset($url)) { try { $path = [ 'name' => $letter . $this->id . '.' . $ext, 'type' => $ext, 'content' => getHeader($ext) . base64_encode(Storage::get('public/' . str_replace('/storage/', '', $url))), ]; } catch (\Throwable $th) { $path = ''; } } return $path; } public function getFullData() { return [ 'name' => $this->name, 'description' => $this->description, 'certification_type_id' => $this->certification_type_id, 'modality_type_id' => $this->modality_type_id, 'duration' => $this->duration, 'image' => $this->getFile('png', 'CY', $this->imagen), 'file' => $this->getFile('pdf', 'R', $this->requirement), 'alliances' => $this->alliances(), ]; } public function createPage($name) { $page = Page::create([ 'name' => 'Ciclo de ' . $name, 'slug' => '/ciclos/'.$this->slug, 'is_menu' => false ])->id; /** Sección de requerimientos */ PageSection::updateOrCreate( ['page_id' => $page, 'section_id' => 17], ['order' => 1, 'data' => [ 'title' => '', 'text' => '' ]] ); /** Sección de cursos */ PageSection::updateOrCreate( ['page_id' => $page, 'section_id' => 18], ['order' => 2, 'data' => [ 'title' => '', 'text' => '' ]] ); /** Sección de terminos y condiciones */ PageSection::updateOrCreate( ['page_id' => $page, 'section_id' => 19], ['order' => 3, 'data' => [ 'title' => '', 'text' => '' ]] ); } public function updateData($request, $create = false) { $fechaActual = Carbon::now()->format('Y-m-d_H:i:s'); $this->update($request); $this->update(['certification' => 'Diplomado']); $this->saveFile($request['image'], 'img/cycle/CY' . $this->id .'-'.$fechaActual. '.png', 'imagen', $this->imagen); $this->saveFile($request['file'] ?? null, 'doc/cycle/R' . $this->id .'-'.$fechaActual. '.pdf', 'requirement', $this->requirement); $this->saveAlliances($request['alliances']); if ($create) { $this->createPage($request['name']); } } }