Sindbad~EG File Manager

Current Path : /home/numerotech/www/livemt2023.aios-scientificcommittee.org/app/controllers/
Upload File :
Current File : //home/numerotech/www/livemt2023.aios-scientificcommittee.org/app/controllers/ReplyController.php

<?php

class ReplyController extends Controller {
	
	
    // public function store()
    // {   

    //     try
    //     {
    //         $operationStatus = false;

    //         $user_id    = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
    //         $topic_id   = Input::has("topic_id") ? Helper::trimAndTruncate(Input::get("topic_id")) : null;
    //         $reply      = Input::has("reply") ? Helper::trimAndTruncate(Input::get("reply"), 5000) : null;
    //         $user_name  = Input::has("user_name") ? Helper::trimAndTruncate(Input::get("user_name"),50) : null;

            

    //         $r = new Reply();
    //         $r->user_id         = $user_id;
    //         $r->topic_id        = $topic_id;
    //         $r->reply           = $reply;
    //         $r->user_name       = $user_name;
            

    //         $operationStatus    = $r->save();

    //         if($operationStatus)
    //         {
    //             $topic = Topic::find($r->topic_id);
    //             if(isset($topic) && intval($topic->topic_id) > 0)
    //             {
    //                 $topic->updated_at = new DateTime();
    //                 $topic->save();
    //             }
    //         }
            
    //         //Notifications
            
    //         if($operationStatus)
    //         {   
    //             $userIds = [];
    //             if($topic_id > 0)
    //             {
    //                 $userIdsReplied     = DB::table("replies as r")
    //                                         ->where("r.topic_id", "=", $topic_id)
    //                                         ->whereNull("r.deleted_at")
    //                                         ->distinct("r.user_id")
    //                                         ->lists("r.user_id");
                    
    
    //                 $userIdsFollowed     = DB::table("topic_follow as tf")
    //                                         ->where("tf.topic_id", "=", $topic_id)
    //                                         ->whereNull("tf.deleted_at")
    //                                         ->distinct("tf.user_id")
    //                                         ->lists("tf.user_id");

    //                 $userIds = array_merge($userIdsReplied , $userIdsFollowed);
    //             } 

    //             $usrdevices = [];
    //             if(count($userIds) > 0)
    //                 $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

    //             $iosDeviceIds = [];
    //             $androidDeviceIds = [];

    //             if(count($usrdevices) > 0)
    //             {
    //                 foreach ($usrdevices as $key => $ud)
    //                 {   
    //                     if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
    //                         array_push($iosDeviceIds, $ud->device_id);
    //                     else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
    //                         array_push($androidDeviceIds,$ud->device_id);
                        
    //                 }
    //             }
    //             $msg =  $user_name.' replied "'.Helper::trimAndTruncate($reply, 250).'"';
                
    //             //IOS
    //             if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
    //                 Helper::sendNotificationToIOS($iosDeviceIds, $msg);

    //             //ANDROID
    //             if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
    //                 Helper::sendNotificationToAndroid($androidDeviceIds, $msg);



    //         }

    //     } catch (Exception $e)
    //     {
    //         Log::error("ReplyController=>store".$e->getMessage());
    //     }

        
    //     if($operationStatus)
    //         return AppMessage::$REPLY_INSERT_SUCCESS;
    //     else
    //         return AppMessage::$GENERAL_ERROR; 
    // }

