Sindbad~EG File Manager

Current Path : /home/numerotech/public_html/livemt2023.aios-scientificcommittee.org/app/models/
Upload File :
Current File : //home/numerotech/public_html/livemt2023.aios-scientificcommittee.org/app/models/Topic.php

<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Topic extends Eloquent 
{
	use SoftDeletingTrait;
	protected $table = 'topics';
	protected $primaryKey = 'topic_id';

	public Static function getTopicsByCatID($catID,$page,$no_of_records)
    {
        try
        {
          if(intval($catID) > 0 )
           {
                if(isset($page) && isset($no_of_records))
                {
                    $topics = DB::table("topics as t")
                                ->leftJoin("categories as c", "c.category_id", "=", "t.category_id")
                                ->whereNull("t.deleted_at")
                                ->where("c.category_id", "=", $catID)
                                ->orderBy("t.topic_id")
                                ->orderBy("t.updated_at", "desc")
                                ->skip(($page - 1)*$no_of_records)
                                ->take($no_of_records)
                                ->select("t.*", "c.name as category_name", "c.color_code", "c.forum_is_visible as category_is_visible")
                                ->get();


                                //->paginate($no_of_records,array("t.*", "c.name as category_name", "c.color_code", "c.forum_is_visible as category_is_visible"));
                                /*->get();*/                 
                    
                }
                else
                $topics = DB::table("topics as t")
                            ->leftJoin("categories as c", "c.category_id", "=", "t.category_id")
                            ->whereNull("t.deleted_at")
                            ->where("c.category_id", "=", $catID)
                            ->orderBy("t.updated_at", "desc")
                            ->select("t.*", "c.name as category_name", "c.color_code", "c.forum_is_visible as category_is_visible")
                            ->get();
           }
          
           
           					return $topics;

        } catch (Exception $e)
        {
            Log::error('Models: Topic  -> getTopicsByCatID '.$e->getMessage());
            return [];
        }
    }
      public Static function getTopicsByCatIDUserID($catID,$userID,$now,$lastWeek,$page,$no_of_records)
    {
        try
        {
           	$topicsQuery    =   DB::table("topics as t")
                                        ->leftJoin("categories as c", "c.category_id", "=", "t.category_id")
                                        ->leftJoin('topic_follow as tf', function($join)use ($userID){
                                                    $join->on('tf.topic_id', '=', "t.topic_id");
                                                    $join->where('tf.user_id', '=', $userID);
                                                    $join->whereNull('tf.deleted_at');
                                                  }) 
                                        ->leftJoin(DB::raw("(select topic_id,vote from forum_votes where reply_id is null and user_id = ".$userID.") as sq"), "sq.topic_id", "=", "t.topic_id")                           
                                        //->leftJoin(DB::raw("(select t.topic_id, ifnull(count(r.topic_id),0) as topic_reply_count from topics t left join replies r on r.topic_id = t.topic_id where r.created_at >= '".$now->format('Y-m-d H:i:s')."' and r.created_at <= '".$lastWeek->format('Y-m-d H:i:s')."' group by r.topic_id) as trc"), "trc.topic_id", "=", "t.topic_id")
                                        //->leftJoin(DB::raw("(select t.topic_id, ifnull(count(r.topic_id),0) as topic_read_reply_count from topics t left join replies r on r.topic_id = t.topic_id left join replies_read rr on rr.reply_id = r.reply_id where rr.user_id = ".$userID." group by r.topic_id) as trrc"), "trrc.topic_id", "=", "t.topic_id")
                                        ->leftJoin(DB::raw("(select  
                                              t.topic_id, count(t.topic_id) as unread_reply_count
                                              from replies r
                                              left join (select trr.replies_read_id,trr.reply_id from replies_read trr where trr.user_id = ".$userID.") as rr on rr.reply_id = r.reply_id
                                              left join topics t on t.topic_id = r.topic_id
                                             left join moderator_section ms on ms.category_id =  t.category_id   and ms.user_id = ".$userID."
                                              where
                                                    rr.replies_read_id  is  null   
                                                and (r.moderator_status =1 or ms.moderator_section_id is not null) 
                                              
                                              and
                                              r.created_at >= '".$lastWeek->format('Y-m-d H:i:s')."'
                                              and 
                                              r.created_at <= '".$now->format('Y-m-d H:i:s')."'
                                              group by t.topic_id) as mrr"),"mrr.topic_id", "=", "t.topic_id")

                                        ->whereNull("t.deleted_at")
                                        ->where("c.category_id", "=", $catID)
                                        ->orderBy("tf.topic_follow_id", "desc")
                                        ->orderBy("t.updated_at", "desc")
                                        ->orderBy("t.topic_id", "asc");
                 if(isset($page) && isset($no_of_records))
                    $topicsQuery->skip(($page - 1) * $no_of_records)
                                ->take($no_of_records);


                //Check whether user is a moderator to the given category nor not. if so, take all the topics to that category
                $countModeratorSection  =   intval(DB::table("moderator_section as ms")
                                                ->whereNull("ms.deleted_at")
                                                ->where("ms.category_id", "=", $catID)
                                                ->where("ms.user_id", "=", $userID)
                                                ->count());
                  
                
                if( !($countModeratorSection > 0) )    // User is not moderator to given category
                {
                    
                    $topicsQuery->where(function($query) use($userID){
                                    
                                    return $query->where('t.moderator_status', '=', 1)
                                                 ->orWhere('t.user_id', '=', $userID);
                                })
                                ->select("t.topic_id", "t.title", "t.user_id", "t.category_id", 
                                         "t.created_at", "t.updated_at","t.deleted_at", 
                                         "t.user_name", "t.moderator_user_id","t.moderator_status", 
                                         "t.approved_on", "t.rejected_on",DB::raw("0 as approval_count"),"t.upvote_count","t.downvote_count",
                                         "c.name as category_name","c.color_code","c.forum_is_visible as category_is_visible",DB::raw('(CASE WHEN tf.topic_id > 0  THEN 1 ELSE 0 END) AS is_following') ,"tf.topic_id as following_topic_id", DB::raw("0 as can_moderate"),
                                         // DB::raw("ifnull(sq.vote, 0) as vote"), DB::raw("(ifnull(trc.topic_reply_count,0) - ifnull(trrc.topic_read_reply_count,0) ) as unread_reply_count"));
                                         DB::raw("ifnull(sq.vote, 0) as vote"), DB::raw("ifnull(mrr.unread_reply_count,0) as unread_reply_count"));
                }
                else //User is moderator
                {
                    $topicsQuery->select("t.*", "c.name as category_name","c.color_code","c.forum_is_visible as category_is_visible",DB::raw('(CASE WHEN tf.topic_id > 0  THEN 1 ELSE 0 END) AS is_following') ,"tf.topic_id as following_topic_id", DB::raw("1 as can_moderate"),
                                 //DB::raw("ifnull(sq.vote, 0) as vote"), DB::raw("(ifnull(trc.topic_reply_count,0) - ifnull(trrc.topic_read_reply_count,0) ) as unread_reply_count"));
                                DB::raw("ifnull(sq.vote, 0) as vote"), DB::raw("ifnull(mrr.unread_reply_count,0) as unread_reply_count"));

                }
                
               
                 
           						    return $topicsQuery;

        } catch (Exception $e)
        {
            Log::error('Models: Topic  -> getTopicsByCatIDUserID '.$e->getMessage());
            return [];
        }
    }


}


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