<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Controller;
use Carbon\Carbon;
use App\Website\Tool\Text;
use Pimcore\Model\DataObject;
// use Pimcore\Twig\Extension\Templating\HeadTitle;
// use Pimcore\Twig\Extension\Templating\Placeholder;
use Pimcore\Model\DataObject\Blogs;
use Knp\Component\Pager\PaginatorInterface;
use Pimcore\Model\DataObject\Blogcategories;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class BlogController extends BaseController
{
/**
* Insights Listing Function
*
* @param Request $request
* @param PaginatorInterface $paginator
* @return void
*/
public function blogAction(Request $request, PaginatorInterface $paginator)
{
$data = [];
$blogCategories = new DataObject\Blogcategories\Listing();
$blogCategories->setOrderKey('name');
$blogCategories->setOrder('asc');
$centricCategoryData = null; // Initialize the variable to store "Centric" category data
foreach ($blogCategories as $blogCategory) {
if (!$blogCategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
throw new NotFoundHttpException('Blog category not found.');
}
$blogs = new DataObject\Blogs\Listing();
$blogs->setCondition('category__id IN (?)', [$blogCategory->getId()])
->setOrderKey('date')
->setOrder('desc')
->setLimit(6);
$categoryData = [
'category' => $blogCategory,
'blogData' => [
'blogs' => $blogs
]
];
if ($blogCategory->getUrl() === 'business-insights') {
// If the category name is "Centric," store the data separately
$centricCategoryData = $categoryData;
} else {
// For other categories, add them to the regular data array
$data[] = $categoryData;
}
}
if ($centricCategoryData !== null) {
// If there's "Centric" category data, add it at index 0
array_unshift($data, $centricCategoryData);
}
$page = ($request->get('page')) ? $request->get('page') : 1;
$page = (int)$page;
$pagination = $paginator->paginate(
$data,
$page,
20 // Number of items per page
);
return $this->render('Blog/blog_listing.html.twig', [
'data' => $pagination,
'pagination' => $pagination->getPaginationData(),
'categories' => $this->getBlogCatogeries(),
]);
}
/**
* @Route("/insights/{category}", name="insights_category")
*/
public function blogCategoryAction(Request $request, PaginatorInterface $paginator)
{
$blogcategory = DataObject\Blogcategories::getByUrl($request->get('category'), true);
if (!$blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
throw new NotFoundHttpException('Blog category not found.');
}
//get parm from request
$limit = 6;
$page = ($request->get('page')) ? $request->get('page') : 1;
$page = (int)$page;
// Load blog listing
$categoryId = $blogcategory->getId();
$blogs = new DataObject\Blogs\Listing();
$blogs->setCondition('category__id IN (?)', [$blogcategory->getId()])
->setOrderKey('date')
->setOrder('desc');
$paginate = $paginator->paginate(
$blogs,
$page,
$limit
);
return $this->render('Blog/blog_category.html.twig', [
'blogs' => $paginate,
'pagination' => $paginate->getPaginationData(),
'categories' => $this->getBlogCatogeries(),
'blogcategory' => $blogcategory
]);
}
/**
* @Route("/insights/{category}/{url}", name="insights_category_detail")
*/
public function blogDetailAction(Request $request)
{
$blogcategory = DataObject\Blogcategories::getByUrl($request->get('category'), true);
if (!$blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
throw new NotFoundHttpException('Blog category not found.');
}
$blog = DataObject\Blogs::getByUrl($request->get('url'), true);
if (!$blog instanceof \Pimcore\Model\DataObject\Blogs) {
throw new NotFoundHttpException('Blog not found.');
}
$blogPosts = new Blogs\Listing();
$blogPosts->setLimit(3);
$blogPosts->setOrderKey('date');
$blogPosts->setOrder('desc');
$blogPosts->setCondition('o_id != ? and category__id IN (?)', [$blog->getId(), $blogcategory->getId()]);
if ($blog->getcategory() && $blog->getcategory()->getId() == $blogcategory->getId()) {
return $this->render(
'Blog/blogDetail.html.twig',
[
'blog' => $blog,
'blogPosts' => $blogPosts,
'categories' => $this->getBlogCatogeries()
]
);
}
throw new NotFoundHttpException('Blog not found.');
}
/**
*@Route("/blog-migration", name="blog-migration")
*/
public function importBlogsAction()
{
$csvFile = PIMCORE_PROJECT_ROOT . '/public/all_csvs/AdobeExperienceManager-articles.csv'; // Replace with the actual path to your CSV file
$counter = 1;
if (($handle = fopen($csvFile, "r")) !== false) {
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
if ($counter > 1) {
dd("stop");
$catId = 448;
$blogcategory = DataObject\Blogcategories::getById($catId, true);
if (!$blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
throw new NotFoundHttpException('Blog category not found.');
}
$url = $this->titleToUrl($data[0]);
$dublicateBlog = DataObject\Blogs::getByUrl($url, true);
if (!$dublicateBlog instanceof \Pimcore\Model\DataObject\Blogs) {
//throw new NotFoundHttpException('Blog category not found.');
$blog = new Blogs();
$blog->setParent(\Pimcore\Model\DataObject\Service::createFolderByPath("/insights/" . $blogcategory->getName()));
$blog->setKey($url);
$blog->setUrl($url);
$blog->setTitle($data[0]);
$blog->setContent($data[1]);
$blog->setCategory($blogcategory);
$currentDate = date("d-m-Y"); // Format: Year-Month-Day (e.g., 2023-08-26)
$carbonDate = Carbon::parse($currentDate);
$blog->setDate($carbonDate);
$blog->setPostedBy('Usman Khalid');
$blog->setSeoTitle($data[2]);
$blog->setSeoDescription($data[3]);
$blog->setPublished(true);
$blog->Save();
}
//update existing blog
}
$counter++;
}
fclose($handle);
}
return new Response("Blogs imported successfully!");
}
function titleToUrl($title)
{
// Convert to lowercase
$url = strtolower($title);
// Replace spaces with dashes
$url = str_replace(' ', '-', $url);
// Remove special characters except dashes and alphanumeric characters
$url = preg_replace('/[^a-z0-9\-]/', '', $url);
// Remove consecutive dashes
$url = preg_replace('/-+/', '-', $url);
// Trim dashes from the beginning and end
$url = trim($url, '-');
return $url;
}
function getBlogCatogeries()
{
$blogPosts = new Blogcategories\Listing();
$blogPosts->setOrderKey('o_Creationdate');
$blogPosts->setOrder('desc');
$blogPosts->Load();
return $blogPosts;
}
/**
* @Route("/insights-search", name="insights_search")
*/
public function insightsSearchAction(Request $request, PaginatorInterface $paginator)
{
$name = strip_tags($request->get('name'));
$category = strip_tags($request->get('category'));
$queryParms = "Query = " . $name . ',' . $category;
$blogs = new DataObject\Blogs\Listing();
if (!empty($category) || !empty($name)) {
if ($category != "all") {
$blogcategory = DataObject\Blogcategories::getByUrl($category, true);
if ($blogcategory instanceof \Pimcore\Model\DataObject\Blogcategories) {
$blogs->addConditionParam('category__id IN (?)', [$blogcategory->getId()]);
}
} else {
$blogs->addConditionParam('category__id != ?', ['']);
}
if ($name) {
$blogs->setCondition("title LIKE ? and category__id != ?", ["%" . $name . "%", '']);
}
$blogs->setOrderKey('o_Creationdate');
$blogs->setOrder('desc');
$limit = 6;
$page = ($request->get('page')) ? $request->get('page') : 1;
$page = (int)$page;
$paginate = $paginator->paginate(
$blogs,
$page,
$limit
);
return $this->render('Blog/insights_search.html.twig', [
'blogs' => $paginate,
'pagination' => $paginate->getPaginationData(),
'categories' => $this->getBlogCatogeries(),
'queryParms' => $queryParms
]);
} else {
return $this->render('Blog/insights_search.html.twig', [
'blogs' => null,
'pagination' => null,
'categories' => $this->getBlogCatogeries(),
'queryParms' => $queryParms
]);
}
}
}