src/Kiwi/EshopBundle/Service/EshopProductsHandle.php line 1547

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Matěj Böswart
  5.  * Date: 6.11.2017
  6.  * Time: 20:34
  7.  */
  8. namespace Kiwi\EshopBundle\Service;
  9. use Kiwi\EshopBundle\Entity\Contingents\ProductRecommended;
  10. use Kiwi\EshopBundle\Entity\Product;
  11. use Kiwi\EshopBundle\Entity\Contingents\ProductParams;
  12. use Kiwi\EshopBundle\Entity\ProductHolders;
  13. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\Form\Extension\Core\Type\TextType;
  16. use Symfony\Component\Form\Extension\Core\Type\IntegerType;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\Form\Extension\Core\Type\FormType;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Kiwi\ImageGalleryBundle\Entity\ImageFolderTree;
  21. use Symfony\Component\HttpFoundation\Session\Session;
  22. use Symfony\Component\HttpFoundation\Cookie;
  23. class EshopProductsHandle {
  24.     protected $em;
  25.     protected $languages;
  26.     protected $imageGalleryHandle;
  27.     protected $eshopCategoriesHandle;
  28.     protected $eshopParamsHandle;
  29.     protected $eshopCacheHandle;
  30.     protected $attachementsHandle;
  31.     protected $webHandle;
  32.     protected $eshopUsersHandle;
  33.     protected $article;
  34.     var $controller null;
  35.     var $request null;
  36.     var $root_image_folder_name '_ESHOP_IMAGES_';
  37.     var $root_image_sub_folder_name '_PRODUCT_IMAGES_';
  38.     var $repo 'Kiwi\EshopBundle\Entity\Product';
  39.     var $repo_holders 'Kiwi\EshopBundle\Entity\ProductHolders';
  40.     var $repo_params 'Kiwi\EshopBundle\Entity\Contingents\ProductParams';
  41.     var $repo_categories 'Kiwi\EshopBundle\Entity\Contingents\ProductCategory';
  42.     //var $qr_url_pattern = 'http://dev.effectiveonline.cz:81/qr-%s';
  43.     //var $qr_url_pattern = 'http://vape.e-online.cz:4081/qr-%s';
  44.     var $qr_url_pattern 'http://farline-v2.e-online.cz/qr-%s';
  45.     var $no_image '/bundles/Farline/img/logo.svg';
  46.     var $cookie_key 'last_products';
  47.     var $log_path '';
  48.     /**
  49.      * @var \Kiwi\PageBundle\Twig\PageExtensions
  50.      */
  51.     private $pageExtensions;
  52.     public function __construct(\Doctrine\ORM\EntityManager $em,
  53.                                 \Kiwi\LanguagesBundle\Service\LanguagesHandle $languages,
  54.                                 \Kiwi\ImageGalleryBundle\Service\ImageGalleryHandle $imageGalleryHandle,
  55.                                 \Kiwi\EshopBundle\Service\EshopCategoriesHandle $eshopCategoriesHandle,
  56.                                 \Kiwi\EshopBundle\Service\EshopParamsHandle $eshopParamsHandle,
  57.                                 \Kiwi\EshopBundle\Service\EshopCacheHandle $eshopCacheHandle,
  58.                                 \Kiwi\EshopBundle\Service\EshopAttachementsHandle $attachementsHandle,
  59.                                 \Security\UserBundle\Service\WebHandle $webHandle,
  60.                                 \Kiwi\EshopBundle\Service\EshopUsersHandle $eshopUsersHandle,
  61.                                 \Kiwi\PageBundle\Twig\PageExtensions $pageExtensions,
  62.                                 \Kiwi\ArticleBundle\Service\ArticleHandle $article) {
  63.         $this->em $em;
  64.         $this->languages $languages;
  65.         $this->imageGalleryHandle $imageGalleryHandle;
  66.         $this->eshopCategoriesHandle $eshopCategoriesHandle;
  67.         $this->eshopParamsHandle $eshopParamsHandle;
  68.         $this->eshopCacheHandle $eshopCacheHandle;
  69.         $this->attachementsHandle $attachementsHandle;
  70.         $this->webHandle $webHandle;
  71.         $this->eshopUsersHandle $eshopUsersHandle;
  72.         $this->pageExtensions $pageExtensions;
  73.         $this->article $article;
  74.         $this->log_path dirname(dirname(dirname(dirname(__DIR__)))).'/tmp/ProductsHandle.log';
  75.         //$this->NC1();
  76.         //$this->NC2();
  77.     }
  78.     public function fixCodes() {
  79.         foreach ($this->em->getRepository('Kiwi\EshopBundle\Entity\Producers')->findAll() as $producer) {
  80.             $producer->setCode($this->pageExtensions->webalizeFunction($producer->getCode()));
  81.             $this->em->persist($producer);
  82.             $this->em->flush($producer);
  83.         }
  84.         foreach ($this->em->getRepository('Kiwi\EshopBundle\Entity\KategorieTree')->findAll() as $kategorie) {
  85.             $kategorie->setSeoName($this->pageExtensions->webalizeFunction($kategorie->getSeoName()));
  86.             $this->em->persist($kategorie);
  87.             $this->em->flush($kategorie);
  88.         }
  89.         foreach ($this->em->getRepository('Kiwi\EshopBundle\Entity\CustomParamsLists')->findAll() as $param) {
  90.             $param->setCode($this->pageExtensions->webalizeFunction($param->getCode()));
  91.             $this->em->persist($param);
  92.             $this->em->flush($param);
  93.         }
  94.     }
  95.     public function setController($controller) {
  96.         $this->controller $controller;
  97.         return $this;
  98.     }
  99.     public function setRequest(Request $request) {
  100.         $this->request $request;
  101.         return $this;
  102.     }
  103.     public function handleProductUpdate(\Kiwi\EshopBundle\Entity\Product $product, Array $update, Array $categories, Array $params$categories_handle '_REPLACE_') {
  104.         /*
  105.          * $categories_handle
  106.          * _REPLACE_ = replace current categories (drop current)
  107.          * */
  108.         //collect data for alter
  109.         $multilang_cols = array(
  110.             'name',
  111.             'name_alt_1',
  112.             'name_alt_2',
  113.             'description_fast',
  114.             'description',
  115.             'holder_name',
  116.             'actionText',
  117.         );
  118.         $redirect_call = array(
  119.             'setHolderNameHolderId' => function(\Kiwi\EshopBundle\Entity\Product $product$bs){
  120.                 $product->setHolder($this->getProduct($bs));
  121.             },
  122.             'setHolderName' => function(\Kiwi\EshopBundle\Entity\Product $product$bs){
  123.                 /*
  124.                 if(!$product->getHolder()) {
  125.                     //article for holder
  126.                     $article = $this->article->createBlankArticle($this->languages->getLangEntry($bs, 'cz'));
  127.                     //holder
  128.                     $holder = new ProductHolders();
  129.                     $holder->setArticle($article);
  130.                     $holder->setTransportCode(md5($bs));
  131.                     $this->em->persist($holder);
  132.                     $this->em->flush($holder);
  133.                     $this->em->refresh($holder);
  134.                     $product->setHolder($holder);
  135.                 }
  136.                 */
  137.             }
  138.         );
  139.         $base = array();
  140.         foreach($update as $kfd => $fd) {
  141.             if(in_array(substr($kfd,0,-3),$multilang_cols)) {
  142.                 $a_key substr($kfd,0,-3);
  143.                 $a_lang substr(str_replace($a_key,'',$kfd),1);
  144.                 $base[$a_key][$a_lang] = $fd;
  145.             } else {
  146.                 $base[$kfd] = $fd;
  147.             }
  148.         }
  149.         foreach($base as $kbs => &$bs) {
  150.             if(is_array($bs)) {
  151.                 $a_bs '';
  152.                 foreach($bs as $a_lang => $a_content) {
  153.                     $a_bs $this->languages->updateLangEntry($a_lang,$a_bs,$a_content);
  154.                 }
  155.                 $bs $a_bs;
  156.             }
  157.         }
  158.         $base unserialize(serialize($base));
  159.         //update obect
  160.         foreach($base as $kbs => $bs) {
  161.             $method_name $this->getSetMethodName($kbs);
  162.             switch($method_name) {
  163.                 default:
  164.                     //echo "method".$method_name."::".$bs."\n";
  165.                     if(!isset($redirect_call[$method_name])) {
  166.                         $product->$method_name($bs);
  167.                     } else {
  168.                         $redirect_call[$method_name]($product$bs);
  169.                     }
  170.                 break;
  171.                 case 'setCurrency':
  172.                     $product->$method_name($this->em->getRepository('Kiwi\EshopBundle\Entity\Currency')->find($bs));
  173.                 break;
  174.                 case 'setVat':
  175.                     $product->$method_name($this->em->getRepository('Kiwi\EshopBundle\Entity\Vat')->find($bs));
  176.                 break;
  177.                 case 'setEshop':
  178.                     $product->$method_name($this->em->getRepository('Kiwi\EshopBundle\Entity\Eshops')->find($bs));
  179.                 break;
  180.                 case 'setHolder':
  181.                     if($bs) {
  182.                         $product->$method_name($this->em->getRepository('Kiwi\EshopBundle\Entity\ProductHolders')->find($bs));
  183.                     } else {
  184.                         $method_name_id $method_name.'Id';
  185.                         $product->$method_name(null);
  186.                         $product->$method_name_id(null);
  187.                         //var_dump($product->getHolderId()); exit;
  188.                     }
  189.                 break;
  190.                 case 'setUnit':
  191.                     if($bs) {
  192.                         $product->$method_name($this->em->getRepository('Kiwi\EshopBundle\Entity\Unit')->find($bs));
  193.                     } else {
  194.                         $product->$method_name(null);
  195.                     }
  196.                 break;
  197.                 case 'setProducer':
  198.                     if($bs) {
  199.                         $product->$method_name($this->em->getRepository('Kiwi\EshopBundle\Entity\Producers')->find($bs));
  200.                     } else {
  201.                         $product->$method_name(null);
  202.                     }
  203.                 break;
  204.                 case 'setProductId':
  205.                 case 'setRef':
  206.                     null;
  207.                 break;
  208.             }
  209.         }
  210.         $product->setCreatedTime(new \DateTime('now'));
  211.         $product->setUpdatedTime(new \DateTime('now'));
  212.         //$product->setEshop($this->getEshop());
  213.         //$this->em->merge($product);
  214.         $this->em->persist($product);
  215.         $this->em->flush();
  216.         //categories
  217.         $categories array_unique($categories);
  218.         switch($categories_handle) {
  219.             case '_REPLACE_':
  220.                 foreach($this->eshopCategoriesHandle->getProductCategoriesNoCache($product) as $product_category) {
  221.                     $this->em->remove($product_category);
  222.                     $this->em->flush();
  223.                 }
  224.                 foreach($categories as $category) {
  225.                     $category $this->eshopCategoriesHandle->getCategory($category);
  226.                     if($category) {
  227.                         $this->eshopCategoriesHandle->addCategoryProduct(
  228.                             $category,
  229.                             $product
  230.                         );
  231.                     }
  232.                 }
  233.             break;
  234.         }
  235.         //params update
  236.         $params_processed = [];
  237.         foreach($params as $param_id => $param_value) {
  238.             $a_param $this->getProductParam($product->getId(), $param_id);
  239.             if(!$a_param) {
  240.                 $a_param = new ProductParams();
  241.                 $a_param->setProduct($product);
  242.                 $a_param->setParam($this->eshopParamsHandle->getCustomParam($param_id));
  243.             }
  244.             if(is_array($param_value)) {
  245.                 $a_param->setParamValue(serialize($param_value));
  246.             } else {
  247.                 $a_param->setParamValue($param_value);
  248.             }
  249.             $this->em->persist($a_param);
  250.             $this->em->flush();
  251.             $params_processed[] = $param_id;
  252.         }
  253.         //params cleanup (not processed ones)
  254.         $allCustomParams $this->eshopParamsHandle->getCustomParams();
  255.         foreach ($allCustomParams as $acp) {
  256.             if(!in_array($acp->getId(), $params_processed)) {
  257.                 $a_param $this->getProductParam($product->getId(), $acp->getId());
  258.                 if($a_param) {
  259.                     $a_param->setParamValue(serialize(array()));
  260.                     $this->em->persist($a_param);
  261.                     $this->em->flush();
  262.                 }
  263.             }
  264.         }
  265.         $this->eshopCacheHandle->clearCache('vape-eshop-product-'.$product->getId().'-compiled');
  266.         $this->eshopCacheHandle->clearCache('eshop-product-'.$product->getId().'-categories');
  267.         return $product;
  268.     }
  269.     public function handleProductsBatchCategoryUpdate(Array $products, Array $categories) {
  270.         if($products) {
  271.             foreach ($products as $product_id) {
  272.                 $product $this->getProduct($product_id);
  273.                 $this->handleProductUpdate(
  274.                     $product,
  275.                     array(),
  276.                     $categories,
  277.                     array()
  278.                 );
  279.             }
  280.             return true;
  281.         } else {
  282.             return false;
  283.         }
  284.     }
  285.     public function getProduct($product_id) {
  286.         return $this->em->getRepository($this->repo)->find((int)$product_id);
  287.     }
  288.     public function getHolder($holder_id) {
  289.         return $this->em->getRepository($this->repo_holders)->find((int)$holder_id);
  290.     }
  291.     public function getHolders() {
  292.         return $this->em->getRepository($this->repo_holders)->findAll();
  293.     }
  294.     public function getEshops(Array $eshops null) {
  295.         $ret = array();
  296.         $eshops = (count($eshops) >= 1) ? $eshops $this->em->getRepository('Kiwi\EshopBundle\Entity\Eshops')->findAll();
  297.         foreach($eshops as $eshop) {
  298.             $web $this->em->getRepository('Security\UserBundle\Entity\Web')->findOneBy(array('eshop_id' => $eshop->getId()));
  299.             if($web) {
  300.                 $languages = array();
  301.                 foreach($this->em->getRepository('Kiwi\LanguagesBundle\Entity\Languages')->findBy(array('web_id' => $web->getId())) as $language) {
  302.                     $languages[] = $language->getCode();
  303.                 }
  304.                 $ret[] = array(
  305.                     'id' => $eshop->getId(),
  306.                     'name' => $this->languages->getLangEntry($eshop->getName(),$this->languages->getDefaultLang($this->webHandle->getCurrentWeb()->getId())),
  307.                     'code' => $eshop->getCode(),
  308.                     'languages' => $languages
  309.                 );
  310.             }
  311.         }
  312.         return $ret;
  313.     }
  314.     public function getUsersEshops() {
  315.         $user $this->eshopUsersHandle->getCurrentUser();
  316.         if($user) {
  317.             $_eshops = array();
  318.             foreach($this->eshopUsersHandle->getUsersEshops($user->getId(),true) as $eshop) {
  319.                 $_eshops[] = $this->em->getRepository('Kiwi\EshopBundle\Entity\Eshops')->find($eshop['id']);
  320.             }
  321.             return $this->getEshops($_eshops);
  322.         } else {
  323.             return $this->getEshops();
  324.         }
  325.     }
  326.     public function getEshopById($eshop_id) {
  327.         return $this->em->getRepository('Kiwi\EshopBundle\Entity\Eshops')->find((int)$eshop_id);
  328.     }
  329.     public function getEshop() {
  330.         $web $this->webHandle->getCurrentWeb();
  331.         if(!$web)
  332.             return null;
  333.         return $web->getEshop();
  334.     }
  335.     public function checkForEshop() {
  336.         if(!$this->getEshop()) {
  337.             Header('Location: '.
  338.                 $this->controller->get('router')->generate('admin_webselector'));
  339.             exit();
  340.         }
  341.     }
  342.     public function getEshopDefaultLang($eshop_id) {
  343.         if($eshop_id >= 1) {
  344.             $web $this->webHandle->getWebByEshopId($eshop_id);
  345.             if($web) {
  346.                 return $this->languages->getDefaultLang($web->getId());
  347.             }
  348.         }
  349.         return null;
  350.     }
  351.     public function getProductByCode($product_code) {
  352.         return $this->em->getRepository($this->repo)->findOneBy(array('code' => addslashes($product_code)));
  353.     }
  354.     public function getProductImages(\Kiwi\EshopBundle\Entity\Product $product$lang 'cz') {
  355.         $ret = array();
  356.         if($product->getGallery()) {
  357.             foreach($this->imageGalleryHandle->getFolderImages($product->getGallery()->getId()) as $image) {
  358.                 $label $this->getProductName($product$lang);
  359.                 $label_alt $this->languages->getLangEntry($image->getAltName(),$lang);
  360.                 $ret[] = array(
  361.                     'id' => $image->getId(),
  362.                     'label' => ($label_alt != '') ? $label_alt $label
  363.                 );
  364.             }
  365.         }
  366.         return $ret;
  367.     }
  368.     public function getRecommended(\Kiwi\EshopBundle\Entity\Product $product$lang 'cz') {
  369.         $recommended $this->em->getRepository('Kiwi\EshopBundle\Entity\Contingents\ProductRecommended')->findBy(array('product_id' => $product->getId()));
  370.         $ret = [];
  371.         foreach($recommended as $recommend) {
  372.             $ret[] = $this->getProductReturnArrayBlock($recommend->getProductRecommended(), $lang, []);
  373.         }
  374.         return $ret;
  375.     }
  376.     //public function getProduct
  377.     public function getProductParams($product_id) {
  378.         return $this->em->getRepository($this->repo_params)->findBy(array(
  379.             'product_id' => (int)$product_id
  380.         ));
  381.     }
  382.     public function getProductParam($product_id$param_id) {
  383.         return $this->em->getRepository($this->repo_params)->findOneBy(array(
  384.             'product_id' => (int)$product_id,
  385.             'param_id' => (int)$param_id
  386.         ));
  387.     }
  388.     public function editHolderForm(int $holder_id$request$justCreateArticle false){
  389.         $holder $this->getHolder($holder_id);
  390.         $referer false;
  391.         if(isset($_GET['ref'])) {
  392.             if($_GET['ref'] != '') {
  393.                 $referer urldecode($_GET['ref']);
  394.             }
  395.         }
  396.         if(!$holder->getArticle()) {
  397.             $article $this->article->createBlankArticle($this->languages->getLangEntry($holder->getName(), 'cz'));
  398.             $holder->setArticle($article);
  399.             $this->em->persist($holder);
  400.             $this->em->flush($holder);
  401.             $this->em->refresh($holder);
  402.         }
  403.         if($justCreateArticle) {
  404.             return true;
  405.         }
  406.         $article_forms $this->article->getArticleUpdateForm($holder->getArticle(),$request,$referer);
  407.         $K $this->controller->get('K');
  408.         return $this->controller->get('BL')->setContent($K->getRows(array(
  409.             $K->getPageHeader('Editace článku / '.$this->languages->getLangEntry($holder->getArticle()->getName(), 'cz')),
  410.             $K->getRow(array(
  411.                 $K->getBox(
  412.                     $K->LABEL_HIDE_BOX,
  413.                     11,
  414.                     $article_forms,
  415.                     'Editace článku'
  416.                 )
  417.             ),true)
  418.         )))->getLayoutResponse();
  419.     }
  420.     public function getProductForm($product_id 0$holder_id 0) {
  421.         $is_product_update false;
  422.         if($product_id >= 1) {
  423.             $product $this->getProduct($product_id);
  424.             $is_product_update true;
  425.         } else {
  426.             $product = new Product();
  427.             $web $this->controller->get('web_handle')->getCurrentWeb();
  428.             $product->setEshop($web->getEshop());
  429.             $product->setIsHidden(false);
  430.             $product->setIsDeleted(false);
  431.             if($holder_id >= 1) {
  432.                 $product->setHolderId($holder_id);
  433.             }
  434.         }
  435.         $gallery $this->getProductGallery($product);
  436.         if($is_product_update) {
  437.             if(!$product->getGallery())
  438.                 $product->setGallery($gallery);
  439.         } else {
  440.             $product->setGallery($gallery);
  441.         }
  442.         $form $this->controller->get('form.factory')->createNamedBuilder('product_form'FormType::class, array());
  443.         $reflect = new \ReflectionClass(new $this->repo);
  444.         $tO = new Transformations\ProductFormTransformations();
  445.         $tO->setForm($form);
  446.         $tO->setFormData($product);
  447.         $tO->setController($this->controller);
  448.         $tO->setLanguagesHandle($this->languages);
  449.         foreach($reflect->getProperties() as $method) {
  450.             $method_name 'fm'.ucfirst($method->name);
  451.             if(method_exists($tO$method_name)) {
  452.                 $tO->$method_name($method->name);
  453.             } else {
  454.                 $tO->fmDefalut($method->name);
  455.             }
  456.         }
  457.         $form->add('product_id'HiddenType::class, array('data' => $product->getId()));
  458.         $form->add('ref'HiddenType::class, array('data' => $this->request->getRequestUri()));
  459.         $form->add('save'SubmitType::class, array('label' => 'Uložit / upravit'));
  460.         $form $form->getForm();
  461.         $form->handleRequest($this->request);
  462.         if($form->isValid()) {
  463.             $post_params = (isset($_POST['params'])) ? $_POST['params'] : array();
  464.             $this->handleProductUpdate($product$form->getData(), array_keys($_POST['product_category']), $post_params);
  465.             //remove gallery session persister
  466.             $session = new Session();
  467.             $session->remove('getProductGallery_freelink');
  468.             if($this->controller->get'kernel' )->getEnvironment() == 'dev') {
  469.                 Header("Location: /app_dev.php/kiwi/eshop/produkt/edit/".$product->getId());
  470.             } else {
  471.                 Header("Location: /kiwi/eshop/produkt/edit/".$product->getId());
  472.             }
  473.             exit();
  474.         }
  475.         $form_v $form->createView();
  476.         //gallery folder
  477.         $tnObj $this->controller->get('ad13_tree_navigation');
  478.         $tnObj->setRepository('\Kiwi\ImageGalleryBundle\Entity\ImageFolderTree');
  479.         $tnObj->setController($this->controller);
  480.         $tnObj->setRequest($this->request);
  481.         $tnObj->setLevel($gallery->getId());
  482.         $tnObj->setRefreshPath('kiwi_module_image_gallery_folder_level');
  483.         $tnObj->setUploadPath('kiwi_module_image_gallery_folder_upload');
  484.         $tnObj->setFileDownloadPath('kiwi_module_image_gallery_folder_download_file');
  485.         $K $this->controller->get('K');
  486.         $languages_codes $this->languages->getLanguagesCodes();
  487.         //Attachements
  488.         $attachements_row null;
  489.         $recommended_row null;
  490.         $holderproducts_row null;
  491.         if($is_product_update) {
  492.             $attachements_holders $this->controller->get('eshop_attachements_handle')->getAttachementsHolders();
  493.             $attachements $this->attachementsHandle->getProductAttachements($product_id);
  494.             foreach($attachements_holders as &$ah) {
  495.                 $ah->current_file null;
  496.                 foreach($attachements as $attachement) {
  497.                     if($ah->getId() == $attachement->getAttachmentId()) {
  498.                         $ah->current_file $attachement->getFile();
  499.                         break;
  500.                     }
  501.                 }
  502.             }
  503.             $attachements_row $K->getRow(array(
  504.                 $K->getTwigBox(
  505.                     $K->LABEL_HIDE_BOX,
  506.                     $K->WIDTH_FULL,
  507.                     'KiwiEshopBundle:Forms:product_attachments.html.twig',
  508.                     array(
  509.                         'referer' => '',
  510.                         'lang' => $this->languages->getDefaultLang(),
  511.                         'product_id' => $product->getId(),
  512.                         'attachements_holders' => $attachements_holders
  513.                     ),
  514.                     'Přílohy'
  515.                 )
  516.             ),false);
  517.             $form_recommended $this->controller->get('form.factory')->createNamedBuilder('product_recommended_form'FormType::class, array());
  518.             $form_recommended->add('product_name'TextType::class);
  519.             $form_recommended->add('product_id'HiddenType::class);
  520.             $form_recommended->add('ref'HiddenType::class, array('data' => $this->request->getRequestUri()));
  521.             $form_recommended->add('save'SubmitType::class, array('label' => 'Uložit / upravit'));
  522.             $form_recommended $form_recommended->getForm();
  523.             $form_recommended->handleRequest($this->request);
  524.             if($form_recommended->isValid()) {
  525.                 $post_params = (isset($_POST['params'])) ? $_POST['params'] : array();
  526.                 $recommended_obj = new ProductRecommended();
  527.                 $recommended_obj->setProduct($product);
  528.                 $recommended_obj->setProductRecommended(
  529.                     $this->getProduct($form_recommended->getData()['product_id'])
  530.                 );
  531.                 $this->em->persist($recommended_obj);
  532.                 $this->em->flush();
  533.                 Header("Location: ".$form_recommended->getData()['ref']."#recommended");
  534.                 exit();
  535.             }
  536.             $recommended_row $K->getRow(array(
  537.                 $K->getTwigBox(
  538.                     $K->LABEL_HIDE_BOX,
  539.                     $K->WIDTH_FULL,
  540.                     'KiwiEshopBundle:Forms:product_recommended.html.twig',
  541.                     array(
  542.                         'form' => $form_recommended->createView(),
  543.                         'recommended' => $this->getRecommended($product),
  544.                         'product' => $product,
  545.                         'ref' => $this->request->getRequestUri()
  546.                     ),
  547.                     'Doporučené produkty'
  548.                 )
  549.             ),false);
  550.             if($product->getIsHolder()) {
  551.                 $search $this->controller->get('search_handle');
  552.                 //->setRequest($request)
  553.                 $search->setFrom('Kiwi\EshopBundle\Entity\Product','p')
  554.                     ->addWhere(sprintf("p.holder_id = '%d'"$product->getId()))
  555.                     ->setOrderBy('p.sort_num''ASC')
  556.                     ->setRpp(100);
  557.                 $inholder_products = [];
  558.                 foreach ($search->getResult()['data'] as $inholder_product) {
  559.                     $inholder_products[] = $this->getProductReturnArrayBlock($inholder_product'cz', []);
  560.                 }
  561.                 $form_inproduct $this->controller->get('form.factory')->createNamedBuilder('holder_inproducts_form'FormType::class, array());
  562.                 $form_inproduct->add('product_name'TextType::class);
  563.                 $form_inproduct->add('product_id'HiddenType::class);
  564.                 $form_inproduct->add('ref'HiddenType::class, array('data' => $this->request->getRequestUri()));
  565.                 $form_inproduct->add('save'SubmitType::class, array('label' => 'Uložit'));
  566.                 $form_inproduct $form_inproduct->getForm();
  567.                 $form_inproduct->handleRequest($this->request);
  568.                 if($form_inproduct->isValid()) {
  569.                     $formdata $this->request->get('holder_inproducts_form');
  570.                     $inproduct_id = (isset($formdata['product_id'])) ? (int)$formdata['product_id'] : 0;
  571.                     if($inproduct_id >= 1) {
  572.                         $inproduct $this->getProduct($inproduct_id);
  573.                         $inproduct->setHolder($product);
  574.                         $this->em->persist($inproduct);
  575.                         $this->em->flush();
  576.                     }
  577.                     Header("Location: ".$formdata['ref']."#inholder_products");
  578.                     exit;
  579.                 }
  580.                 $holderproducts_row $K->getRow(array(
  581.                     $K->getTwigBox(
  582.                         $K->LABEL_HIDE_BOX,
  583.                         $K->WIDTH_FULL,
  584.                         'KiwiEshopBundle:Forms:product_holderproducts.html.twig',
  585.                         array(
  586.                             'form' => $form_inproduct->createView(),
  587.                             'products' => $inholder_products,
  588.                             'ref' => $this->request->getRequestUri()
  589.                         ),
  590.                         /*
  591.                         array(
  592.                             'form' => $form_recommended->createView(),
  593.                             'recommended' => $this->getRecommended($product),
  594.                             'product' => $product,
  595.                             'ref' => $this->request->getRequestUri()
  596.                         ),
  597.                         */
  598.                         'Produkty v holderu'
  599.                     )
  600.                 ),false);
  601.             }
  602.         }
  603.         $inner_view $K->getRows(array(
  604.             $K->getPageHeader('Vložení nového produktu'),
  605.             $K->getRow(array(
  606.                 $K->getTwigBox(
  607.                     $K->LABEL_HIDE_BOX,
  608.                     7,
  609.                     'KiwiEshopBundle:Forms:product_main.html.twig',
  610.                     array('form' => $form_v'languages' => $languages_codes),
  611.                     'Základní parametry produktu'
  612.                 ),
  613.                 $K->getTwigBox(
  614.                     $K->LABEL_HIDE_BOX,
  615.                     5,
  616.                     'KiwiEshopBundle:Forms:product_price.html.twig',
  617.                     array('form' => $form_v),
  618.                     'Cena'
  619.                 )
  620.             ),false),
  621.             $K->getRow(array(
  622.                 $K->getTwigBox(
  623.                     $K->LABEL_HIDE_BOX,
  624.                     8,
  625.                     'KiwiEshopBundle:Forms:product_categories.html.twig',
  626.                     array('data' => $this->eshopCategoriesHandle->getProductAssociationForm($product)),
  627.                     'Kategorie'
  628.                 ),
  629.                 $K->getTwigBox(
  630.                     $K->LABEL_HIDE_BOX,
  631.                     4,
  632.                     'KiwiEshopBundle:Forms:product_flags.html.twig',
  633.                     array('form' => $form_v),
  634.                     'Flagy'
  635.                 )
  636.             ),false),
  637.             $K->getRow(array(
  638.                 $K->getTwigBox(
  639.                     $K->LABEL_HIDE_BOX,
  640.                     12,
  641.                     'KiwiEshopBundle:Forms:product_params.html.twig',
  642.                     array('data' => $this->eshopParamsHandle->getProductParamsForm($product)),
  643.                     'Volitelné parametry'
  644.                 )
  645.             ),false),
  646.             $K->getRow(array(
  647.                 $K->getTwigBox(
  648.                     $K->LABEL_HIDE_BOX,
  649.                     12,
  650.                     'KiwiEshopBundle:Forms:product_description.html.twig',
  651.                     array('form' => $form_v'languages' => $languages_codes),
  652.                     'Hlavní popis'
  653.                 )
  654.             ),false),
  655.             $K->getRow(array(
  656.                 $K->getTwigBox(
  657.                     $K->LABEL_HIDE_BOX,
  658.                     12,
  659.                     'KiwiEshopBundle:Forms:product_action.html.twig',
  660.                     array('form' => $form_v'languages' => $languages_codes),
  661.                     'Akce'
  662.                 )
  663.             ),false),
  664.             $K->getRow(array(
  665.                 $K->getTwigBox(
  666.                     $K->LABEL_HIDE_BOX,
  667.                     12,
  668.                     'KiwiEshopBundle:Forms:product_holder.html.twig',
  669.                     array('form' => $form_v'languages' => $languages_codes'product' => $product),
  670.                     'Vícenásobný produkt'
  671.                 )
  672.             ),true),
  673.             $K->getRow(array(
  674.                 $K->getTwigBox(
  675.                     $K->LABEL_HIDE_BOX,
  676.                     12,
  677.                     'KiwiEshopBundle:Forms:product_submit.html.twig',
  678.                     array('form' => $form_v),
  679.                     'Uložení / úprava produktu'
  680.                 )
  681.             ),true),
  682.             $K->getRow(array(
  683.                 $K->getBox(
  684.                     $K->LABEL_HIDE_BOX,
  685.                     $K->WIDTH_FULL,
  686.                     '<div class="tree_navigator gallery_sortable">'.$this->controller->get('image_gallery_handle')->setRefPath($_SERVER['REQUEST_URI'])->disablePlaceholder()->getFolderBrowser($gallery->getId()).'</div>',
  687.                     'Galerie'
  688.                 )
  689.             ),false),
  690.             $K->getRow(array(
  691.                 $K->getBox(
  692.                     $K->LABEL_HIDE_BOX,
  693.                     $K->WIDTH_THIRD,
  694.                     $tnObj->getUploader(array('{title : "Image files", extensions : "jpg,gif,png"}')),
  695.                     'Upload souborů do galerie'
  696.                 ),
  697.                 $K->getBox(
  698.                     $K->LABEL_HIDE_BOX,
  699.                     $K->WIDTH_THIRD,
  700.                     $tnObj->getFileDownloader(),
  701.                     'Zdroje'
  702.                 )
  703.             ),false),
  704.             $attachements_row,
  705.             $recommended_row,
  706.             $holderproducts_row
  707.         ));
  708.         $edit_canvas $this->controller->get('templating')->render('KiwiEshopBundle:Forms:product_form_holder.html.twig',array(
  709.             'form' => $form_v,
  710.             'content' => '<script src="\bundles\Kiwi\js\plugins\tinymce\js\tinymce\tinymce.min.js"></script>'.$inner_view
  711.         ));
  712.         return $this->controller->get('BL')
  713.                                 ->setContent($edit_canvas,true,true)
  714.                                 ->getLayoutResponse();
  715.     }
  716.     public function dropRecommendedProduct($product_id$recommended_product_id) {
  717.         $recommended_ref $this->em->getRepository('Kiwi\EshopBundle\Entity\Contingents\ProductRecommended')->findOneBy(array(
  718.             'product_id' => $product_id,
  719.             'product_recommended_id' => $recommended_product_id
  720.         ));
  721.         $this->em->remove($recommended_ref);
  722.         $this->em->flush();
  723.     }
  724.     public function removeProductHolder($product_id) {
  725.         $product $this->getProduct($product_id);
  726.         $product->setHolder(null);
  727.         $this->em->persist($product);
  728.         $this->em->flush();
  729.     }
  730.     public function unDropProduct($product_id) {
  731.         $product $this->getProduct($product_id);
  732.         $product->setIsDeleted(false);
  733.         $this->em->persist($product);
  734.         $this->em->flush();
  735.     }
  736.     public function dropProduct($product_id) {
  737.         $product $this->getProduct($product_id);
  738.         $product->setIsDeleted(true);
  739.         $this->em->persist($product);
  740.         $this->em->flush();
  741.     }
  742.     public function unInvisibleProduct($product_id) {
  743.         $product $this->getProduct($product_id);
  744.         $product->setIsHidden(false);
  745.         $this->em->persist($product);
  746.         $this->em->flush();
  747.     }
  748.     public function invisibleProduct($product_id) {
  749.         $product $this->getProduct($product_id);
  750.         $product->setIsHidden(true);
  751.         $this->em->persist($product);
  752.         $this->em->flush();
  753.     }
  754.     public function getProductGallery(\Kiwi\EshopBundle\Entity\Product $product$enable_sessions true) {
  755.         $gallery $product->getGallery();
  756.         if(!$gallery) {
  757.             //if persist in session, return them instead of create new one
  758.             if($enable_sessions) {
  759.                 $session = new Session();
  760.                 if($session->get('getProductGallery_freelink'))
  761.                     return $this->imageGalleryHandle->getFolder($session->get('getProductGallery_freelink'));
  762.             }
  763.             //find if root folder exists
  764.             $root_folder null;
  765.             foreach($this->imageGalleryHandle->getFolderByName($this->root_image_folder_name) as $folder) {
  766.                 if(!$folder->getParentRow()) {
  767.                     $root_folder $folder;
  768.                     break;
  769.                 }
  770.             }
  771.             //create if not
  772.             if(!$root_folder) {
  773.                 $root_folder = new ImageFolderTree();
  774.                 $root_folder->setName($this->root_image_folder_name);
  775.                 $root_folder->setPreventChanges(true);
  776.                 $root_folder->setSystemFolder(true);
  777.                 $root_folder->setWeb($this->controller->get('web_handle')->getCurrentWeb());
  778.                 $this->em->persist($root_folder);
  779.                 $this->em->flush();
  780.             }
  781.             $gallery = new ImageFolderTree();
  782.             $gallery->setName($this->root_image_sub_folder_name.$product->getId());
  783.             $gallery->setParentRow($root_folder);
  784.             $gallery->setPreventChanges(true);
  785.             $gallery->setSystemFolder(true);
  786.             //$gallery->setWeb($this->controller->get('web_handle')->getCurrentWeb());
  787.             $gallery->setWeb($this->controller->get('web_handle')->getWebByEshopId($product->getEshop()->getId()));
  788.             $this->em->persist($gallery);
  789.             $this->em->flush();
  790.             if($enable_sessions)
  791.                 $session->set('getProductGallery_freelink',$gallery->getId());
  792.         }
  793.         return $gallery;
  794.     }
  795.     private function getProductCategories(\Kiwi\EshopBundle\Entity\Product $product) {
  796.     }
  797.     private function getSetMethodName($variable_name) {
  798.         $get_method str_replace('_',' ',$variable_name);
  799.         $get_method ucwords($get_method);
  800.         $get_method str_replace(' ','',$get_method);
  801.         return 'set'.$get_method;
  802.     }
  803.     private function getGetMethodName($variable_name) {
  804.         $get_method str_replace('_',' ',$variable_name);
  805.         $get_method ucwords($get_method);
  806.         $get_method str_replace(' ','',$get_method);
  807.         return 'get'.$get_method;
  808.     }
  809.     //partialy frontend methods
  810.     private function findNotificationEmails(\Kiwi\EshopBundle\Entity\KategorieTree $kategorieTree,$be_verbose){
  811.         $ret '';
  812.         //$kategorieTree = $this->eshopCacheHandle->getHydrated($kategorieTree);
  813.         if($be_verbose) {
  814.             echo 'looking in '.$kategorieTree->getId().'/'.$kategorieTree->getInfoEmails().'<br>';
  815.         }
  816.         if($kategorieTree->getInfoEmails()) {
  817.             if($kategorieTree->getInfoEmails() != '') {
  818.                 $ret $kategorieTree->getInfoEmails();
  819.             } else {
  820.                 $parentRow $this->eshopCacheHandle->getHydrated($kategorieTree->getParentRow());
  821.                 if($parentRow) {
  822.                     $ret $this->findNotificationEmails($parentRow,$be_verbose);
  823.                 }
  824.             }
  825.         } else {
  826.             $parentRow $this->eshopCacheHandle->getHydrated($kategorieTree->getParentRow());
  827.             if($parentRow) {
  828.                 $ret $this->findNotificationEmails($parentRow,$be_verbose);
  829.             }
  830.         }
  831.         return $ret;
  832.     }
  833.     public function findProduct($lang$product_id) {
  834.         $product $this->getProduct($product_id);
  835.         //kategorie produktu
  836.         $product_categories = array();
  837.         $product_order_notifications_emails = array();
  838.         foreach($this->controller->get('eshop_categories_handle')->getProductCategories($product) as $product_category) {
  839.             $product_categories[] = $product_category->getCategoryId();
  840.             $category $this->eshopCacheHandle->getHydrated($product_category->getCategory());
  841.             $emails_raw $category->getInfoEmails();
  842.             $be_verbose false;
  843.             if($emails_raw == '')
  844.                 $emails_raw $this->findNotificationEmails($category,$be_verbose);
  845.             foreach(explode(',',$emails_raw) as $email) {
  846.                 if($email != '')
  847.                     $product_order_notifications_emails[] = str_replace(' ','',$email);
  848.             }
  849.             $product_order_notifications_emails array_unique($product_order_notifications_emails);
  850.         }
  851.         //siblings getter
  852.         $siblings = array();
  853.         if($product->getHolder()) {
  854.             foreach($this->em->getRepository($this->repo)->findBy(array('holder_id' => $product->getHolder()->getId())) as $sibling_product) {
  855.                 if($sibling_product->getStockAlt() >= 1) {
  856.                     if($sibling_product->getId() != $product->getId())
  857.                         $siblings[] = $this->getProductReturnArrayBlock($sibling_product,$lang,array());
  858.                 }
  859.             }
  860.         }
  861.         //similars getter
  862.         $similars = array();
  863.         if(count($product_categories) >= 1) {
  864.             $qb_similar $this->em->createQueryBuilder();
  865.             $qb_similar->select('p')
  866.                 ->from($this->repo_categories'c')
  867.                 ->leftJoin($this->repo'p''WITH''p.id = c.product_id')
  868.                 ->where('p.is_deleted = 0')
  869.                 ->andWhere('p.eshop_id = '.$product->getEshopId())
  870.                 ->andWhere('p.stock_alt >= 1');
  871.             if($product->getHolder())
  872.                 $qb_similar->andWhere('p.holder_id != '.$product->getHolder()->getId());
  873.             $qb_similar->andWhere('c.category_id IN ('.implode(","$product_categories).')')
  874.                 ->orderBy('p.id''DESC')
  875.                 //->groupBy('p.holder_id')
  876.                 ->setMaxResults(10);
  877.             foreach($qb_similar->getQuery()->getResult() as $similar_product) {
  878.                 if($similar_product->getId() != $product->getId())
  879.                     $similars[] = $this->getProductReturnArrayBlock($similar_product,$lang,array());
  880.             }
  881.         }
  882.         return $this->getProductReturnArrayBlock($product,$lang,$product_categories$siblings$similars);
  883.     }
  884.     public function findProducts($lang$limit = array(), $eshop 1$group_by_holder false$minimal_stock 0) {
  885.         $this->logClean();
  886.         /*
  887.         $list_in_eshops = array($this->getEshop()->getId());
  888.         $user = $this->eshopUsersHandle->getCurrentUser();
  889.         if($user) {
  890.             $list_in_eshops = array();
  891.             foreach($this->eshopUsersHandle->getUsersEshops($user->getId(),true) as $a_eshop) {
  892.                 $list_in_eshops[] = $a_eshop['id'];
  893.             }
  894.         }
  895.         */
  896.         /*
  897.         $data = $this->em->getRepository($this->repo)->findBy(
  898.             array(),
  899.             array(),
  900.             99000,
  901.             0
  902.         );
  903.         */
  904.         $eshop = ($eshop >= 1) ? $eshop 1;
  905.         $group_by = ($group_by_holder) ? 'p.holder_id' 'p.id';
  906.         $results_pp 20;
  907.         $results_page 1;
  908.         if(isset($limit['results_page'])) {
  909.             if($limit['results_page'] >= 2) {
  910.                 $results_page = (int)$limit['results_page'];
  911.             }
  912.         }
  913.         if(isset($limit['results_per_page'])) {
  914.             if($limit['results_per_page'] >= 1) {
  915.                 $results_pp = (int)$limit['results_per_page'];
  916.             }
  917.         }
  918.         $dynamic_params_grouped = array();
  919.         if(isset($limit['dynamic-params'])) {
  920.             if(is_array($limit['dynamic-params'])) {
  921.                 //group by category
  922.                 foreach($limit['dynamic-params'] as $dynamic_param) {
  923.                     $_dynamic_param explode('@',$dynamic_param);
  924.                     $dynamic_params_grouped[$_dynamic_param[0]][] = $_dynamic_param[1];
  925.                 }
  926.             }
  927.         }
  928.         $qb $this->em->createQueryBuilder();
  929.         $qb->select('p')
  930.            ->from($this->repo_categories'c')
  931.            ->leftJoin($this->repo'p''WITH''p.id = c.product_id')
  932.            ->leftJoin($this->repo_params'params''WITH''params.product_id = p.id');
  933.            //->leftJoin($this->repo_params, 'params', 'WITH', 'params.product_id = p.id AND params.param_id = 8')
  934.         if(isset($limit['dynamic-params'])) {
  935.             if(is_array($limit['dynamic-params'])) {
  936.                 foreach($dynamic_params_grouped as $group_id => $group_data) {
  937.                     $qb->leftJoin($this->repo_params'params'.$group_id'WITH''params'.$group_id.'.product_id = p.id');
  938.                 }
  939.             }
  940.         }
  941.         $qb->where('p.is_deleted = 0')
  942.            //->andwhere('p.eshop_id = '.$this->getEshop()->getId())
  943.             //->add('where', $qb->expr()->in('p.eshop_id', $list_in_eshops))
  944.             //->add('where', 'p.eshop_id = '.$eshop)
  945.             ->andWhere('p.eshop_id = '.$eshop)
  946.             ->andWhere('p.stock_alt >= '.$minimal_stock)
  947.            //->orderBy('p.price_with_vat', 'DESC');
  948.            ->orderBy('p.id''DESC')
  949.            ->groupBy($group_by);
  950.         //params.param_value LIKE '%".$limit['fulltext_search']."%' OR
  951.         if(isset($limit['fulltext_search'])) {
  952.             if($limit['fulltext_search'] != '') {
  953.                 /*
  954.                 $xml_part = '\'<?xml version="1.0" encoding="utf-8" ?>%<cz><![CDATA[%'.strtolower($limit['fulltext_search']).'%]]></cz>%</data>\'';
  955.                 */
  956.                 $qb->andWhere("(
  957.                     p.code LIKE '%".$limit['fulltext_search']."' OR
  958.                     p.name LIKE '%".$limit['fulltext_search']."%' OR
  959.                     p.name_alt_1 LIKE '%".$limit['fulltext_search']."%' OR
  960.                     p.name_alt_2 LIKE '%".$limit['fulltext_search']."%'
  961.                 )");
  962.                 /*
  963.                 params.param_value LIKE '%".$limit['fulltext_search']."%' OR
  964.                 p.code LIKE '%".$limit['fulltext_search']."%' OR
  965.                 p.name LIKE $xml_part OR
  966.                 p.name_alt_1 LIKE $xml_part OR
  967.                 p.name_alt_2 LIKE $xml_part
  968.                 */
  969.             }
  970.         }
  971.         if(isset($limit['category'])) {
  972.             if(is_array($limit['category'])) {
  973.                 $categories = array();
  974.                 foreach($limit['category'] as $limit_category) {
  975.                     if($limit_category >= 1) {
  976.                         $categories[] = $limit_category;
  977.                         $subcategories $this->controller->get('eshop_categories_handle')->getCategories($limit_categorytrue);
  978.                         if(is_array($subcategories))
  979.                             $categories array_merge($categories,$subcategories);
  980.                     }
  981.                 }
  982.                 if(count($categories) >= 1)
  983.                     $qb->andWhere('c.category_id IN ('.implode(","$categories).')');
  984.             }
  985.         }
  986.         if(isset($limit['producer'])) {
  987.             if(is_array($limit['producer'])) {
  988.                 $qb->andWhere('p.producer_id IN ('.implode(","$limit['producer']).')');
  989.             }
  990.         }
  991.         if(isset($limit['dynamic-params'])) {
  992.             if(is_array($limit['dynamic-params'])) {
  993.                 //make groups independent wheres
  994.                 foreach($dynamic_params_grouped as $group_id => $group_data) {
  995.                     $dynamic_params_likes '';
  996.                     foreach($group_data as $agd) {
  997.                         if(is_numeric($agd)) {
  998.                             $dynamic_params_likes .= "params".$group_id.".param_value LIKE '%i:".addslashes($agd)."%' OR ";
  999.                         } else {
  1000.                             $dynamic_params_likes .= "params".$group_id.".param_value = '".base64_decode($agd)."' OR ";
  1001.                         }
  1002.                     }
  1003.                     if($dynamic_params_likes != '') {
  1004.                         $dynamic_params_likes substr($dynamic_params_likes,0,-4);
  1005.                         $qb->andWhere("($dynamic_params_likes)");
  1006.                     }
  1007.                 }
  1008.             }
  1009.         }
  1010.         if(isset($limit['holder'])) {
  1011.             if(is_numeric($limit['holder'])) {
  1012.                 $qb->andWhere('p.holder_id = '.$limit['holder']);
  1013.             }
  1014.         }
  1015.         if(isset($limit['not-holder'])) {
  1016.             $qb->andWhere('p.is_holder = 0');
  1017.         }
  1018.         if(isset($limit['not-holder-member'])) {
  1019.             $qb->andWhere('p.holder_id IS NULL');
  1020.         }
  1021.         if(isset($limit['action'])) {
  1022.             if(is_numeric($limit['action'])) {
  1023.                 $qb->andWhere('p.is_action = '.$limit['action']);
  1024.             }
  1025.         }
  1026.         $qb_nolimit = clone $qb;
  1027.         $qb_forcount = clone $qb;
  1028.         if(isset($limit['letter'])) {
  1029.             if($limit['letter'] != '') {
  1030.                 $qb->andWhere("p.name_letter = '".$limit['letter']."'");
  1031.             }
  1032.         }
  1033.         //$qb->groupBy('p.holder_id');
  1034.         $qb->setFirstResult(($results_page*$results_pp)-$results_pp)->setMaxResults($results_pp+1);
  1035.         $query $qb->getQuery();
  1036.         //echo $query->getSQL();
  1037.         $this->logState($query->getSQL());
  1038.         //exit;
  1039.         $data $query->getResult();
  1040.         $truncateCzech = function($text){
  1041.             $prevodni_tabulka = Array(
  1042.                 'ä'=>'a',
  1043.                 'Ä'=>'A',
  1044.                 'á'=>'a',
  1045.                 'Á'=>'A',
  1046.                 'à'=>'a',
  1047.                 'À'=>'A',
  1048.                 'ã'=>'a',
  1049.                 'Ã'=>'A',
  1050.                 'â'=>'a',
  1051.                 'Â'=>'A',
  1052.                 'č'=>'c',
  1053.                 'Č'=>'C',
  1054.                 'ć'=>'c',
  1055.                 'Ć'=>'C',
  1056.                 'ď'=>'d',
  1057.                 'Ď'=>'D',
  1058.                 'ě'=>'e',
  1059.                 'Ě'=>'E',
  1060.                 'é'=>'e',
  1061.                 'É'=>'E',
  1062.                 'ë'=>'e',
  1063.                 'Ë'=>'E',
  1064.                 'è'=>'e',
  1065.                 'È'=>'E',
  1066.                 'ê'=>'e',
  1067.                 'Ê'=>'E',
  1068.                 'í'=>'i',
  1069.                 'Í'=>'I',
  1070.                 'ï'=>'i',
  1071.                 'Ï'=>'I',
  1072.                 'ì'=>'i',
  1073.                 'Ì'=>'I',
  1074.                 'î'=>'i',
  1075.                 'Î'=>'I',
  1076.                 'ľ'=>'l',
  1077.                 'Ľ'=>'L',
  1078.                 'ĺ'=>'l',
  1079.                 'Ĺ'=>'L',
  1080.                 'ń'=>'n',
  1081.                 'Ń'=>'N',
  1082.                 'ň'=>'n',
  1083.                 'Ň'=>'N',
  1084.                 'ñ'=>'n',
  1085.                 'Ñ'=>'N',
  1086.                 'ó'=>'o',
  1087.                 'Ó'=>'O',
  1088.                 'ö'=>'o',
  1089.                 'Ö'=>'O',
  1090.                 'ô'=>'o',
  1091.                 'Ô'=>'O',
  1092.                 'ò'=>'o',
  1093.                 'Ò'=>'O',
  1094.                 'õ'=>'o',
  1095.                 'Õ'=>'O',
  1096.                 'ő'=>'o',
  1097.                 'Ő'=>'O',
  1098.                 'ř'=>'r',
  1099.                 'Ř'=>'R',
  1100.                 'ŕ'=>'r',
  1101.                 'Ŕ'=>'R',
  1102.                 'š'=>'s',
  1103.                 'Š'=>'S',
  1104.                 'ś'=>'s',
  1105.                 'Ś'=>'S',
  1106.                 'ť'=>'t',
  1107.                 'Ť'=>'T',
  1108.                 'ú'=>'u',
  1109.                 'Ú'=>'U',
  1110.                 'ů'=>'u',
  1111.                 'Ů'=>'U',
  1112.                 'ü'=>'u',
  1113.                 'Ü'=>'U',
  1114.                 'ù'=>'u',
  1115.                 'Ù'=>'U',
  1116.                 'ũ'=>'u',
  1117.                 'Ũ'=>'U',
  1118.                 'û'=>'u',
  1119.                 'Û'=>'U',
  1120.                 'ý'=>'y',
  1121.                 'Ý'=>'Y',
  1122.                 'ž'=>'z',
  1123.                 'Ž'=>'Z',
  1124.                 'ź'=>'z',
  1125.                 'Ź'=>'Z'
  1126.             );
  1127.             return strtr($text$prevodni_tabulka);
  1128.         };
  1129.         $ret = array();
  1130.         $ret_nofilter = array();
  1131.         $ret_nolettermatter = array();
  1132.         foreach($data as $product) {
  1133.             $pass_product true;
  1134.             $pass_product_by_category true;
  1135.             $pass_product_without_letter_matter true;
  1136.             //$a_name = $this->languages->getLangEntry($product->getName(),$lang);
  1137.             //kategorie produktu
  1138.             $product_categories = array();
  1139.             $product_order_notifications_emails = array();
  1140.             foreach($this->controller->get('eshop_categories_handle')->getProductCategories($product) as $product_category) {
  1141.                 $product_categories[] = $product_category->getCategoryId();
  1142.                 $category $this->eshopCacheHandle->getHydrated($product_category->getCategory());
  1143.                 $emails_raw $category->getInfoEmails();
  1144.                 $be_verbose false;
  1145.                 if($emails_raw == '')
  1146.                     $emails_raw $this->findNotificationEmails($category,$be_verbose);
  1147.                 foreach(explode(',',$emails_raw) as $email) {
  1148.                     if($email != '')
  1149.                         $product_order_notifications_emails[] = str_replace(' ','',$email);
  1150.                 }
  1151.                 $product_order_notifications_emails array_unique($product_order_notifications_emails);
  1152.             }
  1153.             //FILTR -- ceny
  1154.             if(isset($limit['price_range'])) {
  1155.                 if(is_array($limit['price_range'])) {
  1156.                     $a_price $product->getPrice();
  1157.                     if($a_price['price_with_vat'] >= $limit['price_range'][0] && $a_price['price_with_vat'] <= $limit['price_range'][1]) {} else {
  1158.                         $pass_product false;
  1159.                         $pass_product_without_letter_matter false;
  1160.                     }
  1161.                 }
  1162.             }
  1163.             $productArrayBlock $this->getProductReturnArrayBlock($product,$lang,$product_categories);
  1164.             //append notifications emails by theirs categories
  1165.             $productArrayBlock['notifications_emails'] = $product_order_notifications_emails;
  1166.             if($pass_product) {
  1167.                 $ret[] = $productArrayBlock;
  1168.             }
  1169.             if($pass_product_by_category) {
  1170.                 $ret_nofilter[] = $productArrayBlock;
  1171.             }
  1172.             if($pass_product_without_letter_matter) {
  1173.                 $ret_nolettermatter[] = $productArrayBlock;
  1174.             }
  1175.         }
  1176.         $sort_name = function($a,$b){
  1177.             return strcmp($a["name"], $b["name"]);
  1178.         };
  1179.         usort($ret,$sort_name);
  1180.         uasort($ret_nofilter,$sort_name);
  1181.         uasort($ret_nolettermatter,$sort_name);
  1182.         $ret array_values($ret);
  1183.         $ret_nofilter array_values($ret_nofilter);
  1184.         //echo count($qb_forcount->getQuery()->getResult());
  1185.         //exit();
  1186.         return array(
  1187.             'filtered' => array_slice($ret,0,$results_pp),
  1188.             'nofilter' => $ret_nofilter,
  1189.             'limit' => $limit,
  1190.             'results_page' => $results_page,
  1191.             'has_more_results' => (count($ret) > $results_pp) ? true false,
  1192.             'total_results' => count($qb_forcount->getQuery()->getResult()),
  1193.             'q' => $qb->getDQL()
  1194.         );
  1195.     }
  1196.     public function getProductReturnArrayBlock(\Kiwi\EshopBundle\Entity\Product $product$lang$product_categories$siblings = array(), $similars = array()) {
  1197.         //$cache = $this->eshopCacheHandle->setCacheKey('farline-eshop-product-'.$lang.'-'.$product->getId().'-compiled');
  1198.         //if($cache->isKeySet()) {
  1199.         $cache false;
  1200.         if($cache) {
  1201.             return $cache->getData();
  1202.         } else {
  1203.             $images = array();
  1204.             $images_data $this->getProductImages($product$lang);
  1205.             $images_helper $this->controller->get('image_twig_extension');
  1206.             $stockObj $this->controller->get('eshop_stock_handle');
  1207.             foreach($images_data as $image) {
  1208.                 $images[] = array(
  1209.                     'id' => $image['id'],
  1210.                     'label' => $image['label'],
  1211.                     'list' => $image['id'],
  1212.                     'full' => $images_helper->imageCutFunction($image['id'],670,0),
  1213.                     'maxi' => $images_helper->imageCutFunction($image['id'],1920,0)
  1214.                 );
  1215.             }
  1216.             $attachments = array();
  1217.             foreach($this->attachementsHandle->getProductAttachements($product->getId()) as $attachement_row) {
  1218.                 $attachments[] = array(
  1219.                     'name' => $this->languages->getLangEntry($attachement_row->getAttachment()->getName(),$lang),
  1220.                     'link' => '/dt/'.$attachement_row->getFile()->getHashName().'.'.$attachement_row->getFile()->getExtension(),
  1221.                     'extension' => $attachement_row->getFile()->getExtension()
  1222.                 );
  1223.             }
  1224.             $main_image $this->no_image;
  1225.             if(isset($images[0]))
  1226.                 $main_image $images[0]['list'];
  1227.             $main_product_image $this->no_image;
  1228.             if(isset($images[0]))
  1229.                 $main_product_image $images[0]['full'];
  1230.             $unit false;
  1231.             if($product->getUnit()) {
  1232.                 $unit = array(
  1233.                     'name' => $this->languages->getLangEntry($product->getUnit()->getName(),$lang),
  1234.                     'code' => $product->getUnit()->getCode(),
  1235.                 );
  1236.             }
  1237.             $currency false;
  1238.             if($product->getCurrency()) {
  1239.                 $currency = array(
  1240.                     'name' => $this->languages->getLangEntry($product->getCurrency()->getName(),$lang),
  1241.                     'code' => $product->getCurrency()->getCode(),
  1242.                 );
  1243.             }
  1244.             $producer false;
  1245.             if($product->getProducer()) {
  1246.                 $image '';
  1247.                 $image2 '';
  1248.                 if($product->getProducer()->getImage())
  1249.                     $image '/ds/orig/'.$product->getProducer()->getImage()->getName().'.'.$product->getProducer()->getImage()->getExtension();
  1250.                 if($product->getProducer()->getImage2())
  1251.                     $image2 '/ds/orig/'.$product->getProducer()->getImage2()->getName().'.'.$product->getProducer()->getImage2()->getExtension();
  1252.                 $eshop_producers_handle $this->controller->get('eshop_producers_handle');
  1253.                 $producer = array(
  1254.                     'id' => $product->getProducer()->getId(),
  1255.                     'name' => $this->languages->getLangEntry($product->getProducer()->getName(),$lang),
  1256.                     'image' => $image,
  1257.                     'image2' => $image2,
  1258.                     'full_link' => $eshop_producers_handle->getProducerLink($product->getProducer(), $lang)
  1259.                 );
  1260.             }
  1261.             $master_category = array(
  1262.                 'id' => 0,
  1263.                 'name' => null,
  1264.                 'title_name' => null
  1265.             );
  1266.             $master_category_data $this->eshopCategoriesHandle->getProductTopCategory($product);
  1267.             if($master_category_data) {
  1268.                 $master_category['id'] = $master_category_data->getId();
  1269.                 $master_category['name'] = $master_category_data->getName();
  1270.                 $master_category['title_name'] = $master_category_data->getTitle();
  1271.             }
  1272.             //return $cache->setData(
  1273.                 return array(
  1274.                     'id' => $product->getId(),
  1275.                     'code' => $product->getCode(),
  1276.                     'name' => $this->getProductName($product$lang),
  1277.                     //'name_holder' => $this->languages->getLangEntry($product->getHolder()->getArticle()->getName(), $lang),
  1278.                     'name_alt_1' => $this->languages->getLangEntry($product->getNameAlt1(), $lang),
  1279.                     'name_alt_2' => $this->languages->getLangEntry($product->getNameAlt2(), $lang),
  1280.                     'description' => str_replace("\n",'%NEW-LINE%',$this->languages->getLangEntry($product->getDescription(), $lang)),
  1281.                     'price' => $product->getPrice(),
  1282.                     'quantity_in_package' => $product->getQuantityInPackage(),
  1283.                     'main_image' => $main_image,
  1284.                     'main_product_image' => $main_product_image,
  1285.                     'unit' => $unit,
  1286.                     'currency' => $currency,
  1287.                     'producer' => $producer,
  1288.                     'categories' => $product_categories,
  1289.                     'is_action' => $product->getIsAction(),
  1290.                     'action_text' => $this->languages->getLangEntry($product->getActionText(), $lang),
  1291.                     'is_new' => $product->getIsNew(),
  1292.                     'is_tip' => $product->getIsTip(),
  1293.                     'is_selected' => false,
  1294.                     'images' => $images,
  1295.                     'params' => $this->eshopParamsHandle->getParamsCollection($lang$producttruetrue),
  1296.                     'params_entity' => $this->eshopParamsHandle->getParamsCollection($lang$producttruetruetrue),
  1297.                     'params_full' => $this->eshopParamsHandle->getParamsCollection($lang$producttruefalse),
  1298.                     'attachments' => $attachments,
  1299.                     'stock' => ($product->getStockShow()) ? $stockObj->getStock($product->getId())['stock'] : false,
  1300.                     'siblings' => $siblings,
  1301.                     'similars' => $similars,
  1302.                     'detail_url' => $this->getProductUrl($product0$lang),
  1303.                     //'detail_url' => $this->getHolderUrl($product->getHolder(),$lang).'/'.$product->getCode(),
  1304.                     'master_category' => $master_category,
  1305.                     'holder' => $product->getHolder(),
  1306.                     'is_holder' => $product->getIsHolder()
  1307.                 );
  1308.             //);
  1309.         }
  1310.     }
  1311.     public function getFinder($lang$limit = array(), $nofilter_products = array()) {
  1312.         //fulltext_search
  1313.         $fulltext_search '';
  1314.         if(isset($limit['fulltext_search'])) {
  1315.             $fulltext_search $limit['fulltext_search'];
  1316.         }
  1317.         return array(
  1318.             'fulltext_search' => array('actual' => $fulltext_search),
  1319.             'dynamic_model' => $this->eshopParamsHandle->getParamsCollection($lang)
  1320.         );
  1321.     }
  1322.     public function getFinderFull($lang$current_products$limit = array()) {
  1323.         //price range
  1324.         $min_product_price 0;
  1325.         $max_product_price 0;
  1326.         foreach($current_products as $product) {
  1327.             $a_price $product['price'];
  1328.             if($min_product_price $a_price['price_with_vat'])
  1329.                 $min_product_price $a_price['price_with_vat'];
  1330.             if($max_product_price $a_price['price_with_vat'])
  1331.                 $max_product_price $a_price['price_with_vat'];
  1332.         }
  1333.         $a_price_from $min_product_price;
  1334.         $a_price_to $max_product_price;
  1335.         if(isset($limit['price_range'])) {
  1336.             if(is_array($limit['price_range'])) {
  1337.                 $a_price_from $limit['price_range'][0];
  1338.                 $a_price_to $limit['price_range'][1];
  1339.             }
  1340.         }
  1341.         //fulltext_search
  1342.         $fulltext_search '';
  1343.         if(isset($limit['fulltext_search'])) {
  1344.             $fulltext_search $limit['fulltext_search'];
  1345.         }
  1346.         return array(
  1347.             'price_range' => array('actual' => array($a_price_from,$a_price_to), 'data' => array($min_product_price,$max_product_price)),
  1348.             'fulltext_search' => array('actual' => $fulltext_search),
  1349.             'dynamic_model' => $this->eshopParamsHandle->getParamsCollection($lang)
  1350.         );
  1351.     }
  1352.     public function getFirstLetter($a_name){
  1353.         $a_name_low strtolower($a_name);
  1354.         $a_name_letter '';
  1355.         if(isset($a_name_low[0])) {
  1356.             $a_name_letter $a_name_low[0];
  1357.             if(!$this->isLetter($a_name_letter)) {
  1358.                 for($i 0$i strlen($a_name_low); $i++) {
  1359.                     if($i >= 1) {
  1360.                         $a_name_letter $a_name_low[$i];
  1361.                         if($this->isLetter($a_name_letter))
  1362.                             break;
  1363.                     }
  1364.                 }
  1365.             }
  1366.         }
  1367.         return $a_name_letter;
  1368.     }
  1369.     public function getProductQrCode($product_id) {
  1370.         require(dirname(__DIR__).'/Lib/phpqrcode/qrlib.php');
  1371.         $product $this->getProduct($product_id);
  1372.         if(!$product)
  1373.             return false;
  1374.         $write sprintf($this->qr_url_pattern,$product->getCode());
  1375.         $img_path dirname(__DIR__).'/tmp/qrcode_'.$product_id.'_'.md5($write).'.png';
  1376.         if(file_exists($img_path))
  1377.             return $img_path;
  1378.         \QRcode::png($write$img_path'H'62);
  1379.         return $img_path;
  1380.     }
  1381.     public function getEshopInitParams($config) {
  1382.         $uri explode('?',str_replace('/app_dev.php','',$_SERVER['REQUEST_URI'])); $uri $uri[0];
  1383.         $deep count(explode('/',$uri))-2;
  1384.         //$deep = $uri;
  1385.         $request_type 'category';
  1386.         //$category = null;
  1387.         $images_helper $this->controller->get('image_twig_extension');
  1388.         $ret = array(
  1389.             'product' => null,
  1390.             'products' => null,
  1391.             'category' => array('deep' => $deep'category' => null'subcategories' => null),
  1392.             'request_type' => $request_type
  1393.         );
  1394.         $category $this->eshopCategoriesHandle->getCategoryBySeoName(substr(str_replace('/app_dev.php','',$uri),4));
  1395.         if($category) {
  1396.             $ret['category']['category'] = $category;
  1397.             $ret['category']['subcategories'] = $this->eshopCategoriesHandle->getCategories($category->getId());
  1398.         } else {
  1399.             foreach($this->eshopCategoriesHandle->getRouteableCategories() as $category_seo_data) {
  1400.                 if(substr($uri,4,strlen($category_seo_data['link'])) == $category_seo_data['link']) {
  1401.                     $ret['category']['category'] = $category_seo_data['category_obj'];
  1402.                     $ret['category']['subcategories'] = $this->eshopCategoriesHandle->getCategories($category_seo_data['category']['id']);
  1403.                 }
  1404.             }
  1405.         }
  1406.         if($ret['category']['category']) {
  1407.             $ret['products'] = array();
  1408.             //test
  1409.             if($_SERVER["REMOTE_ADDR"] == '81.201.53.119') {
  1410.                 //\Doctrine\Common\Util\Debug::dump($ret['products']);
  1411.                 //\Doctrine\Common\Util\Debug::dump($this->eshopCategoriesHandle->getCategoryProducts($ret['category']['category']->getId(),false));
  1412.                 //echo 'c'.$ret['category']['category']->getId()."\n";
  1413.                 //foreach($this->eshopCategoriesHandle->getCategoryProducts($ret['category']['category']->getId(),false) as $product_category) {
  1414.                 //    echo $product_category->getProductId()."\n";
  1415.                 //}
  1416.                 //exit;
  1417.             }
  1418.             foreach($this->eshopCategoriesHandle->getCategoryProducts($ret['category']['category']->getId(),false) as $product_category) {
  1419.                 $ret['products'][] = $this->getProductReturnArrayBlock($product_category->getProduct(), 'cz', array());
  1420.             }
  1421.         } else {
  1422.             $ret['category']['subcategories'] = $this->eshopCategoriesHandle->getCategories();
  1423.         }
  1424.         if($ret['category']['subcategories']) {
  1425.             $to_exclude = array();
  1426.             foreach($ret['category']['subcategories'] as &$subcategory) {
  1427.                 $product_in_category $this->eshopCategoriesHandle->getCategoryProducts($subcategory['id'],false);
  1428.                 if(count($product_in_category) == 1) {
  1429.                     $product $this->getProductReturnArrayBlock($product_in_category[0]->getProduct(), 'cz', array());
  1430.                     if($product['main_image'] != $this->no_image)
  1431.                         $subcategory['alt_image'] = $product['main_image'];
  1432.                     if($subcategory['article']) {
  1433.                         if($subcategory['article']->getImage())
  1434.                             $subcategory['alt_image'] = $images_helper->imageCutFunction($subcategory['article']->getImage()->getId(),700,320,'crop-best');
  1435.                        }
  1436.                     $subcategory['full_link'] = str_replace('/cz/','',$product['detail_url']);
  1437.                 }
  1438.                 if(count($product_in_category) == 0) {
  1439.                     $to_exclude[] = $subcategory['id'];
  1440.                 }
  1441.             }
  1442.             //exclude empty
  1443.             //$r_c_s_temp = $ret['category']['subcategories'];
  1444.             //$ret['category']['subcategories'] = [];
  1445.             //foreach($r_c_s_temp as $subcategory) {
  1446.                 //if(!in_array($subcategory['id'],$to_exclude))
  1447.                     //$ret['category']['subcategories'][] = $subcategory;
  1448.             //}
  1449.             //echo count($product_in_category).'<br>';
  1450.             //\Doctrine\Common\Util\Debug::dump($to_exclude);
  1451.             //exit();
  1452.         }
  1453.         if($ret['category']['category']) {
  1454.             $this->controller->get('page_frontend_handle')->setPageMeta(
  1455.                 'F.a.r.line - '.$ret['category']['category']->getName()
  1456.             );
  1457.         }
  1458.         //\Doctrine\Common\Util\Debug::dump($ret['category']['subcategories']);
  1459.         //exit();
  1460.         if($_SERVER["REMOTE_ADDR"] == '81.201.53.119') {
  1461.             //\Doctrine\Common\Util\Debug::dump($ret['products']);
  1462.             //exit;
  1463.         }
  1464.         return $ret;
  1465.     }
  1466.     public function getEshopInitProduct($config) {
  1467.         $uri explode('?',str_replace('/app_dev.php','',$_SERVER['REQUEST_URI'])); $uri $uri[0];
  1468.         $route_url_split explode('/',$uri);
  1469.         $product_id = (int)end($route_url_split);
  1470.         if($product_id >= 1) {
  1471.             $product $this->getProduct($product_id);
  1472.             if($product) {
  1473.                 $product_full $this->getProductReturnArrayBlock($product,'cz',array());
  1474.                 $this->controller->get('page_frontend_handle')->setPageMeta(
  1475.                     'F.a.r.line - '.$product_full['name'],
  1476.                     $product_full['main_product_image']
  1477.                 );
  1478.                 return $product_full;
  1479.             }
  1480.         }
  1481.         exit('Produkt nenalezen');
  1482.     }
  1483.     public function getEshopInitParamsOld($config) {
  1484.         $uri explode('?',str_replace('/app_dev.php','',$_SERVER['REQUEST_URI'])); $uri $uri[0];
  1485.         $request_type null;
  1486.         $product_url_prefix = array('product/');
  1487.         $product null;
  1488.         $ret = array(
  1489.             'product_id' => 0,
  1490.             'product' => null,
  1491.             'categories' => array(),
  1492.             'category' => array('category' => null'subcategories' => null),
  1493.             'set_categories' => array()
  1494.         );
  1495.         //product url
  1496.         foreach($product_url_prefix as $pp) {
  1497.             if(strpos($uri$pp) !== false) {
  1498.                 $request_type 'product';
  1499.             }
  1500.         }
  1501.         //product dynamic url
  1502.         if(!$request_type) {
  1503.             $l1_match false;
  1504.             $l2_match false;
  1505.             foreach($this->eshopCategoriesHandle->getRouteableCategories() as $category_seo_data) {
  1506.                 if(substr($uri,4,strlen($category_seo_data['link'])) == $category_seo_data['link']) {
  1507.                     $l1_match true;
  1508.                     $ret['category']['category'] = $category_seo_data['category_obj'];
  1509.                     $ret['category']['subcategories'] = $this->eshopCategoriesHandle->getCategories($category_seo_data['category']['id']);
  1510.                 }
  1511.             }
  1512.             if($l1_match) {
  1513.                 $uri_explode explode('/',$uri);
  1514.                 $code $uri_explode[count($uri_explode)-1];
  1515.                 $product $this->getProductByCode($code);
  1516.                 if($product)
  1517.                     $l2_match true;
  1518.             }
  1519.             if($l1_match && $l2_match) {
  1520.                 $request_type 'product';
  1521.             } else {
  1522.                 if($l1_match)
  1523.                     $request_type 'category-direct';
  1524.             }
  1525.         }
  1526.         $category $this->eshopCategoriesHandle->getCategoryBySeoName(substr(str_replace('/app_dev.php','',$uri),4));
  1527.         if($category) {
  1528.             $request_type 'category';
  1529.         }
  1530.         switch($request_type) {
  1531.             default:
  1532.             break;
  1533.             case 'product':
  1534.                 $uri_explode explode('/',$uri);
  1535.                 $code $uri_explode[count($uri_explode)-1];
  1536.                 if(!$product)
  1537.                     $product $this->getProductByCode($code);
  1538.                 if($product) {
  1539.                     $ret['product_id'] = $product->getId();
  1540.                     $ret['product'] = $product;
  1541.                     $product_full $this->getProductReturnArrayBlock($product,'cz',array());
  1542.                     $this->controller->get('page_frontend_handle')->setPageMeta(
  1543.                         $product_full['name'].' '.$product_full['master_category']['title_name'],
  1544.                         $product_full['main_product_image']
  1545.                     );
  1546.                 }
  1547.             break;
  1548.             case 'category':
  1549.                 $sub_categories $this->eshopCategoriesHandle->getCategories($category->getId());
  1550.                 if($category->getParentRow()) {
  1551.                     $this->controller->get('page_frontend_handle')->setPageMeta(
  1552.                         ucfirst($category->getName()).' '.$category->getParentRow()->getTitle().' - Katalog F.A.R. Line'
  1553.                     );
  1554.                 } else {
  1555.                     $this->controller->get('page_frontend_handle')->setPageMeta(
  1556.                         ucfirst($category->getTitle()).' - Katalog F.A.R. Line'
  1557.                     );
  1558.                 }
  1559.                 //if has subcategories -> include them
  1560.                 if(is_array($sub_categories)) {
  1561.                     foreach($sub_categories as $sc) {
  1562.                         $ret['set_categories'][] = $sc['id'];
  1563.                     }
  1564.                 } else {
  1565.                     $ret['set_categories'] = array($category->getId());
  1566.                 }
  1567.             break;
  1568.         }
  1569.         //eshop categories
  1570.         $ret['categories'] = $this->eshopCategoriesHandle->getCategories(0);
  1571.         $ret['dynamic_model'] = $this->eshopParamsHandle->getParamsCollection('cz');
  1572.         //print_r($ret);
  1573.         //exit();
  1574.         return $ret;
  1575.     }
  1576.     public function getEshopTopProducts($config) {
  1577.         return $this->findProducts($config['lang'],array('results_per_page' => 20),1,true,1);
  1578.     }
  1579.     private function isLetter($letter) {
  1580.         if(preg_match("/^[a-z]$/"$letter))
  1581.             return true;
  1582.         return false;
  1583.     }
  1584.     private function getProductName(\Kiwi\EshopBundle\Entity\Product $product$lang) {
  1585.         //name constructor
  1586.         $name '';
  1587.         if($product->getQuantityInPackage() >= 2)
  1588.             $name .= $product->getQuantityInPackage().'x ';
  1589.         $name .= $this->languages->getLangEntry($product->getName(),$lang);
  1590.         return $name;
  1591.     }
  1592.     private function getProductUrl(\Kiwi\EshopBundle\Entity\Product $product null$product_id 0$lang) {
  1593.         $url $this->controller->get('page_twig_extension')->kpathFunction(3$lang);
  1594.         $category null;
  1595.         foreach($this->controller->get('eshop_categories_handle')->getProductCategories($product) as $product_category) {
  1596.             if($product_category->getCategory()->getParentRow()) {
  1597.                 $category $product_category->getCategory();
  1598.                 break;
  1599.             }
  1600.         }
  1601.         if($category)
  1602.         {
  1603.             if($category->getParentRow())
  1604.             {
  1605.                 if($category->getParentRow()->getParentRow())
  1606.                 {
  1607.                     if($category->getParentRow()->getParentRow()->getParentRow())
  1608.                     {
  1609.                         $url .= $category->getParentRow()->getParentRow()->getParentRow()->getName().'/';
  1610.                     }
  1611.                     
  1612.                     $url .= $category->getParentRow()->getParentRow()->getName().'/';
  1613.                 }
  1614.                 
  1615.                 $url .= $category->getParentRow()->getName().'/';
  1616.             }
  1617.             $url .= $category->getName().'/';
  1618.             $url .= str_replace(array('  '),' ',$this->getProductName($product$lang)).'/';
  1619.             $url .= $product->getId();
  1620.         }
  1621.         else
  1622.         {
  1623.             
  1624.             $url .= 'produkt/';
  1625.             $url .= str_replace(array('  '),' ',$this->getProductName($product$lang)).'/';
  1626.             $url .= $product->getId();
  1627.         }
  1628.         $url str_replace('app_dev-php','app_dev.php',$this->pageExtensions->webalizeFunction($url));
  1629.         return $url;
  1630.     }
  1631.     private function getHolderUrl(ProductHolders $holder$lang) {
  1632.         $dev_prefix '';
  1633.         $url '/'.$lang.'/';
  1634.         if($this->controller->get'kernel' )->getEnvironment() == 'dev')
  1635.             $dev_prefix '/app_dev.php';
  1636.         /*
  1637. $category = null;
  1638. foreach($this->controller->get('eshop_categories_handle')->getProductCategories($product) as $product_category) {
  1639.     if($product_category->getCategory()->getParentRow()) {
  1640.         $category = $product_category->getCategory();
  1641.         break;
  1642.     }
  1643. }
  1644. if($category)
  1645. {
  1646.     if($category->getParentRow())
  1647.     {
  1648.         if($category->getParentRow()->getParentRow())
  1649.         {
  1650.             if($category->getParentRow()->getParentRow()->getParentRow())
  1651.             {
  1652.                 $url .= $category->getParentRow()->getParentRow()->getParentRow()->getName().'/';
  1653.             }
  1654.             $url .= $category->getParentRow()->getParentRow()->getName().'/';
  1655.         }
  1656.         $url .= $category->getParentRow()->getName().'/';
  1657.     }
  1658.     $url .= $category->getName().'/';
  1659.     $url .= str_replace(array('  '),' ',$this->getProductName($product, $lang)).'/';
  1660.     $url .= $product->getId();
  1661. }
  1662. else
  1663. {
  1664.     $url .= 'produkt/'; //$category->GetName().'/';//'drevene-podlahy/';//$category->getName().'/';
  1665.     $url .= str_replace(array('  '),' ',$this->getProductName($product, $lang)).'/';
  1666.     $url .= $product->getId();
  1667. }
  1668. */
  1669.         $url .= 'produkt/';
  1670.         $product $this->em->getRepository('Kiwi\EshopBundle\Entity\Product')->findOneBy(array(
  1671.             'holder_id' => $holder->getId()
  1672.         ));
  1673.         if($product) {
  1674.             foreach($this->eshopCategoriesHandle->getProductCategoriesNoCache($product) as $product_category) {
  1675.                 if($product_category->getCategory()) {
  1676.                     $url .= $product_category->getCategory()->getSeoName()."/";
  1677.                 }
  1678.             }
  1679.         }
  1680.         $url .= $this->languages->getLangEntry($holder->getArticle()->getName(), $lang).'/';
  1681.         $url .= $holder->getTransportCode();
  1682.         $url $dev_prefix.$this->pageExtensions->webalizeFunction($url);
  1683.         return $url;
  1684.     }
  1685.     public function storeCookieProductReference($product_idRequest $requestResponse $response) {
  1686.         $current = [];
  1687.         if($request->cookies->has($this->cookie_key)) {
  1688.             $current explode(',',$request->cookies->get($this->cookie_key));
  1689.         }
  1690.         $current array_merge([$product_id], $current);
  1691.         $cookie = new Cookie($this->cookie_keyjoin(",",$current), time()+(86400*7));
  1692.         $response->headers->setCookie($cookie);
  1693.     }
  1694.     public function getCookieProduct(Request $request$lang) {
  1695.         $current = [];
  1696.         if($request->cookies->has($this->cookie_key)) {
  1697.             $current explode(',',$request->cookies->get($this->cookie_key));
  1698.             $current array_unique($current);
  1699.             $slice array_slice($current,0,10);
  1700.             $out = [];
  1701.             foreach ($slice as $product_id) {
  1702.                 $out[] = $this->findProduct($lang$product_id);
  1703.             }
  1704.             return $out;
  1705.         }
  1706.         return $current;
  1707.     }
  1708.     //debug
  1709.     private function logClean() {
  1710.         file_put_contents($this->log_path'');
  1711.     }
  1712.     private function logState($s) {
  1713.         file_put_contents($this->log_path$s."\n"FILE_APPEND);
  1714.     }
  1715. }