<commit_before><?php

namespace Core\HeurekaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Site\ShopBundle\Controller\ShopController;

class HeurekaController extends ShopController
{
    public function exportAction()
    {
        $request = $this->getRequest();
        
        $minishop  = $this->container->getParameter('minishop');
        $em = $this->getDoctrine()->getManager();
        $query = $em->getRepository("CoreProductBundle:Product")->findByCategoryQuery(null, false, true, true, false);
        if ($request->get('_locale')) {
            $query->setHint(
                \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
                $request->get('_locale') // take locale from session or request etc.
            );
        }

        $medias = $em->getRepository("CoreProductBundle:ProductMedia")->getProductsMediasArray();
        $stocks = $em->getRepository("CoreProductBundle:Stock")->getStocksArray();
        
        $pricegroup_id = $request->get('pricegroup');
        $priceGroup = null;
        if ($pricegroup_id !== null) {
            $priceGroup = $em->getRepository('CoreUserBundle:PriceGroup')->find($pricegroup_id);
        }
        $priceGroup = ($priceGroup) ? $priceGroup : $this->getPriceGroup();
        $currency_id = $request->get('currency');
        $currency = null;
        if ($currency_id !== null) {
            $currency = $em->getRepository('CorePriceBundle:Currency')->find($currency_id);
        }
        $currency = ($currency) ? $currency : $this->getCurrency();
        $pricetypes = $this->container->getParameter('heureka.prices');
        
        $request= $this->getRequest();
        $document = new \DOMDocument('1.0', 'utf-8');
        $document->formatOutput = true;
        $shop = $document->appendChild($document->createElement('SHOP'));
        $paths = array();

        foreach ($query->getResult()  as $product) {
            $item = $document->createElement('SHOPITEM');

            $code = $document->createElement('ITEM_ID');
            $code->appendChild($document->createTextNode($product->getId()));
            $item->appendChild($code);

            $name = $document->createElement('PRODUCT');
            $pname = str_replace(array('&nbsp;', '&amp;'), array(" ", "&"), strip_tags($product->getTitle()));
            $name->appendChild($document->createCDATASection($pname));
            $item->appendChild($name);

            $name = $document->createElement('PRODUCTNAME');
            $name->appendChild($document->createCDATASection($pname));
            $item->appendChild($name);

            $description = str_replace(array("\x0B", "\0", "\r", "\t"), ' ', strip_tags($product->getLongDescription() . " " . $product->getLongDescription())); // zrusenie niektorych whitespacesnakov za medzery
            $description = preg_replace('/\s+/', ' ', $description);
            $description = str_replace(array('&nbsp;', '&amp;'), array(" ", "&"), $description);
            $desc = $document->createElement('DESCRIPTION');
            if (!empty($description)) {
                $desc->appendChild($document->createCDATASection($description));
            }
            $item->appendChild($desc);

            $ean = $document->createElement('EAN');
            $eancode = trim($product->getId());
            $ean->appendChild($document->createTextNode($eancode));
            $item->appendChild($ean);

            if (isset($medias[$product->getId()])) {
                $img = $document->createElement('IMGURL');
                $media = current($medias[$product->getId()]);
                $img->appendChild($document->createTextNode($request->getScheme() . '://' . $request->getHttpHost() . '/'. $media->getWebPath('original')));
                $item->appendChild($img);
            }

            $price = $document->createElement('PRICE_VAT');
            $pprice = 0;
            foreach($pricetypes as $type) {
                $priceEntity = $product->getMinimalPrice($currency, $priceGroup, $type);
                if ($priceEntity) {
                    $pprice = $priceEntity->getPriceVat();
                    break;
                }
            }
            if (isset($prices[$product->getId()])) {
                $pprice = PriceRepository::getProductPrice($prices[$product->getId()], $types);
                $pprice = $pprice->calculatePriceVAT($pricegroup, $currency);
                break;
            }
            $price->appendChild($document->createTextNode(number_format($pprice, 2)));
            $item->appendChild($price);

            $manuf = $document->createElement('MANUFACTURER');
            if ($product->getVendor()) {
                $manuf->appendChild($document->createCDATASection(trim($product->getVendor()->getTitle())));
            }
            $item->appendChild($manuf);

            $avb = $document->createElement('DELIVERY_DATE');

            if (isset($stocks[$product->getId()])) {
                $stock = current($stocks[$product->getId()]);
                $qdocument = ($stock->getAmount() > 0 || ($stock->getAvailability())) ? "0" : "";
                $avb->appendChild($document->createTextNode($qdocument));
            }
            $item->appendChild($avb);

            $cat = $document->createElement('CATEGORYTEXT');
            $pom = "";
            if ($product->getProductCategories()->count() > 0) {
                $category  = $product->getProductCategories()->first()->getCategory();
                $category_id = $category->getId();
                if (isset($paths[$category_id])) {
                    $pom = $paths[$category_id];
                } else {
                    $path ="";
                    $categoryquery = $em->getRepository('CoreCategoryBundle:Category')->getPathQueryBuilder($category)->getQuery();
                    if ($request->get('_locale')) {
                        $categoryquery->setHint(
                            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 
                            'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
                        );
                        $categoryquery->setHint(
                            \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
                            $request->get('_locale') // take locale from session or request etc.
                        );
                    }
                    $pathcategories = $categoryquery->getResult();
                    if (!empty($pathcategories)) {
                        foreach ($pathcategories as $pathcat) {
                            if (!empty($path)) {
                                $path .= " | ";
                            }
                            $path .= $pathcat->getTitle();
                        }
                    }
                    $pom = $path;
                    $paths[$category_id] = $path;
                }<commit_msg>only enabled categories<commit_after><?php

namespace Core\HeurekaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Site\ShopBundle\Controller\ShopController;

class HeurekaController extends ShopController
{
    public function exportAction()
    {
        $request = $this->getRequest();
        
        $minishop  = $this->container->getParameter('minishop');
        $em = $this->getDoctrine()->getManager();
        $query = $em->getRepository("CoreProductBundle:Product")->findByCategoryQuery(null, false, true, true, false);
        if ($request->get('_locale')) {
            $query->setHint(
                \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
                $request->get('_locale') // take locale from session or request etc.
            );
        }

        $medias = $em->getRepository("CoreProductBundle:ProductMedia")->getProductsMediasArray();
        $stocks = $em->getRepository("CoreProductBundle:Stock")->getStocksArray();
        
        $pricegroup_id = $request->get('pricegroup');
        $priceGroup = null;
        if ($pricegroup_id !== null) {
            $priceGroup = $em->getRepository('CoreUserBundle:PriceGroup')->find($pricegroup_id);
        }
        $priceGroup = ($priceGroup) ? $priceGroup : $this->getPriceGroup();
        $currency_id = $request->get('currency');
        $currency = null;
        if ($currency_id !== null) {
            $currency = $em->getRepository('CorePriceBundle:Currency')->find($currency_id);
        }
        $currency = ($currency) ? $currency : $this->getCurrency();
        $pricetypes = $this->container->getParameter('heureka.prices');
        
        $request= $this->getRequest();
        $document = new \DOMDocument('1.0', 'utf-8');
        $document->formatOutput = true;
        $shop = $document->appendChild($document->createElement('SHOP'));
        $paths = array();

        foreach ($query->getResult()  as $product) {
            $item = $document->createElement('SHOPITEM');

            $code = $document->createElement('ITEM_ID');
            $code->appendChild($document->createTextNode($product->getId()));
            $item->appendChild($code);

            $name = $document->createElement('PRODUCT');
            $pname = str_replace(array('&nbsp;', '&amp;'), array(" ", "&"), strip_tags($product->getTitle()));
            $name->appendChild($document->createCDATASection($pname));
            $item->appendChild($name);

            $name = $document->createElement('PRODUCTNAME');
            $name->appendChild($document->createCDATASection($pname));
            $item->appendChild($name);

            $description = str_replace(array("\x0B", "\0", "\r", "\t"), ' ', strip_tags($product->getLongDescription() . " " . $product->getLongDescription())); // zrusenie niektorych whitespacesnakov za medzery
            $description = preg_replace('/\s+/', ' ', $description);
            $description = str_replace(array('&nbsp;', '&amp;'), array(" ", "&"), $description);
            $desc = $document->createElement('DESCRIPTION');
            if (!empty($description)) {
                $desc->appendChild($document->createCDATASection($description));
            }
            $item->appendChild($desc);

            $ean = $document->createElement('EAN');
            $eancode = trim($product->getId());
            $ean->appendChild($document->createTextNode($eancode));
            $item->appendChild($ean);

            if (isset($medias[$product->getId()])) {
                $img = $document->createElement('IMGURL');
                $media = current($medias[$product->getId()]);
                $img->appendChild($document->createTextNode($request->getScheme() . '://' . $request->getHttpHost() . '/'. $media->getWebPath('original')));
                $item->appendChild($img);
            }

            $price = $document->createElement('PRICE_VAT');
            $pprice = 0;
            foreach($pricetypes as $type) {
                $priceEntity = $product->getMinimalPrice($currency, $priceGroup, $type);
                if ($priceEntity) {
                    $pprice = $priceEntity->getPriceVat();
                    break;
                }
            }
            if (isset($prices[$product->getId()])) {
                $pprice = PriceRepository::getProductPrice($prices[$product->getId()], $types);
                $pprice = $pprice->calculatePriceVAT($pricegroup, $currency);
                break;
            }
            $price->appendChild($document->createTextNode(number_format($pprice, 2)));
            $item->appendChild($price);

            $manuf = $document->createElement('MANUFACTURER');
            if ($product->getVendor()) {
                $manuf->appendChild($document->createCDATASection(trim($product->getVendor()->getTitle())));
            }
            $item->appendChild($manuf);

            $avb = $document->createElement('DELIVERY_DATE');

            if (isset($stocks[$product->getId()])) {
                $stock = current($stocks[$product->getId()]);
                $qdocument = ($stock->getAmount() > 0 || ($stock->getAvailability())) ? "0" : "";
                $avb->appendChild($document->createTextNode($qdocument));
            }
            $item->appendChild($avb);

            $cat = $document->createElement('CATEGORYTEXT');
            $pom = "";
            if ($product->getProductCategories()->count() > 0) {
                $category  = $product->getProductCategories()->first()->getCategory();
                $category_id = $category->getId();
                if (isset($paths[$category_id])) {
                    $pom = $paths[$category_id];
                } else {
                    $path ="";
                    $categoryquery = $em->getRepository('CoreCategoryBundle:Category')
                    ->getPathQueryBuilder($category)
                    ->andWhere("node.enabled=:enabled")
                    ->setParameter("enabled", true)
                    ->getQuery();
                    if ($request->get('_locale')) {
                        $categoryquery->setHint(
                            \Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER, 
                            'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
                        );
                        $categoryquery->setHint(
                            \Gedmo\Translatable\TranslatableListener::HINT_TRANSLATABLE_LOCALE,
                            $request->get('_locale') // take locale from session or request etc.
                        );
                    }
                    $pathcategories = $categoryquery->getResult();
                    if (!empty($pathcategories)) {
                        foreach ($pathcategories as $pathcat) {
                            if (!empty($path)) {
                                $path .= " | ";
                            }
                            $path .= $pathcat->getTitle();
                        }
                    }
                    $pom = $path;
                    $paths[$category_id] = $path;
                }
{'fraction_non_alphanumeric': 0.16164036076662908, 'fraction_numerical': 0.0014092446448703494, 'mean_word_length': 2.0, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 217, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5038664', 'n_tokens_mistral': 3710, 'n_tokens_neox': 3590, 'n_words': 499}