HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: 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,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/sigedo-demo.allup.com.co/app/Http/Controllers/NotificationsController.php
<?php

namespace App\Http\Controllers;

use App\User;
use App\Role;
use App\Notification;
use App\NotificationRole;
use App\NotificationUser;
use Illuminate\Http\Request;
use App\Mail\SendNotification;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Database\Eloquent\Builder;

class NotificationsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        abort_unless(\Gate::allows('notification_access'), 403);
        $notifications = Notification::orderBy('created_at', 'desc')->get();
        return view('notifications.list', compact('notifications'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        abort_unless(\Gate::allows('notification_create'), 403);
        $roles = Role::where('active', true)->get();
        $users = User::select('id','name','last_name')->where('active', true)->get();
        return view('notifications.create', compact('roles','users'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        abort_unless(\Gate::allows('notification_create'), 403);
        if ($notification = Notification::create(array_slice($request->all(), 2))) {
            foreach ($request->input('roles') as $key => $item) {
                NotificationRole::create([
                    "notification_id" => $notification->id,
                    "role_id" => $item,
                ]);
            }

            foreach ($request->input('users') as $key => $item) {
                NotificationUser::create([
                    "notification_id" => $notification->id,
                    "user_id" => $item,
                ]);
            }
            return response(array("status" => true, "type" => "success", "title" => "", "message" => __('created_successfully'), "data" => null));
        } else {
            return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_creating'), "data" => null));
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $notification_id
     * @return \Illuminate\Http\Response
     */
    public function show($notification_id)
    {
        abort_unless(\Gate::allows('notification_show'), 403);
        $data = $this->getDataNotification($notification_id);
        return view('mails.notifications', compact('data'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $notification_id
     * @return \Illuminate\Http\Response
     */
    public function edit($notification_id)
    {
        abort_unless(\Gate::allows('notification_edit'), 403);
        $notification = Notification::with('notification_roles','notification_users')->findOrFail($notification_id);
        $roles = Role::where('active', true)->get();
        $users = User::select('id','name','last_name')->where('active', true)->get();
        return view('notifications.edit', compact('notification','roles','users'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $notification_id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $notification_id)
    {
        abort_unless(\Gate::allows('notification_edit'), 403);
        if (Notification::where('id', $notification_id)->update(array_slice($request->all(), 3))) {
            NotificationRole::where('notification_id', $notification_id)->delete();
            NotificationUser::where('notification_id', $notification_id)->delete();
            foreach ($request->input('roles') as $key => $item) {
                NotificationRole::create([
                    "notification_id" => $notification_id,
                    "role_id" => $item,
                ]);
            }

            foreach ($request->input('users') as $key => $item) {
                NotificationUser::create([
                    "notification_id" => $notification_id,
                    "user_id" => $item,
                ]);
            }
            return response(array("status" => true, "type" => "success", "title" => "", "message" => __('updated_successfully'), "data" => null));
        } else {
            return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_updating'), "data" => null));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $notification_id
     * @return \Illuminate\Http\Response
     */
    public function destroy($notification_id)
    {
        abort_unless(\Gate::allows('notification_destroy'), 403);
        try {
            if (Notification::where('id', $notification_id)->delete()) {
                return response(array("status" => true, "type" => "success", "title" => "", "message" => __('deleted_successfully'), "data" => null));
            } else {
                return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_removing'), "data" => null));
            }
        } catch (\Illuminate\Database\QueryException $e) {
            return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('delete_relation_data'), "data" => null));
        }
    }

    public function getDataNotification($notification_id, $extra_body = '', $attached = false, $attached_name = '')
    {
        $noti = Notification::with('notification_roles','notification_users')->findOrFail($notification_id);
        $users = [];
        foreach ($noti->notification_roles as $key => $item) {
            $current = User::where('active', true)
                            ->whereHas('roles', function (Builder $query) use($item) {
                                $query->where('role_id', $item->role_id);
                            })->get();
            $users[] = $current->pluck('email');
        }

        foreach ($noti->notification_users as $key => $value) {
            $users[] = [$value->user->email];
        }

        if($noti->to){
            $to = explode(";", $noti->to);
            foreach ($to as $key => $email) {
                $users[] = [$email];
            }
        }

        $collection = collect($users);
        $collapsed = $collection->collapse();

        $data = new \stdClass();
        $data->body = $noti->body . $extra_body;
        $data->subject = $noti->subject;
        $data->for = $collapsed;
        $data->attached = $attached;
        $data->attached_name = $attached_name;
        return $data;
    }

    public function sendNotification($notification_id, $extra_body = '', $attached = false, $attached_name = '')
    {
        try {
            $data = $this->getDataNotification($notification_id,$extra_body,$attached,$attached_name);
            Mail::to($data->for)->send(new SendNotification($data));
            return response(array("status" => true, "type" => "success", "title" => "", "message" => __('mail_sending_success'), "data" => null));
        } catch (\Throwable $th) {
            return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('mail_sending_error'), "data" => null));
        }
    }

    public function sendNotificationExpired($email,$filetype){
        try {
            $data = $this->getDataNotification(2,$filetype);
            Mail::to($email)->send(new SendNotification($data));
            return response(array("status" => true, "type" => "success", "title" => "", "message" => __('mail_sending_success'), "data" => null));
        } catch (\Throwable $th) {
            return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('mail_sending_error'), "data" => null));
        }
    }
}