src/Controller/ArticlesController.php line 99
<?phpnamespace App\Controller;use App\Entity\ArticleDetails;use App\Entity\Articles;use App\Entity\Pages;use App\Entity\User;use App\Services\PaginationManager;use Doctrine\ORM\EntityManagerInterface;use Nzo\UrlEncryptorBundle\Encryptor\Encryptor;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;class ArticlesController extends AbstractController{private $em;private $encryptor;private $pageManager;const ITEMS_PER_PAGE = 10;public function __construct(EntityManagerInterface $em, Encryptor $encryptor, PaginationManager $pageManager){$this->em = $em;$this->encryptor = $encryptor;$this->pageManager = $pageManager;}#[Route('/article/{pageId}', name: 'articles_page')]public function articlesAction(Request $request): Response{$article = $this->em->getRepository(Articles::class)->findByPageId($request->get('pageId'));return $this->render('frontend/articles.html.twig', ['articles' => $article,]);}#[Route('/support/authors/{articleId}', name: 'support_authors')]public function articleAuthorsAction(Request $request): Response{$authors = $this->em->getRepository(ArticleDetails::class)->findUsersByld($request->get('articleId'));$response = '';$count = count($authors);if($count > 3){$remaining = $count - 3;$response = 'and ' . $remaining . ' others';}return new Response($response);}#[Route('/article-list/authors/{articleDetailId}/{showAuthors}', name: 'article_list_authors')]public function articleListAuthorsAction(Request $request): Response{$articleDetails = $this->em->getRepository(ArticleDetails::class)->find($request->get('articleDetailId'));$lastUpdated = $this->em->getRepository(ArticleDetails::class)->findByLastUpdated($request->get('articleDetailId'));$response = '';if($request->get('showAuthors') == 1) {$response = 'Written By ';$authors = [];foreach ($articleDetails as $articleDetail) {$authors[] = $this->encryptor->decrypt($articleDetail->getUser()->getFirstName()) . ' ' . $this->encryptor->decrypt($articleDetail->getUser()->getLastName());}$separator = ', ';if(count($authors) <= 2){$separator = ' & ';}$response .= implode($separator, $authors);}if(array_key_exists(0, $lastUpdated)) {$modified = $lastUpdated[0]->getModified()->format('Y-m-d H:i:s');$response .= '<br>Updated ' . $this->timeAgo($modified);}return new Response($response);}#[Route('/article/{pageId}/{articleId}', name: 'article_list_page')]public function articleListAction(Request $request): Response{$articleId = $request->get('articleId');$article = $this->em->getRepository(Articles::class)->find($articleId);return $this->render('frontend/articles_list.html.twig', ['article' => $article,]);}#[Route('/article/{pageId}/{articleId}/{articleDetailId}', name: 'article_details_page')]public function articleDetailsAction(Request $request): Response{$articleDetailId = $request->get('articleDetailId');$articleDetails = $this->em->getRepository(ArticleDetails::class)->find($articleDetailId);return $this->render('frontend/article_details.html.twig', ['articleDetails' => $articleDetails,]);}#[Route('/admin/get/articles-list', name: 'get_articles_list')]public function getArticlesList(Request $request): Response{$searchString = $request->request->get('search-string');$articles = $this->em->getRepository(Articles::class)->adminFindAll($searchString);$results = $this->pageManager->paginate($articles[0], $request, self::ITEMS_PER_PAGE);$dataAction = 'data-action="click->admin--articles#onClickGetList"';$pagination = $this->getPagination($request->request->get('page-iid'), $results, $dataAction, self::ITEMS_PER_PAGE);$html = $this->render('Admin/articles/articles_list.html.twig',['articles' => $results,'pagination' => $pagination])->getContent();return new JsonResponse($html);}#[Route('/admin/get/article-form', name: 'get_article_form')]public function getArticlesForm(Request $request): Response{$articleId = $request->request->get('article-id') ?? 0;$article = $this->em->getRepository(Articles::class)->find($articleId);$pages = $this->em->getRepository(Pages::class)->findAll();if($article == null){$article = new Articles();}$html = $this->render('Admin/articles/articles.html.twig',['article' => $article,'pages' => $pages,])->getContent();$response = ['html' => $html,'articleCount' => $article->getArticleCount()];return new JsonResponse($response);}#[Route('/admin/get-articles-details', name: 'get_article_details')]public function getArticleDetailsAction(Request $request): Response{$articleId = $request->request->get('article-id');$articleDetails = $this->em->getRepository(ArticleDetails::class)->find($articleId);$response = ['name' => $articleDetails->getName(),'description' => $articleDetails->getDescription(),'copy' => $articleDetails->getCopy(),];return new JsonResponse($response);}#[Route('/admin/submit/article/crud', name: 'submit_article_crud')]public function articleCrudAction(Request $request): Response{$data = $request->request;$articleId = $data->get('article-id') ?? $data->get('delete');$article = $this->em->getRepository(Articles::class)->find($articleId);$response = [];if($data->get('delete') != null){foreach($article->getArticleDetails() as $articleDetail){$this->em->remove($articleDetail);}$this->em->remove($article);$this->em->flush();$response['flash'] = 'Article Successfully Deleted.';return new JsonResponse($response);}if(!empty($data)){if($article == null){$article = new Articles();}$page = $this->em->getRepository(Pages::class)->find($data->get('page-id'));$articleCount = count($article->getArticleDetails());$article->setIsMulti($data->get('is-multi') ?? 0);$article->setPage($page);$article->setName($data->get('name'));$article->setIcon($data->get('icon'));$article->setDescription($data->get('description'));$article->setArticleCount($articleCount);$this->em->persist($article);$this->em->flush();$response['flash'] = 'Article Successfully Saved.';$response['name'] = $article->getName();}return new JsonResponse($response);}#[Route('/admin/articles-details/crud', name: 'submit_article_details_crud')]public function articleDetailsCrudAction(Request $request): Response{$data = $request->request;$articleId = $data->get('article-id') ?? 0;$articleDetailId = $data->get('article-detail-id') ?? $data->get('delete');$article = $this->em->getRepository(Articles::class)->find($articleId);$articleDetails = $this->em->getRepository(ArticleDetails::class)->find((int) $articleDetailId);$user = $this->em->getRepository(User::class)->find($this->getUser()->getId() ?? 0);$response = [];if($data->get('delete') != null){$this->em->remove($articleDetails);$this->em->flush();$response['flash'] = 'Article Successfully Deleted.';$response['articleCount'] = count($article->getArticleDetails()->toArray()) - 1;return new JsonResponse($response);}if(!empty($data)){if($articleDetailId == 0){$articleDetails = new ArticleDetails();$articleDetails->setUser($user);}$articleDetails->setArticle($article);$articleDetails->setName($data->get('article-name'));$articleDetails->setDescription($data->get('article-description'));$articleDetails->setCopy($data->get('article-copy'));$this->em->persist($articleDetails);$this->em->flush();$articleDetails = $this->em->getRepository(ArticleDetails::class)->findBy(['article' => $articleDetails->getArticle()->getId(),]);$article->setArticleCount(count($articleDetails));$this->em->persist($article);$this->em->flush();// Generate html$html = $this->render('Admin/articles/article_details.html.twig', ['articleDetails' => $article->getArticleDetails()->toArray(),]);$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>';$response['html'] = $html;$response['articleCount'] = count($article->getArticleDetails()->toArray());}return new JsonResponse($response);}public function timeAgo($date) {$timestamp = strtotime($date);$strTime = ["second", "minute", "hour", "day", "month", "year"];$length = ["60","60","24","30","12","10"];$currentTime = time();if($currentTime >= $timestamp) {$diff = time() - $timestamp;for($i = 0; $diff >= $length[$i] && $i < count($length) - 1; $i++) {$diff = $diff / $length[$i];}$diff = round($diff);return $diff . " " . $strTime[$i] . "s ago ";}return '';}public function getPagination($currentPage, $results, $dataAction = '', $itemsPerPage = 10): string{return $this->render('pagination.html.twig', ['currentPage' => $currentPage,'results' => $results,'dataAction' => $dataAction,'itemsPerPage' => $itemsPerPage,'lastPage' => $this->pageManager->lastPage($results),'totalPages' => ceil(count($results) / $itemsPerPage),'i' => max(1, $currentPage - 5),'forLimit' => min($currentPage + 5, ceil(count($results) / $itemsPerPage)),])->getContent();}}