<?php
namespace App\project\OfficeBrain\CustomBundle\UserBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use App\project\OfficeBrain\CustomBundle\UserBundle\Entity\ContactBook;
use App\OfficeBrain\Bundle\CoreBundle\CustomeHandler as FormErrorManager;
use Symfony\Component\HttpFoundation\JsonResponse;
use App\project\OfficeBrain\CustomBundle\UserBundle\Form\ContactBookType;
use App\OfficeBrain\Bundle\UserBundle\Controller\ContactBookController as baseContactBookController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;
use App\OfficeBrain\Bundle\UserBundle\Manager\Service\ContactBookService;
use App\OfficeBrain\Bundle\CoreBundle\Manager\Service\CoreService;
use App\OfficeBrain\Bundle\QueueBundle\Manager\Service\ApiManager as QueueApiManager;
use App\project\OfficeBrain\CustomBundle\UserBundle\Manager\Service\ContactBookService as CustomContactBookService;
class ContactBookController extends baseContactBookController
{
/**
*
* @author Employee Id: 4493
* set default value
*
* @return NULL
*/
public function __construct(ContainerInterface $container, ContactBookService $contactBookService, QueueApiManager $queueApiManager, CustomContactBookService $customContactBookService, CoreService $coreService)
{
parent::__construct($container, $contactBookService, $queueApiManager, $coreService);
$this->serviceContainer = $container;
$this->customContactBookService = $customContactBookService->getContactBookExtended();
}
/**
*
* @author Employee Id: 4493
* To unset value
*
* @return NULL
*/
public function __destruct()
{
parent::__destruct();
}
private function prepareFormJsonError()
{
return array(
'form_name' => $this->form->getName(),
'form_status' => 'error',
'form_errors' => $this->formErrorManager->prepareErrorArray($this->get('validator')->validate($this->form))
);
}
/**
*
* @author Employee Id: 4493
* Create objects which use this class
*
* @return NULL
*/
public function initAction(Request $request)
{
parent::initAction($request);
// $this->contactbookmanager = $this->serviceContainer->get('office_brain_user_contact_book_manager_custom'); // symfony 4
$this->contactbookmanager = $this->customContactBookService; // symfony 4
// $this->contactbookmanager = $this->container->get('office_brain_custom_user_contact_book_manager'); // commented for symfony 5
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - contact book form
*
* @Route("{_locale}/my-account/address-book/add",
* name="office_brain_userbundle_contactbook_addcontactbook",
* options={"expose"=true}, methods={"GET"}
* )
* @Template("project/OfficeBrain/CustomBundle/UserBundle/ContactBook/addContactBook.html.twig")
*/
public function addContactBookAction(Request $request)
{
$this->initAction($request);
// $this->form = $this->createForm(new ContactBookType($this->container), new ContactBook());
$this->form = $this->createForm(ContactBookType::class, new ContactBook(), [
'container' => $this->serviceContainer,
'contactBookService' => $this->contactBookService,
'coreService' => $this->coreService,
]);
$this->form->handleRequest($this->request);
$this->responseArray['contactBookForm'] = $this->form->createView();
return $this->responseArray;
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - contact book form submit
*
* @Route("{_locale}/my-account/address-book/add",
* name="office_brain_userbundle_contactbook_addcontactbookpost",
* options={"expose"=true}, methods={"POST"}
* )
* @Template("project/OfficeBrain/CustomBundle/UserBundle/ContactBook/addContactBook.html.twig")
*/
public function addContactBookPostAction(Request $request)
{
$this->initAction($request);
$requestarray=$this->request->request->all();
if($requestarray['contact_book']['countryId']=='739')
{
$this->responseArray['othercountry']=1;
}
// $this->form = $this->createForm(new ContactBookType($this->container,'request-quote-add-address'), new ContactBook()); // symfony 4
$this->form = $this->createForm(ContactBookType::class, new ContactBook(), [
'container' => $this->serviceContainer,
'contactBookService' => $this->contactBookService,
'coreService' => $this->coreService,
'side' => 'request-quote-add-address'
]);
$this->form ->handleRequest($this->request);
if ($this->form->isValid()) {
$contactBookData = $this->form->getData();
$add=$this->contactbookmanager->addContactBook($contactBookData, '', 1);
switch ($add['status']) {
case 'SUCCESS' :
if ($this->erpIntegration && $this->erpIntegration['userManagement'] == 1 && !empty($this->queueApiManager)) {
$this->queueApiManager->mapQueue("create", $add['data']);
}
$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('user.contact_book.add_message.address_success'));
break;
case 'FAIL':
$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('user.contact_book.add_message.address_fail'));
break;
}
if($this->request->isXmlHttpRequest ()){
$this->responseArray['status'] = 'success';
$this->responseArray['call_ajax_url_checkout'] = $this->generateUrl("office_brain_requestquotebundle_requestquote_getshippingaddressbyid") ; //'office_brain_requestquotebundle_requestquote_getshippingaddressbyid';
$this->responseArray['contact_book_id'] = $contactBookData->getId();
$this->responseArray['success_message'] = $this->serviceContainer->get('translator')->trans('request_quote.shipping_type.address_added_successfully');
$counter = $this->request->get('counter',null); // Used for request Quote Add Address
if($counter!=null){
$this->responseArray['counter'] = $counter;
}
return new JsonResponse($this->responseArray);
}
return $this->redirect($this->generateUrl("office_brain_userbundle_contactbook_viewcontactbook"));
}
$this->responseArray['contactBookForm'] = $this->form->createView();
if($this->request->isXmlHttpRequest ()){
if ($this->request->get('call_type')=='country_code' || $this->request->get('call_type')=='state_code') {
return $this->responseArray;
} else {
return new JsonResponse ( $this->prepareFormJsonError ($this->form) );
}
}
return $this->responseArray;
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - contact book form
*
* @Route("{_locale}/my-account/address-book/edit/{contactBookId}",
* name="office_brain_userbundle_contactbook_editcontactbook",
* options={"expose"=true}, methods={"GET"}
* )
* @Template("project/OfficeBrain/CustomBundle/UserBundle/ContactBook/editContactBook.html.twig")
*/
public function editContactBookAction(Request $request)
{
$this->initAction($request);
$contactBookId = $this->request->get('contactBookId');
$contactBookArray = $this->contactbookmanager->getSingleContactBook($contactBookId);
$contactBookForm = $this->createForm(ContactBookType::class, $contactBookArray, [
'container' => $this->serviceContainer,
'contactBookService' => $this->contactBookService,
'coreService' => $this->coreService,
'side' => null,
]);
if($contactBookArray->getCountryId()=='739')
{
$othercountry=1;
return array('contactBookForm'=> $contactBookForm->createView(),'contactBookId'=>$contactBookId,'othercountry'=>$othercountry);
}
return array('contactBookForm'=> $contactBookForm->createView(),'contactBookId'=>$contactBookId);
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - contact book form submit
*
* @Route("{_locale}/my-account/address-book/edit/{contactBookId}",
* name="office_brain_userbundle_contactbook_editcontactbookpost",
* options={"expose"=true}, methods={"POST"}
* )
* @Template("project/OfficeBrain/CustomBundle/UserBundle/ContactBook/editContactBook.html.twig")
*/
public function editContactBookPostAction(Request $request)
{
$this->initAction($request);
$contactBookId = $this->request->get('contactBookId');
$contactBookObject = $this->contactbookmanager->getSingleContactBook($contactBookId);
$previousAddressType=$contactBookObject->getAdressType()->getId();
// $this->form = $this->createForm(new ContactBookType($this->container,'request-quote-add-address'), $contactBookObject);
$this->form = $this->createForm(ContactBookType::class, $contactBookObject, [
'container' => $this->serviceContainer,
'contactBookService' => $this->contactBookService,
'coreService' => $this->coreService,
'side' => 'request-quote-add-address',
]);
$this->form ->handleRequest($this->request);
$requestarray=$this->request->request->all();
if ($this->form->isValid()) {
$contactBookData = $this->form->getData();
if($previousAddressType!=$contactBookData->getAdressType()->getId()) {
$this->changeAddressTypeFlag=1;
}
$edit=$this->contactbookmanager->editContactBook($contactBookData,$this->changeAddressTypeFlag);
switch ($edit['status']) {
case 'SUCCESS' :
if ($this->erpIntegration && $this->erpIntegration['userManagement'] == 1 && !empty($this->queueApiManager) ) {
if($edit['data']->getErpId()){
$this->queueApiManager->mapQueue("update", $edit['data']);
}else{
$this->queueApiManager->mapQueue("create", $edit['data']);
}
}
$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('user.contact_book.edit_message.address_success'));
break;
case 'FAIL':
$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('user.contact_book.edit_message.address_fail'));
break;
}
if($this->request->isXmlHttpRequest()){
$this->responseArray['status'] = 'success';
// if(count($this->request->get('is_edit_from_payment'))>0 && $this->request->get('is_edit_from_payment')==1)
if($this->request->get('is_edit_from_payment') > 0 && $this->request->get('is_edit_from_payment')==1)
{
$this->responseArray['call_ajax_url'] = $this->generateUrl("office_brain_orderbundle_order_getcheckoutshippingaddressbyid");
}
else
{
$this->responseArray['call_ajax_url'] = $this->generateUrl("office_brain_requestquotebundle_requestquote_getshippingaddressbyid");
}
$this->responseArray['contact_book_id'] = $contactBookId;
$this->responseArray['success_message'] = $this->serviceContainer->get('translator')->trans('request_quote.shipping_type.edit_address_added_successfully');
$counter = $this->request->get('counter',null); // Used for request Quote Edit Address
if($counter!=null){
$this->responseArray['counter'] = $counter;
}
return new JsonResponse($this->responseArray);
}
return $this->redirect($this->generateUrl("office_brain_userbundle_contactbook_viewcontactbook"));
}
if($this->request->isXmlHttpRequest ()){
if ($this->request->get('call_type')=='country_code' || $this->request->get('call_type')=='state_code') {
return $this->responseArray;
} else {
return new JsonResponse ( $this->prepareFormJsonError ($this->form) );
}
}
if($requestarray['contact_book']['countryId']=='739')
{
$othercountry=1;
return array('contactBookForm'=>$this->form->createView(),'contactBookId'=>$contactBookId,'othercountry'=>$othercountry);
}
return array('contactBookForm'=> $this->form->createView(),'contactBookId'=>$contactBookId);
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - contact book View
*
* @Route("{_locale}/my-account/address-book/view",
* name="office_brain_userbundle_contactbook_viewcontactbook",
* options={"expose"=true}, methods={"GET"}
* )
* @Template("project/OfficeBrain/CustomBundle/UserBundle/ContactBook/viewContactBook.html.twig")
*/
public function viewContactBookAction(Request $request)
{
$this->initAction($request);
$this->responseArray['record_set'] = $this->contactbookmanager->getContactBook($this->userId, $this->addressBookFlag,$this->page,$this->recordsPerPage);
return $this->responseArray;
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - contact book Delete
*
* @Route("{_locale}/my-account/address-book/delete",
* name="office_brain_userbundle_contactbook_deletecontactbook",
* options={"expose"=true}
* )
* @Template()
*/
public function deleteContactBookAction(Request $request)
{
$this->initAction($request);
$contactBook = $this->contactbookmanager->deleteContactBook($this->contactBookId);
switch ($contactBook['status']) {
case 'SUCCESS' :
if ($this->erpIntegration && $this->erpIntegration['userManagement'] == 1 && !empty($this->queueApiManager)) {
$this->queueApiManager->mapQueue("remove", array('id' => $this->contactBookId));
}
$this->responseArray['status'] = 'success';
//$this->responseArray['status_message'] = $this->get('translator')->trans('user.contact_book.remove_message.address_success');
$this->responseArray['page_url'] = $this->generateUrl('office_brain_userbundle_contactbook_viewcontactbook');
break;
case 'FAIL':
$this->responseArray['error_message'] = $this->translator->trans('user.contact_book.remove_message.address_fail');
break;
}
return new JsonResponse($this->responseArray);
}
/**
* @author Employee Id: 4493
* OB eCommerce Product - Is Default Update
*
* @Route("{_locale}/my-account/address-book/is-default",
* name="office_brain_userbundle_contactbook_isdefault",
* options={"expose"=true}
* )
* @Template()
*/
public function isDefaultAction(Request $request)
{
$this->initAction($request);
$data = array();
$contactBookId = $this->request->get('id');
$isDefault = $this->contactbookmanager->updateIsDefault($contactBookId);
switch ($isDefault['status']) {
case 'SUCCESS' :
$this->responseArray['status'] = 'success';
$this->responseArray['addressType'] = $isDefault['data']->getAdressType()->getId();
$this->responseArray['status_message'] = $this->translator->trans('user.contact_book.update_default.address_success');
break;
case 'FAIL':
$this->responseArray['error_message'] = $this->translator->trans('user.contact_book.update_default.address_fail');
break;
}
return new JsonResponse($this->responseArray);
}
}