Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 202
NotificationController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 12
2070
0.00% covered (danger)
0.00%
0 / 202
 index
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 6
 notificationlistdata
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 46
 create
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 6
 store
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 10
 show
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 edit
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 9
 update
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 10
 destroy
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
 showNotification
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
 showParticularNotification
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 7
 markAllAsRead
0.00% covered (danger)
0.00%
0 / 1
156
0.00% covered (danger)
0.00%
0 / 52
 notificationsAjax
0.00% covered (danger)
0.00%
0 / 1
132
0.00% covered (danger)
0.00%
0 / 46
<?php
namespace App\Http\Controllers\NotificationController;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\NotificationModel\NotificationList;
use App\Models\NotificationModel\Notification;
use App\Models\Auth\User;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Session;
use Carbon\Carbon;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use App\Helpers\AttendanceHelper;
class NotificationController extends Controller
{
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = '/Notifications/Notification List';
        $notifications = NotificationList::all();
        // For returning attendance marking for home controller
        $val = AttendanceHelper::getAttendanceMarkValue();
        $valTeam = AttendanceHelper::getAttendanceTeam($request);
        $valUser = AttendanceHelper::getAttendanceUser($request);
        return view('Notification.NotificationTable',compact('title','notifications','val','valTeam','valUser'));
    }
    public function notificationlistdata(Request $request)
    {
        
        $column_key = array("0"=>"notifications.data","1"=>"notifications.created_at");
        $where_condition = "1 = 1";
        $order_key              =   $request->input('order')[0]['column'];
        $order                  =   $column_key[$order_key];
        $dir                    =   $request->input('order')[0]['dir'];
        if(trim($dir)==''){
            $dir = 'desc';   
        }
        if ($order != '') {
            $orderBy = " ORDER BY ".$order." ".$dir;
        }
        if(null !==($request->input('search.value')) && trim($request->input('search.value'))!=''){
            $search = $request->input('search.value');
            $where_condition .= " AND (notifications.created_at Like '%$search%' OR notifications.data Like '%$search%')";
        }
        $count_model = DB::table('users')->where('users.id', auth()->id())
                                   ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
                                  ->whereRaw($where_condition)
                                   ->count();
       
      
        $total_app_count        =   $count_model;
        $totalFiltered          =   $count_model; 
        $data_model = DB::table('users')->where('users.id', auth()->id())
                                   ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
              ->whereRaw($where_condition)
              ->orderBy($order , $dir)
              ->offset( intval($request->input('start')))->limit(intval($request->input('length')))
              ->get();
     
        $data = array();
        if(count($data_model)>0){
            foreach($data_model as $database_model){
                if(isset(json_decode($database_model->data)->notification ) )
                {
                    $message = json_decode($database_model->data)->notification.':'.json_decode($database_model->data)->message;
                }
                else
                {
                    $message = $database_model->data;
                }
                $created_at = Carbon::parse($database_model->created_at);
                        $created_at = ($created_at)->setTimezone(config('settings.time_zone'));
                $created_at = Carbon::parse($created_at)->format(config('settings.php_date_format').' H:i');
                
               
                if(isset($database_model->read_at) && !empty($database_model->read_at))
                {
                    $nestedData['message'] = '<p class="text-info">'.$message.'</p>';
                     $nestedData['date'] = '<p class="text-info">'.$created_at.'</p>';
                }
                else
                {
                    $nestedData['message'] = $message;
                     $nestedData['date'] = $created_at;
                }
               
                $url = url('particular-notification', $database_model->id);
                $nestedData['action'] = '<a href="'.$url.'">View</a>';
                $data[] = $nestedData;
          }
       
        }
      $json_data = array(
        "draw"            => intval( $request->input('draw')),   
        "recordsTotal"    => intval( $total_app_count ),  
        "recordsFiltered" => intval($total_app_count ),
        "data"            => $data,   // total data array
      );
      echo json_encode($json_data);exit;
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $title ='/Notification/Add Notification List';
        $users = User::all(); 
        // For returning attendance marking for home controller
        $val = AttendanceHelper::getAttendanceMarkValue();
        $valTeam = AttendanceHelper::getAttendanceTeam($request);
        $valUser = AttendanceHelper::getAttendanceUser($request);
        return view('Notification.NotificationForm',compact('title','users','val','valTeam','valUser'));
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'notification' => 'required|unique:notification_lists|max:255',
            'user_id'=> 'required',
            
        ]);
        if ($validator->fails())
        {
            return redirect()->route('notification.create')->withErrors($validator);
        }
        else
        {
            $notifications = NotificationList::create($request->all());
            foreach ($request->user_id as $key => $user_id) 
            {
                $user = User::find($user_id); 
                $notifications->users()->attach($user);
            }
            Session::flash('success', 'Notification Added Successfully!');
            return redirect()->route('notification.index'); 
         }
    }
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request, $id)
    {
        $title = '/Notification/Edit Notification List';
        $notifications = NotificationList::find($id); 
        $users = User::all();
        $usersArr = $notifications->users;
        // For returning attendance marking for home controller
        $val = AttendanceHelper::getAttendanceMarkValue();
        $valTeam = AttendanceHelper::getAttendanceTeam($request);
        $valUser = AttendanceHelper::getAttendanceUser($request);
        return view('Notification.NotificationForm',compact('title','notifications','users','usersArr',
                    'val','valTeam','valUser'));
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            // 'notification'          => 'required',
            // 'user_id'=> 'required',
        ]);
        $notification = NotificationList::find($id);
        $notification->update($request->only(['notification']));
        $usersArr = collect([]);
        foreach ($request->user_id as $key => $user_id)
        {
            $usersArr->push($user_id);
        }
        $notification->users()->sync($usersArr);
        Session::flash('success', 'Notification Updated Successfully!');
        return redirect()->route('notification.index')->withErrors($this);
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $notification = NotificationList::find($id);
        $notification->delete();
        Session::flash('success', 'Notification Deleted Successfully!');
        return redirect()->route('notification.index');
    }
    public function showNotification(Request $request)
    {
        $title = '/Notifications/Notification';
        // For returning attendance marking for home controller
        $val = AttendanceHelper::getAttendanceMarkValue();
        $valTeam = AttendanceHelper::getAttendanceTeam($request);
        $valUser = AttendanceHelper::getAttendanceUser($request);
        return view('Notification.NotificationList',compact('title','val','valTeam','valUser'));
    }
    public function showParticularNotification(Request $request, $id)
      {
        $title = '/Notifications/Particular Notification';
        $users = DB::table('users')->where('users.id', auth()->id())->where('notifications.id', $id)->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')->first(); 
    
        Notification::where('id',$id)->update(['read_at'=>Carbon::now()]);
        // For returning attendance marking for home controller
        $val = AttendanceHelper::getAttendanceMarkValue();
        $valTeam = AttendanceHelper::getAttendanceTeam($request);
        $valUser = AttendanceHelper::getAttendanceUser($request);
        return view('Notification.ParticularNotificationList',compact('title','users','val','valTeam','valUser'));
      }
    
      public function markAllAsRead(){
        $notifications = DB::table('users')->where('users.id', auth()->id())
        ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
        ->orderBy('notifications.created_at','DESC')->limit(10)->get();
        foreach($notifications as $notification){
            Notification::where('id',$notification->id)->update(['read_at'=>Carbon::now()]);
        }
        
        //$notificationsAjax = $this->notificationsAjax();
        $itms = DB::table('users')->where('users.id', auth()->id())
                                    ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
                                    ->orderBy('notifications.created_at','DESC')->limit(10)->get();
        $text = '';
        $count = 0;
        if(!empty($itms)){
        foreach($itms as $itm){
        if(isset(json_decode($itm->data)->notification ) ){
            if( ! isset($itm->read_at ) ){
                $bold = 'style="font-weight:bold"';
                $count = 1;
            }else{
                $bold = '';
            }    
                if(json_decode($itm->data)->actionUrl != null){ 
                 if(json_decode($itm->data)->notification == 'Absent'){ 
                   
                    $url =   route(json_decode($itm->data)->actionUrl, urlencode(base64_encode(Carbon::parse($itm->created_at)->format('Y-m-d')))) ;
                   }else{
                    $contains = Str::contains(json_decode($itm->data)->actionUrl,'#');
                     
                    if($contains){
                        $url_explode = explode('#',json_decode($itm->data)->actionUrl);
                        $url =   Route::has($url_explode[0]) ? route($url_explode[0],'#'.$url_explode[1])  : '';
                    }else{
                        $url =   Route::has(json_decode($itm->data)->actionUrl) ? route(json_decode($itm->data)->actionUrl)  : '';
                    }
                 } 
                 }else{
                   $url =  url('particular-notification', $itm->id);
                  }      
            $created_at = Carbon::parse($itm->created_at);
            $created_at = ($created_at)->setTimezone(config('settings.time_zone'));
            
            $text .= '<a  href="'.$url.'">';
            $text .= '<div class="media-title" id="show-message" '.$bold.'>';
                         
            $text .= '<span class="font-weight-semibold">'.json_decode($itm->data)->notification.'</span>';
            $text .= '<span class="text-muted float-right font-size-sm">';
                    
            $text .= Carbon::parse($created_at)->format(config('settings.php_date_format').' H:i' ).'</span>';
            $text .= '</div>';
            $text .=  '<span class="text-muted" '.$bold.'>'.json_decode($itm->data)->message.'</span>';
            $text .= '</a>';
        }else{
            if( ! isset($itm->read_at ) ){
                $bold = 'style="font-weight:bold"';
                $count = 1;
            }else{
                $bold = '';
            }
                
            $url = url('particular-notification', $itm->id);
            $text .= '<a  href="'.$url.'">';
            $text .= '<div class="media-title" id="show-message" '.$bold.'>';
            $text .= '<span class="font-weight-semibold">'.$itm->name.'</span>';
            $text .= '<span class="text-muted float-right font-size-sm">'.$itm->data.'</span>';
            $text .= '</div>';
            $text .= '</a>'; 
                
        }
        }
        }   
        $text.= '<span class="text-muted"></span>';                     
        
        return response()->json([
            'success'=> 'Marked all Recent Notifications as read',
            'notificationsText'=> $text,
            'notificationsCount' => $count
        ]);
      }
      public function notificationsAjax(){
        $itms = DB::table('users')->where('users.id', auth()->id())
                                    ->Join('notifications', 'users.id', '=', 'notifications.notifiable_id')
                                    ->orderBy('notifications.created_at','DESC')->limit(10)->get();
        $text = '';
        $count = 0;
        if(!empty($itms)){
        foreach($itms as $itm){
        if(isset(json_decode($itm->data)->notification ) ){
            if( ! isset($itm->read_at ) ){
                $bold = 'style="font-weight:bold"';
                $count = 1;
            }else{
                $bold = '';
            }    
                if(json_decode($itm->data)->actionUrl != null){ 
                 if(json_decode($itm->data)->notification == 'Absent'){ 
                   
                    $url =   route(json_decode($itm->data)->actionUrl, urlencode(base64_encode(Carbon::parse($itm->created_at)->format(config('settings.php_date_format'))))) ;
                   }else{
                    $contains = Str::contains(json_decode($itm->data)->actionUrl,'#');
                     
                    if($contains){
                        $url_explode = explode('#',json_decode($itm->data)->actionUrl);
                        $url =   Route::has($url_explode[0]) ? route($url_explode[0],'#'.$url_explode[1])  : '';
                    }else{
                        $url =   Route::has(json_decode($itm->data)->actionUrl) ? route(json_decode($itm->data)->actionUrl)  : '';
                    }
                 } 
                 }else{
                   $url =  url('particular-notification', $itm->id);
                  }      
            $created_at = Carbon::parse($itm->created_at);
            $created_at = ($created_at)->setTimezone(config('settings.time_zone'));
            
            $text .= '<a  href="'.$url.'">';
            $text .= '<div class="media-title" id="show-message" '.$bold.'>';
                         
            $text .= '<span class="font-weight-semibold">'.json_decode($itm->data)->notification.'</span>';
            $text .= '<span class="text-muted float-right font-size-sm">';
                    
            $text .= Carbon::parse($created_at)->format(config('settings.php_date_format').' H:i' ).'</span>';
            $text .= '</div>';
            $text .=  '<span class="text-muted" '.$bold.'>'.json_decode($itm->data)->message.'</span>';
            $text .= '</a>';
        }else{
            if( ! isset($itm->read_at ) ){
                $bold = 'style="font-weight:bold"';
                $count = 1;
            }else{
                $bold = '';
            }
                
            $url = url('particular-notification', $itm->id);
            $text .= '<a  href="'.$url.'">';
            $text .= '<div class="media-title" id="show-message" '.$bold.'>';
            $text .= '<span class="font-weight-semibold">'.$itm->name.'</span>';
            $text .= '<span class="text-muted float-right font-size-sm">'.$itm->data.'</span>';
            $text .= '</div>';
            $text .= '</a>'; 
                
        }
        }
        }   
        $text.= '<span class="text-muted"></span>';                     
        return response()->json([
            'notificationsText'=> $text,
            'notificationsCount' => $count
        ]);                            
      }
}