<?php
namespace App\Controller;
use App\Constant\UtilsConstant;
use App\Entity\Action;
use App\Form\UserType;
use App\Services\StatisticManager;
use DateTime;
use App\Services\SeoManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Constant\AjaxLoadingLimitConstant;
use App\Constant\PeopleActionConstant;
use App\Constant\ActionStatusConstant;
use App\Entity\FileAction;
use App\Entity\PremiumSubscription;
use App\Entity\User;
use App\Entity\PeopleAction;
use Symfony\Contracts\Translation\TranslatorInterface;
const PROFILE_USER = 'profileUser';
const USER_NAME = 'user_name';
class ProfileController extends WhiplayController
{
/**
* Show profile of specified user (or current user if there is no id in the url)
*
* @param Request $request
* @param SeoManager $seoManager
* @param StatisticManager $statisticManager
* @param TranslatorInterface $translator
* @param $id
* @return Response
*/
public function show(
Request $request,
SeoManager $seoManager,
StatisticManager $statisticManager,
TranslatorInterface $translator,
$id
): Response
{
/** @var User $user */
$user = $this->getUser();
if (!$id) {
if ($user instanceof User) {
$profileUser = $user;
} else {
return $this->ajaxForward($request, UtilsConstant::LOGIN_ROUTE);
}
} else {
/** @var User $profileUser*/
$profileUser = $this->getDoctrine()
->getRepository(User::class)
->find($id);
if (!($profileUser instanceof User)) {
return $this->render('@Twig/views/Exception/error404.html.twig', [
'customMessage' => $translator->trans('profile.show.not_found', ['id' => $id])
]);
}
}
$seoManager
->setTitleAndDescription(
'profile.show.seo.title',
'profile.show.seo.description',
['user_name' => $profileUser->getUsername()]
)
->useLogo();
$currentPremiumSubscription = false;
if ($user instanceof User) {
$currentPremiumSubscription = $this
->getDoctrine()
->getRepository(PremiumSubscription::class)
->findOneBy(array('default' => true));
$currentOrder = $user->getCurrentOrder();
if ($currentOrder) {
$currentPremiumSubscription = $currentOrder->getPremiumSubscription();
}
}
//STATISTIC VIEWS
$statisticManager->addCommonVisit($profileUser->getCommonInformation());
return $this->render('Profile/show.html.twig', array(
PROFILE_USER => $profileUser,
'currentPremiumSubscription' => $currentPremiumSubscription,
));
}
/**
* Show edition page for current user
*
* @param Request $request
* @param SeoManager $seoManager
* @param TranslatorInterface $translator
* @return Response
*/
public function edit(
Request $request,
SeoManager $seoManager,
TranslatorInterface $translator
): Response {
/** @var User $user */
$user = $this->getUser();
if (!$user instanceof User) {
return $this->ajaxForward($request, UtilsConstant::LOGIN_ROUTE);
}
$form = $this->createForm(UserType::class, $user);
$entityManager = $this
->getDoctrine()
->getManager();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if ($request->get('pictureProfileDelete') && $user->getCommonInformation()->getProfilePicture()) {
$entityManager->remove($user->getCommonInformation()->getProfilePicture());
$entityManager->flush();
$user->getCommonInformation()->setProfilePicture();
}
if ($request->get('pictureCoverDelete') && $user->getCommonInformation()->getCoverPicture()) {
$entityManager->remove($user->getCommonInformation()->getCoverPicture());
$entityManager->flush();
$user->getCommonInformation()->setCoverPicture();
}
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash(
UtilsConstant::NOTICE,
$translator->trans('profile.edit.saved')
);
}
$seoManager
->setTitleAndDescription('profile.edit.seo.title', 'profile.edit.seo.description')
->useLogo();
return $this->render('Profile/edit.html.twig', array(
'user' => $user,
'form' => $form->createView(),
PROFILE_USER => $this->getUser(),
));
}
/**
* Method for the current user to follow the specified user (based on user_source GET variable)
* There is no view, it redirects to referer
*
* @param Request $request
* @param TranslatorInterface $translator
* @return Response
*/
public function follow(Request $request, TranslatorInterface $translator): Response
{
/** @var User $user */
$user = $this->getUser();
if (!($user instanceof User)) {
return $this->ajaxForward($request, UtilsConstant::LOGIN_ROUTE);
}
$userId = $request->request->get('user_source');
/* @var User $user */
$followingUser = $this->getDoctrine()
->getRepository(User::class)
->find($userId);
if (!$followingUser) {
return $this->ajaxForward($request, 'whiplay_home');
}
$parameters = array(
'userPeople' => $followingUser->getId(),
'action' => PeopleActionConstant::FOLLOW,
'user' => $user->getId()
);
$userFollowAction = $this
->getDoctrine()
->getRepository(PeopleAction::class)
->findOneBy($parameters);
$em = $this->getDoctrine()->getManager();
if ($userFollowAction) {
$em->remove($userFollowAction);
$this->addFlash(
'notice',
$translator->trans('profile.follow.deleted')
);
} else {
$userFollowAction = new PeopleAction();
$userFollowAction->setCreatedAt(new DateTime());
$userFollowAction->setUser($user);
$userFollowAction->setUserPeople($followingUser);
$userFollowAction->setAction(PeopleActionConstant::FOLLOW);
$userFollowAction->setStatus(ActionStatusConstant::STATUS_PUBLIC);
$em->persist($userFollowAction);
$this->addFlash(
'notice',
$translator->trans('profile.follow.added')
);
}
$em->flush();
return $this->ajaxForwardToReferer(
$request,
'user_profile_show',
array('id' => $followingUser->getId())
);
}
/**
* Show likes of specified user based on $type
*
* @param SeoManager $seoManager
* @param User $profileUser
* @param $type
* @return Response
*/
public function likes(SeoManager $seoManager, User $profileUser, $type): Response
{
$ajaxLoadingLimitNumber = AjaxLoadingLimitConstant::getConstantFromType($type);
$likes = $this
->getDoctrine()
->getRepository(Action::class)
->getUserPublicLikes($profileUser, $type, 0, $ajaxLoadingLimitNumber);
$totalLikesCount = count($this
->getDoctrine()
->getRepository(Action::class)
->getUserPublicLikes($profileUser, $type));
$seoManager
->setTitleAndDescription(
'profile.likes.seo.title',
'profile.likes.seo.description',
[USER_NAME => $profileUser->getUsername(), 'type' => $type]
)
->useUserLogo($profileUser);
return $this->render('Profile/likes.html.twig', array(
PROFILE_USER => $profileUser,
'likes' => $likes,
'selectedType' => $type,
'totalLikesCount' => $totalLikesCount,
'ajaxLoadingLimitNumber' => $ajaxLoadingLimitNumber,
));
}
/**
* Api method to fetch specified user likes based on $type
* lastId get parameter define from which id actions will be fetched
*
* @param Request $request
* @param User $profileUser
* @param string $type
* @return JsonResponse
*/
public function likesApi(Request $request, User $profileUser, string $type): JsonResponse
{
if (!$request->isXmlHttpRequest()) {
return new JsonResponse(array('Forbidden'), Response::HTTP_BAD_REQUEST);
}
$lastId = $request->get('lastId') ? $request->get('lastId') : 0;
$ajaxLoadingLimitNumber = AjaxLoadingLimitConstant::getConstantFromType($type);
if ($profileUser === $this->getUser()) {
$likes = $this
->getDoctrine()
->getRepository(Action::class)
->getUserLikes($profileUser, $type, $lastId, $ajaxLoadingLimitNumber);
} else {
$likes = $this
->getDoctrine()
->getRepository(Action::class)
->getUserPublicLikes($profileUser, $type, $lastId, $ajaxLoadingLimitNumber);
}
$response = array();
if (count($likes) < $ajaxLoadingLimitNumber) {
$response['noMore'] = true;
}
$response['actions'] = array();
/** @var FileAction $action */
foreach ($likes as $action) {
$actionInfos = array();
$actionInfos['id'] = $action->getId();
$actionInfos['view'] = $this->render('File/item.html.twig', array(
'file' => $action->getFile(),
))->getContent();
$response['actions'][] = $actionInfos;
}
return new JsonResponse($response, Response::HTTP_OK);
}
/**
* Show subscriptions of specified user
*
* @param SeoManager $seoManager
* @param TranslatorInterface $translator
* @param $id
* @return Response
*/
public function subscriptions(SeoManager $seoManager, TranslatorInterface $translator, $id): Response
{
$profileUser = $this->getDoctrine()
->getRepository(User::class)
->find($id);
if (!($profileUser instanceof User)) {
return $this->render('@Twig/views/Exception/error404.html.twig', [
'customMessage' => $translator->trans('profile.show.not_found', ['id' => $id])
]);
}
$seoManager
->setTitleAndDescription(
'profile.subscriptions.seo.title',
'profile.subscriptions.seo.description',
[USER_NAME => $profileUser->getUsername()]
)
->useUserLogo($profileUser);
return $this->render('Profile/subscriptions.html.twig', array(
PROFILE_USER => $profileUser,
));
}
}