src/Zsdev/MarketplaceBundle/Controller/CategoriesSyncController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Zsdev\MarketplaceBundle\Controller;
  3. use Pimcore\Model\DataObject;
  4. use Pimcore\Controller\FrontendController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. class CategoriesSyncController extends FrontendController
  8. {
  9.     public function indexAction(string $site null)
  10.     {
  11.         $sitesListing DataObject\Site::getByUnique_name($site);
  12.         $sitesArray $sitesListing->getData();
  13.         if(!$sitesArray) {
  14.             return $this->json([]);
  15.         }
  16.         $site null;
  17.         if(sizeof($sitesArray) === 1) {
  18.             $site $sitesArray[0];
  19.         }
  20.         if(!$site) {
  21.             return $this->json([]);
  22.         }
  23.         $catListing = new DataObject\Category\Listing();
  24.         $catListing->setOffset(0);
  25.         $catListing->setLimit(50000);
  26.         $catListing->setOrderKey("name");
  27.         $catListing->setOrder("asc");
  28.         $catListing->setCondition("site__id = :siteId", ['siteId' => $site->getId()]);
  29.         $catListing->getData();
  30.         $preparedCategoriesFlat = [];
  31.         foreach($catListing as $cat) {
  32.             $explodedPathArray explode("/"$cat->getPath() ?? '');
  33.             // Remove empty values from the path array
  34.             $pathArray array_slice($explodedPathArray1, -1);
  35.             $seller $pathArray[0];
  36.             // Remove the seller from the array
  37.             unset($pathArray[0]);
  38.             if(isset($pathArray[1])) { // Remove the Categories value from array
  39.                 unset($pathArray[1]);
  40.             }
  41.             $hasParent true;
  42.             if(empty($pathArray)) {
  43.                 $hasParent false;
  44.             }
  45.             $preparedCategoriesFlat[] = $this->fillCategoryArray($cat$hasParent);
  46.         }
  47.         //Sort in multidimensional array
  48.         $multi $this->buildTree($preparedCategoriesFlat'parent_id''id''children');
  49.         return $this->json($multi);
  50.     }
  51.     private function fillCategoryArray($category$hasParent)
  52.     {
  53.         $array = [];
  54.         // SEO Include in sitemap
  55.         $seoIncludeInSitemap 0;
  56.         $seoIncludeInSitemapValue $category->getZsdev_include_in_sitemap();
  57.         if (!strlen($seoIncludeInSitemapValue)) {
  58.             $seoIncludeInSitemap 1;
  59.         } else {
  60.             $seoIncludeInSitemap = (int)$seoIncludeInSitemapValue;
  61.         }
  62.         // SEO Include in sitemap (pagination)
  63.         $seoIncludeInSitemapPagination $category->getZsdev_include_in_sitemap_pagination();
  64.         if(!strlen($seoIncludeInSitemapPagination)) {
  65.             $seoIncludeInSitemapPagination null;
  66.         } else {
  67.             $seoIncludeInSitemapPagination = (int)$seoIncludeInSitemapPagination;
  68.         }
  69.         // Include images in sitemap
  70.         $includeImagesInSitemap $category->getZsdev_images_include_in_sitemap();
  71.         if(!strlen($includeImagesInSitemap)) {
  72.             $includeImagesInSitemap null;
  73.         } else {
  74.             $includeImagesInSitemap = (int)$includeImagesInSitemap;
  75.         }
  76.         $array['id'] = $category->getId();
  77.         $array['parent_id'] = $hasParent $category->getParentId() : 0;
  78.         $array['enabled'] = $category->getEnabled() ? $category->getEnabled() : false;
  79.         $array['anchor'] = $category->getAnchor() ? $category->getAnchor() : false;
  80.         $array['include_in_menu'] = $category->getInclude_in_menu() ? $category->getInclude_in_menu() : false;
  81.         $array['layer_category_only'] = $category->getLayer_category_only() ? $category->getLayer_category_only() : false;
  82.         $array['ubongo_exclude'] = $category->getUbongo_exclude() ? $category->getUbongo_exclude() : false;
  83.         $array['search_exclude'] = $category->getSearch_exclude() ? $category->getSearch_exclude() : false;
  84.         $array['exclude_from_category_filter'] = $category->getExclude_from_category_filter() ? $category->getExclude_from_category_filter() : false;
  85.         $array['display_mode'] = $category->getDisplay_mode() ? $category->getDisplay_mode() : false;
  86.         $array['name'] = $category->getName() ? $category->getName() : false;
  87.         $array['default_sort_by'] = $category->getDefault_sort_by() ? $category->getDefault_sort_by() : false;
  88.         $array['description'] = $category->getDescription() ? $category->getDescription() : false;
  89.         $array['zsdev_images_include_in_sitemap'] = $includeImagesInSitemap;
  90.         $array['seo_page_title'] = $category->getSeo_page_title() ? $category->getSeo_page_title() : false;
  91.         $array['seo_heading'] = $category->getSeo_heading() ? $category->getSeo_heading() : false;
  92.         $array['additional_category'] = $category->getAdditional_category() ? $category->getAdditional_category() : false;
  93.         $array['touchpoint_id'] = $category->getTouchpoint_id() ? $category->getTouchpoint_id() : '';
  94.         $array['meta_title'] = $category->getMeta_title() ? $category->getMeta_title() : false;
  95.         $array['meta_keywords'] = $category->getMeta_keywords() ? $category->getMeta_keywords() : false;
  96.         $array['meta_description'] = $category->getMeta_description() ? $category->getMeta_description() : false;
  97.         $array['zsdev_robots'] = $category->getZsdev_robots() ? $category->getZsdev_robots() : false;
  98.         $array['zsdev_canonical'] = $category->getZsdev_canonical() ? $category->getZsdev_canonical() : false;
  99.         $array['zsdev_include_in_sitemap'] = $seoIncludeInSitemap;
  100.         $array['zsdev_robots_pagination'] = $category->getZsdev_robots_pagination() ? $category->getZsdev_robots_pagination() : false;
  101.         $array['zsdev_canonical_pagination'] = $category->getZsdev_canonical_pagination() ? $category->getZsdev_canonical_pagination() : false;
  102.         $array['zsdev_include_in_sitemap_pagination'] = $seoIncludeInSitemapPagination;
  103.         $array['new_in'] = $category->getNew_in() ? $category->getNew_in() : false;
  104.         $array['sale'] = $category->getSale() ? $category->getSale() : false;
  105.         $array['outlet'] = $category->getOutlet() ? $category->getOutlet() : false;
  106.         $array['action'] = $category->getAction() ? $category->getAction() : false;
  107.         $array['google_id'] = $category->getGoogle_id() ? $category->getGoogle_id() : false;
  108.         $array['image'] = $category->getImage() ? \Pimcore\Tool::getHostUrl().$category->getImage()->getFullPath() : false;
  109.         $array['thumbnail'] = $category->getThumbnail() ? \Pimcore\Tool::getHostUrl().$category->getThumbnail()->getFullPath() : false;
  110.         $array['special_title'] = $category->getSpecial_title() ? $category->getSpecial_title() : false;
  111.         $array['special_url'] = $category->getSpecial_url() ? $category->getSpecial_url() : false;
  112.         $array['shipping_level'] = $category->getShipping_level() ? $category->getShipping_level() : false;
  113.         $array['shipping_time'] = $category->getShipping_time() ? $category->getShipping_time() : false;
  114.         $array['is_parent_outlet'] = $category->getIs_outlet_parent() ? $category->getIs_outlet_parent() : false;
  115.         $array['children'] = [];
  116.         return $array;
  117.     }
  118.     private function buildTree(array $flatList$parentKey$idKey$childrenKey)
  119.     {
  120.         $grouped = [];
  121.         foreach ($flatList as $node){
  122.             $grouped[$node[$parentKey]][] = $node;
  123.         }
  124.         $treeBuilder = function($siblings) use (&$treeBuilder$grouped$idKey$childrenKey) {
  125.             foreach ($siblings as $k => $sibling) {
  126.                 $id $sibling[$idKey];
  127.                 if(isset($grouped[$id])) {
  128.                     $sibling[$childrenKey] = $treeBuilder($grouped[$id]);
  129.                 }
  130.                 $siblings[$k] = $sibling;
  131.             }
  132.             return $siblings;
  133.         };
  134.         $tree $treeBuilder($grouped[0]);
  135.         return $tree;
  136.     }
  137. }