<?php
class EmailingAjax extends EntityRepository {
    
    public $flashmessenger = null;
    
    public function __construct() {
        if(!$this->flashmessenger instanceof FlashMessenger){
            $this->flashmessenger = new FlashMessenger();
        }
    }
    
    public function getResponse($request, $options) {
        return $this->$request($options);
    }
    
    public function _getTranslation($text){
        $translator = new Translator();
        return $translator->_getTranslation($text);
    }
    
    public function getTranslation($options){
        $msj = $options['msj'];
        
        return array(
            'response'=>true,
            'translation'=>$this->_getTranslation($msj)
                );
    }   

    public function getDataOperation($options){
        $operation = $options['operation_name'];
        $id = $options['operation_id'];
        
        switch($operation){                              
            case 'rewards_default':
                $rewardsCustomerRepo = new RewardsCustomersRepository();
                $rewardsCustomerData = $rewardsCustomerRepo->getById($id);               
                
                
                $subject = 'Rewards Paris Bakery';
                $messageMail = '';         
                $to = $rewardsCustomerData['email'];
                
                break;          
        }      
        
        return array(
            'response'=>true,
            'to'=>$to,
            'subject'=>$subject,
            'messageMail'=>$messageMail
        );        
    }
    
    public function emailing($options){
        $attachments = null;
        if(isset($options['attachmentFiles'])){
            $attachments = array();
            foreach($options['attachmentFiles'] as $row){
                $attachments[] = $row['value'];
            }
        }
        
        $data = array();
        foreach($options['options'] as $row){
            $data[$row['name']] = $row['value'];
        }
       
        $emailer = new RewardEmailer();
        $sent = $emailer->sendEmail(array(
            'to'=>$data['to'],
            'cc'=>$data['cc'],
            'subject'=>$data['subject'],
            'message'=>$data['message'],
            'attachment'=>$attachments
        ));

        if($sent){
            switch($data['operation_name']){
                case 'rewards_default':
                    $rewardsCustomerRepo = new RewardsCustomersRepository();
                    //$invoiceRepo->setInvoiceAsSent($data['operation_id']);
                    break;
            }
            
            return array(
                'response'=>true,
                'msg'=>$this->flashmessenger->getMessageString()
            );

        }else{            
            return array(
                'response'=>null,
                'msg'=>$this->flashmessenger->getMessageString()
            );
        }        
    }  
}