    public function store()
    {   
        $replyID = 0;

        $r = new Reply();
        $topic = new Topic();

        try
        {
            $operationStatus = false;


            $user_id    = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
            $topic_id   = Input::has("topic_id") ? Helper::trimAndTruncate(Input::get("topic_id")) : null;
            $reply      = Input::has("reply") ? Helper::trimAndTruncate(Input::get("reply"), 5000) : null;
            $user_name  = Input::has("user_name") ? Helper::trimAndTruncate(Input::get("user_name"),50) : null;

            

            
            $r->user_id         = $user_id;
            $r->topic_id        = $topic_id;
            $r->reply           = $reply;
            $r->user_name       = $user_name;
            

            $operationStatus    = $r->save();
            
            Log::info("reply : ".strval($operationStatus));

            if($operationStatus)
            {   
                $replyID = $r->reply_id;
                $topic = Topic::find($r->topic_id);
                if(isset($topic) && intval($topic->topic_id) > 0)
                {
                    $topic->updated_at = new DateTime();
                    $topic->save();
                    Log::info("topic : ".strval($operationStatus));
                }
            }


            //Notifications to all users who replied to the same topic
            $iosDeviceIds = [];
            $androidDeviceIds = [];

            if($operationStatus)
            {   
                $userIds = [];
                if($topic_id > 0)
                {
                    $userIdsReplied     = DB::table("replies as r")
                                            ->where("r.topic_id", "=", $topic_id)
                                            ->where("r.user_id", "<>", $user_id)
                                            ->whereNull("r.deleted_at")
                                            ->distinct("r.user_id")
                                            ->lists("r.user_id");
                    
    
                    $userIdsFollowed     = DB::table("topic_follow as tf")

                                            ->where("tf.topic_id", "=", $topic_id)
                                            ->whereNull("tf.deleted_at")
                                            ->distinct("tf.user_id")
                                            ->lists("tf.user_id");

                    $userIds = array_merge($userIdsReplied , $userIdsFollowed);
                } 
                
                Log::info("userIds : ".implode(",", $userIds));

                $usrdevices = [];
                if(count($userIds) > 0)
                    $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                if(count($usrdevices) > 0)
                {
                    foreach ($usrdevices as $key => $ud)
                    {   

                        //Commented by sridhar
                        //Android and IOS Same FCM Notification

                        // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                        //     array_push($iosDeviceIds, $ud->device_id);
                        // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                        if(isset($ud->device_id))
                            array_push($androidDeviceIds,$ud->device_id);
                        
                    }
                }
                $msg =  $user_name.' replied "'.Helper::trimAndTruncate($reply, 250).'"';
                

                //Commented by sridhar
                //Android and IOS Same FCM Notification
                
                // //IOS
                // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);

                //ANDROID
                if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                    Helper::sendNotificationToAndroid($androidDeviceIds, $msg);

            }
            

            //Notification
            //On posting a topic or reply - alert will be sent to all the moderators of the section 
            //Notification message - "<<Count>> items pending for approval in <<Section Name>>"
            if($operationStatus && isset($topic) && isset($r))
            {
                try
                {
                    $userIds    =   DB::table("moderator_section as ms")
                                            ->whereNull("ms.deleted_at")
                                            ->where("ms.category_id", "=", $topic->category_id)
                                            ->distinct('ms.user_id')
                                            ->lists("ms.user_id");

                    $usrdevices = [];
                    if(count($userIds) > 0)
                        $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                    $iosDeviceIds = [];
                    $androidDeviceIds = [];
                    if(count($usrdevices) > 0)
                    {
                        foreach ($usrdevices as $key => $ud)
                        {   

                            //Commented by sridhar
                            //Android and IOS Same FCM Notification

                            // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                            //     array_push($iosDeviceIds, $ud->device_id);
                            // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                            if(isset($ud->device_id))
                                array_push($androidDeviceIds,$ud->device_id);                            
                            
                        }
                    }

                    $categoryName       =  DB::table("categories as c")->where("c.category_id", "=", $topic->category_id)->pluck("c.name");
                    



                    $countToModerate    =   DB::table("topics as t")->whereNull("t.deleted_at")->where("t.category_id", "=", $topic->category_id)->sum("t.approval_count");

                    $msg = $countToModerate.' posts pending for approval in '.$categoryName;

                    //Commented by sridhar
                    //Android and IOS Same FCM Notification

                    // //IOS
                    // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                    //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);

                    //ANDROID
                    if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                        Helper::sendNotificationToAndroid($androidDeviceIds, $msg);             
                     
                } catch (Exception $e)
                {
                   Log::error("ReplyController=>store1 notification".$e->getMessage());             
                }            
            }
        } catch (Exception $e)
        {
            Log::error("ReplyController=>store".$e->getMessage());
        }


        //File uploads
        if($operationStatus && $replyID > 0)
        {   

            try
            {
                $repliesImages = [];

                //Create directory to store images related to particular reply
                $directoryPath = public_path().'/forum_images/'.$replyID ;
                if(!File::exists($directoryPath))
                    File::makeDirectory($directoryPath, 0777, true);


                //Attachments 
                $files      =   Input::hasFile('files') ? Input::file('files') : [];
                

                $maxFile    =   intval(DB::table("replies_images as ri")
                                    ->where("ri.reply_id", "=", $replyID)
                                    ->whereNull("ri.deleted_at")
                                    ->count("ri.reply_image_id"));

                if(isset($files) && count($files) > 0)
                {
                    if(File::exists($directoryPath))
                    {                        
                        foreach ($files as $key => $file)
                        {
                            if(isset($file) && $file->isValid())
                            {
                                $extension =  $file->getClientOriginalExtension();

                                $maxFile += 1 ;
                                $fileName  =  $maxFile;

                                $fileNameWithExtension = $fileName.'.'.$extension;
                                
                                $destinationPath = $directoryPath."/".$fileNameWithExtension ;
                                $path = URL::to('/').'/forum_images/'.$replyID."/".$fileNameWithExtension ;
                                $file->move($directoryPath, $fileNameWithExtension);
                                array_push($repliesImages,array("reply_id" => $replyID, "file_name" => $fileName, "extension" => $extension, "url" => $path, "created_at" => new DateTime(),"updated_at" =>  new DateTime() ));
                            }
                        }
                    }
                }

                if(isset($repliesImages) && count($repliesImages) > 0)
                    DB::table("replies_images")->insert($repliesImages);                
            } catch (Exception $ex)
            {
                Log::error("ReplyController=>store1 file upload".$ex->getMessage());
            }
        }

        if($operationStatus)
            return AppMessage::$REPLY_INSERT_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR; 
    }    

    public function getRepliesByTopicID($topicID)
    {
        $topics = [];
        $page           = Input::has("page") ? intval(Input::get("page")) : null ;
        $no_of_records  = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;

        try
        {
              $topics = Reply::getRepliesByTopicID($topicID,$page,$no_of_records);
           

        } catch (Exception $e)
        {
            Log::error("ReplyController=>getRepliesByTopicID".$e->getMessage());
        }
        
        return Response::json($topics);
    }   

    public function getRepliesByTopicID1($topicID)
    {
        $replies = [];
        $page           = Input::has("page") ? intval(Input::get("page")) : null ;
        $no_of_records  = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;

        try
        {
            $topics = Reply::getRepliesByTopicID1($topicID,$page,$no_of_records);


        } catch (Exception $e)
        {
            Log::error("ReplyController=>getRepliesByTopicID1".$e->getMessage());
        }
        
        return Response::json($replies);
    } 

