src/Controller/BlogController.php line 108

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Enterprise License (PEL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  * @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  * @license    http://www.pimcore.org/license     GPLv3 and PEL
  13.  */
  14. namespace App\Controller;
  15. use Carbon\Carbon;
  16. use App\Website\Tool\Text;
  17. use Pimcore\Model\DataObject;
  18. // use Pimcore\Twig\Extension\Templating\HeadTitle;
  19. // use Pimcore\Twig\Extension\Templating\Placeholder;
  20. use Pimcore\Model\DataObject\Blogs;
  21. use Knp\Component\Pager\PaginatorInterface;
  22. use Pimcore\Model\DataObject\Blogcategories;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\HttpFoundation\JsonResponse;
  27. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  28. class BlogController extends BaseController
  29. {   
  30.     /**
  31.      * Insights Listing Function
  32.      *
  33.      * @param Request $request
  34.      * @param PaginatorInterface $paginator
  35.      * @return void
  36.      */
  37.     public function blogAction(Request $requestPaginatorInterface $paginator)
  38.     {
  39.         $data = [];
  40.         $blogCategories = new DataObject\Blogcategories\Listing();
  41.         $blogCategories->setOrderKey('name');
  42.         $blogCategories->setOrder('asc');
  43.         $centricCategoryData null// Initialize the variable to store "Centric" category data
  44.         foreach ($blogCategories as $blogCategory) {
  45.             if (!$blogCategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
  46.                 throw new NotFoundHttpException('Blog category not found.');
  47.             }
  48.             $blogs = new DataObject\Blogs\Listing();
  49.             $blogs->setCondition('category__id IN (?)', [$blogCategory->getId()])
  50.                 ->setOrderKey('date')
  51.                 ->setOrder('desc')
  52.                 ->setLimit(6);
  53.             $categoryData = [
  54.                 'category' => $blogCategory,
  55.                 'blogData' => [
  56.                     'blogs' => $blogs
  57.                 ]
  58.             ];
  59.             if ($blogCategory->getUrl() === 'business-insights') {
  60.                 // If the category name is "Centric," store the data separately
  61.                 $centricCategoryData $categoryData;
  62.             } else {
  63.                 // For other categories, add them to the regular data array
  64.                 $data[] = $categoryData;
  65.             }
  66.         }
  67.         if ($centricCategoryData !== null) {
  68.             // If there's "Centric" category data, add it at index 0
  69.             array_unshift($data$centricCategoryData);
  70.         }
  71.         $page = ($request->get('page')) ? $request->get('page') : 1;
  72.         $page = (int)$page;
  73.         $pagination $paginator->paginate(
  74.             $data,
  75.             $page,
  76.             20 // Number of items per page
  77.         );
  78.         return $this->render('Blog/blog_listing.html.twig', [
  79.             'data' => $pagination,
  80.             'pagination' => $pagination->getPaginationData(),
  81.             'categories' => $this->getBlogCatogeries(),
  82.         ]);
  83.     }
  84.     /**
  85.      * @Route("/insights/{category}", name="insights_category")
  86.      */
  87.     public function blogCategoryAction(Request $requestPaginatorInterface $paginator)
  88.     {
  89.         $blogcategory DataObject\Blogcategories::getByUrl($request->get('category'), true);
  90.         if (!$blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
  91.             throw new NotFoundHttpException('Blog category not found.');
  92.         }
  93.         //get parm from request
  94.         $limit 6;
  95.         $page = ($request->get('page')) ? $request->get('page') : 1;
  96.         $page = (int)$page;
  97.         // Load blog listing
  98.         $categoryId $blogcategory->getId();
  99.         $blogs = new DataObject\Blogs\Listing();
  100.         $blogs->setCondition('category__id IN (?)', [$blogcategory->getId()])
  101.             ->setOrderKey('date')
  102.             ->setOrder('desc');
  103.         $paginate $paginator->paginate(
  104.             $blogs,
  105.             $page,
  106.             $limit
  107.         );
  108.         return $this->render('Blog/blog_category.html.twig', [
  109.             'blogs' => $paginate,
  110.             'pagination' => $paginate->getPaginationData(),
  111.             'categories' => $this->getBlogCatogeries(),
  112.             'blogcategory' => $blogcategory
  113.         ]);
  114.     }
  115.     /**
  116.      * @Route("/insights/{category}/{url}", name="insights_category_detail")
  117.      */
  118.     public function blogDetailAction(Request $request)
  119.     {
  120.         $blogcategory DataObject\Blogcategories::getByUrl($request->get('category'), true);
  121.         if (!$blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
  122.             throw new NotFoundHttpException('Blog category not found.');
  123.         }
  124.         $blog DataObject\Blogs::getByUrl($request->get('url'), true);
  125.         if (!$blog instanceof \Pimcore\Model\DataObject\Blogs) {
  126.             throw new NotFoundHttpException('Blog not found.');
  127.         }
  128.         $blogPosts = new Blogs\Listing();
  129.         $blogPosts->setLimit(3);
  130.         $blogPosts->setOrderKey('date');
  131.         $blogPosts->setOrder('desc');
  132.         $blogPosts->setCondition('o_id != ? and category__id IN (?)', [$blog->getId(), $blogcategory->getId()]);
  133.         if ($blog->getcategory() && $blog->getcategory()->getId() == $blogcategory->getId()) {
  134.             return $this->render(
  135.                 'Blog/blogDetail.html.twig',
  136.                 [
  137.                     'blog' => $blog,
  138.                     'blogPosts' => $blogPosts,
  139.                     'categories' => $this->getBlogCatogeries()
  140.                 ]
  141.             );
  142.         }
  143.         throw new NotFoundHttpException('Blog not found.');
  144.     }
  145.     /**
  146.      *@Route("/blog-migration", name="blog-migration")
  147.      */
  148.     public function importBlogsAction()
  149.     {
  150.         $csvFile PIMCORE_PROJECT_ROOT '/public/all_csvs/AdobeExperienceManager-articles.csv'// Replace with the actual path to your CSV file
  151.         $counter 1;
  152.         if (($handle fopen($csvFile"r")) !== false) {
  153.             while (($data fgetcsv($handle1000",")) !== false) {
  154.                 if ($counter 1) {
  155.                     dd("stop");
  156.                     $catId 448;
  157.                     $blogcategory DataObject\Blogcategories::getById($catIdtrue);
  158.                     if (!$blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
  159.                         throw new NotFoundHttpException('Blog category not found.');
  160.                     }
  161.                     $url $this->titleToUrl($data[0]);
  162.                     $dublicateBlog DataObject\Blogs::getByUrl($urltrue);
  163.                     if (!$dublicateBlog instanceof \Pimcore\Model\DataObject\Blogs) {
  164.                         //throw new NotFoundHttpException('Blog category not found.');
  165.                         $blog = new Blogs();
  166.                         $blog->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath("/insights/" $blogcategory->getName()));
  167.                         $blog->setKey($url);
  168.                         $blog->setUrl($url);
  169.                         $blog->setTitle($data[0]);
  170.                         $blog->setContent($data[1]);
  171.                         $blog->setCategory($blogcategory);
  172.                         $currentDate date("d-m-Y"); // Format: Year-Month-Day (e.g., 2023-08-26)
  173.                         $carbonDate Carbon::parse($currentDate);
  174.                         $blog->setDate($carbonDate);
  175.                         $blog->setPostedBy('Usman Khalid');
  176.                         $blog->setSeoTitle($data[2]);
  177.                         $blog->setSeoDescription($data[3]);
  178.                         $blog->setPublished(true);
  179.                         $blog->Save();
  180.                     }
  181.                     //update existing blog
  182.                 }
  183.                 $counter++;
  184.             }
  185.             fclose($handle);
  186.         }
  187.         return new Response("Blogs imported successfully!");
  188.     }
  189.     function titleToUrl($title)
  190.     {
  191.         // Convert to lowercase
  192.         $url strtolower($title);
  193.         // Replace spaces with dashes
  194.         $url str_replace(' ''-'$url);
  195.         // Remove special characters except dashes and alphanumeric characters
  196.         $url preg_replace('/[^a-z0-9\-]/'''$url);
  197.         // Remove consecutive dashes
  198.         $url preg_replace('/-+/''-'$url);
  199.         // Trim dashes from the beginning and end
  200.         $url trim($url'-');
  201.         return $url;
  202.     }
  203.     function getBlogCatogeries()
  204.     {
  205.         $blogPosts = new Blogcategories\Listing();
  206.         $blogPosts->setOrderKey('o_Creationdate');
  207.         $blogPosts->setOrder('desc');
  208.         $blogPosts->Load();
  209.         return $blogPosts;
  210.     }
  211.     /**
  212.      * @Route("/insights-search", name="insights_search")
  213.      */
  214.     public function insightsSearchAction(Request $requestPaginatorInterface $paginator)
  215.     {
  216.         $name strip_tags($request->get('name'));
  217.         $category strip_tags($request->get('category'));
  218.         $queryParms "Query = " $name ',' $category;
  219.         $blogs = new DataObject\Blogs\Listing();
  220.         if (!empty($category) || !empty($name)) {
  221.             if ($category != "all") {
  222.                 $blogcategory DataObject\Blogcategories::getByUrl($categorytrue);
  223.                 if ($blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
  224.                     $blogs->addConditionParam('category__id IN (?)', [$blogcategory->getId()]);
  225.                 }
  226.             } else {
  227.                 $blogs->addConditionParam('category__id != ?', ['']);
  228.             }
  229.             if ($name) {
  230.                 $blogs->setCondition("title LIKE ? and category__id != ?", ["%" $name "%"'']);
  231.             }
  232.             $blogs->setOrderKey('o_Creationdate');
  233.             $blogs->setOrder('desc');
  234.             $limit 6;
  235.             $page = ($request->get('page')) ? $request->get('page') : 1;
  236.             $page = (int)$page;
  237.             $paginate $paginator->paginate(
  238.                 $blogs,
  239.                 $page,
  240.                 $limit
  241.             );
  242.             return $this->render('Blog/insights_search.html.twig', [
  243.                 'blogs' => $paginate,
  244.                 'pagination' => $paginate->getPaginationData(),
  245.                 'categories' => $this->getBlogCatogeries(),
  246.                 'queryParms' => $queryParms
  247.             ]);
  248.         } else {
  249.             return $this->render('Blog/insights_search.html.twig', [
  250.                 'blogs' => null,
  251.                 'pagination' => null,
  252.                 'categories' => $this->getBlogCatogeries(),
  253.                 'queryParms' => $queryParms
  254.             ]);
  255.         }
  256.     }
  257. }