Sindbad~EG File Manager
<?php
class TopicController extends Controller {
// public function store()
// {
// try
// {
// //Jquery
// /*
// Client (JQUERY AJAX CALL)
// -----------------------
// JSON object to be passed to server in ajax call
// ------------------------------------------------
// input:
// -------
// JSON.stringify(po);
// output:
// -------
// po : {"id":0,"po_no":0,"date":"12/02/2016","delivery_date":"12/02/2016","contact_person_name":"121","contact_no":"212","associate_id":"41","associate_name":"KYOTO APPARELS","add1":"INDERPURI","add2":null,"add3":null,"city":"NEWDELHI","state":"NEW DELHI","pincode":null,"tin":null,"bouser_id":null,"bu_id":null,"sku_type_id":1,"sku_type_name":"End Product"}
// { po : {"id":0,"po_no":0,"date":"12/02/2016","delivery_date":"12/02/2016","contact_person_name":"121","contact_no":"212","associate_id":"41","associate_name":"KYOTO APPARELS","add1":"INDERPURI","add2":null,"add3":null,"city":"NEWDELHI","state":"NEW DELHI","pincode":null,"tin":null,"bouser_id":null,"bu_id":null,"sku_type_id":1,"sku_type_name":"End Product"} }
// Server (PHP)
// -------------------------
// Converting json object to be normal object to access
// -----------------------------------------------------
// $po = json_decode(Input::all());
// $pono = $po->po_no;
// */
// $operationStatus = false;
// $title = Input::has("title") ? Helper::trimAndTruncate(Input::get("title"),1000) : null;
// $user_id = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
// $category_id = Input::has("category_id") ? Helper::trimAndTruncate(Input::get("category_id")) : null;
// $user_name = Input::has("user_name") ? Helper::trimAndTruncate(Input::get("user_name"),50) : null;
// $description = Input::has("description") ? Helper::trimAndTruncate(Input::get("description"),21844) : null;
// $topic = new Topic();
// $topic->title = $title;
// $topic->user_id = $user_id;
// $topic->category_id = $category_id;
// $topic->user_name = $user_name;
// DB::beginTransaction();
// $operationStatus = $topic->save();
// if($operationStatus)
// {
// $r = new Reply();
// $r->user_id = $user_id;
// $r->topic_id = $topic->topic_id;
// $r->reply = $description;
// $r->user_name = Helper::trimAndTruncate($user_name,50);
// $operationStatus = $r->save();
// }
// if($operationStatus)
// DB::commit();
// } catch (Exception $e)
// { $operationStatus = false;
// DB::rollback();
// Log::error("TopicController=>store".$e->getMessage());
// }
// if($operationStatus)
// return AppMessage::$TOPIC_INSERT_SUCCESS;
// else
// return AppMessage::$GENERAL_ERROR;
// }
public function store()
{
$replyID = 0;
try
{
$operationStatus = false;
$title = Input::has("title") ? Helper::trimAndTruncate(Input::get("title"),1000) : null;
$user_id = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
$category_id = Input::has("category_id") ? Helper::trimAndTruncate(Input::get("category_id")) : null;
$user_name = Input::has("user_name") ? Helper::trimAndTruncate(Input::get("user_name"),50) : null;
$description = Input::has("description") ? Helper::trimAndTruncate(Input::get("description"),21844) : null;
$topic = new Topic();
$topic->title = $title;
$topic->user_id = $user_id;
$topic->category_id = $category_id;
$topic->user_name = $user_name;
DB::beginTransaction();
$operationStatus = $topic->save();
if($operationStatus)
{
$r = new Reply();
$r->user_id = $user_id;
$r->topic_id = $topic->topic_id;
$r->reply = $description;
$r->user_name = Helper::trimAndTruncate($user_name,50);
$operationStatus = $r->save();
$replyID = $r->reply_id;
}
if($operationStatus)
DB::commit();
} catch (Exception $e)
{ $operationStatus = false;
DB::rollback();
Log::error("TopicController=>store".$e->getMessage());
}
//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))
{
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")->whereNull("c.deleted_at")->where("c.category_id", "=", $topic->category_id)->pluck("c.name");
$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)->whereNull("t.moderator_status")->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("TopicController=>store notification".$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("TopicController=>store file upload".$ex->getMessage());
}
}
if($operationStatus)
return AppMessage::$TOPIC_INSERT_SUCCESS;
else
return AppMessage::$GENERAL_ERROR;
}
//http://stackoverflow.com/questions/18204342/laravel-4-how-to-limit-take-and-skip-for-eloquent-orm
public function getTopicsByCatID($catID)
{
$page = Input::has("page") ? intval(Input::get("page")) : null ;
$no_of_records = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;
$topics = [];
try
{
$topics = Topic::getTopicsByCatID($catID,$page,$no_of_records);
} catch (Exception $e)
{
Log::error("TopicController=>getTopicsByCatID".$e->getMessage());
}
return Response::json($topics);
}
//https://laracasts.com/discuss/channels/code-review/sql-union-to-eloquent
public function getTopicsByCatIDUserID($catID, $userID)
{
$page = Input::has("page") ? intval(Input::get("page")) : null ;
$no_of_records = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;
$topics = [];
$now = new DateTime();
$lastWeek = (new DateTime())->modify('-7 day');
try
{
if(intval($catID) > 0 && intval($userID) > 0)
{
$topicsQuery = Topic::getTopicsByCatIDUserID($catID,$userID,$now,$lastWeek,$page,$no_of_records);
$topics = $topicsQuery->get();
}
} catch (Exception $e)
{
Log::error("TopicController=>getTopicsByCatIDUserID".$e->getMessage());
}
return Response::json($topics);
}
public function updateTopicStatus()
{
$operationStatus = false;
$topic = new Topic();
//DB::beginTransaction();
try
{
$topicID = Input::has("topic_id") ? Input::get("topic_id") : null;
$userID = Input::has("user_id") ? Input::get("user_id") : null;
$moderationStatus = Input::has("moderator_status") ? Input::get("moderator_status") : null;
//Topic
if($topicID > 0 && $userID > 0 && !is_null($moderationStatus))
//if($topicID > 0 && $userID > 0)
{
$topic = Topic::find($topicID);
if(isset($topic) && intval($topic->topic_id) > 0 && is_null($topic->moderator_status))
//if(isset($topic) && intval($topic->topic_id) > 0)
{
$topic->moderator_user_id = $userID;
$topic->moderator_status = $moderationStatus;
if($moderationStatus == 1)
{
$topic->approved_on = new DateTime();
$topic->updated_at = new DateTime();
}
else if($moderationStatus == 0)
$topic->rejected_on = new DateTime();
//$operationStatus = $topic->save();
////Update updated at column of topic table only when moderation status of reply is true
$operationStatus = DB::table('topics')->where('topic_id', "=", $topic->topic_id)->update($topic->toArray());
}
}
//Reply -- Becuase, We saved topic in reply table as reply
/* if($operationStatus)
{
$replyID = DB::table("replies as r")
->whereNull("r.deleted_at")
->where("r.topic_id", "=", $topicID)
->min("r.reply_id");
$reply = Reply::find($replyID);
if(isset($reply) && intval($reply->reply_id) > 0)
{
$reply->moderator_user_id = $userID;
$reply->moderator_status = $userStatus;
if($userStatus == 1)
$reply->approved_on = new DateTime();
else if($userStatus == 0)
$reply->rejected_on = new DateTime();
$operationStatus = $reply->save();
}
}*/
//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))
{
$otherModerators = DB::table("moderator_section as ms")
->whereNull("ms.deleted_at")
->where("ms.category_id", "=", $topic->category_id)
->where("ms.user_id", "<>", $topic->moderator_user_id)
->distinct('ms.user_id')
->lists("ms.user_id");
$topicPostedUserID = array($topic->user_id);
$userIds = array_merge($otherModerators, $topicPostedUserID);
$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);
}
}
$topicName = Helper::trimAndTruncate($topic->title,20).strval("..");
$moderatorName = DB::table("users as u")->whereNull("u.deleted_at")->where("u.user_id", "=", $topic->moderator_user_id)->pluck("u.first_name");
if($moderationStatus == 1 )
$msg = strval($topic->topic_id)." - ".$topicName.' approved by '.$moderatorName ;
else
$msg = strval($topic->topic_id)." - ".$topicName.' disapproved by '.$moderatorName ;
//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", "<>", $topic->moderator_user_id)->where("tf.user_id", "<>", $topic->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);
}
}
//$topicPostedUserName = DB::table("users u")->whereNull("u.deleted_at")->where("u.user_id", "=", $topic->user_id)->pluck("u.name");
$topicPostedUserName = $topic->user_name;
$msg = $topicPostedUserName.' posted '.$topicName;
//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("TopicController=>updateTopicStatus".$e->getMessage());
}
return Response::json($topic);
}
public function voteStore()
{
$operationStatus = false;
try
{
$userID = Input::has("user_id") ? Input::get("user_id") : null;
$topicID = Input::has("topic_id") ? Input::get("topic_id") : null;
$vote = Input::has("vote") ? Input::get("vote") : null;
if(isset($topicID) && isset($userID) && isset($vote))
{
$forumVotesCount = intval(DB::table("forum_votes as fv")
->whereNull("fv.deleted_at")
->where("fv.user_id", "=", $userID)
->where("fv.topic_id", "=", $topicID)
->count());
if($forumVotesCount > 0)//Update
{
$operationStatus = DB::table("forum_votes as fv")
->whereNull("fv.deleted_at")
->where("fv.user_id", "=", $userID)
->where("fv.topic_id", "=", $topicID)
->limit(1)
->update(array("fv.vote" => $vote,"fv.updated_at" => new DateTime()));
}
else//Insert
{
$operationStatus = DB::table("forum_votes")
->insertGetId(array("user_id" => $userID, "topic_id" => $topicID, "vote" => $vote,"created_at" => new DateTime()));
}
}
} catch (Exception $e)
{
Log::error("TopicController=>voteStore".$e->getMessage());
}
if($operationStatus)
return AppMessage::$FORUM_REPLY_VOTE_SUCCESS;
else
return AppMessage::$GENERAL_ERROR;
}
public function followStore()
{
try
{
$operationStatus = false;
$userID = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
$topicID = Input::has("topic_id") ? Helper::trimAndTruncate(Input::get("topic_id")) : null;
if(isset($userID) && intval($userID) > 0 && isset($topicID) && intval($topicID) > 0)
{
$count = intval(TopicFollow::where("user_id", "=", $userID)
->where("topic_id", "=", $topicID)
->count()
);
if($count > 0)
{
$operationStatus = TopicFollow::where("user_id", "=", $userID)
->where("topic_id", "=", $topicID)
->delete();
}
else
{
$topicFollow = new TopicFollow();
$topicFollow->user_id = $userID;
$topicFollow->topic_id = $topicID;
$operationStatus = $topicFollow->save();
}
}
} catch (Exception $e)
{
Log::error("TopicController=>followStore".$e->getMessage());
}
if($operationStatus)
return AppMessage::$TOPIC_FOLLOW_INSERT_SUCCESS;
else
return AppMessage::$GENERAL_ERROR;
}
public function getTopicFollowsByUserID($userID)
{
$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 = TopicFollow::getTopicFollowsByUserID($userID,$page,$no_of_records);
} catch (Exception $e)
{
Log::error("TopicController=>getTopicFollowsByUserID".$e->getMessage());
}
return Response::json($topics);
}
public function webStore()
{
$replyID = 0;
try
{
$operationStatus = false;
$title = Input::has("title") ? Helper::trimAndTruncate(Input::get("title"),1000) :
null;
$user_id = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
$category_id = Input::has("category_id") ? Helper::trimAndTruncate(Input::get("category_id")) : null;
$user_name = Input::has("user_name") ? Helper::trimAndTruncate(Input::get("user_name"),50) : null;
$description = Input::has("description") ? Helper::trimAndTruncate(Input::get("description"),21844) : null;
$topic = new Topic();
$topic->title = $title;
$topic->user_id = $user_id;
$topic->category_id = $category_id;
$topic->user_name = $user_name;
DB::beginTransaction();
$operationStatus = $topic->save();
$topic_id = $topic->topic_id;
if($operationStatus)
{
$r = new Reply();
$r->user_id = $user_id;
$r->topic_id = $topic->topic_id;
$r->reply = $description;
$r->user_name = Helper::trimAndTruncate($user_name,50);
$operationStatus = $r->save();
$replyID = $r->reply_id;
}
if($operationStatus)
DB::commit();
} catch (Exception $e)
{ $operationStatus = false;
DB::rollback();
Log::error("TopicController=>store".$e->getMessage());
}
//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))
{
try
{
$usrIds = intval(DB::table("moderator_section as ms")
->whereNull("ms.deleted_at")
->where("ms.category_id", "=", $category_id)
->where("ms.user_id", "=", $user_id)
->count());
if(($usrIds) > 0)
{
$topic->moderator_status =1;
$topic->approved_on =new DateTime();
$topic ->approval_count=1;
$topic->save();
$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")->whereNull("c.deleted_at")->where("c.category_id", "=", $topic->category_id)->pluck("c.name");
$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)->whereNull("t.moderator_status")->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("TopicController=>store notification".$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("TopicController=>store file upload".$ex->getMessage());
}
}
if($operationStatus)
return Redirect::to('forum/topics/'.Helper::encrypt($category_id).'/'.Helper::encrypt($user_id))->with("msgSuccess", AppMessage::$TOPIC_INSERT_MODERATORS_SUCCESS);
else
return AppMessage::$GENERAL_ERROR;
}
//http://stackoverflow.com/questions/18204342/laravel-4-how-to-limit-take-and-skip-for-eloquent-orm
public function getWebTopicsByCatID($catID)
{
$page = Input::has("page") ? intval(Input::get("page")) : null ;
$no_of_records = Input::has("no_of_records") ? intval(Input::get("no_of_records")) : null ;
$topics = [];
try
{
$topics = Topic::getTopicsByCatID($catID,$page,$no_of_records);
} catch (Exception $e)
{
Log::error("TopicController=>getTopicsByCatID".$e->getMessage());
}
return Response::json($topics);
}
//https://laracasts.com/discuss/channels/code-review/sql-union-to-eloquent
public function getWebTopicsByCatIDUserID($catID, $userID)
{
$topics = [];
$topic_not_follow = [];
$topic_follow = [];
$catID = Helper::decrypt($catID);
$userID = Helper::decrypt($userID);
$category = Category::find($catID);
$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 ;
$topics = [];
$now = new DateTime();
$lastWeek = (new DateTime())->modify('-7 day');
try
{
if(intval($catID) > 0 && intval($userID) > 0)
{
$topicsQuery = Topic::getTopicsByCatIDUserID($catID,$userID,$now,$lastWeek,$page,$no_of_records);
$topics = $topicsQuery->paginate(10);
foreach($topics as $key=>$value)
{
if(intval($value->is_following)==0)
{
array_push($topic_not_follow,$value);
}
if(intval($value->is_following)==1)
{
array_push($topic_follow,$value);
}
}
}
} catch (Exception $e)
{
Log::error("TopicController=>getWebTopicsByCatIDUserID".$e->getMessage());
}
return View::make('forum.topics')->with('topics',$topics)
->with('category',$category)
->with('userID',$userID)
->with('moderator_id',$moderator_id)
->with('userName',$userName)
->with('topic_follow',$topic_follow)
->with('topic_not_follow', $topic_not_follow);
}
public function updateWebTopicStatus()
{
$operationStatus = false;
$topic = new Topic();
//DB::beginTransaction();
try
{
$topicID = Input::has("topic_id") ? Input::get("topic_id") : null;
$userID = Input::has("user_id") ? Input::get("user_id") : null;
$moderationStatus = Input::has("moderator_status") ? Input::get("moderator_status") : null;
//Topic
if($topicID > 0 && $userID > 0 && !is_null($moderationStatus))
//if($topicID > 0 && $userID > 0)
{
$topic = Topic::find($topicID);
if(isset($topic) && intval($topic->topic_id) > 0 && is_null($topic->moderator_status))
//if(isset($topic) && intval($topic->topic_id) > 0)
{
$topic->moderator_user_id = $userID;
$topic->moderator_status = $moderationStatus;
if($moderationStatus == 1)
{
$topic->approved_on = new DateTime();
$topic->updated_at = new DateTime();
}
else if($moderationStatus == 0)
$topic->rejected_on = new DateTime();
//$operationStatus = $topic->save();
////Update updated at column of topic table only when moderation status of reply is true
$operationStatus = DB::table('topics')->where('topic_id', "=", $topic->topic_id)->update($topic->toArray());
}
}
//Reply -- Becuase, We saved topic in reply table as reply
/* if($operationStatus)
{
$replyID = DB::table("replies as r")
->whereNull("r.deleted_at")
->where("r.topic_id", "=", $topicID)
->min("r.reply_id");
$reply = Reply::find($replyID);
if(isset($reply) && intval($reply->reply_id) > 0)
{
$reply->moderator_user_id = $userID;
$reply->moderator_status = $userStatus;
if($userStatus == 1)
$reply->approved_on = new DateTime();
else if($userStatus == 0)
$reply->rejected_on = new DateTime();
$operationStatus = $reply->save();
}
}*/
//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))
{
$otherModerators = DB::table("moderator_section as ms")
->whereNull("ms.deleted_at")
->where("ms.category_id", "=", $topic->category_id)
->where("ms.user_id", "<>", $topic->moderator_user_id)
->distinct('ms.user_id')
->lists("ms.user_id");
$topicPostedUserID = array($topic->user_id);
$userIds = array_merge($otherModerators, $topicPostedUserID);
$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);
}
}
$topicName = Helper::trimAndTruncate($topic->title,20).strval("..");
$moderatorName = DB::table("users as u")->whereNull("u.deleted_at")->where("u.user_id", "=", $topic->moderator_user_id)->pluck("u.first_name");
if($moderationStatus == 1 )
$msg = strval($topic->topic_id)." - ".$topicName.' approved by '.$moderatorName ;
else
$msg = strval($topic->topic_id)." - ".$topicName.' disapproved by '.$moderatorName ;
//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", "<>", $topic->moderator_user_id)->where("tf.user_id", "<>", $topic->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);
}
}
//$topicPostedUserName = DB::table("users u")->whereNull("u.deleted_at")->where("u.user_id", "=", $topic->user_id)->pluck("u.name");
$topicPostedUserName = $topic->user_name;
$msg = $topicPostedUserName.' posted '.$topicName;
//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("TopicController=>updateWebTopicStatus".$e->getMessage());
}
return Response::json($topic);
}
public function webVoteStore(){
$operationStatus = false;
try
{
$userID = Input::has("user_id") ? Input::get("user_id") : null;
$topicID = Input::has("topic_id") ? Input::get("topic_id") : null;
$vote = Input::has("vote") ? Input::get("vote") : null;
if(isset($topicID) && isset($userID) && isset($vote))
{
$forumVotesCount = intval(DB::table("forum_votes as fv")
->whereNull("fv.deleted_at")
->where("fv.user_id", "=", $userID)
->where("fv.topic_id", "=", $topicID)
->count());
if($forumVotesCount > 0)//Update
{
$operationStatus = DB::table("forum_votes as fv")
->whereNull("fv.deleted_at")
->where("fv.user_id", "=", $userID)
->where("fv.topic_id", "=", $topicID)
->limit(1)
->update(array("fv.vote" => $vote,"fv.updated_at" => new DateTime()));
}
else//Insert
{
$operationStatus = DB::table("forum_votes")
->insertGetId(array("user_id" => $userID, "topic_id" => $topicID, "vote" => $vote,"created_at" => new DateTime()));
}
}
$topic = Topic::find($topicID);
return Response::json($topic);
} catch (Exception $e)
{
Log::error("TopicController=>voteStore".$e->getMessage());
}
if($operationStatus)
return AppMessage::$FORUM_REPLY_VOTE_SUCCESS;
else
return AppMessage::$GENERAL_ERROR;
}
public function webFollowStore()
{
try
{
$operationStatus = false;
$userID = Input::has("user_id") ? Helper::trimAndTruncate(Input::get("user_id")) : null;
$topicID = Input::has("topic_id") ? Helper::trimAndTruncate(Input::get("topic_id")) : null;
if(isset($userID) && intval($userID) > 0 && isset($topicID) && intval($topicID) > 0)
{
$count = intval(TopicFollow::where("user_id", "=", $userID)
->where("topic_id", "=", $topicID)
->count()
);
if($count > 0)
{
$operationStatus = TopicFollow::where("user_id", "=", $userID)
->where("topic_id", "=", $topicID)
->delete();
$operationStatus = false;
}
else
{
$topicFollow = new TopicFollow();
$topicFollow->user_id = $userID;
$topicFollow->topic_id = $topicID;
$operationStatus = $topicFollow->save();
$operationStatus =true;
}
}
//return Response::json_decode($topicID);
} catch (Exception $e)
{
Log::error("TopicController=>followStore".$e->getMessage());
}
if($operationStatus==true)
return Redirect::to('forum/replies/'.Helper::encrypt($topicID).'/'.Helper::encrypt($userID))->with("msgSuccess", AppMessage::$TOPIC_FOLLOW_SUCCESS);
else
return Redirect::to('forum/replies/'.Helper::encrypt($topicID).'/'.Helper::encrypt($userID))->with("msgError", AppMessage::$TOPIC_FOLLOW_ERROR);
}
public function getWebTopicFollowsByUserID($userID)
{
$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 = TopicFollow::getTopicFollowsByUserID($userID,$page,$no_of_records);
} catch (Exception $e)
{
Log::error("TopicController=>getTopicFollowsByUserID".$e->getMessage());
}
return Response::json($topics);
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists