src/Controller/HymnsController.php line 163

Open in your IDE?
  1. <?php namespace App\Controller;
  2. use App\Entity\{Authcomp,
  3. HymnCategory,
  4. HymnsHymnCategory,
  5. Hymns,
  6. HymnsArtistView,
  7. HymnTypesView,
  8. Hymntypes,
  9. User,
  10. UserComments};
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use App\Form\{HymnSearchType, AdminDashboardFormType};
  14. use App\Helpers\BrowserDetect;
  15. use App\Repository\HymnsRepository;
  16. use DateTime;
  17. use Doctrine\ORM\NonUniqueResultException;
  18. use Jaybizzle\CrawlerDetect\CrawlerDetect;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\HttpFoundation\{Request, Response};
  21. /**
  22. * Class HymnsController.
  23. */
  24. class HymnsController extends AbstractController
  25. {
  26. /**
  27. * @Symfony\Component\Routing\Annotation\Route("/hymns/{hymnType}", name="hymns")
  28. *
  29. * @param ManagerRegistry $doctrine
  30. * @param string $hymnType
  31. * @return Response
  32. */
  33. public function index(ManagerRegistry $doctrine, string $hymnType): Response
  34. {
  35. $hymns = $doctrine->getRepository(HymnTypesView::class)->findByTypeOrderedByTitle($hymnType);
  36. return $this->render('hymns/index.html.twig', ['hymns' => $hymns]);
  37. }
  38. /**
  39. * @Symfony\Component\Routing\Annotation\Route("/hymns/info/{slug}", name="hymnInfo")
  40. *
  41. * @throws NonUniqueResultException
  42. */
  43. public function info(Hymns $hymn, ManagerRegistry $em, Request $request): Response
  44. {
  45. /** @var HymnsRepository $repo */
  46. $repo = $em->getRepository(Hymns::class);
  47. $cd = new CrawlerDetect();
  48. if( ! $cd->isCrawler() && ! $this->isGranted( 'ROLE_ADMIN' ) ) {
  49. $hymn->setLastAccessed( new DateTime() );
  50. $hymn->setHymnHitCount( $hymn->getHymnHitCount() + 1 );
  51. $em->getManager()->persist($hymn);
  52. $em->getManager()->flush();
  53. }
  54. $htype = $hymn->getTypeId();
  55. $prevHymn = $repo->findPreviousHymn($hymn->getTitle(), $htype) ??
  56. $repo->findLastHymn($hymn->getTypeId());
  57. $nextHymn = $repo->findNextHymn($hymn->getTitle(), $htype) ??
  58. $repo->findFirstHymn($htype);
  59. $hymn->setPrevSlug($prevHymn->getSlug());
  60. $hymn->setNextSlug($nextHymn->getSlug());
  61. $hymn->setFirstLetter();
  62. $hymn->setNextFL();
  63. $browserDetect = new BrowserDetect();
  64. $useImg = 'Firefox' === $browserDetect->getBrowser() || $browserDetect->isMobileDevice();
  65. $hymnType = $em->getRepository(Hymntypes::class)->find(['id' => $htype]);
  66. $xref = $em->getRepository(HymnsHymnCategory::class)->findBy(['HymnsId' => $hymn->getId()]);
  67. $categories = null;
  68. $cat_repo = $em->getRepository(HymnCategory::class);
  69. foreach ($xref as $item) {
  70. $tmp = $cat_repo->findOneBy(['id' => $item->getHymnCategoryId()]);
  71. if(!is_null($tmp)) {
  72. $categories[] = [
  73. 'id' => $tmp->getId(),
  74. 'slug' => $tmp->getSlug(),
  75. 'title' => $tmp->getTitle(),
  76. 'description' => $tmp->getDescription(),
  77. 'created_at' => $tmp->getCreatedAt(),
  78. 'hymns' => $tmp->getHymns()
  79. ];
  80. }
  81. }
  82. $acrepo = $em->getRepository(Authcomp::class);
  83. /** @var Authcomp $auth */
  84. $auth = $acrepo->find(['id' => $hymn->getAuthorId() ?? 713]);
  85. $auth->setBirthDate();
  86. $auth->setDeathDate();
  87. /** @var Authcomp $comp */
  88. $comp = $acrepo->find(['id' => $hymn->getComposerId() ?? 713]);
  89. $comp->setBirthDate();
  90. $comp->setDeathDate();
  91. /** @var Authcomp $arrn */
  92. $arrn = $acrepo->find(['id' => $hymn->getArrangerId() ?? 713]);
  93. $arrn->setBirthDate();
  94. $arrn->setDeathDate();
  95. $comments = $em->getRepository(UserComments::class)
  96. ->findBy(['hymnId' => $hymn->getId()], ['commentDate' => 'DESC']);
  97. foreach ($comments as $c) {
  98. $c->setStrCommentDate($c->getCommentDate());
  99. }
  100. $userComment = new UserComments();
  101. $form = $this->createForm(AdminDashboardFormType::class, $userComment);
  102. $form->handleRequest($request);
  103. if ($form->isSubmitted() && $form->isValid()) {
  104. $mgr = $em->getManager();
  105. $user_repo = $em->getRepository(User::class);
  106. $user = $user_repo->findBy(['username' => $this->getUser()->getUsername()])[0];
  107. $userComment->setHymnId($hymn->getID());
  108. $userComment->setUserId($this->getUser()->getId());
  109. $userComment->setUserName($this->getUser());
  110. $userComment->setIpAddress($request->getClientIp());
  111. $userComment->setCommentDate(new DateTime());
  112. $userComment->setComment($form->getData()->getComment());
  113. $userComment->setReferringUrl($request->getHttpHost().$request->getRequestUri());
  114. $userComment->setHttpAgent($request->server->get('HTTP_USER_AGENT'));
  115. $userComment->setEmail($user->getEmail());
  116. $mgr->persist($userComment);
  117. $mgr->flush();
  118. }
  119. $artist_hymns = $em->getRepository(HymnsArtistView::class)
  120. ->findByTypeOrderedByLastAccessed($hymn->getTypeId(), $hymn->getSlug());
  121. $is_recorded = count($artist_hymns) > 0;
  122. $form = $form->createView();
  123. return $this->render(
  124. 'hymns/info.html.twig',
  125. compact(
  126. 'hymn',
  127. 'hymnType',
  128. 'categories',
  129. 'auth',
  130. 'comp',
  131. 'arrn',
  132. 'comments',
  133. 'form',
  134. 'useImg',
  135. 'artist_hymns',
  136. 'is_recorded'
  137. )
  138. );
  139. }
  140. /**
  141. * @Symfony\Component\Routing\Annotation\Route("/search/hymns", name="hymnsSearch")
  142. */
  143. public function search(ManagerRegistry $doctrine, Request $request): Response
  144. {
  145. $hymns = [];
  146. $form = $this->createForm(HymnSearchType::class);
  147. $form->handleRequest($request);
  148. if ($form->isSubmitted() && $form->isValid()) {
  149. $srchTerms = $form->getData()['hymnsSearch'];
  150. if (!empty($srchTerms)) {
  151. /** @var HymnsRepository $hymnsRepo */
  152. $hymnsRepo = $doctrine->getRepository(Hymns::class);
  153. $hymns = $hymnsRepo->searchHymns($srchTerms);
  154. }
  155. }
  156. return $this->render('hymns/search.html.twig', ['hymns' => $hymns, 'form' => $form->createView()]);
  157. }
  158. /**
  159. * @Symfony\Component\Routing\Annotation\Route("/last10", name="hymnsLast10")
  160. */
  161. public function last10(ManagerRegistry $doctrine, Request $request): Response
  162. {
  163. $lastHymnsViewedCount = 10;
  164. if($request->isMethod('POST')) {
  165. $lastHymnsViewedCount = $_POST['inputLast'];
  166. }
  167. $all = $doctrine->getRepository(HymnTypesView::class)->findBy([], ['last_accessed' => 'DESC']);
  168. $mens = $satb = $bshop = [];
  169. $mcount = $scount = $bcount = 0;
  170. /** @var Hymns $item */
  171. foreach ($all as $item) {
  172. switch ($item->getTypeId()) {
  173. case 1:
  174. if($mcount++ < $lastHymnsViewedCount) {
  175. $mens[] = $item;
  176. }
  177. break;
  178. case 2:
  179. if($scount++ < $lastHymnsViewedCount) {
  180. $satb[] = $item;
  181. }
  182. break;
  183. default:
  184. if($bcount++ < $lastHymnsViewedCount) {
  185. $bshop[] = $item;
  186. }
  187. }
  188. }
  189. return $this->render('hymns/last10.html.twig',
  190. [
  191. 'mens' => $mens,
  192. 'satb' => $satb,
  193. 'bshop' => $bshop,
  194. 'lastHymnsViewedCount' => $lastHymnsViewedCount
  195. ]
  196. );
  197. }
  198. /**
  199. * @Symfony\Component\Routing\Annotation\Route("/top5", name="hymnsTop5")
  200. */
  201. public function top5(ManagerRegistry $doctrine, Request $request): Response
  202. {
  203. $lastHymnsViewedCount = 5;
  204. if($request->isMethod('POST')) {
  205. $lastHymnsViewedCount = $_POST['inputLast'];
  206. }
  207. $all = $doctrine->getRepository(HymnTypesView::class)->findBy([], [
  208. 'hymn_hit_count' => 'DESC',
  209. 'last_accessed' => 'DESC'
  210. ]);
  211. $mens = $satb = $bshop = [];
  212. $mcount = $scount = $bcount = 0;
  213. /** @var Hymns $item */
  214. foreach ($all as $item) {
  215. switch ($item->getTypeId()) {
  216. case 1:
  217. if($mcount++ < $lastHymnsViewedCount) {
  218. $mens[] = $item;
  219. }
  220. break;
  221. case 2:
  222. if($scount++ < $lastHymnsViewedCount) {
  223. $satb[] = $item;
  224. }
  225. break;
  226. default:
  227. if($bcount++ < $lastHymnsViewedCount) {
  228. $bshop[] = $item;
  229. }
  230. }
  231. }
  232. return $this->render('hymns/top5.html.twig',
  233. [
  234. 'mens' => $mens,
  235. 'satb' => $satb,
  236. 'bshop' => $bshop,
  237. 'lastHymnsViewedCount' => $lastHymnsViewedCount
  238. ]
  239. );
  240. }
  241. }