<?php
namespace App\Zsdev\MarketplaceBundle\Controller;
use Pimcore\Model\DataObject;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class CategoriesSyncController extends FrontendController
{
public function indexAction(string $site = null)
{
$sitesListing = DataObject\Site::getByUnique_name($site);
$sitesArray = $sitesListing->getData();
if(!$sitesArray) {
return $this->json([]);
}
$site = null;
if(sizeof($sitesArray) === 1) {
$site = $sitesArray[0];
}
if(!$site) {
return $this->json([]);
}
$catListing = new DataObject\Category\Listing();
$catListing->setOffset(0);
$catListing->setLimit(50000);
$catListing->setOrderKey("name");
$catListing->setOrder("asc");
$catListing->setCondition("site__id = :siteId", ['siteId' => $site->getId()]);
$catListing->getData();
$preparedCategoriesFlat = [];
foreach($catListing as $cat) {
$explodedPathArray = explode("/", $cat->getPath() ?? '');
// Remove empty values from the path array
$pathArray = array_slice($explodedPathArray, 1, -1);
$seller = $pathArray[0];
// Remove the seller from the array
unset($pathArray[0]);
if(isset($pathArray[1])) { // Remove the Categories value from array
unset($pathArray[1]);
}
$hasParent = true;
if(empty($pathArray)) {
$hasParent = false;
}
$preparedCategoriesFlat[] = $this->fillCategoryArray($cat, $hasParent);
}
//Sort in multidimensional array
$multi = $this->buildTree($preparedCategoriesFlat, 'parent_id', 'id', 'children');
return $this->json($multi);
}
private function fillCategoryArray($category, $hasParent)
{
$array = [];
// SEO Include in sitemap
$seoIncludeInSitemap = 0;
$seoIncludeInSitemapValue = $category->getZsdev_include_in_sitemap();
if (!strlen($seoIncludeInSitemapValue)) {
$seoIncludeInSitemap = 1;
} else {
$seoIncludeInSitemap = (int)$seoIncludeInSitemapValue;
}
// SEO Include in sitemap (pagination)
$seoIncludeInSitemapPagination = $category->getZsdev_include_in_sitemap_pagination();
if(!strlen($seoIncludeInSitemapPagination)) {
$seoIncludeInSitemapPagination = null;
} else {
$seoIncludeInSitemapPagination = (int)$seoIncludeInSitemapPagination;
}
// Include images in sitemap
$includeImagesInSitemap = $category->getZsdev_images_include_in_sitemap();
if(!strlen($includeImagesInSitemap)) {
$includeImagesInSitemap = null;
} else {
$includeImagesInSitemap = (int)$includeImagesInSitemap;
}
$array['id'] = $category->getId();
$array['parent_id'] = $hasParent ? $category->getParentId() : 0;
$array['enabled'] = $category->getEnabled() ? $category->getEnabled() : false;
$array['anchor'] = $category->getAnchor() ? $category->getAnchor() : false;
$array['include_in_menu'] = $category->getInclude_in_menu() ? $category->getInclude_in_menu() : false;
$array['layer_category_only'] = $category->getLayer_category_only() ? $category->getLayer_category_only() : false;
$array['ubongo_exclude'] = $category->getUbongo_exclude() ? $category->getUbongo_exclude() : false;
$array['search_exclude'] = $category->getSearch_exclude() ? $category->getSearch_exclude() : false;
$array['exclude_from_category_filter'] = $category->getExclude_from_category_filter() ? $category->getExclude_from_category_filter() : false;
$array['display_mode'] = $category->getDisplay_mode() ? $category->getDisplay_mode() : false;
$array['name'] = $category->getName() ? $category->getName() : false;
$array['default_sort_by'] = $category->getDefault_sort_by() ? $category->getDefault_sort_by() : false;
$array['description'] = $category->getDescription() ? $category->getDescription() : false;
$array['zsdev_images_include_in_sitemap'] = $includeImagesInSitemap;
$array['seo_page_title'] = $category->getSeo_page_title() ? $category->getSeo_page_title() : false;
$array['seo_heading'] = $category->getSeo_heading() ? $category->getSeo_heading() : false;
$array['additional_category'] = $category->getAdditional_category() ? $category->getAdditional_category() : false;
$array['touchpoint_id'] = $category->getTouchpoint_id() ? $category->getTouchpoint_id() : '';
$array['meta_title'] = $category->getMeta_title() ? $category->getMeta_title() : false;
$array['meta_keywords'] = $category->getMeta_keywords() ? $category->getMeta_keywords() : false;
$array['meta_description'] = $category->getMeta_description() ? $category->getMeta_description() : false;
$array['zsdev_robots'] = $category->getZsdev_robots() ? $category->getZsdev_robots() : false;
$array['zsdev_canonical'] = $category->getZsdev_canonical() ? $category->getZsdev_canonical() : false;
$array['zsdev_include_in_sitemap'] = $seoIncludeInSitemap;
$array['zsdev_robots_pagination'] = $category->getZsdev_robots_pagination() ? $category->getZsdev_robots_pagination() : false;
$array['zsdev_canonical_pagination'] = $category->getZsdev_canonical_pagination() ? $category->getZsdev_canonical_pagination() : false;
$array['zsdev_include_in_sitemap_pagination'] = $seoIncludeInSitemapPagination;
$array['new_in'] = $category->getNew_in() ? $category->getNew_in() : false;
$array['sale'] = $category->getSale() ? $category->getSale() : false;
$array['outlet'] = $category->getOutlet() ? $category->getOutlet() : false;
$array['action'] = $category->getAction() ? $category->getAction() : false;
$array['google_id'] = $category->getGoogle_id() ? $category->getGoogle_id() : false;
$array['image'] = $category->getImage() ? \Pimcore\Tool::getHostUrl().$category->getImage()->getFullPath() : false;
$array['thumbnail'] = $category->getThumbnail() ? \Pimcore\Tool::getHostUrl().$category->getThumbnail()->getFullPath() : false;
$array['special_title'] = $category->getSpecial_title() ? $category->getSpecial_title() : false;
$array['special_url'] = $category->getSpecial_url() ? $category->getSpecial_url() : false;
$array['shipping_level'] = $category->getShipping_level() ? $category->getShipping_level() : false;
$array['shipping_time'] = $category->getShipping_time() ? $category->getShipping_time() : false;
$array['is_parent_outlet'] = $category->getIs_outlet_parent() ? $category->getIs_outlet_parent() : false;
$array['children'] = [];
return $array;
}
private function buildTree(array $flatList, $parentKey, $idKey, $childrenKey)
{
$grouped = [];
foreach ($flatList as $node){
$grouped[$node[$parentKey]][] = $node;
}
$treeBuilder = function($siblings) use (&$treeBuilder, $grouped, $idKey, $childrenKey) {
foreach ($siblings as $k => $sibling) {
$id = $sibling[$idKey];
if(isset($grouped[$id])) {
$sibling[$childrenKey] = $treeBuilder($grouped[$id]);
}
$siblings[$k] = $sibling;
}
return $siblings;
};
$tree = $treeBuilder($grouped[0]);
return $tree;
}
}