     public function getRepliesByTopicID2($topicID, $userID)
    {
        $replies = [];
        $page           = Input::has("page") ? intval(Input::get("page")) : null ;
        $no_of_records  = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;

        try
        {   
            $now = new DateTime();
            $lastWeek = (new DateTime())->modify('-7 day');            
           //Replies
           if(intval($topicID) > 0 && intval($userID) > 0)
           {    
                $repliesQuery =  Reply::getRepliesByTopicID2($topicID,$userID,$now,$lastWeek,$page,$no_of_records);

                $replies    =  $repliesQuery->get();

           }

           //Get images to the corresponding reply
           if(isset($replies) && count($replies) > 0)
           {    
                $replyIds = [];
                foreach ($replies as $r)
                    array_push($replyIds, $r->reply_id);
                
                $replyImages = DB::table("replies_images as ri")
                                  ->whereIn("ri.reply_id", $replyIds)
                                  ->whereNull("ri.deleted_at")
                                  ->select("ri.*")
                                  ->get();

/*                if(isset($replyImages) && count($replyImages) > 0)
                {*/
                    foreach ($replies as $rKey => $r)
                    {   
                        $tmpReplyImages = [];
                        foreach ($replyImages as $riKey => $ri)
                        {
                            if($r->reply_id == $ri->reply_id)        
                                array_push($tmpReplyImages, $ri);
                        }    
                        $replies[$rKey]->files = $tmpReplyImages;
                    }
                /*}*/
           }


        } catch (Exception $e)
        {
            Log::error("ReplyController=>getRepliesByTopicID2".$e->getMessage());
        }
        
        return Response::json($replies);
    }  


    public function updateReplyStatus()
    {   
        
        $operationStatus = false;
        $reply = new Reply();
        $topic = new Topic();

        try
        {   
            $replyID             =    Input::has("reply_id")    ? Input::get("reply_id")       : null;
            $userID              =    Input::has("user_id")     ? Input::get("user_id")        : null;
            $moderationStatus    =    Input::has("moderator_status") ? Input::get("moderator_status")    : null;
            Log::info($replyID);
            Log::info($userID);
            Log::info($moderationStatus);

            //Reply
            if($replyID > 0 && $userID > 0 && !is_null($moderationStatus))
            {
                $reply = Reply::find($replyID);
                if(isset($reply) && intval($reply->reply_id) > 0 && is_null($reply->moderator_status))
                {
                    $reply->moderator_user_id   = $userID;
                    $reply->moderator_status    = $moderationStatus;
                    
                    if($moderationStatus == 1)
                        $reply->approved_on     = new DateTime();   
                    else if($moderationStatus == 0)
                        $reply->rejected_on     = new DateTime();   

                    $operationStatus = $reply->save();  

                    $topic = Topic::find($reply->topic_id);                 
                    
                    //Update updated at column of topic table when moderation status of reply is true
                    if(isset($topic) && intval($topic->topic_id) > 0 && $moderationStatus == 1)
                    {
                        $topic->updated_at = new DateTime();
                        $topic->save();
                    }
                }
            }   

            Log::info($operationStatus);
            //Notification
            if($operationStatus)
            {
                try
                {

                    //Approved
                    //Notification message to other moderators & the user who posted it - "<<Topic or reply title (truncated)>> approved by <<moderator_name>>"
                    //Rejected
                    // Notification message to other moderators & the user who posted it - "<<Topic or reply title (truncated)>> rejected by <<moderator_name>>"
                    if( ($moderationStatus == 1 || $moderationStatus == 0) && isset($topic) && isset($reply))
                    {
                        $otherModerators    =   DB::table("moderator_section as ms")
                                                    ->whereNull("ms.deleted_at")
                                                    ->where("ms.category_id", "=", $topic->category_id)
                                                    ->where("ms.user_id", "<>", $reply->moderator_user_id)
                                                    ->distinct('ms.user_id')
                                                    ->lists("ms.user_id");

                        $replyPostedUserID  =   array($reply->user_id);

                        $userIds            =   array_merge($otherModerators, $replyPostedUserID);

                        $usrdevices = [];
                        if(count($userIds) > 0)
                            $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                        $iosDeviceIds = [];
                        $androidDeviceIds = [];
                        if(count($usrdevices) > 0)
                        {
                            foreach ($usrdevices as $key => $ud)
                            {   

                                //Commented by sridhar
                                //Android and IOS Same FCM Notification

                                // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                                //     array_push($iosDeviceIds, $ud->device_id);
                                // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                                if(isset($ud->device_id))
                                    array_push($androidDeviceIds,$ud->device_id);                                
                                
                            }
                        }
                        

                        $replyName          =   Helper::trimAndTruncate($reply->reply,20).strval("..");
                        $moderatorName      =   DB::table("users as u")->whereNull("u.deleted_at")->where("u.user_id", "=", $reply->moderator_user_id)->pluck("u.first_name");

                        if($moderationStatus == 1 )
                            $msg = strval($reply->reply_id)." - ".$replyName.' approved by '.$moderatorName ;
                        else
                            $msg = strval($reply->reply_id)." - ".$replyName.' disapproved by '.$moderatorName ;

                        Log::info("user IDs count".implode(", ", $userIds));
                        Log::info("Message".$msg);  
                        Log::info("User device table count".count($usrdevices)); 
                        Log::info("IOS device ids count".count($iosDeviceIds)); 


                        //Commented by sridhar
                        //Android and IOS Same FCM Notification

                        //IOS
                        // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                        //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);

                        //ANDROID
                        if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                            Helper::sendNotificationToAndroid($androidDeviceIds, $msg);

                        
                        //Approved - Sent to other users who follow the topic - "<<Username>> posted <<Reply title (truncated)>>"
                        //Variable Re Declarations
                        if($moderationStatus == 1)
                        {
                            $msg     = '';
                            $userIds = [];

                            $userIds = DB::table("topic_follow as tf")->whereNull("tf.deleted_at")->where("tf.topic_id", "=", $topic->topic_id)->where("tf.user_id", "<>", $reply->moderator_user_id)->where("tf.user_id", "<>", $reply->user_id)->distinct("tf.user_id")->lists("tf.user_id");

                            $usrdevices = [];
                            if(count($userIds) > 0)
                                $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                            $iosDeviceIds = [];
                            $androidDeviceIds = [];
                            if(count($usrdevices) > 0)
                            {
                                foreach ($usrdevices as $key => $ud)
                                {   
                                    //Commented by sridhar
                                    //Android and IOS Same FCM Notification

                                    // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                                    //     array_push($iosDeviceIds, $ud->device_id);
                                    // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                                    if(isset($ud->device_id))
                                        array_push($androidDeviceIds,$ud->device_id);                                    
                                }
                            }
                            
                            //$replyPostedUserName = DB::table("users u")->whereNull("u.deleted_at")->where("u.user_id", "=", $reply->user_id)->pluck("u.name");
                            $replyPostedUserName = $reply->user_name;
                            $msg     = $replyPostedUserName.' posted '.$replyName;


                            //Commented by sridhar
                            //Android and IOS Same FCM Notification

                            // //IOS
                            // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                            //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);
                            
                            //ANDROID
                            if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                                Helper::sendNotificationToAndroid($androidDeviceIds, $msg);                             
                            
                        }                     
                    }  


                } catch (Exception $ex)
                {
                   Log::error("TopicController=>updateTopicStatus notification".$ex->getMessage());  
                }

            }          
        }
        catch (Exception $e)
        {
            Log::error("ReplyController=>updateReplyStatus".$e->getMessage()); 
        }     

        return Response::json($reply);

/*        if($operationStatus)
            return AppMessage::$FORUM_REPLY_STATUS_UPDATE_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR; */
    }     


