src/Controller/ArticlesController.php line 99

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\ArticleDetails;
  4. use App\Entity\Articles;
  5. use App\Entity\Pages;
  6. use App\Entity\User;
  7. use App\Services\PaginationManager;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. class ArticlesController extends AbstractController
  16. {
  17. private $em;
  18. private $encryptor;
  19. private $pageManager;
  20. const ITEMS_PER_PAGE = 10;
  21. public function __construct(EntityManagerInterface $em, Encryptor $encryptor, PaginationManager $pageManager)
  22. {
  23. $this->em = $em;
  24. $this->encryptor = $encryptor;
  25. $this->pageManager = $pageManager;
  26. }
  27. #[Route('/article/{pageId}', name: 'articles_page')]
  28. public function articlesAction(Request $request): Response
  29. {
  30. $article = $this->em->getRepository(Articles::class)->findByPageId($request->get('pageId'));
  31. return $this->render('frontend/articles.html.twig', [
  32. 'articles' => $article,
  33. ]);
  34. }
  35. #[Route('/support/authors/{articleId}', name: 'support_authors')]
  36. public function articleAuthorsAction(Request $request): Response
  37. {
  38. $authors = $this->em->getRepository(ArticleDetails::class)->findUsersByld($request->get('articleId'));
  39. $response = '';
  40. $count = count($authors);
  41. if($count > 3){
  42. $remaining = $count - 3;
  43. $response = 'and ' . $remaining . ' others';
  44. }
  45. return new Response($response);
  46. }
  47. #[Route('/article-list/authors/{articleDetailId}/{showAuthors}', name: 'article_list_authors')]
  48. public function articleListAuthorsAction(Request $request): Response
  49. {
  50. $articleDetails = $this->em->getRepository(ArticleDetails::class)->find($request->get('articleDetailId'));
  51. $lastUpdated = $this->em->getRepository(ArticleDetails::class)->findByLastUpdated($request->get('articleDetailId'));
  52. $response = '';
  53. if($request->get('showAuthors') == 1) {
  54. $response = 'Written By ';
  55. $authors = [];
  56. foreach ($articleDetails as $articleDetail) {
  57. $authors[] = $this->encryptor->decrypt($articleDetail->getUser()->getFirstName()) . ' ' . $this->encryptor->decrypt($articleDetail->getUser()->getLastName());
  58. }
  59. $separator = ', ';
  60. if(count($authors) <= 2){
  61. $separator = ' & ';
  62. }
  63. $response .= implode($separator, $authors);
  64. }
  65. if(array_key_exists(0, $lastUpdated)) {
  66. $modified = $lastUpdated[0]->getModified()->format('Y-m-d H:i:s');
  67. $response .= '<br>Updated ' . $this->timeAgo($modified);
  68. }
  69. return new Response($response);
  70. }
  71. #[Route('/article/{pageId}/{articleId}', name: 'article_list_page')]
  72. public function articleListAction(Request $request): Response
  73. {
  74. $articleId = $request->get('articleId');
  75. $article = $this->em->getRepository(Articles::class)->find($articleId);
  76. return $this->render('frontend/articles_list.html.twig', [
  77. 'article' => $article,
  78. ]);
  79. }
  80. #[Route('/article/{pageId}/{articleId}/{articleDetailId}', name: 'article_details_page')]
  81. public function articleDetailsAction(Request $request): Response
  82. {
  83. $articleDetailId = $request->get('articleDetailId');
  84. $articleDetails = $this->em->getRepository(ArticleDetails::class)->find($articleDetailId);
  85. return $this->render('frontend/article_details.html.twig', [
  86. 'articleDetails' => $articleDetails,
  87. ]);
  88. }
  89. #[Route('/admin/get/articles-list', name: 'get_articles_list')]
  90. public function getArticlesList(Request $request): Response
  91. {
  92. $searchString = $request->request->get('search-string');
  93. $articles = $this->em->getRepository(Articles::class)->adminFindAll($searchString);
  94. $results = $this->pageManager->paginate($articles[0], $request, self::ITEMS_PER_PAGE);
  95. $dataAction = 'data-action="click->admin--articles#onClickGetList"';
  96. $pagination = $this->getPagination($request->request->get('page-iid'), $results, $dataAction, self::ITEMS_PER_PAGE);
  97. $html = $this->render('Admin/articles/articles_list.html.twig',[
  98. 'articles' => $results,
  99. 'pagination' => $pagination
  100. ])->getContent();
  101. return new JsonResponse($html);
  102. }
  103. #[Route('/admin/get/article-form', name: 'get_article_form')]
  104. public function getArticlesForm(Request $request): Response
  105. {
  106. $articleId = $request->request->get('article-id') ?? 0;
  107. $article = $this->em->getRepository(Articles::class)->find($articleId);
  108. $pages = $this->em->getRepository(Pages::class)->findAll();
  109. $html = $this->render('Admin/articles/articles.html.twig',[
  110. 'article' => $article,
  111. 'pages' => $pages,
  112. ])->getContent();
  113. $response = [
  114. 'html' => $html,
  115. 'articleCount' => $article->getArticleCount()
  116. ];
  117. return new JsonResponse($response);
  118. }
  119. #[Route('/admin/get-articles-details', name: 'get_article_details')]
  120. public function getArticleDetailsAction(Request $request): Response
  121. {
  122. $articleId = $request->request->get('article-id');
  123. $articleDetails = $this->em->getRepository(ArticleDetails::class)->find($articleId);
  124. $response = [
  125. 'name' => $articleDetails->getName(),
  126. 'description' => $articleDetails->getDescription(),
  127. 'copy' => $articleDetails->getCopy(),
  128. ];
  129. return new JsonResponse($response);
  130. }
  131. #[Route('/admin/submit/article/crud', name: 'submit_article_crud')]
  132. public function articleCrudAction(Request $request): Response
  133. {
  134. $data = $request->request;
  135. $articleId = $data->get('article-id') ?? $data->get('delete');
  136. $article = $this->em->getRepository(Articles::class)->find($articleId);
  137. $response = [];
  138. if($data->get('delete') != null)
  139. {
  140. foreach($article->getArticleDetails() as $articleDetail)
  141. {
  142. $this->em->remove($articleDetail);
  143. }
  144. $this->em->remove($article);
  145. $this->em->flush();
  146. $response['flash'] = 'Article Successfully Deleted.';
  147. return new JsonResponse($response);
  148. }
  149. if(!empty($data))
  150. {
  151. if($article == null)
  152. {
  153. $article = new Articles();
  154. }
  155. $page = $this->em->getRepository(Pages::class)->find($data->get('page-id'));
  156. $articleCount = count($article->getArticleDetails());
  157. $article->setIsMulti($data->get('is-multi') ?? 0);
  158. $article->setPage($page);
  159. $article->setName($data->get('name'));
  160. $article->setIcon($data->get('icon'));
  161. $article->setDescription($data->get('description'));
  162. $article->setArticleCount($articleCount);
  163. $this->em->persist($article);
  164. $this->em->flush();
  165. $response['flash'] = 'Article Successfully Saved.';
  166. $response['name'] = $article->getName();
  167. }
  168. return new JsonResponse($response);
  169. }
  170. #[Route('/admin/articles-details/crud', name: 'submit_article_details_crud')]
  171. public function articleDetailsCrudAction(Request $request): Response
  172. {
  173. $data = $request->request;
  174. $articleId = $data->get('article-id') ?? 0;
  175. $articleDetailId = $data->get('article-detail-id') ?? $data->get('delete');
  176. $article = $this->em->getRepository(Articles::class)->find($articleId);
  177. $articleDetails = $this->em->getRepository(ArticleDetails::class)->find((int) $articleDetailId);
  178. $user = $this->em->getRepository(User::class)->find($this->getUser()->getId() ?? 0);
  179. $response = [];
  180. if($data->get('delete') != null)
  181. {
  182. $this->em->remove($articleDetails);
  183. $this->em->flush();
  184. $response['flash'] = 'Article Successfully Deleted.';
  185. $response['articleCount'] = count($article->getArticleDetails()->toArray()) - 1;
  186. return new JsonResponse($response);
  187. }
  188. if(!empty($data))
  189. {
  190. if($articleDetailId == 0)
  191. {
  192. $articleDetails = new ArticleDetails();
  193. $articleDetails->setUser($user);
  194. }
  195. $articleDetails->setArticle($article);
  196. $articleDetails->setName($data->get('article-name'));
  197. $articleDetails->setDescription($data->get('article-description'));
  198. $articleDetails->setCopy($data->get('article-copy'));
  199. $this->em->persist($articleDetails);
  200. $this->em->flush();
  201. $articleDetails = $this->em->getRepository(ArticleDetails::class)->findBy([
  202. 'article' => $articleDetails->getArticle()->getId(),
  203. ]);
  204. $article->setArticleCount(count($articleDetails));
  205. $this->em->persist($article);
  206. $this->em->flush();
  207. // Generate html
  208. $html = $this->render('Admin/articles/article_details.html.twig', [
  209. 'articleDetails' => $article->getArticleDetails()->toArray(),
  210. ]);
  211. $response['flash'] = '<b><i class="fas fa-check-circle"></i> Article Successfully Saved.<div class="flash-close"><i class="fa-solid fa-xmark"></i></div>';
  212. $response['html'] = $html;
  213. $response['articleCount'] = count($article->getArticleDetails()->toArray());
  214. }
  215. return new JsonResponse($response);
  216. }
  217. public function timeAgo($date) {
  218. $timestamp = strtotime($date);
  219. $strTime = ["second", "minute", "hour", "day", "month", "year"];
  220. $length = ["60","60","24","30","12","10"];
  221. $currentTime = time();
  222. if($currentTime >= $timestamp) {
  223. $diff = time() - $timestamp;
  224. for($i = 0; $diff >= $length[$i] && $i < count($length) - 1; $i++) {
  225. $diff = $diff / $length[$i];
  226. }
  227. $diff = round($diff);
  228. return $diff . " " . $strTime[$i] . "s ago ";
  229. }
  230. return '';
  231. }
  232. public function getPagination($currentPage, $results, $dataAction = '', $itemsPerPage = 10): string
  233. {
  234. return $this->render('pagination.html.twig', [
  235. 'currentPage' => $currentPage,
  236. 'results' => $results,
  237. 'dataAction' => $dataAction,
  238. 'itemsPerPage' => $itemsPerPage,
  239. 'lastPage' => $this->pageManager->lastPage($results),
  240. 'totalPages' => ceil(count($results) / $itemsPerPage),
  241. 'i' => max(1, $currentPage - 5),
  242. 'forLimit' => min($currentPage + 5, ceil(count($results) / $itemsPerPage)),
  243. ])->getContent();
  244. }
  245. }