<?php namespace App\Controller;
use App\Entity\{Authcomp,
HymnCategory,
HymnsHymnCategory,
Hymns,
HymnsArtistView,
HymnTypesView,
Hymntypes,
User,
UserComments};
use Doctrine\Common\Collections\Collection;
use Doctrine\Persistence\ManagerRegistry;
use App\Form\{HymnSearchType, AdminDashboardFormType};
use App\Helpers\BrowserDetect;
use App\Repository\HymnsRepository;
use DateTime;
use Doctrine\ORM\NonUniqueResultException;
use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\{Request, Response};
/**
* Class HymnsController.
*/
class HymnsController extends AbstractController
{
/**
* @Symfony\Component\Routing\Annotation\Route("/hymns/{hymnType}", name="hymns")
*
* @param ManagerRegistry $doctrine
* @param string $hymnType
* @return Response
*/
public function index(ManagerRegistry $doctrine, string $hymnType): Response
{
$hymns = $doctrine->getRepository(HymnTypesView::class)->findByTypeOrderedByTitle($hymnType);
return $this->render('hymns/index.html.twig', ['hymns' => $hymns]);
}
/**
* @Symfony\Component\Routing\Annotation\Route("/hymns/info/{slug}", name="hymnInfo")
*
* @throws NonUniqueResultException
*/
public function info(Hymns $hymn, ManagerRegistry $em, Request $request): Response
{
/** @var HymnsRepository $repo */
$repo = $em->getRepository(Hymns::class);
$cd = new CrawlerDetect();
if( ! $cd->isCrawler() && ! $this->isGranted( 'ROLE_ADMIN' ) ) {
$hymn->setLastAccessed( new DateTime() );
$hymn->setHymnHitCount( $hymn->getHymnHitCount() + 1 );
$em->getManager()->persist($hymn);
$em->getManager()->flush();
}
$htype = $hymn->getTypeId();
$prevHymn = $repo->findPreviousHymn($hymn->getTitle(), $htype) ??
$repo->findLastHymn($hymn->getTypeId());
$nextHymn = $repo->findNextHymn($hymn->getTitle(), $htype) ??
$repo->findFirstHymn($htype);
$hymn->setPrevSlug($prevHymn->getSlug());
$hymn->setNextSlug($nextHymn->getSlug());
$hymn->setFirstLetter();
$hymn->setNextFL();
$browserDetect = new BrowserDetect();
$useImg = 'Firefox' === $browserDetect->getBrowser() || $browserDetect->isMobileDevice();
$hymnType = $em->getRepository(Hymntypes::class)->find(['id' => $htype]);
$xref = $em->getRepository(HymnsHymnCategory::class)->findBy(['HymnsId' => $hymn->getId()]);
$categories = null;
$cat_repo = $em->getRepository(HymnCategory::class);
foreach ($xref as $item) {
$tmp = $cat_repo->findOneBy(['id' => $item->getHymnCategoryId()]);
if(!is_null($tmp)) {
$categories[] = [
'id' => $tmp->getId(),
'slug' => $tmp->getSlug(),
'title' => $tmp->getTitle(),
'description' => $tmp->getDescription(),
'created_at' => $tmp->getCreatedAt(),
'hymns' => $tmp->getHymns()
];
}
}
$acrepo = $em->getRepository(Authcomp::class);
/** @var Authcomp $auth */
$auth = $acrepo->find(['id' => $hymn->getAuthorId() ?? 713]);
$auth->setBirthDate();
$auth->setDeathDate();
/** @var Authcomp $comp */
$comp = $acrepo->find(['id' => $hymn->getComposerId() ?? 713]);
$comp->setBirthDate();
$comp->setDeathDate();
/** @var Authcomp $arrn */
$arrn = $acrepo->find(['id' => $hymn->getArrangerId() ?? 713]);
$arrn->setBirthDate();
$arrn->setDeathDate();
$comments = $em->getRepository(UserComments::class)
->findBy(['hymnId' => $hymn->getId()], ['commentDate' => 'DESC']);
foreach ($comments as $c) {
$c->setStrCommentDate($c->getCommentDate());
}
$userComment = new UserComments();
$form = $this->createForm(AdminDashboardFormType::class, $userComment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$mgr = $em->getManager();
$user_repo = $em->getRepository(User::class);
$user = $user_repo->findBy(['username' => $this->getUser()->getUsername()])[0];
$userComment->setHymnId($hymn->getID());
$userComment->setUserId($this->getUser()->getId());
$userComment->setUserName($this->getUser());
$userComment->setIpAddress($request->getClientIp());
$userComment->setCommentDate(new DateTime());
$userComment->setComment($form->getData()->getComment());
$userComment->setReferringUrl($request->getHttpHost().$request->getRequestUri());
$userComment->setHttpAgent($request->server->get('HTTP_USER_AGENT'));
$userComment->setEmail($user->getEmail());
$mgr->persist($userComment);
$mgr->flush();
}
$artist_hymns = $em->getRepository(HymnsArtistView::class)
->findByTypeOrderedByLastAccessed($hymn->getTypeId(), $hymn->getSlug());
$is_recorded = count($artist_hymns) > 0;
$form = $form->createView();
return $this->render(
'hymns/info.html.twig',
compact(
'hymn',
'hymnType',
'categories',
'auth',
'comp',
'arrn',
'comments',
'form',
'useImg',
'artist_hymns',
'is_recorded'
)
);
}
/**
* @Symfony\Component\Routing\Annotation\Route("/search/hymns", name="hymnsSearch")
*/
public function search(ManagerRegistry $doctrine, Request $request): Response
{
$hymns = [];
$form = $this->createForm(HymnSearchType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$srchTerms = $form->getData()['hymnsSearch'];
if (!empty($srchTerms)) {
/** @var HymnsRepository $hymnsRepo */
$hymnsRepo = $doctrine->getRepository(Hymns::class);
$hymns = $hymnsRepo->searchHymns($srchTerms);
}
}
return $this->render('hymns/search.html.twig', ['hymns' => $hymns, 'form' => $form->createView()]);
}
/**
* @Symfony\Component\Routing\Annotation\Route("/last10", name="hymnsLast10")
*/
public function last10(ManagerRegistry $doctrine, Request $request): Response
{
$lastHymnsViewedCount = 10;
if($request->isMethod('POST')) {
$lastHymnsViewedCount = $_POST['inputLast'];
}
$all = $doctrine->getRepository(HymnTypesView::class)->findBy([], ['last_accessed' => 'DESC']);
$mens = $satb = $bshop = [];
$mcount = $scount = $bcount = 0;
/** @var Hymns $item */
foreach ($all as $item) {
switch ($item->getTypeId()) {
case 1:
if($mcount++ < $lastHymnsViewedCount) {
$mens[] = $item;
}
break;
case 2:
if($scount++ < $lastHymnsViewedCount) {
$satb[] = $item;
}
break;
default:
if($bcount++ < $lastHymnsViewedCount) {
$bshop[] = $item;
}
}
}
return $this->render('hymns/last10.html.twig',
[
'mens' => $mens,
'satb' => $satb,
'bshop' => $bshop,
'lastHymnsViewedCount' => $lastHymnsViewedCount
]
);
}
/**
* @Symfony\Component\Routing\Annotation\Route("/top5", name="hymnsTop5")
*/
public function top5(ManagerRegistry $doctrine, Request $request): Response
{
$lastHymnsViewedCount = 5;
if($request->isMethod('POST')) {
$lastHymnsViewedCount = $_POST['inputLast'];
}
$all = $doctrine->getRepository(HymnTypesView::class)->findBy([], [
'hymn_hit_count' => 'DESC',
'last_accessed' => 'DESC'
]);
$mens = $satb = $bshop = [];
$mcount = $scount = $bcount = 0;
/** @var Hymns $item */
foreach ($all as $item) {
switch ($item->getTypeId()) {
case 1:
if($mcount++ < $lastHymnsViewedCount) {
$mens[] = $item;
}
break;
case 2:
if($scount++ < $lastHymnsViewedCount) {
$satb[] = $item;
}
break;
default:
if($bcount++ < $lastHymnsViewedCount) {
$bshop[] = $item;
}
}
}
return $this->render('hymns/top5.html.twig',
[
'mens' => $mens,
'satb' => $satb,
'bshop' => $bshop,
'lastHymnsViewedCount' => $lastHymnsViewedCount
]
);
}
}