    public function voteStore()
    {
        $operationStatus = false;
        try
        {
            $userID     = Input::has("user_id")     ? Input::get("user_id")     : null;
            $replyID    = Input::has("reply_id")    ? Input::get("reply_id")    : null;
            $vote       = Input::has("vote")        ? Input::get("vote")        : null;

            if(isset($replyID) && isset($userID) && isset($vote))
            {  


                $forumVotesCount    =   intval(DB::table("forum_votes as fv")
                                            ->whereNull("fv.deleted_at")
                                            ->where("fv.user_id", "=", $userID)
                                            ->where("fv.reply_id", "=", $replyID)
                                            ->count());

                if($forumVotesCount > 0)//Update
                {

                    $operationStatus    =   DB::table("forum_votes as fv")
                                                ->whereNull("fv.deleted_at")
                                                ->where("fv.user_id", "=", $userID)
                                                ->where("fv.reply_id", "=", $replyID)
                                                ->limit(1) 
                                                ->update(array("fv.vote" => $vote,"fv.updated_at" => new DateTime()));

                }
                else//Insert
                {   
                    $operationStatus    = DB::table("forum_votes")
                                            ->insertGetId(array("user_id" => $userID, "reply_id" => $replyID, "vote" => $vote,"created_at" => new DateTime()));
                }
            }

        } catch (Exception $e)
        {
            Log::error("ReplyController=>voteStore".$e->getMessage()); 
        }
        
        if($operationStatus)
            return AppMessage::$FORUM_REPLY_VOTE_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR;
    }

