File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/NotificationsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Notification;
use App\NotificationTag;
use App\Tag;
use App\User;
use App\TargetNotification;
use App\UserTag;
use Carbon\Carbon;
use DB;
use Datatables;
use Exception;
use App\Http\Controllers\UtilController;
use App\Jobs\Notifications\ClearAllNotificationJob;
use App\Jobs\Notifications\Entities\TargetTypeEnum;
use App\Jobs\Notifications\Interfaces\SendAllNotification;
use App\Jobs\Notifications\Interfaces\SendNotification;
use App\Jobs\Notifications\SendAllNotificationJob;
use App\Jobs\Notifications\SendNotificationJob;
class NotificationsController extends Controller
{
private $utilController;
public function __construct()
{
$this->utilController = new UtilController();
}
public function index()
{
return view('notifications.notifications');
}
public function indexAdd()
{
$targets = TargetNotification::all();
$multiselectItems = Tag::where('active', true)->get();
return view('notifications.addNotification')
->with('targets', $targets)
->with('multiselectItems', $multiselectItems);
}
public function create(Request $request)
{
$when_send = Carbon::now();
$when_sendNotification = null;
if ($request["when_send"] && Carbon::parse($request["when_send"]) > $when_send) {
$when_send = Carbon::parse($request["when_send"]);
$when_sendNotification = $when_send->toDateTimeString() . " " . config('app.gmt');
}
$push = new PushNotificationController();
$academyLocationController = new AcademyLocationController();
$id = Auth::user() ? Auth::user()->id : 1;
$notification = new Notification();
$notification->message = $request["message"];
$notification->when_send = $when_send;
$notification->is_pending = 1;
$notification->admin_id = $id;
$notification->user_id = $request["user_id"];
$notification->link = $request["link"];
$notification->target_notification_id = $request["target_id"];
$notification->notifications_origin = 'PUSH';
$notification->save(); //Guarda el nuevo objeto
$data = new \stdClass();
$data->link = $notification->link;
$academyLocations = Auth::user() ? $academyLocationController->getByAuthUser()->pluck('id')->toArray() : [];
if ($request["target_id"] != null) {
$segment = TargetNotification::find($request["target_id"]);
$push->sendToSegments($request["message"], $segment->name, null, $data, null, $when_sendNotification, 'U');
} else if ($request["user_id"] != null) {
$user = User::select('id', 'pns_id')->where('id', $request["user_id"])->first();
SendNotificationJob::dispatch(new SendNotification(
$notification->id,
[$user->pns_id],
$request["message"],
$data,
$when_sendNotification,
TargetTypeEnum::USER
));
} else if ($request["tags"] != null) {
foreach ($request["tags"] as $tagId) {
NotificationTag::create([
'tag_id' => $tagId,
'notification_id' => $notification->id
]);
$users = UserTag::select('users.id', 'users.pns_id')
->join('users', 'users.id', '=', 'user_tags.user_id')
->where('user_tags.tag_id', $tagId)
->whereNull('users.deleted_at')
->whereNotNull('users.pns_id')
->get();
$pnsIdsArray = $users->pluck('pns_id')->toArray();
SendNotificationJob::dispatch(new SendNotification(
$notification->id,
$pnsIdsArray,
$request["message"],
$data,
$when_sendNotification,
TargetTypeEnum::USER
));
}
} else if (count($academyLocations)) {
$users = User::select('users.id', 'users.pns_id')
->join('academy_users', 'academy_users.user_id', '=', 'users.id')
->whereNotNull('users.pns_id')
->whereIn('academy_users.academy_location_id', $academyLocations)
->groupBy('users.id', 'users.pns_id')
->get();
$pnsIdsArray = $users->pluck('pns_id')->toArray();
SendNotificationJob::dispatch(new SendNotification(
$notification->id,
$pnsIdsArray,
$request["message"],
$data,
$when_sendNotification,
TargetTypeEnum::USER
));
} else {
SendAllNotificationJob::dispatch(new SendAllNotification(
$notification->id,
$request["message"],
$data,
$when_sendNotification,
TargetTypeEnum::USER
));
}
$logObject = $notification;
$this->registerLog($id, 'Creó una notificación', json_encode($logObject), "Create", $this->getModule($request));
if ($notification) {
return array('r' => true, 'd' => null, 'm' => trans('messages.screen_create_notifications_tag9'));
} else {
return array('r' => false, 'd' => null, 'm' => trans('messages.screen_create_notifications_tag10'));
}
}
public function tableFilter()
{
DB::statement("SET sql_mode = ''");
$obj = Notification::select(
'notifications.id',
'notifications.message',
'notifications.when_send',
'notifications.is_pending',
'notifications.link',
'notifications.admin_id',
'notifications.user_id',
'notifications.target_notification_id',
'notifications.target_city',
'notifications.created_at',
'notifications.notifications_origin',
DB::raw('GROUP_CONCAT(DISTINCT(tags.name)) AS segmentation')
)
->with('admin', 'user', 'target_notification')
->leftjoin('notification_tags', 'notifications.id', '=', 'notification_tags.notification_id')
->leftjoin('tags', function ($join) {
$join->on('tags.id', '=', 'notification_tags.tag_id')->where('tags.active', 1);
})
->whereNull('notifications.deleted_at')
->where('notifications_origin', 'PUSH');
$academyLocationController = new AcademyLocationController();
$academyLocations = $academyLocationController->getByAuthUser()->pluck('id')->toArray();
if (count($academyLocations)) {
$obj->join('academy_location_users', 'academy_location_users.user_id', '=', 'notifications.admin_id')
->whereIn('academy_location_users.academy_location_id', $academyLocations);
}
$obj->groupBy('notifications.id');
DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");
return Datatables::of($obj)
->addColumn('actions', function ($obj) {
return '<i class="fa fa-trash iconMini" onClick="clickDeleteNotification(' . $obj->id . ')" data-id="' . $obj->id . '" data-toggle="tooltip" data-placement="bottom" title="Eliminar" style="cursor:pointer;"></i>';
})
->editColumn('user', function ($obj) {
if ($obj->user)
return $obj->user->first_name . ' ' . $obj->user->last_name;
else
return '';
})
->editColumn('segment', function ($obj) {
if ($obj->target_notification) {
return $obj->target_notification->name;
} else {
return '';
}
})
->editColumn('name', function ($obj) {
if ($obj->admin)
return $obj->admin->first_name . ' ' . $obj->admin->last_name;
else
return '';
})
->editColumn('created_at', function ($obj) {
return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
})
->editColumn('when_send', function ($obj) {
if ($obj->when_send) {
return \Carbon\Carbon::parse($obj->when_send)->format('Y-m-d h:i:s A');
} else {
return 'No tiene fecha programada';
}
})
->rawColumns(['actions', 'name', 'user', 'segment', 'when_send', 'segmentation', 'notifications_origin'])
->make(true);
}
public function removeScheduledNotification(Request $request)
{
$notification = Notification::find($request['id']);
$onesignalId = $notification->reference_onesignal_notification_id;
if ($onesignalId) {
$appId = config('onesignal.app_id');
$push = new PushNotificationController();
$push->cancelNotification($onesignalId, $appId, 'U');
}
$notification->delete();
if ($notification) {
return array('r' => true, 'd' => null, 'm' => trans('messages.screen_create_notifications_tag12'));
} else {
return array('r' => false, 'd' => null, 'm' => trans('messages.screen_create_notifications_tag13'));
}
}
public function createSystemNotification($message, $userId, $whenSend = null, $segmentation = null)
{
try {
$this->create(new Request([
'message' => $message,
'user_id' => $userId,
'when_send' => $whenSend,
'tags' => $segmentation
]));
} catch (Exception $e) {
$error = [
'function' => 'NotificationsController.createSystemNotification.Exception',
'message' => $message,
'userId' => $userId,
'whenSend' => $whenSend,
'segmentation' => $segmentation,
'message' => $e->getMessage(),
'getFile' => $e->getFile(),
'getLine' => $e->getLine(),
];
$this->utilController->logFile(json_encode($error));
}
}
public function clearAllNotifications(Request $request)
{
$notificationsExist = Notification::whereNull('deleted_at')->exists();
if (!$notificationsExist) {
return [
'r' => false,
'm' => trans('messages.screen_notifications_clear_all_empty'),
];
}
// Eliminar notificaciones lógicas
$count = Notification::whereNull('deleted_at')->delete();
ClearAllNotificationJob::dispatch();
$this->registerLog($request->user()->id, 'Eliminó todas las notificaciones', json_encode($count), "Delete", $this->getModule($request));
return [
'r' => true,
'd' => $count,
'm' => trans('messages.screen_notifications_clear_all_success')
];
}
}