<?php
namespace OfficeBrain\Bundle\CmsBundle\Manager\AbstractManager;

use OfficeBrain\Bundle\CmsBundle\Form\ContactUsType;
use OfficeBrain\Bundle\CmsBundle\Entity\ContactUs;


/**
 * @author OfficeBrain 4571 <info@officebrain.com>
 *
 * Description : This is the abstract class which contains all the methods of its extended class.
 *
 **/
abstract class CmsContactUsAbstract
{
    protected $projectSettings;
    protected $cmsContactUsRepository;
    protected $instanceId;
    protected $responseArray;
    
    /**
     * @author Employee Id: 4571
     *
     * Description : This function is used to set member variables default value
     *
     * @param 1: $entityManager
     * @param 2: $request
     * @param 3: $translator
     * @param 4: $knpPaginator
     * @param 5: $formFactory
     * @param 5: $country
     * @param 6: $language
     * @param 7: $basePath
     *
     * @return: NULL
     *
     * @throws Exception: Null
     */
    public function __construct($entityManager, $request, $translator, $knpPaginator, $formFactory, $country, $language, $basePath)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->projectSettings = $this->request->get('project_setting');        
        $this->cmsContactUsRepository = $this->entityManager->getRepository('OfficeBrainCmsBundle:ContactUs');
        $this->cmsContactUsRepository->setProjectSetting($this->projectSettings);        
        $this->responseArray = null;               
        $this->translator = $translator;
        $this->formFactory = $formFactory;
        $this->knpPaginator = $knpPaginator;
        $this->country = $country;
        $this->language = $language;
        $this->basePath = $basePath;
        $this->instanceId = $this->projectSettings['instance_id'];
        $this->languageId = $this->projectSettings['culture_id'];
    }
    
   /**
    * @author Employee Id: 4571
    *
    * Description : This function is used to unset member variables
    *
    * @param : None
    *
    * @return NULL
    *
    * @throws Exception: Null
    */
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSettings);
        unset($this->thirdPartySettingRepository);
        unset($this->thirdPartyServiceKeyRepository);
        unset($this->thirdPartyServiceMetaRepository);
        unset($this->instanceId);
        unset($this->translator);
        unset($this->knpPaginator);
        unset($this->country);
        unset($this->language);
        unset($this->basePath);
        unset($this->formFactory);
        unset($this->responseArray);
        unset($this->languageId);
    }
    
   /**
    * @author Employee Id: 4571
    *
    * Description : This function is used to save contact us fields and send mail 
    *
    * @param : None
    *
    * @return NULL
    *
    * @throws Exception: Null
    */
    public function addContactUs($contactUsData, $emailService)
    {
    	
		$this->contactusObj = $this->cmsContactUsRepository->addContacts($contactUsData);
		
		if ($this->contactusObj) {				 
		   // after saving to db send mail from here ---- 			
		   $sendEmail = $this->sendContactUsEmail($contactUsData, " Contact Us Form Enquiry ", $emailService);			
									
		   return array('status'=>'SUCCESS','data'=>$this->contactusObj);
		}
		
		return array('status'=>'FAIL');
    }
    
    /**
     * @author Employee Id: 4571
     * OB eCommerce Product - sendContactUsEmail
     * Function created for sending contact us email
     */
    public function sendContactUsEmail($form, $subject, $emailService)
    {
    	$contactUsFormData = $form;
    	
    	$eventKey = 'site_contact_us_email';
    	//$to = 'pgohil@officebrain.com';   //$this->projectSetting['admin_email'];
    	$to = '';
    	$languageId = $this->languageId;
    	$params = array(
    			'name' => $contactUsFormData->getFullName(),
    			'phone' => $contactUsFormData->getPhone(),
    			'email_id' => $contactUsFormData->getEmail(),
    			'company_name' => $contactUsFormData->getCompanyName(),
    			'city' => $contactUsFormData->getCity(),
    			'message' => $contactUsFormData->getMessage(),
    	);
    	
    	$from =  $contactUsFormData->getEmail(); 
    	$subject = "";
    	$attachment = array();
    	$cc = ''; // array();
    	$bcc = ''; // array();
    	 
    	$emailService->sendMail($eventKey, $to, $params, $languageId, $from, $subject, $attachment, $cc, $bcc);
    	 
    	return true;
    }
    
}