    public function markRead()
    {
        $operationStatus = false;
        try
        {
            
            $topicID    = Input::has("topic_id")    ? Input::get("topic_id")    : null;
            $userID     = Input::has("user_id")     ? Input::get("user_id")     : null;
                   Log::info( $userID ); 



            if(isset($topicID) && isset($userID))
            {  
                $repliesRead = [];
                $replies = DB::table("replies as r")->whereNull("r.deleted_at")->
                where("r.topic_id", "=", $topicID)->get();
                $topic = Topic::find($topicID);
                
                $userIds    =   DB::table("moderator_section as ms")
                                        ->whereNull("ms.deleted_at")
                                        ->where("ms.category_id", "=", $topic->category_id)
                                        ->distinct('ms.user_id')
                                        ->lists("ms.user_id");
                                        
                                      
                
                if(count($replies) > 0)
                { 

                    foreach ($replies as $reply)
                    {


                        if($reply->moderator_status==1 || in_array($userID, $userIds))
                        {
                            array_push($repliesRead, array("reply_id" => $reply->reply_id, "user_id" => $userID, "created_at" => new DateTime()));
                            
                      
                        }
                        DB::table("replies_read")->where("user_id", "=", $userID)->where("reply_id", $reply->reply_id)->delete();
                    }
            

                }


                if(count($repliesRead) > 0)
                    $operationStatus = DB::table("replies_read")->insert($repliesRead);

            }

        } catch (Exception $e)
        {
            Log::error("ReplyController=>markRead".$e->getMessage()); 
        }
        
        if($operationStatus)
            return AppMessage::$FORUM_REPLY_READ_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR;
    }
    public function deleteReplyRead()
    {   
        $operationStatus = false;
        try
        {
            
            $lastWeek = (new DateTime())->modify('-7 day');
            $operationStatus = DB::table("replies_read")->where("created_at", "<=", $lastWeek)->delete();
            

        } catch (Exception $e)
        {
            Log::error("ReplyController=>deleteReplyRead".$e->getMessage()); 
        }

        if($operationStatus)
            return AppMessage::$FORUM_REPLY_READ_DELETE_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR;
    }
    public function webStore()
    {   
        $replyID = 0;

        $r = new Reply();
        $topic = new Topic();

        try
        {
            $operationStatus = false;


            $user_id    = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
            $topic_id   = Input::has("topic_id") ? Helper::trimAndTruncate(Input::get("topic_id")) : null;
            $reply      = Input::has("reply") ? Helper::trimAndTruncate(Input::get("reply"), 5000) : null;
            $user_name  = Input::has("user_name") ? Helper::trimAndTruncate(Input::get("user_name"),50) : null;

            

            
            $r->user_id         = $user_id;
            $r->topic_id        = $topic_id;
            $r->reply           = $reply;
            $r->user_name       = $user_name;
            

            $operationStatus    = $r->save();
            
            Log::info("reply : ".strval($operationStatus));

            if($operationStatus)
            {   
                $replyID = $r->reply_id;

                $topic = Topic::find($r->topic_id);
                if(isset($topic) && intval($topic->topic_id) > 0)
                {
                    $topic->updated_at = new DateTime();
                    $topic->save();
                    Log::info("topic : ".strval($operationStatus));
                }
            }


            //Notifications to all users who replied to the same topic
            $iosDeviceIds = [];
            $androidDeviceIds = [];

            if($operationStatus)
            {   
                $userIds = [];
                if($topic_id > 0)
                {
                    $userIdsReplied     = DB::table("replies as r")
                                            ->where("r.topic_id", "=", $topic_id)
                                            ->where("r.user_id", "<>", $user_id)
                                            ->whereNull("r.deleted_at")
                                            ->distinct("r.user_id")
                                            ->lists("r.user_id");
                    
    
                    $userIdsFollowed     = DB::table("topic_follow as tf")

                                            ->where("tf.topic_id", "=", $topic_id)
                                            ->whereNull("tf.deleted_at")
                                            ->distinct("tf.user_id")
                                            ->lists("tf.user_id");

                    $userIds = array_merge($userIdsReplied , $userIdsFollowed);
                } 
                
                Log::info("userIds : ".implode(",", $userIds));

                $usrdevices = [];
                if(count($userIds) > 0)
                    $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                if(count($usrdevices) > 0)
                {
                    foreach ($usrdevices as $key => $ud)
                    {  

                        //Commented by sridhar
                        //Android and IOS Same FCM Notification

                        // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                        //     array_push($iosDeviceIds, $ud->device_id);
                        // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                        if(isset($ud->device_id))
                            array_push($androidDeviceIds,$ud->device_id);
                        
                    }
                }
                $msg =  $user_name.' replied "'.Helper::trimAndTruncate($reply, 250).'"';
                
                //Commented by sridhar
                //Android and IOS Same FCM Notification

                // //IOS
                // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);

                //ANDROID
                if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                    Helper::sendNotificationToAndroid($androidDeviceIds, $msg);

            }
            

            //Notification
            //On posting a topic or reply - alert will be sent to all the moderators of the section 
            //Notification message - "<<Count>> items pending for approval in <<Section Name>>"
            if($operationStatus && isset($topic) && isset($r))
            {
                try
                {
                    $usrIds    =   intval(DB::table("moderator_section as ms")
                                        ->whereNull("ms.deleted_at")
                                        ->where("ms.category_id", "=", $topic->category_id)
                                        ->where("ms.user_id", "=", $user_id)
                                        ->count());


                 if(($usrIds) > 0)
                  {
                    
                    $r->moderator_status =1;
                    $r->approved_on =new DateTime();
                    $r->save();
                   }
                    $userIds    =   DB::table("moderator_section as ms")
                                            ->whereNull("ms.deleted_at")
                                            ->where("ms.category_id", "=", $topic->category_id)
                                            ->distinct('ms.user_id')
                                            ->lists("ms.user_id");

                    $usrdevices = [];
                    if(count($userIds) > 0)
                        $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                    $iosDeviceIds = [];
                    $androidDeviceIds = [];
                    if(count($usrdevices) > 0)
                    {
                        foreach ($usrdevices as $key => $ud)
                        {   

                            //Commented by sridhar
                            //Android and IOS Same FCM Notification

                            // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                            //     array_push($iosDeviceIds, $ud->device_id);
                            // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                            if(isset($ud->device_id))
                                array_push($androidDeviceIds,$ud->device_id);                            
                            
                        }
                    }

                    $categoryName       =  DB::table("categories as c")->where("c.category_id", "=", $topic->category_id)->pluck("c.name");
                    



                    $countToModerate    =   DB::table("topics as t")->whereNull("t.deleted_at")->where("t.category_id", "=", $topic->category_id)->sum("t.approval_count");

                    $msg = $countToModerate.' posts pending for approval in '.$categoryName;


                    //Commented by sridhar
                    //Android and IOS Same FCM Notification

                    // //IOS
                    // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                    //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);

                    //ANDROID
                    if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                        Helper::sendNotificationToAndroid($androidDeviceIds, $msg);             
                     
                } catch (Exception $e)
                {
                   Log::error("ReplyController=>store1 notification".$e->getMessage());             
                }            
            }
        } catch (Exception $e)
        {
            Log::error("ReplyController=>store".$e->getMessage());
        }


        //File uploads
        if($operationStatus && $replyID > 0)
        {   

            try
            {
                $repliesImages = [];

                //Create directory to store images related to particular reply
                $directoryPath = public_path().'/forum_images/'.$replyID ;
                if(!File::exists($directoryPath))
                    File::makeDirectory($directoryPath, 0777, true);


                //Attachments 
                $files      =   Input::hasFile('files') ? Input::file('files') : [];
                

                $maxFile    =   intval(DB::table("replies_images as ri")
                                    ->where("ri.reply_id", "=", $replyID)
                                    ->whereNull("ri.deleted_at")
                                    ->count("ri.reply_image_id"));

                if(isset($files) && count($files) > 0)
                {
                    if(File::exists($directoryPath))
                    {                        
                        foreach ($files as $key => $file)
                        {
                            if(isset($file) && $file->isValid())
                            {
                                $extension =  $file->getClientOriginalExtension();

                                $maxFile += 1 ;
                                $fileName  =  $maxFile;

                                $fileNameWithExtension = $fileName.'.'.$extension;
                                
                                $destinationPath = $directoryPath."/".$fileNameWithExtension ;
                                $path = URL::to('/').'/forum_images/'.$replyID."/".$fileNameWithExtension ;
                                $file->move($directoryPath, $fileNameWithExtension);
                                array_push($repliesImages,array("reply_id" => $replyID, "file_name" => $fileName, "extension" => $extension, "url" => $path, "created_at" => new DateTime(),"updated_at" =>  new DateTime() ));
                            }
                        }
                    }
                }

                if(isset($repliesImages) && count($repliesImages) > 0)
                    DB::table("replies_images")->insert($repliesImages);                
            } catch (Exception $ex)
            {
                Log::error("ReplyController=>store1 file upload".$ex->getMessage());
            }
        }

        if($operationStatus)
            return Redirect::to('forum/replies/'.Helper::encrypt($topic_id).'/'.Helper::encrypt($user_id))->with("msgSuccess", AppMessage::$REPLY_INSERT_MODERATORS_SUCCESS);
        else
            return AppMessage::$GENERAL_ERROR; 
    }    

