<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use App\Entity\ProductCategory;
use App\Entity\Partner;
use App\Entity\Product;
use App\Entity\ProductVariation;
use App\Entity\ProductImage;
use App\Entity\EntryDetail;
use App\Entity\ProductEntry;
use App\Entity\Attribute;
use App\Entity\AttributeValues;
use App\Entity\ProductAttribute;
class ProductController extends AbstractController
{
/**
* @Route("/admin/nn/list/products", name="sajong_ecom_admin_list_product")
*/
public function productlist(Request $request)
{
$em = $this->getDoctrine()->getManager();
$sizes = array();
$params = array();
if(!empty($request->get('brand'))) {
$extra .= " AND p.brand=:bid";
$params['bid'] = $request->get('brand');
}
if(!empty($request->get('global_category'))) {
$extra .= " AND p.globalcategory=:gid";
$params['gid'] = $request->get('global_category');
}
if(!empty($request->get('product_category'))) {
$extra .= " AND p.productcategory=:pid";
$params['pid'] = $request->get('product_category');
}
if(!empty($request->get('title'))) {
$extra .= " AND p.title=:title";
$params['title'] = '%' . $request->get('title') .'%';
}
if(!empty($request->get('collection'))) {
foreach($request->get('collection') as $attr) {
$collection_filterurl .= '&collection[]=' . $attr;
$selected_coln[$attr] = 'active';
$params['collection'][] = $attr;
}
$extra .= " AND pa.attributeId IN (:collection)";
$extbl = stristr($extbl, ", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
}
if(!empty($request->get('size'))) {
foreach($request->get('size') as $attr) {
$size_filterurl .= '&size[]=' . $attr;
$selected_size[$attr] = 'active';
$params['size'][] = $attr;
}
$extra .= " AND v.size IN (:size)";
}
if(!empty($request->get('category'))) {
foreach($request->get('category') as $attr) {
$category_filterurl .= '&category[]=' . $attr;
$selected_cat[$attr] = 'active';
$params['category'][] = $attr;
}
$extra .= " AND pa.attributeId IN (:category)";
$extbl = stristr($extbl, ", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
}
if(!empty($request->get('style'))) {
$extra .= " AND p.style=:sid";
$params['sid'] = $request->get('style');
$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();
}
$brands = $em->getRepository('App\Entity\Brand')->findByStatus(1, array('title'=>'ASC'));
$globalcategories = $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
$styles = $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
$collection = $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
if($collection != null) {
$collections = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
}
else {
$collections = array();
}
$categoryatr = $em->getRepository('App\Entity\Attribute')->findOneByName('category');
if($categoryatr != null) {
$categoryatv = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
}
else {
$categoryatv = array();
}
if(!empty($extra)) {
$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)
}
else {
$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)
}
//$products = $em->getRepository('App\Entity\Product')->findAll();
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, 'params'=>$params));
}
/**
* @Route("/admin/nn/add/product", name="sajong_ecom_admin_add_product")
*/
public function addproduct()
{
$em = $this->getDoctrine()->getManager();
$brands = $em->getRepository('App\Entity\Brand')->findByStatus(1, array('title'=>'ASC'));
$cities = $em->getRepository('App\Entity\City')->findAll();
$city = array();
foreach ($cities as $c) {
$city[$c->getId()] = $c->getName();
}
$globalcategories = $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
$styles = $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
$sizes = $em->getRepository('App\Entity\Size')->findByStatus(1,array('name'=>'ASC'));
$colors = $em->getRepository('App\Entity\Color')->findByStatus(1,array('name'=>'ASC'));
$producttypes = $em->getRepository('App\Entity\ProductType')->findByStatus(1);
$collection = $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
if($collection != null) {
$collections = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
}
else {
$collections = array();
}
$categoryatr = $em->getRepository('App\Entity\Attribute')->findOneByName('category');
if($categoryatr != null) {
$categoryatv = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
}
else {
$categoryatv = array();
}
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));
}
/**
* @Route("/admin/nn/edit/product/{id}", name="sajong_ecom_admin_edit_product")
*/
public function editproduct($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('App\Entity\Product')->find($id);
$productvariations = $em->getRepository('App\Entity\ProductVariation')->findByProductId($id);
$variationimages = array();
foreach ($productvariations as $productvariation) {
$variationimages[$productvariation->getId()] = $em->getRepository('App\Entity\ProductImage')->findByVariationId($productvariation->getId());
}
$brands = $em->getRepository('App\Entity\Brand')->findByStatus(1, array('title'=>'ASC'));
$cities = $em->getRepository('App\Entity\City')->findAll();
$city = array();
foreach ($cities as $c) {
$city[$c->getId()] = $c->getName();
}
$globalcategories = $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
$apparelcategories = $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory(65, array('name'=>'ASC'));
$styles = $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
$sizes = $em->getRepository('App\Entity\Size')->findByStatus(1,array('name'=>'ASC'));
$colors = $em->getRepository('App\Entity\Color')->findByStatus(1,array('name'=>'ASC'));
$producttypes = $em->getRepository('App\Entity\ProductType')->findByStatus(1);
$collection = $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
if($collection != null) {
$collections = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
}
else {
$collections = array();
}
$categoryatr = $em->getRepository('App\Entity\Attribute')->findOneByName('category');
if($categoryatr != null) {
$categoryatv = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
}
else {
$categoryatv = array();
}
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 ));
}
/**
* @Route("/admin/nn/save/product", name="sajong_ecom_admin_save_product")
*/
public function saveproduct(Request $request)
{
$em = $this->getDoctrine()->getManager();
if(!empty($request->get('productid'))) {
$product = $em->getRepository('App\Entity\Product')->find($request->get('productid'));
$this->addFlash('success', 'Product updated successfully');
}
else {
$product = new Product();
$product->setCreated(new \DateTime());
$this->addFlash('success', 'Product added successfully');
}
$product->setTitle($request->get('title'));
$product->setPath($request->get('path'));
$product->setDescription($request->get('description'));
$product->setStyledescription($request->get('styledescription'));
$product->setBrand($request->get('brand'));
$product->setGlobalcategory($request->get('globalcategory'));
$product->setProductcategory($request->get('productcategory'));
$product->setStyle($request->get('style'));
$product->setType($request->get('type'));
$product->setQuantity(!empty($request->get('quantity'))?$request->get('quantity'):0);
$product->setProductcount(!empty($request->get('productcount'))?$request->get('productcount'):0);
$product->setPrice(!empty($request->get('price'))?($request->get('price')*100):0);
$product->setTax(!empty($request->get('tax'))?($request->get('tax')*100):0);
$product->setDiscount(!empty($request->get('discount'))?($request->get('discount')*100):0);
//$product->setTotal(!empty($request->get('total'))?$request->get('total'):$request->get('price'));
$product->setTotal(!empty($request->get('price'))?($request->get('price')*100):0);
$product->setUpdated(new \DateTime());
$product->setCreatedBy($this->getUser()->getId());
$product->setAccess(new \DateTime());
$product->setTotalcount(0);
$product->setStage('Approved');
$product->setStatus($request->get('status'));
$em->persist($product);
$em->flush();
for($i=0; $i<count($request->get('variationtitle')); $i++) {
if(!empty($request->get('variationtitle')[$i])) {
$productvariation = null;
if (!empty($request->get('product_variationid')[$i])) {
$productvariation = $em->getRepository('App\Entity\ProductVariation')->find($request->get('product_variationid')[$i]);
}
if($productvariation == null) {
$productvariation = new ProductVariation();
$productvariation->setProductId($product->getId());
$productvariation->setCreated(new \DateTime());
$productvariation->setCreatedBy($this->getUser()->getId());
$productvariation->setAccess(new \DateTime());
}
$findex = $request->get('variationai')[$i];
$productvariation->setTitle($request->get('variationtitle')[$i]);
$productvariation->setSku($request->get('variationsku')[$i]);
$productvariation->setColor($request->get('variationcolor')[$i]);
$productvariation->setSize($request->get('variationsize')[$i]);
$productvariation->setActualPrice($request->get('variation_actual_price')[$i]*100);
$productvariation->setPrice($request->get('variation_price')[$i]*100);
$productvariation->setTax(0);
$productvariation->setTotal($request->get('variation_price')[$i]*100);
$productvariation->setUpdated(new \DateTime());
$productvariation->setTotalcount(!empty($request->get('variation_count')[$i])?$request->get('variation_count')[$i]:0);
$productvariation->setStock($request->get('variation_stock')[$i]);
$productvariation->setStatus(!empty($request->get('variation_status')[$i])?1:0);
$em->persist($productvariation);
$em->flush();
if (!empty($request->get('product_variationid')[$i])) {
if(!empty($request->get('evariationimage')[$findex])) {
for($k=0; $k<count($request->get('evariationimage')[$findex]); $k++) {
if(!empty($request->get('evariationimage')[$findex][$k])) {
$productimage = new ProductImage();
$productimage->setProductId($product->getId());
$productimage->setVariationId($productvariation->getId());
$productimage->setImage($request->get('evariationimage')[$findex][$k]);
$productimage->setImageType('Product');
$productimage->setWeight($request->get('evariationimgweight')[$findex][$k]);
$productimage->setCreated(new \DateTime());
$productimage->setStatus(1);
$em->persist($productimage);
$em->flush();
}
}
}
}
else {
if(!empty($request->get('variationimage')[$findex])) {
for($k=0; $k<count($request->get('variationimage')[$findex]); $k++) {
if(!empty($request->get('variationimage')[$findex][$k])) {
$productimage = new ProductImage();
$productimage->setProductId($product->getId());
$productimage->setVariationId($productvariation->getId());
$productimage->setImage($request->get('variationimage')[$findex][$k]);
$productimage->setImageType('Product');
$productimage->setWeight($request->get('variationimgweight')[$findex][$k]);
$productimage->setCreated(new \DateTime());
$productimage->setStatus(1);
$em->persist($productimage);
$em->flush();
}
}
}
}
}
}
if(!empty($request->get('deleted_variations'))) {
$delvariations = explode(",", $request->get('deleted_variations'));
foreach ($delvariations as $dv) {
$delvariation = $em->getRepository('App\Entity\ProductVariation')->find(trim($dv));
$em->remove($delvariation);
$em->flush();
}
}
$files = explode(",", $request->get('deleted_variation_images'));
foreach ($files as $f) {
if(!empty($f) && file_exists($f)) {
unlink($f);
$productimage = $em->getRepository('App\Entity\ProductImage')->findOneByImage($f);
$em->remove($productimage);
$em->flush();
}
}
if(!empty($request->get('imageid'))) {
for($i=0; $i<count($request->get('imageid')); $i++) {
$productimage = $em->getRepository('App\Entity\ProductImage')->find($request->get('imageid')[$i]);
$productimage->setWeight(!empty($request->get('vimageweight')[$i])?$request->get('vimageweight')[$i]:$i);
$em->persist($productimage);
$em->flush();
}
}
return $this->redirect($this->generateUrl('sajong_ecom_admin_list_product'));
}
/**
* @Route("/admin/nn/delete/product/{id}", name="sajong_ecom_admin_delete_product")
*/
public function deleteproduct($id)
{
$em = $this->getDoctrine()->getManager();
$product = $em->getRepository('App\Entity\Product')->find($id);
$em->remove($product);
$em->flush();
$this->addFlash('success', 'Product deleted successfully');
return $this->redirect($this->generateUrl('sajong_ecom_admin_list_product'));
}
/**
* @Route("/uploadproductimage", name="sajong_ecom_upload_productimage")
*/
public function uploadproductimage(Request $request)
{
$img = $request->get('photo');
$fileName = uniqid() . '.png';
$folderPath = "uploads/products/";
$file = $folderPath . $fileName;
$fp = fopen($file, 'w');
$data = explode(',', $img);
fwrite($fp, base64_decode($data[1]));
fclose($fp);
echo $folderPath . $fileName; exit;
}
/**
* @Route("/store", name="sajong_ecom_products_store")
*/
public function store(Request $request)
{
$em = $this->getDoctrine()->getManager();
$srtb = '';
$extra = '';
$lftjoin = '';
$srtodr = '';
$extbl = '';
$params = array();
$selected_prodtype = array();
$selected_style = array();
$selected_size = array();
$selected_color = array();
$globalcategories = $em->getRepository('App\Entity\GlobalCategory')->findByStatus(1, array('name'=>'ASC'));
$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByStatus(1, array('name'=>'ASC'));
$producttypes = $em->getRepository('App\Entity\ProductType')->findByStatus(1, array('name'=>'ASC'));
$styles = $em->getRepository('App\Entity\Style')->findByStatus(1, array('name'=>'ASC'));
$sizes = $em->getRepository('App\Entity\Size')->findByStatus(1, array('name'=>'ASC'));
$colors = $em->getRepository('App\Entity\Color')->findByStatus(1, array('name'=>'ASC'));
$collection = $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
if($collection != null) {
$collections = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
}
else {
$collections = array();
}
$categoryatr = $em->getRepository('App\Entity\Attribute')->findOneByName('category');
if($categoryatr != null) {
$categoryatv = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
}
else {
$categoryatv = array();
}
if(!empty($_GET['global_category'])) {
$extra .= " AND p.globalcategory=:gid";
$params['gid'] = $_GET['global_category'];
$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory($_GET['global_category'], array('name'=>'ASC'));
}
if(!empty($_GET['product_category'])) {
$extra .= " AND p.productcategory=:pid";
$params['pid'] = $_GET['product_category'];
$styles = $em->getRepository('App\Entity\Style')->findByProductCategory($_GET['product_category'], array('name'=>'ASC'));
}
if(!empty($_GET['prodtype'])) {
foreach($_GET['prodtype'] as $attr) {
$params['prodtype'][] = $attr;
$selected_prodtype[$attr] = 'checked';
}
$extra .= " AND p.type IN (:prodtype)";
}
if(!empty($_GET['collection'])) {
foreach($_GET['collection'] as $attr) {
$params['collection'][] = $attr;
}
$extra .= " AND pa.attributeId IN (:collection)";
$extbl = stristr($extbl, ", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
}
if(!empty($_GET['category'])) {
foreach($_GET['category'] as $attr) {
$params['category'][] = $attr;
}
$extra .= " AND pa.attributeId IN (:category)";
$extbl = stristr($extbl, ", App\Entity\ProductAttribute pa")?'':", App\Entity\ProductAttribute pa";
}
if(!empty($_GET['style'])) {
$params['sid'] = $_GET['style'];
$selected_style[$_GET['style']] = 'checked';
$extra .= " AND p.style=:sid";
//$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();
}
if(!empty($_GET['size'])) {
foreach($_GET['size'] as $attr) {
$params['size'][] = $attr;
$selected_size[$attr] = 'checked';
}
$extra .= " AND v.size IN (:size)";
}
if(!empty($_GET['color'])) {
foreach($_GET['color'] as $attr) {
$params['color'][] = $attr;
$selected_color[$attr] = 'checked';
}
$extra .= " AND v.color IN (:color)";
}
if(!empty($_GET['sort_by'])) {
if ($_GET['sort_by'] == 'l')
$srtodr = " ORDER BY v.price ASC";
elseif ($_GET['sort_by'] == 'h')
$srtodr = " ORDER BY v.price DESC";
elseif ($_GET['sort_by'] == 'r')
$srtodr = " ORDER BY v.created DESC";
elseif ($_GET['sort_by'] == 'p') {
$lftjoin = " LEFT JOIN App\Entity\Flagging f WITH f.entityId=p.id AND f.entityType='product' AND f.fid=3";
$srtodr = " ORDER BY COUNT(f.id) DESC";
}
$srtb = $_GET['sort_by'];
}
$size = array();
$color = array();
$size[0] = '';
foreach ($sizes as $s) {
$size[$s->getId()] = " | " . $s->getName();
}
$color[0] = '';
foreach ($colors as $c) {
$color[$c->getId()] = " | " . $c->getName();
}
$products = array();
if(!empty($request->get('page')) && $request->get('page')=='infinitescroll') {
if(!empty($extra)) {
//$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();
$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();
}
else {
//$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();
$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();
}
//$this->get('session')->set('pageno', $this->get('session')->get('pageno')+1);
//$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();
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)));
}
else {
//$this->get('session')->set('pageno', 0);
if(!empty($extra)) {
//$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();
$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();
}
else {
//$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();
$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();
}
//print_r($products); exit;
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));
}
}
/**
* @Route("/product/{path}", name="sajong_ecom_product_details")
*/
public function productdetails(Request $request, $path)
{
$em = $this->getDoctrine()->getManager();
$session = $request->getSession();
$product = $em->getRepository('App\Entity\Product')->findOneByPath($path);
$status_query = " AND p.status=1";
$variations = $em->getRepository('App\Entity\ProductVariation')->findByProductId($product->getId());
$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();
$productcategories = array();
$styles = array();
foreach($globalcategories as $gc) {
$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();
foreach($productcategories[$gc['globalcategory']] as $pc) {
$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();
}
}
$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();
//$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();
$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();
$globalcategory = $em->getRepository('App\Entity\GlobalCategory')->find($product->getGlobalcategory());
$productcategory = $em->getRepository('App\Entity\ProductCategory')->find($product->getProductcategory());
$style = $em->getRepository('App\Entity\Style')->find($product->getStyle());
$brand = $em->getRepository('App\Entity\Brand')->find($product->getBrand());
$style_city = $prod_colors = $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();
//$productcategories = $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory($globalcategory->getId(), array('name'=>'ASC'));
$apparelcategories = $em->getRepository('App\Entity\ProductCategory')->findByGlobalCategory(65, array('name'=>'ASC'));
//$styles = $em->getRepository('App\Entity\Style')->findByProductCategory($product->getProductcategory(), array('name'=>'ASC'));
$collection = $em->getRepository('App\Entity\Attribute')->findOneByName('collections');
if($collection != null) {
$collections = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($collection->getId(), array('title'=>'ASC'));
}
else {
$collections = array();
}
$categoryatr = $em->getRepository('App\Entity\Attribute')->findOneByName('category');
if($categoryatr != null) {
$categoryatv = $em->getRepository('App\Entity\AttributeValues')->findByAttributeId($categoryatr->getId(), array('title'=>'ASC'));
}
else {
$categoryatv = array();
}
$productimages = $em->getRepository('App\Entity\ProductImage')->findByVariationId($variations[0]->getId(), array('weight'=>'ASC'));
$comments = $em->getRepository('App\Entity\Comment')->findByNid($product->getId());
$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();
$dope = '';
$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();
$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();
$extra_flag = 'sajong_ecom_content_flagging';
if($designdope >0 ) { $extra_flag = 'sajong_ecom_content_unflagging'; }
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));
}
/**
* @Route("/comment/{id}", name="sajong_ecom_prodcut_comment")
*/
public function prodcut_comment($id)
{
$em = $this->getDoctrine()->getManager();
$design = $em->getRepository('App\Entity\Product')->find($id);
return $this->render('ecom/submit_design.html.twig', array('categories'=>$categories, 'attributes'=> $attributes, 'attributevalues'=> $attributevalues, 'attcount'=> $attcount, 'vcss'=> $vcss, 'mcss'=> $mcss));
}
/**
* @Route("/saveprodcutcomment", name="sajong_ecom_save_prodcut_comment")
*/
public function save_prodcut_comment(Request $request)
{
$em = $this->getDoctrine()->getManager();
if($this->isCsrfTokenValid('comment_form', $request->get('form_token'))) {
$comment = new Comment();
$comment->setPid(!empty($request->get('pid'))?$request->get('pid'):0);
$comment->setUid($this->getUser()?$this->getUser()->getId():0);
$comment->setSubject($request->get('comment_body'));
$comment->setHostname($_SERVER['REMOTE_ADDR']);
$comment->setCreated(new \DateTime());
$comment->setUpdated(new \DateTime());
$comment->setStatus(1);
$comment->setThread('01');
$comment->setName(!empty($request->get('name'))?$request->get('name'):($this->getUser()?$this->getUser()->getFirstName():''));
$comment->setEmail(!empty($request->get('email'))?$request->get('email'):($this->getUser()?$this->getUser()->getEmail():''));
$comment->setHomepage('');
$comment->setLanguage('EN');
$em->persist($comment);
$em->flush();
return new JsonResponse(array('success'=>'Comment added successfully'));
}
else {
return new JsonResponse(array('success'=>'You are not authorized to add comment'));
}
}
/**
* @Route("/product/size", name="sajong_ecom_prodcut_sizes")
*/
public function prodcutsize(Request $request)
{
$em = $this->getDoctrine()->getManager();
$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();
$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();
//$prod_images = $em->getRepository('App\Entity\ProductImage')->findByVariationId($prod_sizes[0]['vid']);
return new JsonResponse(array('sizes'=>$prod_sizes, 'images'=>$prod_images));
}
/**
* @Route("/product/price", name="sajong_ecom_prodcut_price")
*/
public function prodcutprice(Request $request)
{
$em = $this->getDoctrine()->getManager();
//$price = $em->createQuery("SELECT v.price FROM App\Entity\ProductVariation v WHERE v.id=:vid")->setParameters(['vid'=>$request->get('vid')])->getSingleScalarResult();
$prod = $em->getRepository('App\Entity\ProductVariation')->find($request->get('vid'));
//$prod_images = $em->getRepository('App\Entity\ProductImage')->findByVariationId($request->get('vid'));
$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();
return new JsonResponse(array('price'=>$prod->getPrice(), 'images'=>$prod_images));
}
/**
* @Route("/ecomsearch", name="sajong_ecom_prodcut_ecomsearch")
*/
public function ecomsearch(Request $request)
{
$em = $this->getDoctrine()->getManager();
$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();
$design = array();
foreach($designs as $d) {
$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']);
}
return new JsonResponse($design);
}
/*
* Auto Complete search for utmos global search
*/
function _sajong_ecom_products_ecomsearch() {
$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'] . '%'));
$design = array();
foreach($designs as $d) {
$design_details = node_load($d->nid);
$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']));
}
//$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'));
print json_encode($design);
exit;
}
public function variable_get($path) {
$em = $this->getDoctrine()->getManager();
$content = $em->getRepository('App\Entity\Content')->findOneByPath($path);
if($content != null)
return $content->getBody();
else
return '';
}
}