<?php

namespace OfficeBrain\Bundle\PageBundle\Manager\Extended;

use Symfony\Component\HttpFoundation\Request;
use OfficeBrain\Bundle\PageBundle\Manager\AbstractManager\NewsletterAbstract;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use OfficeBrain\Bundle\PageBundle\Entity\Newsletter;

class NewsletterExtended extends NewsletterAbstract
{
	public function __construct($entityManager, $request,$emailService)
	{
		parent::__construct($entityManager, $request,$emailService);
	}
	
	public function __destruct()
	{
	    parent::__destruct();
	}
	
	public function insertEmailId($emailId)
	{
		if(!empty($emailId)){
		    $newsletterId = $this->newsletterRepository->findByEmail($emailId,$this->instanceId);
		    
		    if(!empty($newsletterId)){
		        return "EMAIL_ALREADY_EXISTS";
		    }
		    
			$newsletterId = $this->newsletterRepository->insertEmail($emailId,$this->instanceId);
			if($newsletterId){
				$eventKey = 'newsletter_email_send';
				$to = $emailId;
				$from =  $this->projectSetting['admin_email'];
				$subject = "Newsletter Registration Acknowledgement";
				
				$attachment = array(); //This is optional field. If you have any attachment please provide path in this parameter.
				$cc = array(); // This is optional field.
				$bcc = array(); //This is optional field.
				
				$this->emailService->sendMail($eventKey, $to, array(), $this->languageId, $from, $subject);
				return $newsletterId;
			}
		}
		
	}
}