    public function getWebRepliesByTopicID($topicID)
    {
        $topics = [];
        $page           = Input::has("page") ? intval(Input::get("page")) : null ;
        $no_of_records  = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;

        try
        {
              $topics = Reply::getRepliesByTopicID($topicID,$page,$no_of_records);
           

        } catch (Exception $e)
        {
            Log::error("ReplyController=>getWebRepliesByTopicID".$e->getMessage());
        }
        
        return Response::json($topics);
    }   

    public function getWebRepliesByTopicID1($topicID)
    {
        $replies = [];
        $page           = Input::has("page") ? intval(Input::get("page")) : null ;
        $no_of_records  = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;

        try
        {
            $topics = Reply::getRepliesByTopicID1($topicID,$page,$no_of_records);


        } catch (Exception $e)
        {
            Log::error("ReplyController=>getWebRepliesByTopicID1".$e->getMessage());
        }
        
        return Response::json($replies);
    }
        public function getWebRepliesByTopicID2($topicID, $userID)
    {


        $replies = [];
        $topicID    = Helper::decrypt($topicID);
        $userID     = Helper::decrypt($userID);
        $topicss    =Topic::find($topicID);
        $catID      =$topicss->category_id;
        $topics = "";
     
        

        $userName = Auth::user()->prefix.'.'.Auth::user()->first_name;


       $userIds    =   intval(DB::table("moderator_section as ms")
                                                ->whereNull("ms.deleted_at")
                                                ->where("ms.category_id", "=", $catID)
                                                ->where("ms.user_id", "=", $userID)
                                                ->count());


                     if(($userIds) > 0)
                            $moderator_id = $userID;
                      else
                            $moderator_id = null;
                        
        $page           = Input::has("page") ? intval(Input::get("page")) : null ;
        $no_of_records  = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;

       

        try
        {   
            $now = new DateTime();
            $lastWeek = (new DateTime())->modify('-7 day');            
           //Replies
           if(intval($topicID) > 0 && intval($userID) > 0)
           {    
                $repliesQuery =  Reply::getRepliesByTopicID2($topicID,$userID,$now,$lastWeek,$page,$no_of_records);
                $topicQuery   =  Topic::getTopicsByCatIDUserID($catID,$userID,$now,$lastWeek,$page,$no_of_records);
            
                $arraytopics       =   $topicQuery->get();
             

                $replies    =  $repliesQuery->paginate(10);
              
           }



           //Get images to the corresponding reply
           if(isset($replies) && count($replies) > 0)
           {    
                $replyIds = [];
                foreach ($replies as $r)
                    array_push($replyIds, $r->reply_id);
                
                $replyImages = DB::table("replies_images as ri")
                                  ->whereIn("ri.reply_id", $replyIds)
                                  ->whereNull("ri.deleted_at")
                                  ->select("ri.*")
                                  ->get();

/*                if(isset($replyImages) && count($replyImages) > 0)
                {*/
                    foreach ($replies as $rKey => $r)
                    {   
                        $tmpReplyImages = [];
                        foreach ($replyImages as $riKey => $ri)
                        {
                            if($r->reply_id == $ri->reply_id)        
                                array_push($tmpReplyImages, $ri);
                        }    
                        $replies[$rKey]->files = $tmpReplyImages;
                    }
                  

                /*}*/
           }

                foreach($arraytopics as $value)
                {

                   if($value->topic_id==$topicID)
                   {                      
                    $topics = $value;
                   }
               }


        } catch (Exception $e)
        {
            Log::error("ReplyController=>getWebRepliesByTopicID2".$e->getMessage());
        }
        
        return View::make('forum.replies')->with('replies',$replies)
                                          ->with('topics',$topics)
                                          ->with('userID',$userID)
                                          ->with('moderator_id',$moderator_id)
                                          ->with('userName',$userName);
                                          
    }   

   
    public function updateWebReplyStatus()
    {   
        
        $operationStatus = false;
        $reply = new Reply();
        $topic = new Topic();

        try
        {   
            $replyID             =    Input::has("reply_id")    ? Input::get("reply_id")       : null;
            $userID              =    Input::has("user_id")     ? Input::get("user_id")        : null;
            $moderationStatus    =    Input::has("moderator_status") ? Input::get("moderator_status")    : null;
            Log::info($replyID);
            Log::info($userID);
            Log::info($moderationStatus);

            //Reply
            if($replyID > 0 && $userID > 0 && !is_null($moderationStatus))
            {
                $reply = Reply::find($replyID);
                if(isset($reply) && intval($reply->reply_id) > 0 && is_null($reply->moderator_status))
                {
                    $reply->moderator_user_id   = $userID;
                    $reply->moderator_status    = $moderationStatus;
                    
                    if($moderationStatus == 1)
                        $reply->approved_on     = new DateTime();   
                    else if($moderationStatus == 0)
                        $reply->rejected_on     = new DateTime();   

                    $operationStatus = $reply->save();  

                    $topic = Topic::find($reply->topic_id);                 
                    
                    //Update updated at column of topic table when moderation status of reply is true
                    if(isset($topic) && intval($topic->topic_id) > 0 && $moderationStatus == 1)
                    {
                        $topic->updated_at = new DateTime();
                        $topic->save();
                    }
                }
            }   

            Log::info($operationStatus);
            //Notification
            if($operationStatus)
            {
                try
                {

                    //Approved
                    //Notification message to other moderators & the user who posted it - "<<Topic or reply title (truncated)>> approved by <<moderator_name>>"
                    //Rejected
                    // Notification message to other moderators & the user who posted it - "<<Topic or reply title (truncated)>> rejected by <<moderator_name>>"
                    if( ($moderationStatus == 1 || $moderationStatus == 0) && isset($topic) && isset($reply))
                    {
                        $otherModerators    =   DB::table("moderator_section as ms")
                                                    ->whereNull("ms.deleted_at")
                                                    ->where("ms.category_id", "=", $topic->category_id)
                                                    ->where("ms.user_id", "<>", $reply->moderator_user_id)
                                                    ->distinct('ms.user_id')
                                                    ->lists("ms.user_id");

                        $replyPostedUserID  =   array($reply->user_id);

                        $userIds            =   array_merge($otherModerators, $replyPostedUserID);

                        $usrdevices = [];
                        if(count($userIds) > 0)
                            $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                        $iosDeviceIds = [];
                        $androidDeviceIds = [];
                        if(count($usrdevices) > 0)
                        {
                            foreach ($usrdevices as $key => $ud)
                            {   
                                //Commented by sridhar
                                //Android and IOS Same FCM Notification

                                // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                                //     array_push($iosDeviceIds, $ud->device_id);
                                // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                                if(isset($ud->device_id))
                                    array_push($androidDeviceIds,$ud->device_id);                                
                                
                            }
                        }
                        

                        $replyName          =   Helper::trimAndTruncate($reply->reply,20).strval("..");
                        $moderatorName      =   DB::table("users as u")->whereNull("u.deleted_at")->where("u.user_id", "=", $reply->moderator_user_id)->pluck("u.name");

                        if($moderationStatus == 1 )
                            $msg = strval($reply->reply_id)." - ".$replyName.' approved by '.$moderatorName ;
                        else
                            $msg = strval($reply->reply_id)." - ".$replyName.' disapproved by '.$moderatorName ;

                        Log::info("user IDs count".implode(", ", $userIds));
                        Log::info("Message".$msg);  
                        Log::info("User device table count".count($usrdevices)); 
                        Log::info("IOS device ids count".count($iosDeviceIds)); 


                        //Commented by sridhar
                        //Android and IOS Same FCM Notification

                        // //IOS
                        // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                        //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);

                        //ANDROID
                        if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                            Helper::sendNotificationToAndroid($androidDeviceIds, $msg);

                        
                        //Approved - Sent to other users who follow the topic - "<<Username>> posted <<Reply title (truncated)>>"
                        //Variable Re Declarations
                        if($moderationStatus == 1)
                        {
                            $msg     = '';
                            $userIds = [];

                            $userIds = DB::table("topic_follow as tf")->whereNull("tf.deleted_at")->where("tf.topic_id", "=", $topic->topic_id)->where("tf.user_id", "<>", $reply->moderator_user_id)->where("tf.user_id", "<>", $reply->user_id)->distinct("tf.user_id")->lists("tf.user_id");

                            $usrdevices = [];
                            if(count($userIds) > 0)
                                $usrdevices = DB::table("user_devices as ud")->whereIn('ud.user_id', $userIds)->whereNull("ud.deleted_at")->get();

                            $iosDeviceIds = [];
                            $androidDeviceIds = [];
                            if(count($usrdevices) > 0)
                            {
                                foreach ($usrdevices as $key => $ud)
                                {  

                                //Commented by sridhar
                                //Android and IOS Same FCM Notification 
                                    // if(isset($ud->device_id) && strcasecmp($ud->platform, 'IOS') == 0 )
                                    //     array_push($iosDeviceIds, $ud->device_id);
                                    // else if(isset($ud->device_id) && strcasecmp($ud->platform, 'ANDROID') == 0 )
                                    if(isset($ud->device_id))
                                        array_push($androidDeviceIds,$ud->device_id);                                    
                                }
                            }
                            
                            //$replyPostedUserName = DB::table("users u")->whereNull("u.deleted_at")->where("u.user_id", "=", $reply->user_id)->pluck("u.name");
                            $replyPostedUserName = $reply->user_name;
                            $msg     = $replyPostedUserName.' posted '.$replyName;


                            //Commented by sridhar
                            //Android and IOS Same FCM Notification 

                            // //IOS
                            // if(isset($iosDeviceIds) && count($iosDeviceIds) > 0)
                            //     Helper::sendNotificationToIOS($iosDeviceIds, $msg);
                            
                            //ANDROID
                            if(isset($androidDeviceIds) && count($androidDeviceIds) > 0)
                                Helper::sendNotificationToAndroid($androidDeviceIds, $msg);                             
                            
                        }                     
                    }  


                } catch (Exception $ex)
                {
                   Log::error("TopicController=>updateTopicStatus notification".$ex->getMessage());  
                }

            }          
        }
        catch (Exception $e)
        {
            Log::error("ReplyController=>updateReplyStatus".$e->getMessage()); 
        }     

        return Response::json($reply);

/*        if($operationStatus)
            return AppMessage::$FORUM_REPLY_STATUS_UPDATE_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR; */
    }     


