<?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\OfficeBrain\Bundle\UserBundle\Controller\SignUpController as baseSignUpController;
use App\OfficeBrain\Bundle\UserBundle\Entity\User;
// use App\project\OfficeBrain\CustomBundle\UserBundle\Entity\User; // change project user entity
use App\project\OfficeBrain\CustomBundle\UserBundle\Form\UserType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\OfficeBrain\Bundle\CoreBundle\Manager\Service\CoreService;
use App\OfficeBrain\Bundle\UserBundle\Manager\Service\SignUpService;
use App\project\OfficeBrain\CustomBundle\UserBundle\Manager\Service\SignUpService as CustomSignUpService;
use App\OfficeBrain\Bundle\UserBundle\Manager\Service\LoginService;
use App\OfficeBrain\Bundle\UserBundle\Manager\Service\UserService;
use App\OfficeBrain\Bundle\QueueBundle\Manager\Service\ApiManager as QueueManager;
use App\OfficeBrain\Bundle\CompanyBundle\Manager\Service\CompanyService;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
class SignUpController extends baseSignUpController
{
/**
*
* @author Employee Id: 4273
* To set member variables default value
*
* @return NULL
*/
public function __construct(Container $serviceContainer, CoreService $coreService, SignUpService $signUpService, CustomSignUpService $customSignUpService, LoginService $loginService, UserService $userService, QueueManager $queueApiManager, CompanyService $companyService)
{
parent::__construct($serviceContainer, $coreService, $signUpService, $loginService, $userService, $queueApiManager);
$this->customSignUpManager = $customSignUpService->getSignUpExtended();
$this->companyService = $companyService->getCompanyExtended();
$this->userService = $userService->getUserExtended();
}
public function __destruct()
{
parent::__destruct();
}
public function initAction(Request $request){
parent::initAction($request);
// $this->signUpManager = $this->serviceContainer->get('office_brain_user_sign_up_manager_custom');
$this->signUpManager = $this->customSignUpManager;
// $this->companyManager = $this->serviceContainer->get('office_brain_company_manager');
$this->companyManager = $this->companyService;
// $this->userManagerCore = $this->serviceContainer->get('office_brain_user_manager');
$this->userManagerCore = $this->userService;
}
/**
* @author Employee Id: 4203
* OB eCommerce Product - auto suggest company list
*
* @Route("/{_locale}/fetch-company",
* name="office_brain_userbundle_sign_up_fetchCompany",
* options={"expose"=true}
* )
* @Method({"GET"})
*/
public function fetchCompanyAction(Request $request)
{
//$this->initAction($request);
$this->request = null;
$this->responseArray = null;
$this->request = $request;
$this->companyManager = $this->serviceContainer->get('office_brain_company_manager');
$this->userManagerCore = $this->serviceContainer->get('office_brain_user_manager');
$query = $this->request->get('q',NULL);
$company = $this->request->get('companyName',NULL);
$account_id = $this->request->get('account_id',NULL);
if($query!=NULL){
$this->responseArray = $this->companyManager->autoSearchCompany($query);
if(count($this->responseArray)>0){
return new Response($_GET['callback'] . '('.json_encode($this->responseArray).')');
}else{
$this->responseArray = array();
return new Response($_GET['callback'] . '('.json_encode($this->responseArray).')');
}
}
if($company!=NULL && $account_id!=NULL){
$companies = $this->companyManager->getCompanyByNameSearch($company);
if(count($companies)>0){
$value = $companies[0]->getId();
}else{
$userObj = $this->userManagerCore->getUserById(1); //1- admin user id
$cmpObject = $this->companyManager->addNewCompanyData($userObj,$company,$account_id);
$value = $cmpObject->getId();
}
$this->responseArray = array();
$this->responseArray['value'] = $value;
$this->responseArray['label'] = $company;
$this->responseArray['account_id'] = $account_id;
return new Response($_GET['callback'] . '('.json_encode($this->responseArray).')');
}
}
/**
* @author Employee Id: 4273
* OB eCommerce Product - SignUp
* Function created to show sign up form
*
* @Route("/{_locale}/distributor-registration",
* name="office_brain_userbundle_sign_up_distributor_registration",
* options={"expose"=true}
* )
* @Method({"GET"})
* @Template("project/OfficeBrain/CustomBundle/UserBundle/SignUp/register.html.twig")
*/
public function distributorRegistrationAction(Request $request)
{
$this->initAction($request);
$data = array();
$userType = 'distributor';
$signUpFormFields = $this->signUpManager->getsignUpFormData($userType);
$user = new User();
// $userTypeObj = new UserType($this->coreManager, $signUpFormFields, 'main_register'); // symfony 4
// $form = $this->createForm($userTypeObj, $user); // symfony 4
$form = $this->createForm(UserType::class, $user, [
'core_manager' => $this->coreManager,
'form_fields' => $signUpFormFields,
'form_type' => 'main_register',
]);
$form->handleRequest($this->request);
$data['form_user'] = $form->createView();
$data['userType'] = $userType;
$data['form_fields'] = $signUpFormFields;
$data['states'] = $this->coreManager->getStateData();
$data['distributors'] = null;//$this->userManager->getDistributors();
return $data;
}
/**
* @author Employee Id: 4273
* OB eCommerce Product - SignUp
* Function created to validate sign up form
*
* @Route("/{_locale}/register",
* name="office_brain_userbundle_sign_up_register_post",
* options={"expose"=true}
* )
*
* @Method({"POST"})
* @Template("project/OfficeBrain/CustomBundle/UserBundle/SignUp/register.html.twig")
*/
public function registerPostAction(Request $request)
{
$this->initAction($request);
$data = array();
$format = $this->request->get('_format', 'html') ;
$userType = $this->request->get('userType', 'supplier');
$signUpFormFields = $this->signUpManager->getsignUpFormData($userType);
$requestarray=$this->request->request->all();
if(isset($requestarray['ob_user']) && $requestarray['ob_user']['countryId']=='739')
{
unset($signUpFormFields['validations'][7]);
unset($signUpFormFields['validations'][8]);
unset($signUpFormFields['validations'][9]);
$data['othercountry']=1;
}
$user = new User();
//echo "<pre>";print_R();die;
// $userTypeObj = new UserType($this->coreManager, $signUpFormFields, 'main_register'); // symfony 4
// $form = $this->createForm($userTypeObj, $user); // symfony 4
$form = $this->createForm(UserType::class, $user, [
'core_manager' => $this->coreManager,
'form_fields' => $signUpFormFields,
'form_type' => 'main_register',
]);
$form->handleRequest($this->request);
$userStatus = $this->signUpManager->registerUser($form);
// echo "<pre>";print_r($this->getErrorsAsArray($form));die;
if ($userStatus) {
$user = $userStatus;
$userObj = $this->userManagerCore->getUserById($userStatus->getId());
// $companyObj = $this->companyManager->addNewCompanyData($userObj,$userStatus->getUserCompany()->getCompanyName(),$userStatus->getUserCompany()->getTioryCustomerAccountId()); // symfony 4
// $this-> ->updateUserCompany($userObj,$companyObj, $companyObj->getCompanyName()); // symfony 4
$erp_flag = TRUE;
$user_type = $userStatus->getUserType();
$is_individual = $userStatus->getIsIndividual();
if (($user_type == 'end_user' && $this->projectSetting['instance_type'] == 'supplier') || ($is_individual == TRUE && $this->projectSetting['instance_type'] == 'supplier')) {
$erp_flag = FALSE;
}
$erpIntegration = $this->projectSetting['configuration_rule']['erp_integration'];
if ($erpIntegration && $erpIntegration['userManagement'] == 1 && !empty($this->queueApiManager) && $userStatus->getuserCompany() && ($userStatus->getStatus()=="active" || $userStatus->getStatus()=="inactive") && $erp_flag == TRUE) {
$this->queueApiManager->mapQueue("create", $userStatus);
}
//if ($this->request->get('billing_address')) {
//$billingArray['id'] = 1; $billingArray['default'] = $this->request->get('billing_address',null);
$billingAddressId = 1;
$billingAddress = $this->signUpManager->addAddress($user, $billingAddressId);
if ($erpIntegration && $erpIntegration['userManagement'] == 1 && !empty($this->queueApiManager) && $userStatus->getuserCompany() && ($userStatus->getStatus()=="active" || $userStatus->getStatus()=="inactive") && $erp_flag == TRUE) {
$this->request->attributes->set('object', 'ContactBook');
$this->queueApiManager->mapQueue("create", $billingAddress);
}
//}
//if ($this->request->get('shipping_address')) {
//$shippingArray['id'] = 2;$shippingArray['default'] = $this->request->get('shipping_address',null);
$shippingAddressId = 2;
$shippingAddress = $this->signUpManager->addAddress($user, $shippingAddressId);
if ($erpIntegration && $erpIntegration['userManagement'] == 1 && !empty($this->queueApiManager) && ($userStatus->getStatus()=="active" || $userStatus->getStatus()=="inactive") && $userStatus->getuserCompany() && $erp_flag == TRUE) {
$this->request->attributes->set('object', 'ContactBook');
$this->queueApiManager->mapQueue("create", $shippingAddress);
}
//}
$this->signUpManager->sendEmail($form, $this->translator->trans('user.register.mail.subject'));
if ($signUpFormFields['approval_configuration']['is_auto_approve']->option->approval_email==1) {
$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('user.register.success'));
} else {
$this->get('session')->getFlashBag()->set('success_message', $this->translator->trans('user.register.success'));
}
if ($this->signUpConfigrations['option']['allow_login_after_signup']==1) {
$this->loginManager->doLogin($form);
return $this->redirect($this->generateUrl($this->projectSetting['configuration_rule']['user']['login']['option']['after_login_url']));
} else {
return $this->redirect($this->generateUrl('office_brain_page_homepage_view'));
}
}
$data['form_user'] = $form->createView();
$data['userType'] = $userType;
$data['form_fields'] = $signUpFormFields;
$data['states'] = $this->coreManager->getStateData();
$data['distributors'] = null;//$this->userManager->getDistributors();
if ($this->request->isXmlHttpRequest()) {
if ($this->request->get('call_type')=='country_code' || $this->request->get('call_type')=='state_code') {
return $data;
} else {
$errors = array(
'form_name' => $form->getName(),
'form_status' => 'error',
'form_errors' => $this->formErrorManager->prepareErrorArray($this->get('validator')->validate($form))
);
return new JsonResponse($errors);
}
}
return $data;
}
public function getErrorsAsArray($form)
{
$errors = array();
foreach ($form->getErrors() as $error) {
$errors[] = $error->getMessage();
}
foreach ($form->all() as $key => $child) {
if ($err = $this->getErrorsAsArray($child)) {
$errors[$key] = $err;
}
}
return $errors;
}
}