src/Controller/ProductController.php line 367

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  9. use Symfony\Component\HttpFoundation\Session\Session;
  10. use App\Entity\ProductCategory;
  11. use App\Entity\Partner;
  12. use App\Entity\Product;
  13. use App\Entity\ProductVariation;
  14. use App\Entity\ProductImage;
  15. use App\Entity\EntryDetail;
  16. use App\Entity\ProductEntry;
  17. use App\Entity\Attribute;
  18. use App\Entity\AttributeValues;
  19. use App\Entity\ProductAttribute;
  20. class ProductController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/admin/nn/list/products", name="sajong_ecom_admin_list_product")
  24.      */
  25.     public function productlist(Request $request)
  26.     {
  27.         $em $this->getDoctrine()->getManager();
  28.         $sizes = array();
  29.         $params = array();
  30.         $extra "";
  31.         if(!empty($request->get('brand'))) {
  32.           $extra .= " AND p.brand=:bid";
  33.           $params['bid'] = $request->get('brand');
  34.         }
  35.         
  36.         if(!empty($request->get('global_category'))) {
  37.           $extra .= " AND p.globalcategory=:gid";
  38.           $params['gid'] = $request->get('global_category');
  39.         }
  40.         
  41.         if(!empty($request->get('product_category'))) {
  42.           $extra .= " AND p.productcategory=:pid";
  43.           $params['pid'] = $request->get('product_category');
  44.         }
  45.         
  46.         if(!empty($request->get('title'))) {
  47.           $extra .= " AND p.title=:title";
  48.           $params['title'] = '%' $request->get('title') .'%';
  49.         }
  50.         if(!empty($request->get('collection'))) {
  51.           foreach($request->get('collection') as $attr) {
  52.             $collection_filterurl .= '&collection[]=' $attr;
  53.             $selected_coln[$attr] = 'active';
  54.             $params['collection'][] = $attr;
  55.           }
  56.           $extra .= " AND pa.attributeId IN (:collection)";
  57.           $extbl stristr($extbl", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
  58.         }
  59.         if(!empty($request->get('size'))) {
  60.           foreach($request->get('size') as $attr) {
  61.             $size_filterurl .= '&size[]=' $attr;
  62.             $selected_size[$attr] = 'active';
  63.             $params['size'][] = $attr;
  64.           }
  65.           $extra .= " AND v.size IN (:size)";
  66.         }
  67.         if(!empty($request->get('category'))) {
  68.           foreach($request->get('category') as $attr) {
  69.             $category_filterurl .= '&category[]=' $attr;
  70.             $selected_cat[$attr] = 'active';
  71.             $params['category'][] = $attr;
  72.           }
  73.           $extra .= " AND pa.attributeId IN (:category)";
  74.           $extbl stristr($extbl", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
  75.         }
  76.         if(!empty($request->get('style'))) {
  77.           $extra .= " AND p.style=:sid";
  78.           $params['sid'] = $request->get('style');
  79.           $sizes $em->createQuery("SELECT s.id, s.name, s.price FROM App\Entity\Size s, App\Entity\StyleSize ss WHERE ss.styleId=:stl AND ss.sizeId=s.id")->setParameters(['stl'=>$request->get('style')])->getArrayResult();
  80.         }
  81.         $brands $em->getRepository('App\Entity\Brand')->findByStatus(1, array('title'=>'ASC'));
  82.         $globalcategories $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
  83.         $productcategories $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
  84.         $styles $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
  85.         $collection $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
  86.         if($collection != null) {
  87.             $collections $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
  88.         }
  89.         else {
  90.             $collections = array();
  91.         }
  92.         $categoryatr $em->getRepository('App\Entity\Attribute')->findOneByName('category');
  93.         if($categoryatr != null) {
  94.             $categoryatv $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
  95.         }
  96.         else {
  97.             $categoryatv = array();
  98.         } 
  99.         
  100.         if(!empty($extra)) {
  101.           $products $em->createQuery("SELECT p.id, p.path, p.title, p.description, pi.image, p.styledescription, p.type, MIN(v.price) price, MIN(v.actualPrice) actualPrice, MIN(v.total) total, p.productcount, p.quantity, p.totalcount, p.status, p.brand, b.title brandname, p.globalcategory, g.name gcname, p.productcategory, c.name pcname, p.style, s.name sname FROM App\Entity\Product p, App\Entity\ProductVariation v, App\Entity\ProductImage pi, App\Entity\Brand b, App\Entity\GlobalCategory g, App\Entity\ProductCategory c, App\Entity\Style s WHERE v.productId=p.id AND pi.productId=p.id AND b.id=p.brand AND g.id=p.globalcategory AND c.id=p.productcategory AND s.id=p.style " $extra " GROUP BY p.id ORDER BY p.id DESC")->setParameters($params)->getArrayResult();//->setMaxResults(20)
  102.         }
  103.         else {
  104.           $products $em->createQuery("SELECT p.id, p.path, p.title, p.description, pi.image, p.styledescription, p.type, MIN(v.price) price, MIN(v.actualPrice) actualPrice, MIN(v.total) total, p.productcount, p.quantity, p.totalcount, p.status, p.brand, b.title brandname, p.globalcategory, g.name gcname, p.productcategory, c.name pcname, p.style, s.name sname FROM App\Entity\Product p, App\Entity\ProductVariation v, App\Entity\ProductImage pi, App\Entity\Brand b, App\Entity\GlobalCategory g, App\Entity\ProductCategory c, App\Entity\Style s WHERE v.productId=p.id AND pi.productId=p.id AND b.id=p.brand AND g.id=p.globalcategory AND c.id=p.productcategory AND s.id=p.style GROUP BY p.id ORDER BY p.id DESC")->getArrayResult();//->setMaxResults(20)
  105.         }
  106.         //$products = $em->getRepository('App\Entity\Product')->findAll();
  107.         return $this->render('admin/products.html.twig', array('products'=> $products'status'=> array('Inactive''Active'), 'brands'=>$brands'globalcategories'=>$globalcategories'productcategories'=>$productcategories'styles'=>$styles'sizes'=>$sizes'collections'=>$collections'categoryatr'=>$categoryatr'categoryatv'=>$categoryatv'param'=>$params));
  108.     }
  109.     
  110.     /**
  111.      * @Route("/admin/nn/add/product", name="sajong_ecom_admin_add_product")
  112.      */
  113.     public function addproduct()
  114.     {
  115.         $em $this->getDoctrine()->getManager();
  116.         $brands $em->getRepository('App\Entity\Brand')->findByStatus(1, array('title'=>'ASC'));
  117.         $cities $em->getRepository('App\Entity\City')->findAll();
  118.         $city = array();
  119.         foreach ($cities as $c) {
  120.           $city[$c->getId()] = $c->getName();
  121.         }
  122.         $globalcategories $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
  123.         $productcategories $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
  124.         $styles $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
  125.         $sizes $em->getRepository('App\Entity\Size')->findByStatus(1,array('name'=>'ASC'));
  126.         $colors $em->getRepository('App\Entity\Color')->findByStatus(1,array('name'=>'ASC'));
  127.         $producttypes $em->getRepository('App\Entity\ProductType')->findByStatus(1);
  128.         $collection $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
  129.         if($collection != null) {
  130.             $collections $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
  131.         }
  132.         else {
  133.             $collections = array();
  134.         }
  135.         $categoryatr $em->getRepository('App\Entity\Attribute')->findOneByName('category');
  136.         if($categoryatr != null) {
  137.             $categoryatv $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
  138.         }
  139.         else {
  140.             $categoryatv = array();
  141.         }        
  142.         return $this->render('admin/addproduct.html.twig', array('status'=> array('Inactive''Active'), 'brands'=>$brands'globalcategories'=>$globalcategories'productcategories'=>$productcategories'styles'=>$styles'sizes'=>$sizes'colors'=>$colors'collections'=>$collections'categoryatr'=>$categoryatr'categoryatv'=>$categoryatv'producttypes'=>$producttypes'city'=>$city));
  143.     }
  144.     
  145.     /**
  146.      * @Route("/admin/nn/edit/product/{id}", name="sajong_ecom_admin_edit_product")
  147.      */
  148.     public function editproduct($id)
  149.     {
  150.         $em $this->getDoctrine()->getManager();
  151.         $product $em->getRepository('App\Entity\Product')->find($id);
  152.         $productvariations $em->getRepository('App\Entity\ProductVariation')->findByProductId($id);
  153.         $variationimages = array();
  154.         foreach ($productvariations as $productvariation) {
  155.           $variationimages[$productvariation->getId()] = $em->getRepository('App\Entity\ProductImage')->findByVariationId($productvariation->getId());
  156.         }
  157.         $brands $em->getRepository('App\Entity\Brand')->findByStatus(1, array('title'=>'ASC'));
  158.         $cities $em->getRepository('App\Entity\City')->findAll();
  159.         $city = array();
  160.         foreach ($cities as $c) {
  161.           $city[$c->getId()] = $c->getName();
  162.         }
  163.         $globalcategories $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
  164.         $productcategories $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
  165.         $apparelcategories $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory(65, array('name'=>'ASC'));
  166.         $styles $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
  167.         $sizes $em->getRepository('App\Entity\Size')->findByStatus(1,array('name'=>'ASC'));
  168.         $colors $em->getRepository('App\Entity\Color')->findByStatus(1,array('name'=>'ASC'));
  169.         $producttypes $em->getRepository('App\Entity\ProductType')->findByStatus(1);
  170.         $collection $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
  171.         if($collection != null) {
  172.             $collections $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
  173.         }
  174.         else {
  175.             $collections = array();
  176.         }
  177.         $categoryatr $em->getRepository('App\Entity\Attribute')->findOneByName('category');
  178.         if($categoryatr != null) {
  179.             $categoryatv $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
  180.         }
  181.         else {
  182.             $categoryatv = array();
  183.         }
  184.         return $this->render('admin/editproduct.html.twig', array('status'=> array('Inactive''Active'), 'product'=> $product'variations'=> $productvariations'variationimages'=> $variationimages'brands'=>$brands'globalcategories'=>$globalcategories'productcategories'=>$productcategories'apparelcategories'=>$apparelcategories'styles'=>$styles'sizes'=>$sizes'colors'=>$colors'collections'=>$collections'categoryatr'=>$categoryatr'categoryatv'=>$categoryatv'producttypes'=>$producttypes'city'=>$city ));
  185.     }
  186.     
  187.     /**
  188.      * @Route("/admin/nn/save/product", name="sajong_ecom_admin_save_product")
  189.      */
  190.     public function saveproduct(Request $request)
  191.     {
  192.         $em $this->getDoctrine()->getManager();
  193.         if(!empty($request->get('productid'))) {
  194.             $product $em->getRepository('App\Entity\Product')->find($request->get('productid'));
  195.             $this->addFlash('success''Product updated successfully');
  196.         }
  197.         else {
  198.             $product = new Product();
  199.             $product->setCreated(new \DateTime()); 
  200.             $this->addFlash('success''Product added successfully');
  201.         }
  202.         $product->setTitle($request->get('title')); 
  203.         $product->setPath($request->get('path')); 
  204.         $product->setDescription($request->get('description')); 
  205.         $product->setStyledescription($request->get('styledescription')); 
  206.         $product->setBrand($request->get('brand')); 
  207.         $product->setGlobalcategory($request->get('globalcategory')); 
  208.         $product->setProductcategory($request->get('productcategory')); 
  209.         $product->setStyle($request->get('style')); 
  210.         $product->setType($request->get('type')); 
  211.         $product->setQuantity(!empty($request->get('quantity'))?$request->get('quantity'):0); 
  212.         $product->setProductcount(!empty($request->get('productcount'))?$request->get('productcount'):0); 
  213.         $product->setPrice(!empty($request->get('price'))?($request->get('price')*100):0); 
  214.         $product->setTax(!empty($request->get('tax'))?($request->get('tax')*100):0); 
  215.         $product->setDiscount(!empty($request->get('discount'))?($request->get('discount')*100):0); 
  216.         //$product->setTotal(!empty($request->get('total'))?$request->get('total'):$request->get('price')); 
  217.         $product->setTotal(!empty($request->get('price'))?($request->get('price')*100):0); 
  218.         $product->setUpdated(new \DateTime()); 
  219.         $product->setCreatedBy($this->getUser()->getId()); 
  220.         $product->setAccess(new \DateTime()); 
  221.         $product->setTotalcount(0); 
  222.         $product->setStage('Approved'); 
  223.         $product->setStatus($request->get('status')); 
  224.         
  225.         $em->persist($product);
  226.         $em->flush();
  227.         
  228.         for($i=0$i<count($request->get('variationtitle')); $i++) {
  229.           if(!empty($request->get('variationtitle')[$i])) {
  230.             $productvariation null;
  231.             if (!empty($request->get('product_variationid')[$i])) {
  232.               $productvariation $em->getRepository('App\Entity\ProductVariation')->find($request->get('product_variationid')[$i]);
  233.             }
  234.             if($productvariation == null) {
  235.               $productvariation = new ProductVariation();
  236.               $productvariation->setProductId($product->getId()); 
  237.               $productvariation->setCreated(new \DateTime()); 
  238.               $productvariation->setCreatedBy($this->getUser()->getId()); 
  239.               $productvariation->setAccess(new \DateTime());
  240.             }
  241.             $findex $request->get('variationai')[$i];
  242.             $productvariation->setTitle($request->get('variationtitle')[$i]); 
  243.             $productvariation->setSku($request->get('variationsku')[$i]); 
  244.             $productvariation->setColor($request->get('variationcolor')[$i]); 
  245.             $productvariation->setSize($request->get('variationsize')[$i]); 
  246.             $productvariation->setActualPrice($request->get('variation_actual_price')[$i]*100); 
  247.             $productvariation->setPrice($request->get('variation_price')[$i]*100); 
  248.             $productvariation->setTax(0); 
  249.             $productvariation->setTotal($request->get('variation_price')[$i]*100); 
  250.             $productvariation->setUpdated(new \DateTime()); 
  251.             $productvariation->setTotalcount(!empty($request->get('variation_count')[$i])?$request->get('variation_count')[$i]:0); 
  252.             $productvariation->setStock($request->get('variation_stock')[$i]); 
  253.             $productvariation->setStatus(!empty($request->get('variation_status')[$i])?1:0); 
  254.             $em->persist($productvariation);
  255.             $em->flush();
  256.             if (!empty($request->get('product_variationid')[$i])) {
  257.                 if(!empty($request->get('evariationimage')[$findex])) {
  258.                   for($k=0$k<count($request->get('evariationimage')[$findex]); $k++) {
  259.                     if(!empty($request->get('evariationimage')[$findex][$k])) {
  260.                       $productimage = new ProductImage();
  261.                       $productimage->setProductId($product->getId()); 
  262.                       $productimage->setVariationId($productvariation->getId()); 
  263.                       $productimage->setImage($request->get('evariationimage')[$findex][$k]); 
  264.                       $productimage->setImageType('Product'); 
  265.                       $productimage->setWeight($request->get('evariationimgweight')[$findex][$k]); 
  266.                       $productimage->setCreated(new \DateTime()); 
  267.                       $productimage->setStatus(1); 
  268.                       $em->persist($productimage);
  269.                       $em->flush();
  270.                     }
  271.                   }
  272.                 }
  273.             }
  274.             else {
  275.                 if(!empty($request->get('variationimage')[$findex])) {
  276.                   for($k=0$k<count($request->get('variationimage')[$findex]); $k++) {
  277.                     if(!empty($request->get('variationimage')[$findex][$k])) {
  278.                       $productimage = new ProductImage();
  279.                       $productimage->setProductId($product->getId()); 
  280.                       $productimage->setVariationId($productvariation->getId()); 
  281.                       $productimage->setImage($request->get('variationimage')[$findex][$k]); 
  282.                       $productimage->setImageType('Product'); 
  283.                       $productimage->setWeight($request->get('variationimgweight')[$findex][$k]); 
  284.                       $productimage->setCreated(new \DateTime()); 
  285.                       $productimage->setStatus(1); 
  286.                       $em->persist($productimage);
  287.                       $em->flush();
  288.                     }
  289.                   }
  290.                 }
  291.             }
  292.           }
  293.         }
  294.         if(!empty($request->get('deleted_variations'))) {
  295.           $delvariations explode(","$request->get('deleted_variations'));
  296.           foreach ($delvariations as $dv) {
  297.             $delvariation $em->getRepository('App\Entity\ProductVariation')->find(trim($dv));
  298.             $em->remove($delvariation);
  299.             $em->flush();
  300.           }
  301.         }
  302.         $files explode(","$request->get('deleted_variation_images'));
  303.         foreach ($files as $f) {
  304.           if(!empty($f) && file_exists($f)) {
  305.             unlink($f);
  306.             $productimage $em->getRepository('App\Entity\ProductImage')->findOneByImage($f);
  307.             $em->remove($productimage);
  308.             $em->flush();
  309.           }
  310.         }
  311.         if(!empty($request->get('imageid'))) {
  312.           for($i=0$i<count($request->get('imageid')); $i++) {
  313.             $productimage $em->getRepository('App\Entity\ProductImage')->find($request->get('imageid')[$i]);
  314.             $productimage->setWeight(!empty($request->get('vimageweight')[$i])?$request->get('vimageweight')[$i]:$i);
  315.             $em->persist($productimage);
  316.             $em->flush();
  317.           }
  318.         }
  319.         
  320.         return $this->redirect($this->generateUrl('sajong_ecom_admin_list_product'));
  321.     }
  322.     
  323.     /**
  324.      * @Route("/admin/nn/delete/product/{id}", name="sajong_ecom_admin_delete_product")
  325.      */
  326.     public function deleteproduct($id)
  327.     {
  328.         $em $this->getDoctrine()->getManager();
  329.         $product $em->getRepository('App\Entity\Product')->find($id);
  330.         $em->remove($product);
  331.         $em->flush();
  332.         $this->addFlash('success''Product deleted successfully');
  333.         return $this->redirect($this->generateUrl('sajong_ecom_admin_list_product'));
  334.     }
  335.   
  336.   /**
  337.    * @Route("/uploadproductimage", name="sajong_ecom_upload_productimage")
  338.    */
  339.   public function uploadproductimage(Request $request)
  340.   {
  341.       $img $request->get('photo');
  342.       $fileName uniqid() . '.png';
  343.       $folderPath "uploads/products/";
  344.     
  345.       $file $folderPath $fileName;
  346.       $fp   fopen($file'w');
  347.       $data explode(','$img);
  348.       fwrite($fpbase64_decode($data[1]));
  349.       fclose($fp);
  350.       echo $folderPath $fileName; exit;
  351.   }
  352.     
  353.     /**
  354.      * @Route("/store", name="sajong_ecom_products_store")
  355.      */
  356.     public function store(Request $request)
  357.     {
  358.         $em $this->getDoctrine()->getManager();
  359.         
  360.         $srtb '';
  361.         $extra '';
  362.         $lftjoin '';
  363.         $srtodr '';
  364.         $extbl '';
  365.         $params = array();
  366.         $selected_prodtype = array();
  367.         $selected_style = array();
  368.         $selected_size = array();
  369.         $selected_color = array();
  370.         
  371.         $globalcategories $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
  372.         $productcategories $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
  373.         $producttypes $em->getRepository('App\Entity\ProductType')->findByStatus(1, array('name'=>'ASC'));
  374.         $styles $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
  375.         $sizes $em->getRepository('App\Entity\Size')->findByStatus(1, array('name'=>'ASC'));
  376.         $colors $em->getRepository('App\Entity\Color')->findByStatus(1, array('name'=>'ASC'));
  377.         $collection $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
  378.         if($collection != null) {
  379.             $collections $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
  380.         }
  381.         else {
  382.             $collections = array();
  383.         }
  384.         $categoryatr $em->getRepository('App\Entity\Attribute')->findOneByName('category');
  385.         if($categoryatr != null) {
  386.             $categoryatv $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
  387.         }
  388.         else {
  389.             $categoryatv = array();
  390.         }
  391.         
  392.         if(!empty($_GET['global_category'])) {
  393.           $extra .= " AND p.globalcategory=:gid";
  394.           $params['gid'] = $_GET['global_category'];
  395.           $productcategories $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory($_GET['global_category'], array('name'=>'ASC'));
  396.         }
  397.         if(!empty($_GET['product_category'])) {
  398.           $extra .= " AND p.productcategory=:pid";
  399.           $params['pid'] = $_GET['product_category'];
  400.           $styles $em->getRepository('App\Entity\Style')->findByProductCategory($_GET['product_category'], array('name'=>'ASC'));
  401.         }
  402.         if(!empty($_GET['prodtype'])) {
  403.           foreach($_GET['prodtype'] as $attr) {
  404.             $params['prodtype'][] = $attr;
  405.             $selected_prodtype[$attr] = 'checked';
  406.           }
  407.           $extra .= " AND p.type IN (:prodtype)";
  408.         }
  409.         if(!empty($_GET['collection'])) {
  410.           foreach($_GET['collection'] as $attr) {
  411.             $params['collection'][] = $attr;
  412.           }
  413.           $extra .= " AND pa.attributeId IN (:collection)";
  414.           $extbl stristr($extbl", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
  415.         }
  416.         if(!empty($_GET['category'])) {
  417.           foreach($_GET['category'] as $attr) {
  418.             $params['category'][] = $attr;
  419.           }
  420.           $extra .= " AND pa.attributeId IN (:category)";
  421.           $extbl stristr($extbl", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
  422.         }
  423.         if(!empty($_GET['style'])) {
  424.           $params['sid'] = $_GET['style'];
  425.           $selected_style[$_GET['style']] = 'checked';
  426.           $extra .= " AND p.style=:sid";
  427.           //$sizes = $em->createQuery("SELECT s.id, s.name, s.price FROM App\Entity\Size s, App\Entity\StyleSize ss WHERE ss.styleId=:stl AND ss.sizeId=s.id")->setParameters(['stl'=>$_GET['style']])->getArrayResult();
  428.         }
  429.         if(!empty($_GET['size'])) {
  430.           foreach($_GET['size'] as $attr) {
  431.             $params['size'][] = $attr;
  432.             $selected_size[$attr] = 'checked';
  433.           }
  434.           $extra .= " AND v.size IN (:size)";
  435.         }
  436.         if(!empty($_GET['color'])) {
  437.           foreach($_GET['color'] as $attr) {
  438.             $params['color'][] = $attr;
  439.             $selected_color[$attr] = 'checked';
  440.           }
  441.           $extra .= " AND v.color IN (:color)";
  442.         }
  443.         
  444.         if(!empty($_GET['sort_by'])) {
  445.           if ($_GET['sort_by'] == 'l')
  446.             $srtodr " ORDER BY v.price ASC";
  447.           elseif ($_GET['sort_by'] == 'h')
  448.             $srtodr " ORDER BY v.price DESC";
  449.           elseif ($_GET['sort_by'] == 'r')
  450.             $srtodr " ORDER BY v.created DESC";
  451.           elseif ($_GET['sort_by'] == 'p') {
  452.             $lftjoin " LEFT JOIN App\Entity\Flagging f WITH f.entityId=p.id AND f.entityType='product' AND f.fid=3";
  453.             $srtodr " ORDER BY COUNT(f.id) DESC";
  454.           }
  455.           $srtb $_GET['sort_by'];
  456.         }
  457.         
  458.         $size = array();
  459.         $color = array();
  460.         $size[0] = '';
  461.         foreach ($sizes as $s) {
  462.           $size[$s->getId()] = " | " $s->getName();
  463.         }
  464.         $color[0] = '';
  465.         foreach ($colors as $c) {
  466.           $color[$c->getId()] = " | " $c->getName();
  467.         }
  468.         $products = array();
  469.         if(!empty($request->get('page')) && $request->get('page')=='infinitescroll') {
  470.           if(!empty($extra)) {
  471.             //$products = $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, MIN(v.price) price, MIN(v.actualPrice) actualPrice, MIN(v.total) total, p.totalcount FROM App\Entity\Product p" . $lftjoin . ", App\Entity\ProductVariation v, App\Entity\ProductImage pi" . $extbl . " WHERE v.productId=p.id AND pi.productId=p.id AND p.status=1 " . $extra . " GROUP BY p.id" . $srtodr)->setParameters($params)->setFirstResult(12)->getArrayResult();
  472.             $products $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, v.price, v.actualPrice, v.total, p.totalcount FROM App\Entity\Product p" $lftjoin ", App\Entity\ProductVariation v, App\Entity\ProductImage pi" $extbl " WHERE v.productId=p.id AND pi.productId=p.id AND p.status=1 " $extra  " GROUP BY p.id "$srtodr)->setParameters($params)->setFirstResult(12)->getArrayResult();
  473.           }
  474.           else {
  475.             //$products = $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, MIN(v.price) price, MIN(v.actualPrice) actualPrice, MIN(v.total) total, p.totalcount FROM App\Entity\Product p" . $lftjoin . ", App\Entity\ProductVariation v, App\Entity\User u, App\Entity\ProductImage pi" . $extbl . " WHERE v.productId=p.id AND pi.productId=p.id AND p.createdBy=u.id AND p.status=1 GROUP BY p.id" . $srtodr)->setFirstResult(12)->getArrayResult();
  476.             $products $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, v.price, v.actualPrice, v.total, p.totalcount FROM App\Entity\Product p" $lftjoin ", App\Entity\ProductVariation v, App\Entity\User u, App\Entity\ProductImage pi" $extbl " WHERE v.productId=p.id AND pi.productId=p.id AND p.createdBy=u.id AND p.status=1 "  " GROUP BY p.id "$srtodr)->setFirstResult(12)->getArrayResult();
  477.           }
  478.           //$this->get('session')->set('pageno', $this->get('session')->get('pageno')+1);
  479.           //$products = $em->createQuery("SELECT p.id, p.path, p.title, p.styledescription, p.type, p.price, 'test' as image,  'test' as firstname,  'test' as lastname, p.totalcount FROM App\Entity\Product p")->getArrayResult();
  480.           return $this->render('ecom/store_infinitescroll.html.twig', array('products'=>$products'globalcategories'=>$globalcategories'productcategories'=>$productcategories'producttypes'=> $producttypes'styles'=>$styles'sizes'=>$sizes'size'=>$size'colors'=>$colors'color'=>$color'collections'=>$collections'categoryatv'=>$categoryatv'srtb'=>$srtb'params'=>$params'selected_prodtype'=>$selected_prodtype'selected_style'=>$selected_style'selected_size'=>$selected_size'selected_color'=>$selected_color'page'=>($request->get('np')+1)));
  481.         }
  482.         else {
  483.           //$this->get('session')->set('pageno', 0);
  484.           if(!empty($extra)) {
  485.             //$products = $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, MIN(v.price) price, MIN(v.actualPrice) actualPrice, MIN(v.total) total, p.totalcount FROM App\Entity\Product p" . $lftjoin . ", App\Entity\ProductVariation v, App\Entity\ProductImage pi" . $extbl . " WHERE v.productId=p.id AND pi.productId=p.id AND p.status=1 " . $extra . " GROUP BY p.id" . $srtodr)->setParameters($params)->setMaxResults(12)->getArrayResult();
  486.             $products $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, v.price, v.actualPrice, v.total, p.totalcount FROM App\Entity\Product p" $lftjoin ", App\Entity\ProductVariation v, App\Entity\ProductImage pi" $extbl " WHERE v.productId=p.id AND pi.productId=p.id AND p.status=1 " $extra  " GROUP BY p.id "$srtodr)->setParameters($params)->setMaxResults(12)->getArrayResult();
  487.           }
  488.           else {
  489.             //$products = $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, MIN(v.price) price, MIN(v.actualPrice) actualPrice, MIN(v.total) total, p.totalcount FROM App\Entity\Product p" . $lftjoin . ", App\Entity\ProductVariation v, App\Entity\ProductImage pi" . $extbl . " WHERE v.productId=p.id AND pi.productId=p.id AND p.status=1 GROUP BY p.id" . $srtodr)->setMaxResults(12)->getArrayResult();
  490.             $products $em->createQuery("SELECT p.id, p.path, p.title, v.id vid, v.color, v.size, pi.image, p.styledescription, p.type, v.price, v.actualPrice, v.total, p.totalcount FROM App\Entity\Product p" $lftjoin ", App\Entity\ProductVariation v, App\Entity\ProductImage pi" $extbl " WHERE v.productId=p.id AND pi.productId=p.id AND p.status=1" " GROUP BY p.id " $srtodr)->setMaxResults(12)->getArrayResult();
  491.           }
  492.           //print_r($products); exit;
  493.           return $this->render('ecom/store.html.twig', array('products'=>$products'globalcategories'=>$globalcategories'productcategories'=>$productcategories'producttypes'=> $producttypes'styles'=>$styles'sizes'=>$sizes'size'=>$size'colors'=>$colors'color'=>$color'collections'=>$collections'categoryatv'=>$categoryatv'srtb'=>$srtb'params'=>$params'selected_prodtype'=>$selected_prodtype'selected_style'=>$selected_style'selected_size'=>$selected_size'selected_color'=>$selected_color));
  494.         }
  495.     }
  496.     
  497.     /**
  498.      * @Route("/product/{path}", name="sajong_ecom_product_details")
  499.      */
  500.     public function productdetails(Request $request$path)
  501.     {
  502.         $em $this->getDoctrine()->getManager();
  503.         $session $request->getSession();
  504.         $product $em->getRepository('App\Entity\Product')->findOneByPath($path);
  505.         
  506.         $status_query " AND p.status=1";
  507.         $variations $em->getRepository('App\Entity\ProductVariation')->findByProductId($product->getId());
  508.         $globalcategories $em->createQuery("SELECT p.id, p.path, p.title, pi.image, p.totalcount, p.globalcategory, g.name FROM App\Entity\Product p, App\Entity\ProductImage pi, App\Entity\GlobalCategory g WHERE p.globalcategory=g.id " $status_query " GROUP BY p.globalcategory")->getArrayResult();
  509.         $productcategories = array();
  510.         $styles = array();
  511.         foreach($globalcategories as $gc) {
  512.           $productcategories[$gc['globalcategory']] = $em->createQuery("SELECT p.id, p.path, p.title, pi.image, p.totalcount, p.productcategory, c.name FROM App\Entity\Product p, App\Entity\ProductImage pi, App\Entity\ProductCategory c WHERE c.globalCategory=:gid AND p.productcategory=c.id " $status_query " GROUP BY p.productcategory")->setParameters([ 'gid'=>$gc['globalcategory']])->getArrayResult();
  513.           foreach($productcategories[$gc['globalcategory']] as $pc) {
  514.             $styles[$pc['productcategory']] = $em->createQuery("SELECT p.id, p.path, p.title, pi.image, p.totalcount, p.style, s.name FROM App\Entity\Product p, App\Entity\ProductImage pi, App\Entity\Style s WHERE s.productCategory=:cid AND p.style=s.id " $status_query " GROUP BY p.style")->setParameters(['cid'=>$pc['productcategory']])->getArrayResult();
  515.           }
  516.         }
  517.         $prod_colors $em->createQuery("SELECT DISTINCT v.id vid, c.id, c.name, c.colorCode, c.extraCode FROM App\Entity\ProductVariation v, App\Entity\Color c WHERE v.productId=:pid AND v.color=c.id GROUP BY c.id")->setParameters(['pid'=>$product->getId()])->getArrayResult();
  518.         //$prod_colors = $em->createQuery("SELECT DISTINCT c.id, c.name, c.colorCode, c.extraCode FROM App\Entity\ProductVariation v, App\Entity\Color c WHERE v.productId=:pid AND v.color=c.id")->setParameters(['pid'=>$product->getId()])->getArrayResult();
  519.         $prod_sizes $em->createQuery("SELECT DISTINCT s.id, s.name, v.id vid FROM App\Entity\ProductVariation v, App\Entity\Size s WHERE v.productId=:pid AND v.size=s.id GROUP BY s.id")->setParameters(['pid'=>$product->getId()])->getArrayResult();
  520.         $globalcategory $em->getRepository('App\Entity\GlobalCategory')->find($product->getGlobalcategory());
  521.         $productcategory $em->getRepository('App\Entity\ProductCategory')->find($product->getProductcategory());
  522.         $style $em->getRepository('App\Entity\Style')->find($product->getStyle());
  523.         $brand $em->getRepository('App\Entity\Brand')->find($product->getBrand());
  524.         //$style_city = $em->createQuery("SELECT DISTINCT c.name FROM App\Entity\BrandCategory bc, App\Entity\Brand b, App\Entity\City c WHERE bc.brand=b.id AND b.city=c.id AND bc.category=:style")->setParameters(['style'=>$product->getStyle()])->getSingleScalarResult();
  525.         $style_city $em->createQuery("SELECT DISTINCT c.name FROM App\Entity\Brand b, App\Entity\City c WHERE b.city=c.id AND b.id=:brand")->setParameters(['brand'=>$brand->getId()])->getSingleScalarResult();
  526.         //$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory($globalcategory->getId(), array('name'=>'ASC'));
  527.         //$styles = $em->getRepository('App\Entity\Style')->findByProductCategory($product->getProductcategory(), array('name'=>'ASC'));
  528.         $collection $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
  529.         if($collection != null) {
  530.             $collections $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
  531.         }
  532.         else {
  533.             $collections = array();
  534.         }
  535.         $categoryatr $em->getRepository('App\Entity\Attribute')->findOneByName('category');
  536.         if($categoryatr != null) {
  537.             $categoryatv $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
  538.         }
  539.         else {
  540.             $categoryatv = array();
  541.         }
  542.         $productimages $em->getRepository('App\Entity\ProductImage')->findByVariationId($variations[0]->getId(), array('weight'=>'ASC'));
  543.         $comments $em->getRepository('App\Entity\Comment')->findByNid($product->getId());
  544.         $related_products $em->createQuery("SELECT p.id, p.path, p.title, pi.image, MIN(v.price) price, p.totalcount, p.style, u.username, u.firstname, u.lastname FROM App\Entity\Product p, App\Entity\ProductVariation v, App\Entity\User u, App\Entity\ProductImage pi WHERE v.productId=p.id AND pi.productId=p.id AND p.createdBy=u.id AND p.productcategory=:cid AND p.style=:sid AND p.id!=:pid GROUP BY p.id")->setParameters(['pid'=>$product->getId(),'cid'=>$product->getProductcategory(), 'sid'=>$product->getStyle()])->getArrayResult();
  545.                
  546.         $dope '';
  547.         $designdope $em->createQuery("SELECT COUNT(f.id) FROM App\Entity\Flagging f WHERE f.entityId=:did AND f.entityType=:etype AND f.uid=:uid AND f.fid=:fid")->setParameters(['did'=>$product->getId(),'etype'=>'product''uid'=>$this->getUser()!=null?$this->getUser()->getId():0'fid'=>5])->getSingleScalarResult();
  548.         $like_count $em->createQuery("SELECT COUNT(f.id) FROM App\Entity\Flagging f WHERE f.entityId=:did AND f.entityType=:etype AND f.fid=:fid")->setParameters(['did'=>$product->getId(),'etype'=>'product''fid'=>5])->getSingleScalarResult();
  549.         $extra_flag 'sajong_ecom_content_flagging';
  550.         if($designdope >) { $extra_flag 'sajong_ecom_content_unflagging'; }
  551.         return $this->render('ecom/productdetail.html.twig', array('product'=>$product'variations'=>$variations'globalcategories'=>$globalcategories'globalcategory'=>$globalcategory'productcategories'=>$productcategories'productcategory'=>$productcategory'styles'=>$styles'style'=>$style'brand'=>$brand'productimages'=> $productimages'prod_colors'=> $prod_colors'prod_sizes'=> $prod_sizes'comments'=> $comments'related_products'=> $related_products'designdope'=> $designdope'extra_flag'=> $extra_flag'like_count'=> $like_count'style_city'=> $style_city));
  552.     }
  553.     
  554.     /**
  555.      * @Route("/comment/{id}", name="sajong_ecom_prodcut_comment")
  556.      */
  557.     public function prodcut_comment($id)
  558.     {
  559.         $em $this->getDoctrine()->getManager();
  560.         $design $em->getRepository('App\Entity\Product')->find($id);
  561.         return $this->render('ecom/submit_design.html.twig', array('categories'=>$categories'attributes'=> $attributes'attributevalues'=> $attributevalues'attcount'=> $attcount'vcss'=> $vcss'mcss'=> $mcss));
  562.     }
  563.     
  564.     /**
  565.      * @Route("/saveprodcutcomment", name="sajong_ecom_save_prodcut_comment")
  566.      */
  567.     public function save_prodcut_comment(Request $request)
  568.     {
  569.         $em $this->getDoctrine()->getManager();
  570.         if($this->isCsrfTokenValid('comment_form'$request->get('form_token'))) {
  571.             $comment = new Comment();
  572.             $comment->setPid(!empty($request->get('pid'))?$request->get('pid'):0); 
  573.             $comment->setUid($this->getUser()?$this->getUser()->getId():0); 
  574.             $comment->setSubject($request->get('comment_body')); 
  575.             $comment->setHostname($_SERVER['REMOTE_ADDR']); 
  576.             $comment->setCreated(new \DateTime()); 
  577.             $comment->setUpdated(new \DateTime()); 
  578.             $comment->setStatus(1); 
  579.             $comment->setThread('01'); 
  580.             $comment->setName(!empty($request->get('name'))?$request->get('name'):($this->getUser()?$this->getUser()->getFirstName():'')); 
  581.             $comment->setEmail(!empty($request->get('email'))?$request->get('email'):($this->getUser()?$this->getUser()->getEmail():'')); 
  582.             $comment->setHomepage(''); 
  583.             $comment->setLanguage('EN');
  584.             $em->persist($comment);
  585.             $em->flush();
  586.             
  587.             return new JsonResponse(array('success'=>'Comment added successfully'));
  588.         }
  589.         else {
  590.             return new JsonResponse(array('success'=>'You are not authorized to add comment'));
  591.         }
  592.     }
  593.     
  594.     /**
  595.      * @Route("/product/size", name="sajong_ecom_prodcut_sizes")
  596.      */
  597.     public function prodcutsize(Request $request)
  598.     {
  599.         $em $this->getDoctrine()->getManager();
  600.         $prod_sizes $em->createQuery("SELECT DISTINCT s.id, s.name, MIN(v.price) price, v.id vid, v.title FROM App\Entity\ProductVariation v, App\Entity\Size s, App\Entity\Color c WHERE v.productId=:pid AND v.color=c.id AND v.size=s.id AND c.id=:cid GROUP BY s.id ORDER BY s.id")->setParameters(['pid'=>$request->get('pid'), 'cid'=>$request->get('cid')])->getArrayResult();
  601.         $prod_images $em->createQuery("SELECT DISTINCT pi.id, pi.image FROM App\Entity\ProductImage pi WHERE pi.variationId=:vid ORDER BY pi.weight")->setParameters(['vid'=>$prod_sizes[0]['vid']])->getArrayResult();
  602.         //$prod_images = $em->getRepository('App\Entity\ProductImage')->findByVariationId($prod_sizes[0]['vid']);
  603.         return new JsonResponse(array('sizes'=>$prod_sizes'images'=>$prod_images));
  604.     }
  605.     
  606.     /**
  607.      * @Route("/product/price", name="sajong_ecom_prodcut_price")
  608.      */
  609.     public function prodcutprice(Request $request)
  610.     {
  611.         $em $this->getDoctrine()->getManager();
  612.         //$price = $em->createQuery("SELECT v.price FROM App\Entity\ProductVariation v WHERE v.id=:vid")->setParameters(['vid'=>$request->get('vid')])->getSingleScalarResult();
  613.         $prod $em->getRepository('App\Entity\ProductVariation')->find($request->get('vid'));
  614.         //$prod_images = $em->getRepository('App\Entity\ProductImage')->findByVariationId($request->get('vid'));
  615.         $prod_images $em->createQuery("SELECT DISTINCT pi.id, pi.image FROM App\Entity\ProductImage pi WHERE pi.variationId=:vid")->setParameters(['vid'=>$request->get('vid')])->getArrayResult();
  616.         return new JsonResponse(array('price'=>$prod->getPrice(), 'images'=>$prod_images));
  617.     }
  618.     
  619.     /**
  620.      * @Route("/ecomsearch", name="sajong_ecom_prodcut_ecomsearch")
  621.      */
  622.     public function ecomsearch(Request $request)
  623.     {
  624.         $em $this->getDoctrine()->getManager();
  625.         $designs $em->createQuery("SELECT p.id, v.id vid, v.sku, p.title, p.path, pi.image, v.price, c.name color, s.name size FROM App\Entity\ProductImage pi, App\Entity\Product p, App\Entity\ProductVariation v, App\Entity\Color c, App\Entity\Size s WHERE p.id=v.productId AND pi.variationId=v.id AND c.id=v.color AND s.id=v.size AND (p.title like :title OR p.description like :title) ORDER BY p.title")->setParameters(array('title'=>'%' $_GET['term'] . '%'))->getArrayResult();
  626.         $design = array();
  627.         foreach($designs as $d) {
  628.           $design[] = array('label'=>$d['title'], 'value'=>$d['title'], 'id'=>$d['id'], 'vid'=>$d['vid'], 'sku'=>$d['sku'], 'price'=>$d['price'], 'color'=>$d['color'], 'size'=>$d['size'], 'img'=> $d['image']);
  629.         }
  630.         return new JsonResponse($design);
  631.     }    
  632.     
  633.     /*
  634.     * Auto Complete search for utmos global search
  635.     */
  636.     function _sajong_ecom_products_ecomsearch() {
  637.       $designs db_query("SELECT DISTINCT nid FROM {node}, {field_data_field_design_reference}, {field_data_field_about_the_design} a WHERE type='design_submit' AND status=1 AND (title like :title OR field_about_the_design_value like :title OR name like :title ) AND nid = field_design_reference_target_id AND nid = a.entity_id", array(':title'=>'%' $_GET['term'] . '%'));
  638.       $design = array();
  639.       foreach($designs as $d) {
  640.         $design_details node_load($d->nid);
  641.         $design[] = array('label'=>$design_details->title'value'=>$design_details->title'id'=>$design_details->nid'author'=>$design_details->name'price'=>$design_details->nid'img'=>file_create_url($design_details->field_design_image['und'][0]['uri']));
  642.       }
  643.       //$d = array(array('label'=>'Product 11', 'value'=>'Product 1', 'id'=>'Product 1', 'author'=>'Author 1', 'price'=>'$ 20', 'img'=>'http://localhost/nn/sites/default/files/pdt1.jpg'), array('label'=>'Product 2', 'value'=>'Product 2', 'id'=>'Product 2', 'author'=>'Author 2', 'price'=>'$ 40', 'img'=>'http://localhost/nn/sites/default/files/pdt2.jpg'), array('label'=>'Product 3', 'value'=>'Product 3', 'id'=>'Product 3', 'author'=>'Author 3', 'price'=>'$ 30', 'img'=>'http://localhost/nn/sites/default/files/pdt3.jpg'));
  644.       print json_encode($design);
  645.       exit;
  646.     }
  647.     
  648.     public function variable_get($path) {
  649.       $em $this->getDoctrine()->getManager();
  650.       $content $em->getRepository('App\Entity\Content')->findOneByPath($path);
  651.       if($content != null)
  652.         return $content->getBody();
  653.       else 
  654.         return '';
  655.       
  656.     }
  657. }