    public function webVoteStore()
    {
        $operationStatus = false;
        try
        {
            $userID     = Input::has("user_id")     ? Input::get("user_id")     : null;
            $replyID    = Input::has("reply_id")    ? Input::get("reply_id")    : null;
            $vote       = Input::has("vote")        ? Input::get("vote")        : null;

            if(isset($replyID) && isset($userID) && isset($vote))
            {  


                $forumVotesCount    =   intval(DB::table("forum_votes as fv")
                                            ->whereNull("fv.deleted_at")
                                            ->where("fv.user_id", "=", $userID)
                                            ->where("fv.reply_id", "=", $replyID)
                                            ->count());

                if($forumVotesCount > 0)//Update
                {

                    $operationStatus    =   DB::table("forum_votes as fv")
                                                ->whereNull("fv.deleted_at")
                                                ->where("fv.user_id", "=", $userID)
                                                ->where("fv.reply_id", "=", $replyID)
                                                ->limit(1) 
                                                ->update(array("fv.vote" => $vote,"fv.updated_at" => new DateTime()));

                }
                else//Insert
                {   
                    $operationStatus    = DB::table("forum_votes")
                                            ->insertGetId(array("user_id" => $userID, "reply_id" => $replyID, "vote" => $vote,"created_at" => new DateTime()));
                }
            }
            $reply = Reply::find($replyID);
                    
                     return Response::json($reply);

        } catch (Exception $e)
        {
            Log::error("ReplyController=>voteStore".$e->getMessage()); 
        }
        
        if($operationStatus)
            return AppMessage::$FORUM_REPLY_VOTE_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR;
    }

