Sindbad~EG File Manager
<?php
use Illuminate\Database\Eloquent\SoftDeletingTrait;
class Reply extends Eloquent
{
use SoftDeletingTrait;
protected $table = 'replies';
protected $primaryKey = 'reply_id';
public Static function getRepliesByTopicID($topicID,$page,$no_of_records)
{
try
{
if(intval($topicID) > 0 )
{
if(isset($page) && isset($no_of_records))
$topics = 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","asc")
->skip(($page - 1)*$no_of_records)
->take($no_of_records)
->select("r.reply_id", "r.topic_id", "r.reply" ,DB::raw("cast(r.created_at as date) as created_at"), DB::raw("cast(r.updated_at as date) as updated_at"), "r.deleted_at", "r.user_id", "r.user_name as reply_by", "r.moderator_user_id", "r.moderator_status", DB::raw("cast(r.approved_on as date) as approved_on"), DB::raw("cast(r.rejected_on as date) as rejected_on"),"t.user_name as title_by")
//->select("r.*","u.name as reply_by", "c.name as category_name","us.name as title_by")
->get();
else
$topics = 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","asc")
->select("r.reply_id", "r.topic_id", "r.reply" ,DB::raw("cast(r.created_at as date) as created_at"), DB::raw("cast(r.updated_at as date) as updated_at"), "r.deleted_at", "r.user_id", "r.user_name as reply_by", "r.moderator_user_id", "r.moderator_status", DB::raw("cast(r.approved_on as date) as approved_on"), DB::raw("cast(r.rejected_on as date) as rejected_on"), "t.user_name as title_by")
//->select("r.*","u.name as reply_by", "c.name as category_name","us.name as title_by")
->get();
}
return $topics;
} catch (Exception $e)
{
Log::error('Models: Reply -> getRepliesByTopicID '.$e->getMessage());
return [];
}
}
public Static function getRepliesByTopicID1($topicID,$page,$no_of_records)
{
try
{
if(intval($topicID) > 0 )
{
if(isset($page) && isset($no_of_records))
$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","asc")
->skip(($page - 1)*$no_of_records)
->take($no_of_records)
->select("r.reply_id", "r.topic_id", "r.reply" ,DB::raw("cast(r.created_at as date) as created_at"), DB::raw("cast(r.updated_at as date) as updated_at"), "r.deleted_at", "r.user_id", "r.user_name as reply_by", "r.moderator_user_id", "r.moderator_status", DB::raw("cast(r.approved_on as date) as approved_on"), DB::raw("cast(r.rejected_on as date) as rejected_on"), "t.user_name as title_by")
->get();
else
$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","asc")
->select("r.reply_id", "r.topic_id", "r.reply" ,DB::raw("cast(r.created_at as date) as created_at"), DB::raw("cast(r.updated_at as date) as updated_at"), "r.deleted_at", "r.user_id", "r.user_name as reply_by", "r.moderator_user_id", "r.moderator_status", DB::raw("cast(r.approved_on as date) as approved_on"), DB::raw("cast(r.rejected_on as date) as rejected_on"), "t.user_name as title_by")
->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;
}
}
}
return $replies;
} catch (Exception $e)
{
Log::error('Models: Reply -> getRepliesByTopicID1 '.$e->getMessage());
return [];
}
}
public Static function getRepliesByTopicID2($topicID,$userID,$now,$lastWeek,$page,$no_of_records)
{
try
{
$repliesQuery = 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")
->leftJoin(DB::raw("(select reply_id,vote from forum_votes where topic_id is null and user_id = ".$userID.") as sq"), "sq.reply_id", "=", "r.reply_id")
->leftJoin(DB::raw("(
select tr.reply_id,
(
case when rr.replies_read_id is not null then 1
else 0
end
) as is_read
from
replies tr
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 = tr.reply_id
where
tr.created_at >= '".$lastWeek->format('Y-m-d H:i:s')."'
and
tr.created_at <= '".$now->format('Y-m-d H:i:s')."'
) as mrr"),"mrr.reply_id", "=", "r.reply_id")
->where("r.topic_id", "=", $topicID)
->whereNull("r.deleted_at")
->orderBy("r.reply_id","asc");
//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")
->leftJoin("topics as t", "t.category_id", "=", "ms.category_id")
->whereNull("ms.deleted_at")
->where("t.topic_id", "=", $topicID)
->where("ms.user_id", "=", $userID)
->count());
if(isset($page) && isset($no_of_records))
$repliesQuery->skip(($page - 1)*$no_of_records)
->take($no_of_records);
if(!($countModeratorSection > 0)) // User is not a moderator to given topic
{
$repliesQuery->where(function($query) use($userID){
return $query->where('r.moderator_status', '=', 1)
->orWhere('r.user_id', '=', $userID);
})
->select("r.reply_id", "r.topic_id", "r.reply" ,DB::raw("cast(r.created_at as date) as created_at"), DB::raw("cast(r.updated_at as date) as updated_at"), "r.deleted_at", "r.user_id", "r.user_name as reply_by","t.user_name as title_by","r.moderator_user_id","r.moderator_status",DB::raw("cast(r.approved_on as date) as approved_on"),DB::raw("cast(r.rejected_on as date) as rejected_on"), DB::raw("0 as can_moderate"),'r.upvote_count','r.downvote_count',
DB::raw("ifnull(sq.vote, 0) as vote"), DB::raw("ifnull(mrr.is_read, 1) as is_read"));
}
else
{
$repliesQuery->select("r.reply_id", "r.topic_id", "r.reply" ,DB::raw("cast(r.created_at as date) as created_at"), DB::raw("cast(r.updated_at as date) as updated_at"), "r.deleted_at", "r.user_id", "r.user_name as reply_by","t.user_name as title_by","r.moderator_user_id","r.moderator_status",DB::raw("cast(r.approved_on as date) as approved_on"),DB::raw("cast(r.rejected_on as date) as rejected_on"), DB::raw("1 as can_moderate"),'r.upvote_count','r.downvote_count',
DB::raw("ifnull(sq.vote, 0) as vote"), DB::raw("ifnull(mrr.is_read, 1) as is_read"));
}
return $repliesQuery;
} catch (Exception $e)
{
Log::error('Models: Topic -> getTopicsByCatIDUserID '.$e->getMessage());
return [];
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists