<?php

namespace OfficeBrain\Bundle\EmailBundle\Manager\AbstractManager;

/**
 * @author OfficeBrain 4795 <info@officebrain.com>
 * Description: Abstract class of email
 */
abstract class EmailTemplateAbstract
{
    protected $container;
    protected $em;
    protected $dm;
    protected $session;
    protected $request;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $userId;
    protected $cultureId;
    protected $cultureCode;
    protected $createdUid;
    protected $updatedUid;
    protected $sessionInstanceKey;
    protected $translator;
    
    protected $eventRepo;
    protected $eventUnsubscribeRepo;
    protected $templateRepo;
    protected $templateLangRepo;
    protected $headerRepo;
    protected $headerLangRepo;
    

    /**
     * @author Employee Id: 4795
     * OB eCommerce Product -
     * Initialize the required variable on class load
     */
    public function __construct($container, $request)
    {
        $this->container          = $container;
        $this->em                 = $container->get('doctrine.orm.entity_manager');
        $this->request            = $request;
        $this->projectSetting     = $this->request->get('project_setting');
        $this->instanceId         = $this->projectSetting['instance_id'];
        $this->instanceType       = $this->projectSetting['instance_type'];
        $this->userId             = $this->projectSetting['user_id'];
        $this->cultureId          = $this->projectSetting['culture_id'];
        $this->cultureCode        = $this->projectSetting['culture_code'];
        $this->createdUid         = $this->userId;
        $this->updatedUid         = $this->userId;
        $this->sessionInstanceKey = $this->instanceId.'-'.$this->cultureId;
        $this->session            = $container->get('session');
        $this->translator         = $container->get('translator');
        
        $this->eventRepo          = $this->em->getRepository('OfficeBrainEmailBundle:EmailEventMaster');
        $this->eventUnsubscribeRepo = $this->em->getRepository('OfficeBrainEmailBundle:EmailUnsubscribedEvent');
        $this->templateRepo       = $this->em->getRepository('OfficeBrainEmailBundle:EmailTemplateMaster');
        $this->templateLangRepo   = $this->em->getRepository('OfficeBrainEmailBundle:EmailTemplateLanguage');
        $this->headerRepo         = $this->em->getRepository('OfficeBrainEmailBundle:EmailTemplateHeaderMaster');
        $this->headerLangRepo     = $this->em->getRepository('OfficeBrainEmailBundle:EmailTemplateHeaderLanguage');
        
        $this->eventRepo->setProjectSetting($this->projectSetting);
        $this->eventUnsubscribeRepo->setProjectSetting($this->projectSetting);
        $this->templateRepo->setProjectSetting($this->projectSetting);
        $this->templateLangRepo->setProjectSetting($this->projectSetting);
        $this->headerRepo->setProjectSetting($this->projectSetting);
        $this->headerLangRepo->setProjectSetting($this->projectSetting);
    }
    
    public function __destruct()
    {
        unset($this->container);
        unset($this->em);
        unset($this->dm);
        unset($this->request);
        unset($this->sessionManager);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->createdUid);
        unset($this->upatedUid);
        unset($this->sessionInstanceKey);
        unset($this->session);
        unset($this->translator);
        
        unset($this->eventRepo);
        unset($this->eventUnsubscribeRepo);
        unset($this->templateRepo);
        unset($this->templateLangRepo);
        unset($this->headerRepo);
        unset($this->headerLangRepo);
    }
    
    /** Start of event methods **/
    
    public function setEvent($emailEvent)
    {
    }
    
    public function getChildEventList()
    {
    }
    
    public function getMasterEventList()
    {
    }
    
    public function getEventList()
    {
    }
    
    public function getEvent($id)
    {
    }
    
    public function getChildEventByEventKey($eventKey)
    {
    }
    
    public function updateEvent($emailEvent)
    {
    }
    
    public function deleteMasterEvent($id)
    {
    }
    
    public function deleteChildEvent($id)
    {
    }
    
    public function searchEvent($dataArr)
    {
    }
    
    /** End of event methods **/
    
    /** Start of template methods **/
    
    public function setTemplate($emailTemplate)
    {
    }
    
    public function getTemplateList()
    {
    }
    
    public function getTemplate($id)
    {
    }
    
    public function updateTemplate($emailTemplate)
    {
    }
    
    public function deleteTemplate($id)
    {
    }
    
    public function duplicateTemplate($id, $templateObj)
    {
    }
    
    public function getPreviewTemplateData($content, $culture, $headerId)
    {
    }
    
    public function getContentWithHeaders($content, $culture)
    {
    }
    
    /**
     * @author Employee Id: 4795
     * Description : Replace dynamic parameters with value in content
     *
     * @param 1 : Html content
     * @param 1 : params array
     *
     * @return type : Html content
     *
     * @throws Exception: Null
     */
    public function replaceDynamicParams($content, $params)
    {
        $pattern = '/{{(.*?)[\|\|.*?]?}}/';
        $content = preg_replace_callback($pattern, function ($match) use ($params) {
            $match = trim(strip_tags($match[1]));
            return isset($params[$match]) ? $params[$match] : '';
        }, $content);
        return $content;
    }
    
    /** End of template methods **/
    
    /** Start of Header methods **/
    
    public function setHeader($emailTemplateHeader)
    {
    }
    
    public function getHeaderList($language,$dataArr)
    {
    }
    
    public function getHeader($id)
    {
    }
    
    public function updateHeader($emailTemplateHeader)
    {
    }
    
    public function deleteHeader($id)
    {
    }
    
    /** End of header methods **/
       
    /** Common methods **/
    
    public function commit($entity)
    {
        $this->em->persist($entity);
        $this->em->flush();
        return $entity;
    }
    
    public function remove($entity)
    {
        $this->em->remove($entity);
        $this->em->flush();
    }
    /** End of common methods **/
}