    public function webMarkRead()
    {
        $operationStatus = false;
        try
        {
            
            $topicID    = Input::has("topic_id")    ? Input::get("topic_id")    : null;
            $userID     = Input::has("user_id")     ? Input::get("user_id")     : null;



            if(isset($topicID) && isset($userID))
            {  
                $repliesRead = [];
                $replies = DB::table("replies as r")->whereNull("r.deleted_at")->
                where("r.topic_id", "=", $topicID)->get();
                $topic = Topic::find($topicID);
                
                $userIds    =   DB::table("moderator_section as ms")
                                        ->whereNull("ms.deleted_at")
                                        ->where("ms.category_id", "=", $topic->category_id)
                                        ->distinct('ms.user_id')
                                        ->lists("ms.user_id");
                                        
                                      
                
                if(count($replies) > 0)
                { 

                    foreach ($replies as $reply)
                    {


                        if($reply->moderator_status==1 || in_array($userID, $userIds))
                        {
                            array_push($repliesRead, array("reply_id" => $reply->reply_id, "user_id" => $userID, "created_at" => new DateTime()));
                            
                      
                        }
                        DB::table("replies_read")->where("user_id", "=", $userID)->where("reply_id", $reply->reply_id)->delete();
                    }
              

                }


                if(count($repliesRead) > 0)
                    $operationStatus = DB::table("replies_read")->insert($repliesRead);

            }

        } catch (Exception $e)
        {
            Log::error("ReplyController=>webMarkRead".$e->getMessage()); 
        }
        
        if($operationStatus)
            return AppMessage::$FORUM_REPLY_READ_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR;
    }
    public function deleteWebReplyRead()
    {   
        $operationStatus = false;
        try
        {
            
            $lastWeek = (new DateTime())->modify('-7 day');
            $operationStatus = DB::table("replies_read")->where("created_at", "<=", $lastWeek)->delete();
            

        } catch (Exception $e)
        {
            Log::error("ReplyController=>deleteReplyRead".$e->getMessage()); 
        }

        if($operationStatus)
            return AppMessage::$FORUM_REPLY_READ_DELETE_SUCCESS;
        else
            return AppMessage::$GENERAL_ERROR;
    }
/*    public function getRepliesByTopicIDV2($topicID)
    {
        $replies    = [];
        $topic      = new Topic();

        $result = array("topic" => $topic, "replies" => $replies);
        try
        {
           if(intval($topicID) > 0 )
           {    
                $result["topic"]    = Topic::find($topicID);

                $result["replies"]  = DB::table("replies as r")
                                        ->leftJoin("topics as t", "t.topic_id", "=", "r.topic_id")
                                        ->leftJoin("categories as c", "c.category_id", "=", "t.category_id")
                                        ->where("r.topic_id", "=", $topicID)
                                        ->whereNull("r.deleted_at")
                                        ->orderBy("r.reply_id","desc")
                                        ->select("r.reply_id", "r.topic_id", "r.reply", "r.created_at", "r.updated_at", "r.deleted_at", "r.user_id", "r.user_name as reply_by", "t.user_name as title_by")
                                        //->select("r.*","u.name as reply_by", "c.name as category_name","us.name as title_by")
                                        ->get();

           }

        } catch (Exception $e)
        {
            Log::error("ReplyController=>getRepliesByTopicIDV2".$e->getMessage());
        }
        
        return Response::json($result);
    } */ 
        
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists