PHP 7.4.33
Preview: SpeakerNotesRepository.php Size: 6.57 KB
/var/www/multi-event-cfp.bitkit.dk/httpdocs/app/Repositories/SpeakerNotesRepository.php
<?php

namespace App\Repositories;

use App\Models\User;
use App\Models\SpeakerNotes;
use App\Support\Query;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class SpeakerNotesRepository extends Repository
{
    public function __construct($event = null)
    {
        $this->for($event);
    }

    public function model(): SpeakerNotes
    {
        return new SpeakerNotes;
    }

    public function query($as = null)
    {
        $model = $this->model();
        return $model->newQuery();
    }

    public function selectColumns(): array
    {
        return [
            'speaker_notes.id',
            'speaker_notes.user_id',
            'speaker_notes.speaker_id',
            'speaker_notes.note',
            'speaker_notes.is_global',
            'speaker_notes.files',
            'speaker_notes.created_at',
            'speaker_notes.updated_at',
        ];
    }

    public function scope($arguments, $callback = null)
    {
        $query = $this->query()
            ->select($this->selectColumns());

        //joining user to speaker notes
        $query->leftJoin('users as user', 'user.id', '=', 'speaker_notes.user_id');
        $query->leftJoin('events as note_event', 'note_event.id', '=', 'speaker_notes.event_id');
        $query->addSelect([
            DB::raw(
                "JSON_OBJECT(
            'id',user.id,
            'first_name',user.first_name,
            'last_name',user.last_name,
            'email',user.email
            ) as user"
            ),
            DB::raw("note_event.event_name as event_name"),
        ])->where('speaker_id', '=', $arguments->speaker_id)
        ->orderBy('speaker_notes.created_at', 'desc');

        //applying different scopes based on the arguments
        $this->applyScope($query, $arguments);

        //resolve if there is any callback functions available
        return $this->resolve($query, $arguments, $callback);
    }

    public function applyScope(Builder $query, $arguments): Builder
    {
        $this->applyOrder($query, $arguments);
        $this->applyEventScope($query, $arguments);

        return $query;
    }

    public function applyEventScope(Builder $query, $arguments): Builder
    {
        if ($arguments->event) {
            $query->where(function ($q) use ($arguments) {
                $q->where('speaker_notes.event_id', '=', $arguments->event->id)
                  ->orWhere('speaker_notes.is_global', '=', true);
            });
        }

        return $query;
    }

    public function applyOrder($query, Query $arguments): Builder
    {
        if (!$arguments->sort)
            return $query;

        $columns = [
            'id' => 'speaker_notes.id',
            'user_id' => 'speaker_notes.user_id',
            'speaker_id' => 'speaker_notes.speaker_id',
            'note' => 'speaker_notes.note',
            'files' => 'speaker_notes.files',
            'created_at' => 'speaker_notes.created_at',
            'updated_at' => 'speaker_notes.updated_at',
        ];

        $sorts = json_decode($arguments->sort, true);

        foreach ($sorts as $sort => $method) {
            if (array_key_exists($sort, $columns)) {
                $query->orderBy($sort, $method);
            }
        }

        return $query;
    }


    public function listing(Request $request, $paginate)
    {
        //building arguments
        $arguments = $this->arguments($request);

        //building query
        $query = $this->scope($arguments);

        return ($paginate && $arguments->paging != 'All') ? $query->paginate($arguments->paging, ['*'], 'page', $arguments->page) : $query->get();
    }

    public function createSpeakerNotes(Request $request): SpeakerNotes
    {
        // speaker id
        $speakerId = $request->get('speaker_id');
        // admin id
        $adminId = authUser()->id;
        // note
        $note = $request->get('note');
        // is global
        $isGlobal = $request->get('is_global', false);
        // user
        $user = User::find($speakerId);
        // event
        $event = $request->get('event');
        if (!$user)
            validationErrorResponse(['User not found']);
        // add speaker note
        $speakerNotes = new SpeakerNotes([
            'event_id' => $event->id,
            'user_id' => $adminId,
            'speaker_id' => $speakerId,
            'note' => $note,
            'is_global' => $isGlobal,
        ]);

        $speakerNotes->save();

        // handle files
        $files = $this->handleSpeakerNoteFiles($request, $speakerNotes);

        $speakerNotes->files = $files;
        $speakerNotes->save();

        // re-fetch through scope so response includes user, event_name, is_global
        return $this->query()
            ->select($this->selectColumns())
            ->leftJoin('users as user', 'user.id', '=', 'speaker_notes.user_id')
            ->leftJoin('events as note_event', 'note_event.id', '=', 'speaker_notes.event_id')
            ->addSelect([
                DB::raw("JSON_OBJECT('id',user.id,'first_name',user.first_name,'last_name',user.last_name,'email',user.email) as user"),
                DB::raw("note_event.event_name as event_name"),
            ])
            ->where('speaker_notes.id', $speakerNotes->id)
            ->first();
    }

    public function handleSpeakerNoteFiles(Request $request, SpeakerNotes $SpeakerNotes)
    {
        $removedFiles = $request->get('removed_files');
        $removedFiles = json_decode($removedFiles, true);

        $files = $SpeakerNotes->files ?? [];

        //handling uploaded files
        if ($request->hasFile('files')) {
            $noteFiles = $request->file('files');
            $fileContents = [];
            foreach ($noteFiles as $file) {
                $fileObj = $SpeakerNotes->saveFile(
                    $file,
                    null,
                    "{$SpeakerNotes->id}/files/",
                    "speaker_notes.file",
                    null,
                    false
                );
                $fileContents[] = $fileObj->getFileObject();
            }

            $files = array_values(array_filter(array_merge($files, $fileContents)));
        }

        //handling removed files
        if (isset($removedFiles['files'])) {
            $fileIds = $removedFiles['files'];
            foreach ($fileIds as $fileId) {
                $this->deleteFile($fileId);
            }
        }

        return $files;
    }
}

Directory Contents

Dirs: 0 × Files: 17
Name Size Perms Modified Actions
75.27 KB lrw-rw-r-- 2026-04-30 09:24:04
Edit Download
1.21 KB lrw-rw-r-- 2025-04-21 06:11:52
Edit Download
65 B lrw-r--r-- 2024-02-09 12:37:30
Edit Download
8.53 KB lrw-rw-r-- 2025-03-03 05:39:26
Edit Download
4.14 KB lrw-rw-r-- 2025-10-28 05:24:52
Edit Download
8.53 KB lrw-rw-r-- 2025-10-28 05:24:35
Edit Download
37.58 KB lrw-rw-r-- 2026-04-07 05:00:51
Edit Download
8.71 KB lrw-r--r-- 2024-02-09 12:37:30
Edit Download
59.48 KB lrwxrwxr-x 2026-04-30 09:24:03
Edit Download
4.78 KB lrw-r--r-- 2024-02-09 12:37:30
Edit Download
10.79 KB lrw-rw-r-- 2025-04-21 06:11:52
Edit Download
11.37 KB lrw-rw-r-- 2024-07-24 04:42:48
Edit Download
72.51 KB lrw-rw-r-- 2026-04-22 04:31:21
Edit Download
11.43 KB lrw-rw-r-- 2024-09-20 05:02:14
Edit Download
6.57 KB lrw-rw-r-- 2026-03-31 07:16:20
Edit Download
4.26 KB lrw-r--r-- 2024-02-09 12:37:30
Edit Download
128.96 KB lrw-rw-r-- 2026-05-07 09:06:13
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).