2010-11-29 22:41:08 +00:00
< ? php defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
* This class provides email template functions
*
* @ package OSB
* @ subpackage EmailTemplate
* @ category Helpers
* @ author Deon George
* @ copyright ( c ) 2010 Deon George
* @ license http :// dev . leenooks . net / license . html
*/
class EmailTemplate {
// We'll store the template here
private $template ;
private $template_mail ;
private $email_data = array ();
private $default_lang = 'en' ;
private $components = array ( 'subject' , 'message_text' , 'message_html' );
public function __construct ( $template , $language_id = NULL ) {
$this -> template = ORM :: factory ( 'emailtemplate' , array ( 'name' => $template ));
if ( ! $this -> template -> loaded ())
throw new Kohana_Exception ( 'Email template :template not defined in DB' , array ( ':template' => $template ));
if ( is_null ( $language_id ))
$language_id = $this -> default_lang ;
$this -> template_mail = $this -> template -> emailtemplate_translate -> where ( 'language_id' , '=' , $language_id ) -> find ();
if ( ! $this -> template_mail -> loaded () AND
( $this -> template_mail = $this -> template -> emailtemplate_translate -> where ( 'language_id' , '=' , $this -> default_lang ) -> find ()) AND ! $this -> template_mail -> loaded ())
// @todo Change this to log/email the admin
throw new Kohana_Exception ( 'No template (:template) found for user language (:language_id) or default language (:default_lang)' ,
array ( ':template' => $this -> template -> name , ':language_id' => $language_id , ':default_lang' => $this -> default_lang ));
}
public function __set ( $key , $value ) {
switch ( $key ) {
2011-08-16 02:27:19 +00:00
case 'to' :
if ( ! is_array ( $value ) OR ! array_intersect ( array ( 'email' , 'account' ), array_keys ( $value )))
throw new Kohana_Exception ( 'Values for to should be an array of either "mail" or "account", however :value was given' , array ( ':value' => serialize ( $value )));
$this -> email_data [ $key ] = $value ;
break ;
2010-11-29 22:41:08 +00:00
case 'variables' :
// Our variables should be an array
if ( ! is_array ( $value ))
throw new Kohana_Exception ( 'Values for variables should be an array, however :value was given' , array ( ':value' => $value ));
$this -> email_data [ $key ] = $value ;
break ;
default :
throw new Kohana_Exception ( 'Unknown variable :key (:value)' , array ( ':key' => $key , ':value' => is_string ( $value ) ? $value : serialize ( $value )));
}
}
2011-08-16 02:27:19 +00:00
public function __get ( $key ) {
switch ( $key ) {
case 'to' :
if ( empty ( $this -> email_data [ $key ]))
return array ();
elseif ( isset ( $this -> email_data [ $key ][ 'email' ]))
return $this -> email_data [ $key ][ 'email' ];
elseif ( isset ( $this -> email_data [ $key ][ 'account' ])) {
$list = array ();
foreach ( $this -> email_data [ $key ][ 'account' ] as $id ) {
$ao = ORM :: factory ( 'account' , $id );
if ( $ao -> loaded ())
$list [ $ao -> email ] = $ao -> name ();
}
return $list ;
}
break ;
case 'variables' :
return $this -> email_data [ $key ];
default :
throw new Kohana_Exception ( 'Unknown variable :key (:value)' , array ( ':key' => $key , ':value' => is_string ( $value ) ? $value : serialize ( $value )));
}
}
2010-11-29 22:41:08 +00:00
public static function instance ( $template ) {
return new EmailTemplate ( $template );
}
public function variables () {
$return = array ();
2011-08-16 02:27:19 +00:00
2010-11-29 22:41:08 +00:00
foreach ( $this -> components as $v )
foreach ( $this -> template_mail -> variables ( $v ) as $x => $y )
if ( ! in_array ( $y , $return ))
array_push ( $return , $y );
return $return ;
}
public function send ( $admin = FALSE ) {
$e = Email :: connect ();
$sm = Swift_Message :: newInstance ();
foreach ( $this -> components as $component ) {
$s = $this -> template_mail -> $component ;
foreach ( $this -> template_mail -> variables ( $component ) as $k => $v )
$s = str_replace ( '%' . $v . '%' , $this -> email_data [ 'variables' ][ $v ], $s );
switch ( $component ) {
case 'message_html' :
$sm -> setBody ( $s , 'text/html' );
break ;
case 'message_text' :
$sm -> setBody ( $s , 'text/plain' );
break ;
case 'subject' :
$sm -> setSubject ( $s );
break ;
default :
throw new Kohana_Exception ( 'Component :component has not been configured in :method' , array ( ':component' => $component , ':method' => __METHOD__ ));
}
}
2011-08-16 02:27:19 +00:00
2010-11-29 22:41:08 +00:00
// @todo This should go to the admin defined in email_setup
2011-08-16 02:27:19 +00:00
if ( $admin OR ( $admin = Config :: testmail ( $this -> template -> name ))) {
$sm -> setTo ( $admin );
$sa = array ( 1 );
} else {
$sm -> setTo ( $this -> to );
$sa = $this -> to_accounts ();
}
2010-11-29 22:41:08 +00:00
// @todo - Setup queue mode
2011-08-16 02:27:19 +00:00
$result = $e -> send ( $sm );
if ( $result ) {
// Store our email log.
$data = gzcompress ( serialize ( $this -> email_data [ 'variables' ]));
$elo = ORM :: factory ( 'email_log' );
foreach ( $sa as $id ) {
$elo -> clear ();
$elo -> account_id = $id ;
$elo -> email_template_id = $this -> template_mail -> id ;
$elo -> data = $data ;
$elo -> save ();
}
}
return $result ;
}
private function to_accounts () {
// @todo Set the default account in a configuration file.
$default = array ( 1 );
if ( ! isset ( $this -> email_data [ 'to' ]) OR ! is_array ( $this -> email_data [ 'to' ]) OR ! array_intersect ( array ( 'email' , 'account' ), array_keys ( $this -> email_data [ 'to' ])))
return $default ;
return isset ( $this -> email_data [ 'to' ][ 'account' ]) ? $this -> email_data [ 'to' ][ 'account' ] : $default ;
2010-11-29 22:41:08 +00:00
}
}
?>