src/OfficeBrain/Bundle/SearchBundle/Controller/BrowseCategoryController.php line 119

Open in your IDE?
  1. <?php
  2. namespace App\OfficeBrain\Bundle\SearchBundle\Controller;
  3. // use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Nelmio\ApiDocBundle\Annotation\ApiDoc;
  11. use App\OfficeBrain\Bundle\SearchBundle\Form\Type\ProductSearchType;
  12. use App\OfficeBrain\Bundle\SearchBundle\Form\Data\ProductSearchData;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\DependencyInjection\ContainerInterface as Container;
  16. use Doctrine\ORM\EntityManagerInterface;
  17. use App\OfficeBrain\Bundle\SearchBundle\Manager\Service\SearchManager;
  18. use App\OfficeBrain\Bundle\ProductBundle\Manager\Service\ProductDetailService;
  19. use App\project\OfficeBrain\CustomBundle\ProductBundle\Manager\Service\ProductDetailService as CustomProductDetailService;
  20. use App\OfficeBrain\Bundle\CategoryBundle\Manager\ApiManager as CategoryApiManager;
  21. use App\OfficeBrain\Bundle\CoreBundle\Service\SessionManager;
  22. use Knp\Component\Pager\PaginatorInterface;
  23. use App\OfficeBrain\Bundle\SearchBundle\Manager\Extended\SolrSearchExtended;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use App\OfficeBrain\Bundle\SearchBundle\Manager\Service\BrowseCategoryManager;
  26. use App\OfficeBrain\Bundle\ProductBundle\ApiClient\ProductApiClient;
  27. use App\OfficeBrain\Bundle\CategoryBundle\Manager\ApiManager;
  28. class BrowseCategoryController extends AbstractController
  29. {
  30.     /**
  31.      *
  32.      * @author Employee Id: 4532
  33.      * 
  34.      * To set member variables default value
  35.      * 
  36.      * @return NULL
  37.      */
  38.     public function __construct(EntityManagerInterface $entityManagerRequestStack $requestSearchManager $solrManagerApiManager $categoryManagerSessionManager $sessionManagerPaginatorInterface $paginatorManagerContainer $serviceContainerBrowseCategoryManager $browserCategoryManagerProductDetailService $productDetailServiceCustomProductDetailService $customProductDetailManager)
  39.     {
  40.         $this->form null;
  41.         $this->categoryBreadCrumbsList = array();
  42.         $this->translator null;
  43.         $this->request null;
  44.         $this->container $serviceContainer;
  45.         $this->serviceContainer $serviceContainer;
  46.         $this->browserCategoryManager $browserCategoryManager->getBrowserCategoryExtended();
  47.         // $this->searchManager = $this->container->get("office_brain_search_manager_extended");
  48.         $this->searchManager $solrManager->getSolrSearchExtended();
  49.         $this->productDetailManager $productDetailService->getProductApiExtended();
  50.         $this->customProductDetailManager $customProductDetailManager->getProductApiExtended();
  51.         // $this->productDetailManager = $this->container->get('office_brain_product_api_extended');
  52.     }
  53.     
  54.     /**
  55.      *
  56.      * @author Employee Id: 4861
  57.      *
  58.      * Desciption: To unset member variables
  59.      *
  60.      * @param NULL
  61.      *
  62.      * @return null
  63.      *
  64.      * @throws NULL
  65.      */
  66.     public function __destruct()
  67.     {
  68.         unset($this->form);
  69.         unset($this->categoryBreadCrumbsList);
  70.         unset($this->translator);
  71.         unset($this->request);
  72.     }
  73.     
  74.     
  75.     /**
  76.      *
  77.      * @author Employee Id: 4532
  78.      *
  79.      * Create various objects required action in this class
  80.      *
  81.      * @return NULL
  82.      */
  83.     public function initAction(Request $request)
  84.     {
  85.         $this->request $request;
  86.         $this->entityManager $this->getDoctrine()->getManager();
  87.         $this->projectSetting $this->request->get('project_setting');
  88.         $this->instanceId $this->projectSetting['instance_id'];
  89.         $this->cultureCode $this->projectSetting['culture_code'];
  90.         // $this->searchManager = $this->get('office_brain_search_manager'); // symfony 4
  91.         $this->searchManager $this->searchManager;
  92.         $this->languageId $this->getLanguageId(); // symfony 4
  93.         $this->productSearchForm ProductSearchType::class;
  94.         $this->translator $this->serviceContainer->get('translator');
  95.         // $this->browserCategoryManager = $this->serviceContainer->get('office_brain_browse_category_extended');
  96.         $this->browserCategoryManager $this->browserCategoryManager;
  97.         // $this->productDetailManager = $this->serviceContainer->get('office_brain_product_api_extended');
  98.         $this->productDetailManager $this->productDetailManager;
  99.         // $this->customProductDetailManager = $this->serviceContainer->get('office_brain_custom_bundle_product_api_extended');
  100.         $this->customProductDetailManager $this->customProductDetailManager;
  101.     }
  102.     
  103.     /**
  104.      * @author Employee Id: 4532
  105.      * 
  106.      * Browse by category
  107.      * 
  108.      * @param string category slug
  109.      * 
  110.      * @return if result found return parent category  else null
  111.      * 
  112.      * @Route("/{_locale}/browse-category", name="office_brain_search_bundle_browse_category", options={"expose"=true}, methods={"GET"})
  113.      * 
  114.      * @Template("OfficeBrain/Bundle/SearchBundle/BrowseCategory/browseCategory.html.twig")
  115.      */
  116.     public function browseCategoryAction(Request $request)
  117.     {
  118.         $this->initAction($request);
  119.         $this->responseArray['parent_category_list'] = $this->searchManager->getCategorySubCategoryList();
  120.         $slug $request->query->get('category_slug');
  121.         // Check if the slug is provided
  122.         if ($slug) {
  123.             $filteredCategory null;
  124.             // Filter the category list based on the slug
  125.             foreach ($this->responseArray['parent_category_list'] as $key => $category) {
  126.                 if (isset($category['slug']) && $category['slug'] === $slug) {
  127.                     $filteredCategory[] = $category;
  128.                     break; // Exit the loop once the match is found
  129.                 }
  130.             }
  131.             // If a matching category is found, return it as a JSON response
  132.             if ($filteredCategory) {
  133.                 // echo "<pre>"; dump($filteredCategory);
  134.                 $this->responseArray['parent_category_list'] = $filteredCategory;
  135.                 // echo "<pre>"; dump($this->responseArray['parent_category_list']); exit;
  136.                 return $this->responseArray;
  137.             }
  138.             // If no matching category is found, return a 404 response
  139.             return new JsonResponse(['error' => 'Category not found'], 404);
  140.         }
  141.         if ($this->request->isXmlHttpRequest ()) {
  142.             if (is_array $this->responseArray['parent_category_list'] ) && array_key_exists 'data'$this->responseArray['parent_category_list'] ) && ! count $this->responseArray['parent_category_list'] )) {
  143.                 return new JsonResponse ( array('error' => 'END_OF_PAGINATION' ) );
  144.             }
  145.             return $this->responseArray;
  146.         }
  147.         return $this->responseArray;
  148.     }
  149.     
  150.     
  151.     /**
  152.      * @author Employee Id: 4532
  153.      * 
  154.      * Browse by sub category
  155.      * 
  156.      * @param string category slug
  157.      * 
  158.      * @return if result found return sub category list  else null
  159.      * 
  160.      * @Route("/{_locale}/browse-sub-category/{category_slug}", name="office_brain_search_bundle_browse_sub_category", options={"expose"=true}, methods={"GET"})
  161.      * 
  162.      * @Template("OfficeBrainSearchBundle:BrowseCategory:browseSubCategory.html.twig")
  163.      */
  164.     public function browseSubCategoryAction(Request $request)
  165.     {
  166.         $this->initAction($request);
  167.         
  168.         // $categoryObj = $this->serviceContainer->get('office_brain_category_manager');
  169.         $categoryObj $this->browserCategoryManager;
  170.         $categorySlug $this->request->get('category_slug');
  171.         $this->responseArray['category_id'] = $categoryObj->getCategoryIdFromSlug($categorySlug);
  172.         
  173.         $subCategoryList $categoryObj->getSubCategoryList($this->responseArray['category_id']);
  174.         if(isset($subCategoryList['child'])){    
  175.             $this->responseArray['sub_category_list'] = $subCategoryList['child'];
  176.         }else{
  177.             $this->responseArray['sub_category_list'] = null;
  178.         }
  179.         if(isset($subCategoryList['name'])){
  180.             $this->responseArray['category_name'] = $subCategoryList['name'];
  181.         }else{
  182.             $this->responseArray['category_name'] = '';
  183.         }
  184.         if(isset($subCategoryList['parent'])){
  185.             $this->responseArray['parent_category_info'] = $subCategoryList['parent'];
  186.         }
  187.         if ($this->request->isXmlHttpRequest ()) {
  188.             if (is_array $this->responseArray['sub_category_list'] ) && array_key_exists 'data'$this->responseArray['sub_category_list'] ) && ! count $this->responseArray['sub_category_list'] )) {
  189.                 return new JsonResponse ( array( 'error' => 'END_OF_PAGINATION' ) );
  190.             }
  191.             return $this->responseArray;
  192.         }
  193.         return $this->responseArray;
  194.     }
  195.     
  196.     /**
  197.      * @author Employee Id: 4532
  198.      *
  199.      * Browse by Parent category
  200.      *
  201.      * @param string category slug
  202.      *
  203.      * @return if result found return product related list of parent categories  else null
  204.      *
  205.      * @Route("/{_locale}/browse-category-search/{category_slug}", name="office_brain_search_bundle_browse_category_search", options={"expose"=true}, methods={"GET"})
  206.      *
  207.      * @Template("OfficeBrainSearchBundle:BrowseCategory:browseCategorySearchPage.html.twig")
  208.      */
  209.     public function browseCategorySearchAction(Request $request)
  210.     {
  211.         $this->initAction($request);
  212.          
  213.         // $categoryObj = $this->serviceContainer->get('office_brain_category_manager');
  214.         $categoryObj $this->browserCategoryManager;
  215.         $categorySlug $this->request->get('category_slug');
  216.         $categoryid $categoryObj->getCategoryIdFromSlug($categorySlug);
  217.         $subCategoryList $categoryObj->getSubCategoryList($categoryid);
  218.         $this->session $this->request->getSession();
  219.          
  220.         if(isset($subCategoryList['child'])){
  221.            
  222.             $categoriesList json_encode(array_keys($subCategoryList['child']));
  223.         }else{
  224.             $categoriesList null;
  225.         }
  226.         if($this->session->get("brandId"))
  227.         {
  228.             $this->request->query->set("main_filter",$this->session->get("brandId"));
  229.         }
  230.         //echo $this->request->get("main_filter")." - ".$this->session->get("brandId");
  231.         //echo "<pre>"; print_r($this->responseArray['category_name']);exit;
  232.         //echo "<pre>"; print_r($this->responseArray['sub_category_list']);exit;
  233.         $searchQuery $this->searchManager->getSearchArray();
  234.     //    echo "<pre>";print_r($searchQuery['product_search']);exit;
  235.         $this->prepareSearchForm($searchQuery['product_search'],$categoriesList);
  236.         if ($this->form->isValid()) {
  237.             $this->responseArray $this->searchManager->searchResult($this->form->getData());
  238.             if (!$this->request->get('page'null)) {
  239.                 $this->responseArray['search_attribute_filter_array'] = $this->searchManager->searchFilter($this->form->getData());
  240.             }
  241.         }
  242.         $this->responseArray['sub_category_list'] = $categoriesList;
  243.         $this->responseArray['category_id'] = $categoryid;
  244.         if(isset($subCategoryList['name'])){
  245.             $this->responseArray['category_name'] = $subCategoryList['name'];
  246.         }else{
  247.             $this->responseArray['category_name'] = '';
  248.         }
  249.         
  250.         if(isset($subCategoryList['parent'])){
  251.             $this->responseArray['parent_category_info'] = $subCategoryList['parent'];
  252.         }
  253.         $this->responseArray['breadcrumbs'] = $this->productDetailManager->productDetailBreadcrumbs($this->responseArray['category_id']);
  254.         $this->responseArray['search_query'] = $searchQuery['search']['query'];
  255.         $this->responseArray['main_filter'] = $searchQuery['search']['main_filter'];
  256.         $this->responseArray['form'] = $this->form->createView();
  257.         $this->responseArray['Recent_search'] = $this->request->getSession()->get('recent_search');
  258.         $this->responseArray['save_search'] = $this->searchManager->getSaveSearch();
  259.         $recentSearch $this->request->query->get('is_filter','check');
  260.         if($recentSearch == 'check'){
  261.             $this->searchManager->setRecentSearch();
  262.         }
  263.         
  264.         $this->responseArray['category_breadcrumb']= array($this->translator->trans('breadcrumb_category'),(isset($this->responseArray['category_name'])?$this->responseArray['category_name']:''));
  265.         $this->responseArray['category_slug'] = $categorySlug;
  266.         
  267.         unset($categoryObj);
  268.         unset($categorySlug);
  269.         unset($subCategoryList);
  270.         unset($categoriesList);
  271.         unset($searchQuery);
  272.         unset($recentSearch);
  273.         return $this->responseArray;
  274. // //         if ($this->request->isXmlHttpRequest ()) {
  275. // //             if (is_array ( $this->responseArray['sub_category_list'] ) && array_key_exists ( 'data', $this->responseArray['sub_category_list'] ) && ! count ( $this->responseArray['sub_category_list'] )) {
  276. // //                 return new JsonResponse ( array( 'error' => 'END_OF_PAGINATION' ) );
  277. // //             }
  278. // //             return $this->responseArray;
  279. // //         }
  280.         
  281.     }
  282.     public function create_slug($string)
  283.     {
  284.            $slug=str_replace('-',' ',$string);
  285.            return $slug;
  286.     }
  287.     
  288.     /**
  289.      * @author Employee Id: 4532
  290.      * 
  291.      * Product listing accordingly parent category
  292.      * 
  293.      * @param string category id
  294.      * 
  295.      * @return if result found return category products else null
  296.      * 
  297.      * @Route("/{_locale}/category-product-list/{category_slug}", name="office_brain_search_bundle_category_product_list", options={"expose"=true}, methods={"GET"})
  298.      *
  299.      * @Template("OfficeBrainSearchBundle:BrowseCategory:browseCategoryPage.html.twig")
  300.      */
  301.     public function categoryProductListAction(Request $request)
  302.     {
  303.         $this->initAction($request);
  304.         $searchQuery $this->searchManager->getSearchArray();
  305.         $categoryFilter $this->request->get('category_filter','');
  306.         $categoryslug $this->request->get('category_slug');
  307.         
  308.         // $categoryObj = $this->serviceContainer->get('office_brain_category_manager');
  309.         $categoryObj $this->browserCategoryManager;
  310.         $categoryId $categoryObj->getCategoryIdFromSlug($categoryslug);
  311.         $subCategoryList $categoryObj->getSubCategoryList($categoryId);
  312.         /* if($categoryFilter == ''){
  313.             $caregoryArray[] = $categoryId;
  314.             $categoryListJson = json_encode($caregoryArray);
  315.         }else{
  316.             $categoryListJson = $categoryFilter;
  317.         } */
  318.         if($categoryFilter == ''){
  319.             if( isset($subCategoryList['child']) ) {
  320.                 $categoryListJson json_encode(array_keys($subCategoryList['child']));
  321.             } else {
  322.                 $caregoryArray[] = $categoryId;
  323.                 $categoryListJson json_encode($caregoryArray);
  324.             }
  325.         } else {
  326.             $categoryListJson $categoryFilter;
  327.         }
  328.         $this->prepareSearchForm($searchQuery['product_search'],$categoryListJson);
  329.         $this->responseArray = array();
  330.         if ($this->form->isValid()) {
  331.             $this->responseArray $this->searchManager->searchResult($this->form->getData());
  332.             if (!$this->request->get('page'null)) {
  333.                 $this->responseArray['search_attribute_filter_array'] = $this->searchManager->searchFilter($this->form->getData());
  334.             }
  335.         }
  336.         $this->responseArray['breadcrumbs'] = $this->productDetailManager->productDetailBreadcrumbs($categoryId);
  337.         $this->responseArray['search_query'] = $searchQuery['search']['query'];
  338.         $this->responseArray['main_filter'] = $searchQuery['search']['main_filter'];
  339.         $this->responseArray['form'] = $this->form->createView();
  340.         $this->responseArray['Recent_search'] = $this->request->getSession()->get('recent_search');
  341.         $this->responseArray['save_search'] = $this->searchManager->getSaveSearch();
  342.         $recentSearch $this->request->query->get('is_filter','check');
  343.         if($recentSearch == 'check'){
  344.             $this->searchManager->setRecentSearch();
  345.         }
  346.         $this->responseArray['category_breadcrumb']= array($this->translator->trans('breadcrumb_category'),(isset($subCategoryList['name'])?$subCategoryList['name']:''));
  347.         $this->responseArray['category_slug'] = $categoryslug;
  348.         $this->responseArray['category_id'] = $categoryId;
  349.         return $this->responseArray;
  350.     }
  351.     
  352.     /**
  353.      * @author Employee Id: 4532
  354.      * 
  355.      * Perform search to find attribute list for given search criteria
  356.      * 
  357.      * @param string search_query
  358.      * 
  359.      * @return if result found return attribute list along with product count else null
  360.      * 
  361.      * @Route("/{_locale}/category-attribute-filter-list",
  362.      * 
  363.      * name="office_brain_search_bundle_category_attribute_filter_list", options={"expose"=true}, methods={"GET", "POST"})
  364.      *
  365.      * @Template("OfficeBrainSearchBundle:Search:searchAttributeFilterList.html.twig")
  366.      */
  367.     public function categoryAttributeFilterListAction(Request $request)
  368.     {    
  369.         $this->initAction($request);
  370.         
  371.         $searchQuery $this->searchManager->getSearchArray();
  372.         
  373.         $categoryFilter $this->request->get('category_filter','');
  374.         $categoryId $this->request->get('parent_category_id','');
  375.         
  376.         if($categoryFilter == ''){
  377.             $caregoryArray[] = $categoryId;
  378.             $categoryListJson json_encode($caregoryArray);
  379.         }else{
  380.             $categoryListJson $categoryFilter;
  381.         }
  382.         $this->prepareSearchForm($searchQuery['product_search'],$categoryListJson);
  383.         $this->responseArray['search_attribute_filter_array'] = array();
  384.         if ($this->form->isValid()) {
  385.             $this->responseArray['search_attribute_filter_array'] = $this->searchManager->searchFilter($this->form->getData());
  386.         }
  387.         $this->responseArray['category_id'] = $categoryId;
  388.         return $this->responseArray;
  389.     }
  390.     
  391.     private function prepareSearchForm($searchArray,$categoryListJson) {
  392.         $searchArray['categoryId'] = $categoryListJson;
  393.         $this->productSearchData = new ProductSearchData($this->entityManager$this->languageId);
  394.         $this->form $this->createForm($this->productSearchForm$this->productSearchData, array('csrf_protection' => false));
  395.         $this->form->bind($searchArray);
  396.     }
  397.     public function getLanguageId()
  398.     {
  399.         switch ($this->cultureCode) {
  400.             case 'fr':
  401.                 $language '3';
  402.                 break;
  403.             default:
  404.                 $language '1';
  405.                 break;
  406.         }
  407.     }
  408. }