<?php
namespace App\OfficeBrain\Bundle\SearchBundle\Controller;
// use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use App\OfficeBrain\Bundle\SearchBundle\Form\Type\ProductSearchType;
use App\OfficeBrain\Bundle\SearchBundle\Form\Data\ProductSearchData;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Doctrine\ORM\EntityManagerInterface;
use App\OfficeBrain\Bundle\SearchBundle\Manager\Service\SearchManager;
use App\OfficeBrain\Bundle\ProductBundle\Manager\Service\ProductDetailService;
use App\project\OfficeBrain\CustomBundle\ProductBundle\Manager\Service\ProductDetailService as CustomProductDetailService;
use App\OfficeBrain\Bundle\CategoryBundle\Manager\ApiManager as CategoryApiManager;
use App\OfficeBrain\Bundle\CoreBundle\Service\SessionManager;
use Knp\Component\Pager\PaginatorInterface;
use App\OfficeBrain\Bundle\SearchBundle\Manager\Extended\SolrSearchExtended;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\OfficeBrain\Bundle\SearchBundle\Manager\Service\BrowseCategoryManager;
use App\OfficeBrain\Bundle\ProductBundle\ApiClient\ProductApiClient;
use App\OfficeBrain\Bundle\CategoryBundle\Manager\ApiManager;
class BrowseCategoryController extends AbstractController
{
/**
*
* @author Employee Id: 4532
*
* To set member variables default value
*
* @return NULL
*/
public function __construct(EntityManagerInterface $entityManager, RequestStack $request, SearchManager $solrManager, ApiManager $categoryManager, SessionManager $sessionManager, PaginatorInterface $paginatorManager, Container $serviceContainer, BrowseCategoryManager $browserCategoryManager, ProductDetailService $productDetailService, CustomProductDetailService $customProductDetailManager)
{
$this->form = null;
$this->categoryBreadCrumbsList = array();
$this->translator = null;
$this->request = null;
$this->container = $serviceContainer;
$this->serviceContainer = $serviceContainer;
$this->browserCategoryManager = $browserCategoryManager->getBrowserCategoryExtended();
// $this->searchManager = $this->container->get("office_brain_search_manager_extended");
$this->searchManager = $solrManager->getSolrSearchExtended();
$this->productDetailManager = $productDetailService->getProductApiExtended();
$this->customProductDetailManager = $customProductDetailManager->getProductApiExtended();
// $this->productDetailManager = $this->container->get('office_brain_product_api_extended');
}
/**
*
* @author Employee Id: 4861
*
* Desciption: To unset member variables
*
* @param NULL
*
* @return null
*
* @throws NULL
*/
public function __destruct()
{
unset($this->form);
unset($this->categoryBreadCrumbsList);
unset($this->translator);
unset($this->request);
}
/**
*
* @author Employee Id: 4532
*
* Create various objects required action in this class
*
* @return NULL
*/
public function initAction(Request $request)
{
$this->request = $request;
$this->entityManager = $this->getDoctrine()->getManager();
$this->projectSetting = $this->request->get('project_setting');
$this->instanceId = $this->projectSetting['instance_id'];
$this->cultureCode = $this->projectSetting['culture_code'];
// $this->searchManager = $this->get('office_brain_search_manager'); // symfony 4
$this->searchManager = $this->searchManager;
$this->languageId = $this->getLanguageId(); // symfony 4
$this->productSearchForm = ProductSearchType::class;
$this->translator = $this->serviceContainer->get('translator');
// $this->browserCategoryManager = $this->serviceContainer->get('office_brain_browse_category_extended');
$this->browserCategoryManager = $this->browserCategoryManager;
// $this->productDetailManager = $this->serviceContainer->get('office_brain_product_api_extended');
$this->productDetailManager = $this->productDetailManager;
// $this->customProductDetailManager = $this->serviceContainer->get('office_brain_custom_bundle_product_api_extended');
$this->customProductDetailManager = $this->customProductDetailManager;
}
/**
* @author Employee Id: 4532
*
* Browse by category
*
* @param string category slug
*
* @return if result found return parent category else null
*
* @Route("/{_locale}/browse-category", name="office_brain_search_bundle_browse_category", options={"expose"=true}, methods={"GET"})
*
* @Template("OfficeBrain/Bundle/SearchBundle/BrowseCategory/browseCategory.html.twig")
*/
public function browseCategoryAction(Request $request)
{
$this->initAction($request);
$this->responseArray['parent_category_list'] = $this->searchManager->getCategorySubCategoryList();
$slug = $request->query->get('category_slug');
// Check if the slug is provided
if ($slug) {
$filteredCategory = null;
// Filter the category list based on the slug
foreach ($this->responseArray['parent_category_list'] as $key => $category) {
if (isset($category['slug']) && $category['slug'] === $slug) {
$filteredCategory[] = $category;
break; // Exit the loop once the match is found
}
}
// If a matching category is found, return it as a JSON response
if ($filteredCategory) {
// echo "<pre>"; dump($filteredCategory);
$this->responseArray['parent_category_list'] = $filteredCategory;
// echo "<pre>"; dump($this->responseArray['parent_category_list']); exit;
return $this->responseArray;
}
// If no matching category is found, return a 404 response
return new JsonResponse(['error' => 'Category not found'], 404);
}
if ($this->request->isXmlHttpRequest ()) {
if (is_array ( $this->responseArray['parent_category_list'] ) && array_key_exists ( 'data', $this->responseArray['parent_category_list'] ) && ! count ( $this->responseArray['parent_category_list'] )) {
return new JsonResponse ( array('error' => 'END_OF_PAGINATION' ) );
}
return $this->responseArray;
}
return $this->responseArray;
}
/**
* @author Employee Id: 4532
*
* Browse by sub category
*
* @param string category slug
*
* @return if result found return sub category list else null
*
* @Route("/{_locale}/browse-sub-category/{category_slug}", name="office_brain_search_bundle_browse_sub_category", options={"expose"=true}, methods={"GET"})
*
* @Template("OfficeBrainSearchBundle:BrowseCategory:browseSubCategory.html.twig")
*/
public function browseSubCategoryAction(Request $request)
{
$this->initAction($request);
// $categoryObj = $this->serviceContainer->get('office_brain_category_manager');
$categoryObj = $this->browserCategoryManager;
$categorySlug = $this->request->get('category_slug');
$this->responseArray['category_id'] = $categoryObj->getCategoryIdFromSlug($categorySlug);
$subCategoryList = $categoryObj->getSubCategoryList($this->responseArray['category_id']);
if(isset($subCategoryList['child'])){
$this->responseArray['sub_category_list'] = $subCategoryList['child'];
}else{
$this->responseArray['sub_category_list'] = null;
}
if(isset($subCategoryList['name'])){
$this->responseArray['category_name'] = $subCategoryList['name'];
}else{
$this->responseArray['category_name'] = '';
}
if(isset($subCategoryList['parent'])){
$this->responseArray['parent_category_info'] = $subCategoryList['parent'];
}
if ($this->request->isXmlHttpRequest ()) {
if (is_array ( $this->responseArray['sub_category_list'] ) && array_key_exists ( 'data', $this->responseArray['sub_category_list'] ) && ! count ( $this->responseArray['sub_category_list'] )) {
return new JsonResponse ( array( 'error' => 'END_OF_PAGINATION' ) );
}
return $this->responseArray;
}
return $this->responseArray;
}
/**
* @author Employee Id: 4532
*
* Browse by Parent category
*
* @param string category slug
*
* @return if result found return product related list of parent categories else null
*
* @Route("/{_locale}/browse-category-search/{category_slug}", name="office_brain_search_bundle_browse_category_search", options={"expose"=true}, methods={"GET"})
*
* @Template("OfficeBrainSearchBundle:BrowseCategory:browseCategorySearchPage.html.twig")
*/
public function browseCategorySearchAction(Request $request)
{
$this->initAction($request);
// $categoryObj = $this->serviceContainer->get('office_brain_category_manager');
$categoryObj = $this->browserCategoryManager;
$categorySlug = $this->request->get('category_slug');
$categoryid = $categoryObj->getCategoryIdFromSlug($categorySlug);
$subCategoryList = $categoryObj->getSubCategoryList($categoryid);
$this->session = $this->request->getSession();
if(isset($subCategoryList['child'])){
$categoriesList = json_encode(array_keys($subCategoryList['child']));
}else{
$categoriesList = null;
}
if($this->session->get("brandId"))
{
$this->request->query->set("main_filter",$this->session->get("brandId"));
}
//echo $this->request->get("main_filter")." - ".$this->session->get("brandId");
//echo "<pre>"; print_r($this->responseArray['category_name']);exit;
//echo "<pre>"; print_r($this->responseArray['sub_category_list']);exit;
$searchQuery = $this->searchManager->getSearchArray();
// echo "<pre>";print_r($searchQuery['product_search']);exit;
$this->prepareSearchForm($searchQuery['product_search'],$categoriesList);
if ($this->form->isValid()) {
$this->responseArray = $this->searchManager->searchResult($this->form->getData());
if (!$this->request->get('page', null)) {
$this->responseArray['search_attribute_filter_array'] = $this->searchManager->searchFilter($this->form->getData());
}
}
$this->responseArray['sub_category_list'] = $categoriesList;
$this->responseArray['category_id'] = $categoryid;
if(isset($subCategoryList['name'])){
$this->responseArray['category_name'] = $subCategoryList['name'];
}else{
$this->responseArray['category_name'] = '';
}
if(isset($subCategoryList['parent'])){
$this->responseArray['parent_category_info'] = $subCategoryList['parent'];
}
$this->responseArray['breadcrumbs'] = $this->productDetailManager->productDetailBreadcrumbs($this->responseArray['category_id']);
$this->responseArray['search_query'] = $searchQuery['search']['query'];
$this->responseArray['main_filter'] = $searchQuery['search']['main_filter'];
$this->responseArray['form'] = $this->form->createView();
$this->responseArray['Recent_search'] = $this->request->getSession()->get('recent_search');
$this->responseArray['save_search'] = $this->searchManager->getSaveSearch();
$recentSearch = $this->request->query->get('is_filter','check');
if($recentSearch == 'check'){
$this->searchManager->setRecentSearch();
}
$this->responseArray['category_breadcrumb']= array($this->translator->trans('breadcrumb_category'),(isset($this->responseArray['category_name'])?$this->responseArray['category_name']:''));
$this->responseArray['category_slug'] = $categorySlug;
unset($categoryObj);
unset($categorySlug);
unset($subCategoryList);
unset($categoriesList);
unset($searchQuery);
unset($recentSearch);
return $this->responseArray;
// // if ($this->request->isXmlHttpRequest ()) {
// // if (is_array ( $this->responseArray['sub_category_list'] ) && array_key_exists ( 'data', $this->responseArray['sub_category_list'] ) && ! count ( $this->responseArray['sub_category_list'] )) {
// // return new JsonResponse ( array( 'error' => 'END_OF_PAGINATION' ) );
// // }
// // return $this->responseArray;
// // }
}
public function create_slug($string)
{
$slug=str_replace('-',' ',$string);
return $slug;
}
/**
* @author Employee Id: 4532
*
* Product listing accordingly parent category
*
* @param string category id
*
* @return if result found return category products else null
*
* @Route("/{_locale}/category-product-list/{category_slug}", name="office_brain_search_bundle_category_product_list", options={"expose"=true}, methods={"GET"})
*
* @Template("OfficeBrainSearchBundle:BrowseCategory:browseCategoryPage.html.twig")
*/
public function categoryProductListAction(Request $request)
{
$this->initAction($request);
$searchQuery = $this->searchManager->getSearchArray();
$categoryFilter = $this->request->get('category_filter','');
$categoryslug = $this->request->get('category_slug');
// $categoryObj = $this->serviceContainer->get('office_brain_category_manager');
$categoryObj = $this->browserCategoryManager;
$categoryId = $categoryObj->getCategoryIdFromSlug($categoryslug);
$subCategoryList = $categoryObj->getSubCategoryList($categoryId);
/* if($categoryFilter == ''){
$caregoryArray[] = $categoryId;
$categoryListJson = json_encode($caregoryArray);
}else{
$categoryListJson = $categoryFilter;
} */
if($categoryFilter == ''){
if( isset($subCategoryList['child']) ) {
$categoryListJson = json_encode(array_keys($subCategoryList['child']));
} else {
$caregoryArray[] = $categoryId;
$categoryListJson = json_encode($caregoryArray);
}
} else {
$categoryListJson = $categoryFilter;
}
$this->prepareSearchForm($searchQuery['product_search'],$categoryListJson);
$this->responseArray = array();
if ($this->form->isValid()) {
$this->responseArray = $this->searchManager->searchResult($this->form->getData());
if (!$this->request->get('page', null)) {
$this->responseArray['search_attribute_filter_array'] = $this->searchManager->searchFilter($this->form->getData());
}
}
$this->responseArray['breadcrumbs'] = $this->productDetailManager->productDetailBreadcrumbs($categoryId);
$this->responseArray['search_query'] = $searchQuery['search']['query'];
$this->responseArray['main_filter'] = $searchQuery['search']['main_filter'];
$this->responseArray['form'] = $this->form->createView();
$this->responseArray['Recent_search'] = $this->request->getSession()->get('recent_search');
$this->responseArray['save_search'] = $this->searchManager->getSaveSearch();
$recentSearch = $this->request->query->get('is_filter','check');
if($recentSearch == 'check'){
$this->searchManager->setRecentSearch();
}
$this->responseArray['category_breadcrumb']= array($this->translator->trans('breadcrumb_category'),(isset($subCategoryList['name'])?$subCategoryList['name']:''));
$this->responseArray['category_slug'] = $categoryslug;
$this->responseArray['category_id'] = $categoryId;
return $this->responseArray;
}
/**
* @author Employee Id: 4532
*
* Perform search to find attribute list for given search criteria
*
* @param string search_query
*
* @return if result found return attribute list along with product count else null
*
* @Route("/{_locale}/category-attribute-filter-list",
*
* name="office_brain_search_bundle_category_attribute_filter_list", options={"expose"=true}, methods={"GET", "POST"})
*
* @Template("OfficeBrainSearchBundle:Search:searchAttributeFilterList.html.twig")
*/
public function categoryAttributeFilterListAction(Request $request)
{
$this->initAction($request);
$searchQuery = $this->searchManager->getSearchArray();
$categoryFilter = $this->request->get('category_filter','');
$categoryId = $this->request->get('parent_category_id','');
if($categoryFilter == ''){
$caregoryArray[] = $categoryId;
$categoryListJson = json_encode($caregoryArray);
}else{
$categoryListJson = $categoryFilter;
}
$this->prepareSearchForm($searchQuery['product_search'],$categoryListJson);
$this->responseArray['search_attribute_filter_array'] = array();
if ($this->form->isValid()) {
$this->responseArray['search_attribute_filter_array'] = $this->searchManager->searchFilter($this->form->getData());
}
$this->responseArray['category_id'] = $categoryId;
return $this->responseArray;
}
private function prepareSearchForm($searchArray,$categoryListJson) {
$searchArray['categoryId'] = $categoryListJson;
$this->productSearchData = new ProductSearchData($this->entityManager, $this->languageId);
$this->form = $this->createForm($this->productSearchForm, $this->productSearchData, array('csrf_protection' => false));
$this->form->bind($searchArray);
}
public function getLanguageId()
{
switch ($this->cultureCode) {
case 'fr':
$language = '3';
break;
default:
$language = '1';
break;
}
}
}