<?php
namespace App\Controller\Products;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Pimcore\Localization\LocaleServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Service;
use Pimcore\Model\DataObject\Category;
use Pimcore\Model\DataObject\Products;
use Pimcore\Model\DataObject\Brands;
use App\Model\Product\AbstractProduct;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Model\DataObject\Userloyaltyprogram;
use Pimcore\Model\DataObject\Slider;
use Pimcore\Model\DataObject\Recipe;
use Symfony\Contracts\Translation\TranslatorInterface;
use Pimcore\Model\DataObject\FilterDefinition;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
use Pimcore\Config;
use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
use Knp\Component\Pager\PaginatorInterface;
class categoryController extends FrontendController
{
/**
* @Route("/{locale}/products/{url?}", name="products")
*/
public function categoryProductAction(Request $request , TranslatorInterface $translator,Factory $ecommerceFactory,ListHelper $listHelper, PaginatorInterface $paginator){
$get_param = $request->query->all();
$params = array_merge($request->query->all(), $request->attributes->all());
$category_ids = isset($params["categories"])?$params["categories"]:"";
$brand_ids = isset($params["brands"])?$params["brands"]:"";
$subCategoryIds = isset($params["subCategory"])?$params["subCategory"]:"";
$query = isset($params["query"])?$params["query"]:"";
$cuisine_id = isset($params["cuisine_id"])?$params["cuisine_id"]:"";
$meal_type = isset($params["meal_type"])?$params["meal_type"]:"";
$childList = array();
$brand = null;
$url = $request->get('url');
if(!empty($request->get('url')) && empty($get_param)){
$brand = (string)$this->getBrandId($request->get('url'));
}
$catId = null;
if(!empty($request->get('url')) && empty($get_param)){
$catId = (string)$this->getCategoryId($request->get('url'));
}
$indexService = $ecommerceFactory->getIndexService();
$productListing = $indexService->getProductListForCurrentTenant();
$productListing->addCondition("o_parentId != 74");
if($query){
$productListing->addCondition("name LIKE '%{$query}%'");
}
if(!empty($request->get('url')) && !empty($brand)){
$brand_ids = explode(",",$brand);
$params["brands"] = $brand_ids;
}
if(!empty($request->get('url')) && !empty($catId)){
$category_ids = explode(",",$catId);
$params["categories"] = $category_ids;
}
$productListing->setOrderKey('name');
$productListing->setOrder('asc');
$page = $request->get('page');
if($page){
$page = $page+1;
}else{
$page = 1;
}
if ($request->get('filterdefinition') instanceof FilterDefinition) {
$filterDefinition = $request->get('filterdefinition');
}
if (empty($filterDefinition)) {
$filterDefinition = Config::getWebsiteConfig()->get('fallbackFilterdefinition');
}
$filterService = $ecommerceFactory->getFilterService();
(new \Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper)->setupProductList($filterDefinition, $productListing, $params, $filterService, true, true);
$paginator = $paginator->paginate(
$productListing,
$page,
24
);
// p_r();
foreach ($paginator as $product) {
if($product->getChildren() ){
$parent_id = $product->getParentId();
if($parent_id > 74){
$childList[] = $product->getChildren();
}
}else{
$childList[] = $product;
}
}
$categories = $this->getCategories();
$brands = $this->getBrands();
// filters
// dd($brand_ids);
$filters = array(
'category_ids' => $category_ids,
'brand_ids' => $brand_ids,
'subCategory' => $subCategoryIds,
'query' => $query,
'cuisine_id' => $cuisine_id,
'meal_type' => $meal_type,
);
$currentFilter = [
'categoryIds' => $category_ids,
"categories" => $category_ids,
'subCategory' => $subCategoryIds,
'brands' => $brand_ids,
'meal_type' => $meal_type,
];
// Category Sliders
$category_slider = new Slider\Listing();
$category_slider->setCondition("o_id = 205");
$category_slider->load();
if(empty($category_slider->getdata()[0]->getsliders())){
$category_slider = array();
}else{
$category_slider = $category_slider->getdata()[0]->getsliders();
}
if(!empty($request->get('url'))){
$getCategoryData = $this->getCategoryDescription($request->get('url'));
$getBrandData = $this->getBrandDescription($request->get('url'));
if(!empty($getCategoryData)){
$products_head = array(
'id' => $getCategoryData->getId(),
'name' => $getCategoryData->getName(),
'description' => $getCategoryData->getDescription(),
'Detail_desc' => $getCategoryData->getDetail_desc(),
'breadcrumb' => $getCategoryData->getName(),
'slides' => $getCategoryData->getCategory_slides(),
'brand_logo' => '',
'meta_title' => $getCategoryData->getMeta_title(),
'meta_description' => $getCategoryData->getMeta_description(),
);
}else if(!empty($getBrandData)){
$products_head = array(
'id' => $getBrandData->getId(),
'name' => $getBrandData->getName(),
'description' => $getBrandData->getDescription(),
'breadcrumb' => $getBrandData->getName(),
'slides' => $getBrandData->getCategory_slides(),
'brand_logo' => $getBrandData->getImage(),
'meta_title' => $getBrandData->getMeta_title(),
'meta_description' => $getBrandData->getMeta_description(),
);
}
else{
$products_head = array();
}
}else{
$products_head = array();
}
// Category Mid Banner
$mid_banner = new Slider\Listing();
$mid_banner->setCondition("o_id = 206");
$mid_banner->load();
if(empty($mid_banner->getdata()[0]->getsliders())){
$mid_banner = array();
}else{
$mid_banner = $mid_banner->getdata()[0]->getsliders();
}
// dd($childList);
if ($request->isXmlHttpRequest()) {
return $this->loadMoreProduct($childList, $request ,$translator);
}
// Featured Recipes
$featured_recipes = new Recipe\Listing();
$featured_recipes->setCondition('featured IN (?)', '1');
$featured_recipes->setLimit('10');
$featured_recipes->load();
//dd($childList);
$ObjectIds = [];
foreach($childList as $ChildList){
$ObjectIds[] = $ChildList->getId();
}
// dd(count($ObjectIds));
// echo $ObjectIds;
$uid = $this->get('session')->get('loginUID');
//$rid = $recipedata->getId();
$Liked = new Userloyaltyprogram\Listing();
//for($i=0; $i<count($ObjectIds); $i++){
$Liked->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_data__id IN (?)",['Like',$uid,'Product',$ObjectIds]);
//}
$Liked->load();
$LikedId = [];
foreach($Liked as $liked){
$LikedId[]=$liked->getObj_id();
}
return $this->render('Products/category.html.twig', [
'products' => $childList,
'categories' => $categories,
'brands' => $brands,
'filters' => $filters,
'category_slider' => $category_slider,
'featured_recipes' => $featured_recipes,
'mid_banner' => $mid_banner,
'products_head' => $products_head,
'Liked' => $Liked,
'LikedId' => $LikedId,
'filterService'=> $filterService,
'filterDefinition'=> $filterDefinition,
'productListing'=> $productListing,
'currentFilter' => $currentFilter
]);
}
public function getCategories($category_id = 0){
$categories = new Category\Listing();
$categories->load();
return $categories;
}
public function getCategoryId($url = ''){
$categories = new Category\Listing();
$categories->setCondition("url IN (?)", [[$url]]);
$categories->load();
foreach($categories as $category){
$category_id = $category->getId();
break;
}
if(isset($category_id)){
return $category_id;
}
return '';
}
public function getBrands($brand_id = 0){
$brands = new Brands\Listing();
$brands->load();
return $brands;
}
public function getBrandId($url = ''){
$brands = new Brands\Listing();
$brands->setCondition("url IN (?)", [[$url]]);
$brands->load();
foreach($brands as $brand){
$brand_id = $brand->getId();
break;
}
if(isset($brand_id)){
return $brand_id;
}
return '';
}
// Get Category Description
public function getCategoryDescription($url = ''){
$categories = new Category\Listing();
$categories->setCondition("url IN (?)", [[$url]]);
$categories->load();
foreach($categories as $category_data){
break;
}
if(isset($category_data)){
return $category_data;
}
return '';
}
// Get Brands
public function getBrandDescription($url = ''){
$brands = new Brands\Listing();
$brands->setCondition("url IN (?)", [[$url]]);
$brands->load();
foreach($brands as $brand){
break;
}
if(isset($brand)){
return $brand;
}
return '';
}
/**
* @Route("products/{}/loadmore", name="loadmore")
*/
public function loadMoreProduct($childList, $request ,$translator){
$more_product = '';
if(!empty($childList)){
$total_products = sizeof($childList);
}else{
$total_products = '';
}
foreach($childList as $product){
$uid = $this->get('session')->get('loginUID');
$LikedId = [];
if($uid){
$Liked = new Userloyaltyprogram\Listing();
$Liked->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_id IN (?)",['Like',$uid,'Product',$product->getId()]);
$Liked->load();
foreach($Liked as $liked){
$LikedId[]=$liked->getObj_id();
}
}
if($product->getBrands()->getId()){
if($product->getBrands()->getId() == 263 ){
$class = "slide-img-bg-brown";
}elseif($product->getBrands()->getId() == 262){
$class = "slide-img-bg-blue";
}elseif($product->getBrands()->getId() == 413){
$class = "slide-img-bg-purple";
}elseif($product->getBrands()->getId() == 260){
$class = "slide-img-bg-green";
}else{
$class = "slide-img-bg-green";
}
}
$more_product .= '<div class="col-12 col-md-6 col-lg-4 product-result">';
$more_product .= $this->renderView('/includes/product-card.html.twig', [
'featured_product' => $product,
'class' => $class,
'LikedId' => $LikedId
]);
$more_product .= '</div>';
}
return $this->json([
'success' => $more_product,
'total_products' => $total_products
]);
}
/**
* @Route("/{locale}/brand", name="brand")
*/
public function redirectBrandAction(Request $request)
{
$weblink = $request->get('locale');
return $this->redirect('/'.$weblink.'/products/',301);
}
/**
* @Route("/{locale}/product", name="product")
*/
public function redirectProductDetailAction(Request $request)
{
$weblink = $request->get('locale');
return $this->redirect('/'.$weblink.'/products/',301);
}
// /**
// * @Route("/{locale}/products", name="productslash")
// */
// public function redirectProductslashRemove(Request $request)
// {
// $weblink = $request->get('locale');
// return $this->json(['name'=>'name']);
// }
/**
* @Route("/{locale}/products2/{url?}", name="products2")
*/
public function categoryProduct2Action(Request $request , TranslatorInterface $translator){
$products = new Products\Listing();
$products->load();
$childList = array();
foreach ($products as $product) {
if($product->getChildren()){
$parent_id = $product->getParentId();
if($parent_id > 74){
$childList[] = $product->getChildren();
}
}else{
$childList[] = $product;
}
}
$categories = $this->getCategories();
$brands = $this->getBrands();
// Category Sliders
$category_slider = new Slider\Listing();
$category_slider->setCondition("o_id = 205");
$category_slider->load();
if(empty($category_slider->getdata()[0]->getsliders())){
$category_slider = array();
}else{
$category_slider = $category_slider->getdata()[0]->getsliders();
}
if(!empty($request->get('url'))){
$getCategoryData = $this->getCategoryDescription($request->get('url'));
$getBrandData = $this->getBrandDescription($request->get('url'));
if(!empty($getCategoryData)){
$products_head = array(
'id' => $getCategoryData->getId(),
'name' => $getCategoryData->getName(),
'description' => $getCategoryData->getDescription(),
'Detail_desc' => $getCategoryData->getDetail_desc(),
'breadcrumb' => $getCategoryData->getName(),
'slides' => $getCategoryData->getCategory_slides(),
'brand_logo' => '',
'meta_title' => $getCategoryData->getMeta_title(),
'meta_description' => $getCategoryData->getMeta_description(),
);
}else if(!empty($getBrandData)){
$products_head = array(
'id' => $getBrandData->getId(),
'name' => $getBrandData->getName(),
'description' => $getBrandData->getDescription(),
'breadcrumb' => $getBrandData->getName(),
'slides' => $getBrandData->getCategory_slides(),
'brand_logo' => $getBrandData->getImage(),
'meta_title' => $getBrandData->getMeta_title(),
'meta_description' => $getBrandData->getMeta_description(),
);
}
else{
$products_head = array();
}
}else{
$products_head = array();
}
// Category Mid Banner
$mid_banner = new Slider\Listing();
$mid_banner->setCondition("o_id = 206");
$mid_banner->load();
if(empty($mid_banner->getdata()[0]->getsliders())){
$mid_banner = array();
}else{
$mid_banner = $mid_banner->getdata()[0]->getsliders();
}
// Featured Recipes
$featured_recipes = new Recipe\Listing();
$featured_recipes->setCondition('featured IN (?)', '1');
$featured_recipes->setLimit('10');
$featured_recipes->load();
//dd($childList);
// dd(count($ObjectIds));
// echo $ObjectIds;
$uid = $this->get('session')->get('loginUID');
//$rid = $recipedata->getId();
$Liked = new Userloyaltyprogram\Listing();
//for($i=0; $i<count($ObjectIds); $i++){
// $Liked->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_data__id IN (?)",['Like',$uid,'Product',$ObjectIds]);
//}
$Liked->load();
$LikedId = [];
foreach($Liked as $liked){
$LikedId[]=$liked->getObj_id();
}
//dd($LikedId);
return $this->render('Products/category2.html.twig', [
'products' => $childList,
'categories' => $categories,
'brands' => $brands,
'category_slider' => $category_slider,
'featured_recipes' => $featured_recipes,
'mid_banner' => $mid_banner,
'products_head' => $products_head,
'Liked' => $Liked,
'LikedId' => $LikedId,
]);
}
}
?>