Skip to content

Instantly share code, notes, and snippets.

@tolawho
Created September 15, 2016 10:28
Show Gist options
  • Save tolawho/9384779d4a4824e43c6bf589dd1e8afc to your computer and use it in GitHub Desktop.
Save tolawho/9384779d4a4824e43c6bf589dd1e8afc to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Component\Common\PortalUser;
use App\Component\Common\PortalConst;
use App\Component\Common\PortalCommon;
use App\Repositories\ContentRepository;
use App\Repositories\GolfCourseRepository;
use Illuminate\Http\Request;
use App\Models\Content;
use App\Models\Common;
use App\Models\GolfCourse;
use Session;
class GolfCourseController extends Controller
{
use PortalCommon;
/**
* @var GolfCourse
*/
private $golfCourse;
/**
* @var GolfCourseRepository
*/
private $golfCourseRepo;
/**
* @var ContentRepository
*/
private $contentRepository;
/**
* CalendarController constructor.
*
* @param Content $content
* @param GolfCourse $golfCourse
* @param GolfCourseRepository $golfCourseRepo
*/
public function __construct(
Content $content,
GolfCourse $golfCourse,
GolfCourseRepository $golfCourseRepo,
ContentRepository $contentRepository
) {
parent::__construct($content);
$this->golfCourse = $golfCourse;
$this->golfCourseRepo = $golfCourseRepo;
$this->contentRepository = $contentRepository;
}
/**
* Show golf course comment.
*
* @param Request $request
* @param int $gcId
*
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function comment(Request $request, $gcId)
{
if ($request->isMethod('post')) {
return $this->addComment($request);
}
// get info golf course
$golfCourse = $this->golfCourse->getGolfCourseInfo($gcId);
// Get state name
$stateName = $this->contentRepository->getStateName(PortalUser::getUserId());
// Get country
$country = $this->contentRepository->getCountry(PortalUser::getUserId());
if (!$golfCourse) {
$msgErrors = [];
$msgErrors[] = trans('message.golf_course.not_found');
$urlBack = route('top');
$txtBtn = trans('content.error.back');
Session::set('msgErrors', $msgErrors);
Session::set('urlBack', $urlBack);
Session::set('txtBtn', $txtBtn);
return redirect()->route('error.index');
}
// check like/unlike comment
$this->golfCourseRepo->checkLikeComment($gcId);
// get seo data
$seoData = $this->content->getSeoDataSet(
PortalConst::GOLF_COURSE_COMMENT,
$this->selectedLanguage
);
// user logged in
$user = [
'id' => PortalUser::getUserId(),
'user_name' => PortalUser::getUserLastName(),
'state_name' => PortalUser::getStateName(),
'birth_day' => PortalUser::getBirthday(),
'nick_name' => PortalUser::getNickName(),
'avatar' => PortalUser::getAvatar(),
];
// get lang code
$langCode = $this->getLangCode();
// get list factor of gc
$listFactor = $this->golfCourse->getListFactor($langCode);
// get list comment of gc
$cmtIds = [];
$listComment = $this->golfCourse->getListCommentByGcId($gcId, $cmtIds);
// Get list comment like
$listCommentLike = $this->golfCourse->getListCommentLike($cmtIds);
// Get list comment point
$listCommentPoint = $this->golfCourse->getListCommentPointByCommentId($cmtIds, $langCode);
// get avg comment point of gc group by factor id
$avgCommentPoint = $this->golfCourse->getAvgCommentPointByGcId($gcId, $langCode);
// get report: top ranking, golf course ranking+ comment ranking
$commentAnalytics = $this->golfCourseRepo->getCommentAnalytics($gcId);
// get gc comment evaluation
$evalGolfCourse = $commentAnalytics['evaluation'];
// get gc top ranking
$topRanking = $commentAnalytics['topRanking'];
// build data for radar chart & some variable need in js
$jsDataComment = $this->golfCourseRepo->buildRadarChartData($listFactor, $avgCommentPoint);
$jsDataComment['shorten'] = [
'more' => trans('common.view_more'),
'less' => trans('common.view_less'),
];
$jsDataComment['gcId'] = $gcId;
$jsDataComment['likeUrl'] = route('golf-course.like-comment', [], false); // like/unlike url
$jsData = json_encode($jsDataComment);
// Get commentId when edit comment
$commentId = Session::get('commentId');
$commentData = [];
$commentPoint = [];
if (isset($commentId)) {
$tmpListComment = collect($listComment->items())->keyBy('comment_id');
$commentData = $tmpListComment[$commentId];
$commentPoint = collect($listCommentPoint[$commentId])->keyBy('factor_id')->toArray();
}
/* Start process bookmark */
$displayBookmark = true; // using flag to check display status of tab [favorite] in view
$objCommon = new Common();
if ($objCommon->checkBookmarkGolfCourse($gcId, $displayBookmark) === false) {
abort(PortalConst::INTERNAL_SERVER_ERROR);
}
return view(
'golfcourse.comment',
compact(
'gcId',
'seoData',
'user',
'listFactor',
'listComment',
'listCommentLike',
'listCommentPoint',
'avgCommentPoint',
'golfCourse',
'topRanking',
'evalGolfCourse',
'jsData',
'commentData',
'commentPoint',
'displayBookmark',
'stateName',
'country'
)
);
}
/**
* Add new golf course comment.
*
* @author Thanh D
*
* @param Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
private function addComment(Request $request)
{
// comment point
$point = $request->get('factor');
$gcId = $request->get('gc_id');
// validate before & if error return back
$validator = $this->golfCourseRepo->validateComment($request);
if ($validator->fails()) {
return back()->withInput()->withErrors($validator);
}
// comment data
$commentData = [
'gc_id' => $gcId,
'user_id' => PortalUser::getUserId(),
'comment' => htmlentities($request->get('content')),
// need add calendar and custom playdate at here
'play_date' => $request->get('play_date'),
'comment_status' => 1,
];
$commentId = $request->get('comment_id');
// Check update or insert comment
if (isset($commentId)) {
$this->golfCourse->editComment($commentId, $commentData, $point);
$params = ['gc_id' => $gcId];
if ($request->get('page')) {
$params['page'] = $request->get('page');
}
return redirect()->route('golf-course.comment', $params);
} else {
$this->golfCourse->newComment($commentData, $point);
return redirect()->route('golf-course.comment', ['gc_id' => $gcId]);
}
}
/**
* Delete a comment.
*
* @param Request $request
* @param int $commendId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function deleteComment(Request $request, $commendId)
{
$gcId = $request->get('gc_id');
$cPage = $request->get('c_page');
$this->golfCourse->selfDeleteComment($commendId);
$listComment = $this->golfCourse->getListCommentByGcId($gcId);
if ($listComment->lastPage() >= $cPage) {
return redirect()->back();
} else {
return redirect()->route('golf-course.comment', ['gc_id' => $gcId, 'page' => $listComment->lastPage()]);
}
}
/**
* Like or unlike a comment.
*
* @param Request $request
*
* @return string
*/
public function likeComment(Request $request)
{
$commentId = $request->input('id');
$golfCourseId = $request->input('gcId');
return json_encode($this->golfCourseRepo->doLikeComment($commentId, $golfCourseId));
}
/**
* Edit comment.
*
* @param int $commentId
*
* @return \Illuminate\Http\RedirectResponse
*/
public function editComment($commentId)
{
return redirect()->back()->with('commentId', $commentId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment