<?php

/**
 * MailTemplate
 * 
 * This class has been auto-generated by the Doctrine ORM Framework
 * 
 * @package    sf
 * @subpackage model
 * @author     Atma
 * @version    SVN: $Id: Builder.php 7490 2010-03-29 19:53:27Z jwage $
 */
class MailTemplate extends BaseMailTemplate
{
    public static function sendMailUser($event, $body_mail, $theme_mail, $recipient = false, $attach = false)
    {
        if($event){
            $mail_template = Doctrine::getTable('MailTemplate')->findOneByEvent($event);
            if($mail_template){
                $theme = $mail_template->getTheme();
                $body = $mail_template->getBody();

                if(count($body_mail) > 0){
                    foreach ($body_mail as $ket_b => $b_mail){
                        $body = str_replace('#' . $ket_b . '#', $b_mail , $body);
                    }
                }

                if(count($theme_mail) > 0){
                    foreach ($theme_mail as $ket_th => $th_mail){
                        $theme = str_replace('#'  . $ket_th . '#' , $th_mail , $theme);
                    }
                }

                if($body != ''){
                    $default_email [] = sfConfig::get('app_email_sender');
                    if($mail_template->getCopy() != ''){
                        $copy_email = explode(',', str_replace(' ', '', $mail_template->getCopy()));
                        $emails_array = array_merge($default_email, $copy_email);
                    } else {
                        $emails_array = $default_email;
                    }

                    $context = sfContext::hasInstance() ? sfContext::getInstance() : sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('www', 'prod', false));
                    $mailer = $context->getMailer();
                    $message = Swift_Message::newInstance()
                        ->setFrom(sfConfig::get('app_email_sender'));
                    if($recipient != false){
                        $message->setTo($recipient);
                    } else {
                        $message->setTo($emails_array);
                    }
                    $message->setSubject($theme);

                    $message->setContentType('text/html; charset=UTF-8');
                    $message->setBody($body);
                    //$this->getMailer()->send($message);
//                    if($attach)
//                    {
//                        $message->attach(Swift_Attachment::fromPath($attach));
//                    }
                    try{
                        if(!$mailer->send($message)){
                            throw new Exception();
                        }
                    }
                    catch (Exception $ex) {
                        file_put_contents(sfConfig::get('sf_log_dir').'/mailError.txt', date("Y-m-d H:i:s") .' '. $ex->getMessage() . ' ' . $ex->getFile() . ' ' . $ex->getLine());
                    }
                }
            }
        }

    }
}
