File: /var/www/vhost/disk-apps/agile-selling-wpb/app/Http/Controllers/NotificationsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use App\Notification;
use App\User;
use App\TargetNotification;
use DB;
use Datatables;
class NotificationsController extends Controller
{
public function __construct(Request $request)
{
$this->middleware('auth');
// $this->middleware(function ($request, $next) {
// $this->user = Auth::user();
// if (Auth::user()->rol->id == 1 || Auth::user()->rol->id == 6) {
// $this->changeDbDefault($request);
// }
// return $next($request);
// });
}
public function index(){
// $rol = Auth::user()->rol->id;
// if ($rol == 1 || $rol == 6) {
return view('notifications.notifications');
// }else{
// return redirect()->back();
// }
}
public function indexAdd(){
// $rol = Auth::user()->rol->id;
// if ($rol == 1 || $rol == 6) {
$targets = TargetNotification::all();
return view('notifications.addNotification')->with('targets',$targets);
// }else{
// return redirect()->back();
// }
}
public function create(Request $request){
$id= Auth::user()->id;
$notification = new Notification();
$notification->message = $request["message"];
$notification->when_send = $request["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->save();//Guarda el nuevo objeto
$when_send = $request["when_send"] . " " . "GMT-0500";
$push = new PushNotificationController();
$data = new \stdClass();
$data->link = $notification->link;
if($request["target_id"] != null){
$segment = TargetNotification::find($request["target_id"]);
$push->sendToSegments($request["message"],$segment->name, null, $data, null, $when_send,'U');
}else if($request["user_id"] != null){
$user = User::select('id','pns_id')->where('id', $request["user_id"])->first();
$push->sendToUser($request["message"],$user->pns_id,null,$data,null,$when_send,'U');
}else{
$push->sendToAll($request["message"],null,$data,null,$when_send,'U');
}
$logObject = $notification;
$this->registerLog(Auth::user()->id, 'Creó una notificación', json_encode($logObject),"Create", 7);
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(){
$obj = Notification::select('id','message','when_send','is_pending','link','admin_id','user_id','target_notification_id','target_city','created_at as created')
->with('admin','user','target_notification')
->whereNull('notifications.deleted_at')
->orderBy('created_at', 'desc');
return Datatables::of($obj)
->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) {
return $obj->admin->first_name.' '.$obj->admin->last_name;
})
->editColumn('when_send', function ($obj) {
if($obj->when_send){
return '<p>'.$obj->when_send.'</p>';
}else{
return '<p> No tiene fecha programada</p>';
}
})
->rawColumns(['name','user','segment','when_send'])
->make(true);
}
}