<?php
namespace App\OfficeBrain\Bundle\CompareProductBundle\Controller;
// use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Translation\Translator;
use Symfony\Component\HttpFoundation\Response;
use App\OfficeBrain\Bundle\CompareProductBundle\Form\CompareProductEmailToFriendType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use App\OfficeBrain\Bundle\CoreBundle\Service\SessionManager;
use App\OfficeBrain\Bundle\CoreBundle\Service\OBCommon;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use App\OfficeBrain\Bundle\CompareProductBundle\Manager\Service\CompareProductService;
use App\OfficeBrain\Bundle\EmailBundle\Manager\Service\EmailService;
/**
*
* @author OfficeBrain 4074 <info@officebrain.com>
*
* Desciption: This controller help user to compare various product
*
* @package: OfficeBrain eCommerce Product Web Client
*
* @subpackage: Compare Product
*
* @version 1.0
*
*/
class CompareProductController extends AbstractController
{
/**
*
* @author Employee Id: 4074
*
* Desciption: Define member variables with thier default value
*
* @param NULL
*
* @return NULL
*
* @throws NULL
*
*/
public function __construct(ContainerInterface $container, SessionManager $sessionManager, RequestStack $requestStack, RouterInterface $router, ValidatorInterface $validatorInterface, CompareProductService $compareProductService, EmailService $emailManager)
{
$this->request = null;
$this->cultureId = null;
$this->refererUrl = null;
$this->productId = null;
$this->cultureProductId = null;
$this->responseArray = array();
$this->projectSetting = array();
$this->comapreProductMaxLimit = null;
$this->commonLibraryManager = new OBCommon($router, $validatorInterface, $requestStack);;
$this->compareProductManager = $compareProductService->getCompareProductExtended();
$this->sessionManager = $sessionManager;
$this->emailManager = $emailManager->getEmailExtended();
$this->serviceContainer = $container;
}
/**
*
* @author Employee Id: 4074
*
* Desciption: Unset member variables
*
* @param NULL
*
* @return NULL
*
* @throws NULL
*
*/
public function __destruct()
{
unset($this->request);
unset($this->cultureId);
unset($this->refererUrl);
unset($this->productId);
unset($this->cultureProductId);
unset($this->responseArray);
unset($this->projectSetting);
unset($this->comapreProductMaxLimit);
unset($this->commonLibraryManager);
unset($this->compareProductManager);
unset($this->sessionManager);
unset($this->emailManager);
}
/**
*
* @author Employee Id: 4074
*
* Desciption: Create and initialize various required object and member variables
*
* @param NULL
*
* @return NULL
*
* @throws NULL
*
*/
private function initAction(Request $request)
{
$this->request = $request;
$this->productId = $this->request->get('productId', null);
$this->cultureProductId = $this->request->get('cultureProductId', null);
$this->projectSetting = $this->request->get('project_setting');
$this->cultureId = $this->projectSetting['culture_id'];
$this->comapreProductMaxLimit = $this->projectSetting['configuration_rule']['transaction']['compare_product']['inline_view']['option']['no_of_product'];
// $this->sessionManager = $this->serviceContainer->get('office_brain_session_manager');
$this->sessionManager = $this->sessionManager;
// $this->commonLibraryManager = $this->serviceContainer->get('office_brain_common_library');
$this->commonLibraryManager = $this->commonLibraryManager;
$userId = $this->projectSetting['user_id'];
// if ($userId>0) {
// $this->compareProductManager = $this->serviceContainer->get('office_brain_compare_product_registered_extended');
// // $this->compareProductManager = $this->get('office_brain_compare_product_manager'); // symfony 4
// } else {
// $this->compareProductManager = $this->serviceContainer->get('office_brain_compare_product_guest_extended');
// // $this->compareProductManager = $this->get('office_brain_compare_product_manager'); // symfony 4
// }
$this->compareProductManager = $this->compareProductManager;
// $this->emailManager = $this->serviceContainer->get('office_brain_email_service');
$this->emailManager = $this->emailManager;
$this->responseArray['status'] = 'error';
$this->responseArray['product_id'] = $this->productId;
$this->mediaFilePath = $this->serviceContainer->getParameter('cms.media_file_path');
}
/**
*
* @author Employee Id: 4074
*
* Desciption: Show recently addded product for compare in quick list
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/recent-product-compare-list",
* name="office_brain_compareproductbundle_compareproduct_recentcompareproductlist",
* options={"expose"=true}
* )
*
* @Method({"GET"})
*
* @Template("OfficeBrain/Bundle/CompareProductBundle/CompareProduct/recentCompareProductList.html.twig")
*
*/
public function recentCompareProductListAction(Request $request)
{
$this->initAction($request);
// $this->responseArray['record_set'] = $this->compareProductManager->getCompareProduct();
// $this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
$this->responseArray['record_set'] = 0;
$this->responseArray['total_record'] = 3;
return $this->responseArray;
}
/**
*
* @author Employee Id: 4074
*
* Desciption: This function allow user to compare selected product feature wise.
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product",
* name="office_brain_compareproductbundle_compareproduct_compareproduct"
* )
*
* @Method({"GET", "POST"})
*
* @Template("OfficeBrain/Bundle/CompareProductBundle/CompareProduct/compareProduct.html.twig")
*
*/
public function compareProductAction(Request $request)
{
$this->initAction($request);
$this->responseArray['record_set'] = $this->compareProductManager->getCompareProduct();
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
$this->responseArray['remaining_compare_product'] = $this->comapreProductMaxLimit - $this->responseArray['total_record'];
$this->compareProductManager->setContinueShoppingUrl($this->commonLibraryManager->getRefererRouteName(), $this->commonLibraryManager->getRefererUrl());
return $this->responseArray;
}
/**
*
* @author Employee Id: 4074
*
* Desciption: This function allow user to download compare product page as pdf.
*
* @param integer productId
* @param integer cultureProductId
*
* @return application/pdf compare-product.pdf
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-pdf",
* name="office_brain_compareproductbundle_compareproduct_downloadcompareproductaspdf"
* )
*
* @Method({"GET"})
*
* @Template()
*
*/
public function downloadCompareProductAsPdfAction(Request $request)
{
$this->initAction($request);
$data=$this->request->get('data');
$this->responseArray['QtyValues'] = $data['qty_values'];
$this->responseArray['priceProd'] = $data['price'];
$this->responseArray['record_set'] = $this->compareProductManager->getCompareProduct();
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
$this->responseArray['remaining_compare_product'] = $this->comapreProductMaxLimit - $this->responseArray['total_record'];
$response = $this->render('OfficeBrain/Bundle/CompareProductBundle/CompareProduct/downloadCompareProductAsPdf.html.twig', $this->responseArray);
return new Response(
$this->serviceContainer->get('knp_snappy.pdf')->getOutputFromHtml($response->getContent()),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="compare-product.pdf"'
)
);
}
/**
*
* @author Employee Id: 4074
*
* Desciption: This function allow user add product to compare product feature.
*
* @param integer productId
* @param integer cultureProductId
*
* @return Json Response
*
* @throws NULL
*
* @Route("/{_locale}/add-to-compare-product",
* name="office_brain_compareproductbundle_compareproduct_addproducttocompare",
* options={"expose"=true}
* )
*
* @Method({"POST"})
*
* @Template()
*
*/
public function addProductToCompareAction(Request $request)
{
$this->initAction($request);
if ($this->productId && $this->cultureProductId) {
$status = $this->compareProductManager->setCompareProduct($this->productId, $this->cultureProductId);
switch ($status) {
case 'ERROR_MAX_LIMIT_REACHED':
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.max_product_compare', array('%no_of_compare_product%' => $this->comapreProductMaxLimit));
break;
case 'ERROR_PRODUCT_ALREADY_EXISTS':
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.product_already_exists');
break;
case 'SUCCESS':
$this->responseArray['status'] = 'success';
$this->responseArray['success_message'] = $this->serviceContainer->get('translator')->trans('product_compare.add_success_message');
$this->refererUrl = $this->commonLibraryManager->getRefererRouteName();
break;
}
}
if ($this->refererUrl=='office_brain_compareproductbundle_compareproduct_compareproduct') {
$this->responseArray['page_url'] = $this->generateUrl('office_brain_compareproductbundle_compareproduct_compareproduct');
} else {
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
}
return new JsonResponse($this->responseArray);
}
/**
*
* @author Employee Id: 4074
*
* Desciption: This function allow user to delete product from compare product section.
*
* @param integer productId
* @param integer cultureProductId
*
* @return Json Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-delete",
* name="office_brain_compareproductbundle_compareproduct_deletecompareproduct",
* options={"expose"=true}
* )
*
* @Method({"POST"})
*
* @Template()
*
*/
public function deleteCompareProductAction(Request $request)
{
$this->initAction($request);
if ($this->productId && $this->cultureProductId) {
$status = $this->compareProductManager->deleteCompareProduct($this->productId, $this->cultureProductId);
switch ($status) {
case 'ERROR_PRODUCT_ALREADY_EXISTS':
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.product_already_exists');
break;
case 'SUCCESS':
$this->responseArray['status'] = 'success';
$this->responseArray['success_message'] = $this->serviceContainer->get('translator')->trans('product_compare.remove_success_message');
$this->refererUrl = $this->commonLibraryManager->getRefererRouteName();
break;
}
}
if ($this->refererUrl=='office_brain_compareproductbundle_compareproduct_compareproduct') {
$this->sessionManager->set('compareProd_success_deleted',$this->serviceContainer->get('translator')->trans('product_compare.remove_success_message'));
$this->responseArray['page_url'] = $this->generateUrl('office_brain_compareproductbundle_compareproduct_compareproduct');
} else {
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
}
return new JsonResponse($this->responseArray);
}
/**
*
* @author Employee Id: 4074
*
* Desciption: This function allow user to delete all products from compare product section.
*
* @param NULL
*
* @return Json Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-delete-all",
* name="office_brain_compareproductbundle_compareproduct_deleteallcompareproduct",
* options={"expose"=true}
* )
*
* @Method({"POST"})
*
* @Template()
*
*/
public function deleteAllCompareProductAction(Request $request)
{
$this->initAction($request);
$status = $this->compareProductManager->deleteAllCompareProduct();
switch ($status) {
case 'ERROR_PRODUCT_ALREADY_EXISTS':
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.product_already_exists');
break;
case 'SUCCESS':
$this->responseArray['status'] = 'success';
$this->responseArray['success_message'] = $this->serviceContainer->get('translator')->trans('product_compare.remove_success_message');
$this->refererUrl = $this->commonLibraryManager->getRefererRouteName();
break;
}
if ($this->refererUrl=='office_brain_compareproductbundle_compareproduct_compareproduct') {
$this->sessionManager->set('compareProd_success_deleted',$this->serviceContainer->get('translator')->trans('product_compare.remove_success_message'));
$this->responseArray['page_url'] = $this->generateUrl('office_brain_compareproductbundle_compareproduct_compareproduct');
} else {
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
}
return new JsonResponse($this->responseArray);
}
/**
*
* @author Employee Id: 4471
*
* Desciption: This function generate form which allow user send compare product page as email to friends.
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-email-to-friend",
* name="office_brain_compareproductbundle_compareproduct_emailtofriend",
* )
*
* @Method({"GET"})
*
* @Template("OfficeBrain/Bundle/CompareProductBundle/CompareProduct/emailToFriend.html.twig")
*
*/
public function emailToFriendAction(Request $request)
{
$this->initAction($request);
$this->responseArray = [];
//$this->responseArray['product_id'] = $data['productId'];
$this->responseArray['QtyValues'] = json_encode($this->request->get('QtyValues'));
$this->responseArray['priceProd'] = json_encode($this->request->get('priceProd'));
$this->form = $this->createForm(CompareProductEmailToFriendType::class);
$this->responseArray['form'] = $this->form->createView();
return $this->responseArray;
}
/**
*
* @author Employee Id: 4471
*
* Desciption: This function allow user send compare product page as email to friends.
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-email-to-friend-submit",
* name="office_brain_compareproductbundle_compareproduct_emailtofriend_submit",
* )
*
* @Method({"POST"})
*
* @Template()
*
*/
public function submitMailEmailToFriendCompareProductAction(Request $request)
{
$this->initAction($request);
$this->form = $this->createForm(CompareProductEmailToFriendType::class);
$this->form->handleRequest($this->request);
if ($this->form->isValid()) {
$this->sendMailToFriend();
$this->responseArray['status'] = 'success';
$this->responseArray['close_modal'] = true;
$this->responseArray['success_message'] = $this->serviceContainer->get('translator')->trans('product_compare.email_msg_sucess');
} else {
$this->responseArray = $this->commonLibraryManager->prepareFormJsonError($this->form);
}
return new JsonResponse($this->responseArray);
}
/**
*
* @author Employee Id: 4471
*
* Desciption: This function allow user print compare product page.
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-print",
* name="office_brain_compareproductbundle_compareproduct_print",
* )
*
* @Method({"GET"})
*
* @Template("OfficeBrain/Bundle/CompareProductBundle/CompareProduct/printCompareProduct.html.twig")
*
*/
public function printCompareProductAction(Request $request)
{
$this->initAction($request);
//$this->responseArray = array();
$data = $this->request->get('result');
$data = json_decode("$data",true);
//$this->responseArray['product_id'] = $data['productId'];
$this->responseArray['QtyValues'] = $data['qty'];
$this->responseArray['priceProd'] = $data['price'];
$this->responseArray['record_set'] = $this->compareProductManager->getCompareProduct();
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
$this->responseArray['remaining_compare_product'] = $this->comapreProductMaxLimit - $this->responseArray['total_record'];
return $this->responseArray;
}
/**
*
* @author Employee Id: 4471
*
* Desciption: This function allow user check the product column quantity price
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-price-listing",
* name="office_brain_compareproductbundle_compareproduct_pricelisting"
* )
*
* @Method({"POST"})
*
* @Template()
*
*/
public function priceListingCompareProductAction(Request $request)
{
$this->initAction($request);
$recordSet = $this->compareProductManager->getProductDetailById($this->productId, $this->cultureId);
$this->responseArray['record_set'] = $recordSet[$this->productId];
unset($recordSet);
return $this->responseArray;
}
/**
*
* @author Employee Id: 4471
*
* Desciption: This function allow user check the product price by changing quantity
*
* @param NULL
*
* @return Html Response
*
* @throws NULL
*
* @Route("/{_locale}/compare-product-price-qty",
* name="office_brain_compareproductbundle_compareproduct_priceqty"
* )
*
* @Method({"POST"})
*
* @Template()
*
*/
public function priceQtyCompareProductAction(Request $request)
{
$this->initAction($request);
$this->qty = $this->request->get('qty', null);
if ($this->qty === null) {
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.error_message.require_quantity');
} elseif ($this->qty == 0 || !$this->commonLibraryManager->isInteger($this->qty)) {
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.error_message.invalid_quantity');
} elseif ($this->productId && $this->qty) {
$price = $this->compareProductManager->getProductPriceByQty($this->productId, $this->cultureId, $this->qty);
if ($price != null) {
$this->responseArray['status'] = 'success';
$this->responseArray['price'] = $price;
} else {
$recordSet = $this->compareProductManager->getProductDetailById($this->productId, $this->cultureId);
$this->responseArray['error_message'] = $this->serviceContainer->get('translator')->trans('product_compare.error_message.invalid_lowest_quantity', array('%min_quantity%'=>$this->commonLibraryManager->getLowestProductQty($recordSet[$this->productId]['pricing'])));
unset($recordSet);
}
}
return new JsonResponse($this->responseArray);
}
/**
*
* @author Employee Id: 4074
*
* Desciption: This function allow user go back to page from where he/she came to compare product page.
*
* @param NULL
*
* @return 302 Redirection Response
*
* @throws NULL
*
* @Route("/{_locale}/continue-shopping-from-compare-product",
* name="office_brain_compareproductbundle_compareproduct_continueshopping"
* )
*
* @Method({"GET"})
*
*/
public function continueShoppingAction(Request $request)
{
return $this->redirect($this->generateUrl('office_brain_searchbundle_search_continueshopping'));
/*
$this->initAction($request);
return $this->redirect($this->compareProductManager->getContinueShoppingUrl());
*/
}
/**
*
* @author Employee Id: 4074
*
* Desciption: Prepare email html response and send to friend
*
* @param NULL
*
* @return NULL
*
* @throws NULL
*
*/
private function sendMailToFriend()
{
$eventKey = 'send_mail_to_friend';
$this->responseArray['QtyValues'] = json_decode($this->request->get('QtyValues'));
$this->responseArray['priceProd'] = json_decode($this->request->get('priceProd'));
$this->responseArray['record_set'] = $this->compareProductManager->getCompareProduct();
$this->responseArray['total_record'] = $this->compareProductManager->getTotalCount();
$response = $this->render('OfficeBrain/Bundle/CompareProductBundle/CompareProduct/mailTempleteEmailToFriendCompareProduct.html.twig', $this->responseArray);
$templateParameterArray = array(
'message' => $this->form['message']->getData(),
'send_email_to_friend_template' => $response->getContent()
);
$toEmailData = $this->form['to_email']->getData();
$toEmailAry = explode(",", $toEmailData);
if(count($toEmailAry) > 0 ){
foreach ($toEmailAry as $toEmail){
$this->emailManager->sendMail($eventKey, $toEmail, $templateParameterArray, $this->cultureId , $this->form['from_email']->getData());
}
}
unset($eventKey);
unset($response);
unset($templateParameterArray);
}
}