2010-11-29 22:41:08 +00:00
< ? php defined ( 'SYSPATH' ) or die ( 'No direct access allowed.' );
/**
* This class provides email template functions
*
2013-03-19 22:35:19 +00:00
* @ package Email
2010-11-29 22:41:08 +00:00
* @ category Helpers
* @ author Deon George
2013-03-19 22:35:19 +00:00
* @ copyright ( c ) 2009 - 2013 Open Source Billing
* @ license http :// dev . osbill . net / license . html
2010-11-29 22:41:08 +00:00
*/
2011-08-26 02:01:45 +00:00
class Email_Template {
2010-11-29 22:41:08 +00:00
// We'll store the template here
private $template ;
2011-08-27 06:33:46 +00:00
private $etto ;
2010-11-29 22:41:08 +00:00
private $email_data = array ();
2012-07-30 05:10:58 +00:00
// @todo Default lang should be the site setup
private $default_lang = 1 ;
2010-11-29 22:41:08 +00:00
private $components = array ( 'subject' , 'message_text' , 'message_html' );
public function __construct ( $template , $language_id = NULL ) {
2012-11-09 23:13:57 +00:00
$this -> template = ORM :: factory ( 'Email_Template' , array ( 'name' => $template ));
2010-11-29 22:41:08 +00:00
if ( ! $this -> template -> loaded ())
throw new Kohana_Exception ( 'Email template :template not defined in DB' , array ( ':template' => $template ));
if ( is_null ( $language_id ))
2011-09-17 10:45:08 +00:00
$language_id = $this -> default_lang ;
2010-11-29 22:41:08 +00:00
2011-08-27 06:33:46 +00:00
$this -> etto = $this -> template -> email_template_translate -> where ( 'language_id' , '=' , $language_id ) -> find ();
2011-09-26 10:12:54 +00:00
if ( ! $this -> etto -> loaded ())
$this -> etto = $this -> template -> email_template_translate -> where ( 'language_id' , '=' , $this -> default_lang ) -> find ();
2010-11-29 22:41:08 +00:00
2011-09-26 10:12:54 +00:00
// @todo Change this to log/email the admin
return ;
#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));
2010-11-29 22:41:08 +00:00
}
public function __set ( $key , $value ) {
switch ( $key ) {
2011-09-26 10:12:54 +00:00
case 'bcc' :
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 ) {
2011-09-26 10:12:54 +00:00
case 'bcc' :
2011-08-16 02:27:19 +00:00
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 ) {
2012-11-09 23:13:57 +00:00
$ao = ORM :: factory ( 'Account' , $id );
2011-08-16 02:27:19 +00:00
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 ) {
2011-08-26 02:01:45 +00:00
return new Email_Template ( $template );
2010-11-29 22:41:08 +00:00
}
public function variables () {
2013-04-05 12:50:08 +00:00
$result = array ();
2011-08-16 02:27:19 +00:00
2010-11-29 22:41:08 +00:00
foreach ( $this -> components as $v )
2011-08-27 06:33:46 +00:00
foreach ( $this -> etto -> variables ( $v ) as $x => $y )
2013-04-05 12:50:08 +00:00
if ( ! in_array ( $y , $result ))
array_push ( $result , $y );
2010-11-29 22:41:08 +00:00
2013-04-05 12:50:08 +00:00
return $result ;
2010-11-29 22:41:08 +00:00
}
2011-08-27 06:33:46 +00:00
public function send ( array $admin = array ()) {
2010-11-29 22:41:08 +00:00
$e = Email :: connect ();
2011-08-25 23:21:39 +00:00
$sm = Swift_Message :: newInstance ()
2012-11-09 23:13:57 +00:00
-> setFrom ( Kohana :: $config -> load ( 'config' ) -> email_from );
2010-11-29 22:41:08 +00:00
foreach ( $this -> components as $component ) {
2011-09-17 10:45:08 +00:00
if ( $this -> etto -> loaded ()) {
$s = $this -> etto -> resolve ( $this -> email_data [ 'variables' ], $component );
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__ ));
}
} else {
2013-02-12 11:14:59 +00:00
$sm -> setSubject ( _ ( 'Email from' ) . ' ' . Company :: instance () -> name ());
2011-09-17 10:45:08 +00:00
$sm -> setBody ( print_r ( $this -> email_data [ 'variables' ], TRUE ), 'text/plain' );
2010-11-29 22:41:08 +00:00
}
}
2011-09-26 10:12:54 +00:00
if ( isset ( $this -> email_data [ 'bcc' ]))
$sm -> setBcc ( $this -> bcc );
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.
2012-11-09 23:13:57 +00:00
$elo = ORM :: factory ( 'Email_Log' );
2011-08-16 02:27:19 +00:00
foreach ( $sa as $id ) {
$elo -> clear ();
$elo -> account_id = $id ;
2011-09-26 10:12:54 +00:00
$elo -> email = implode ( ',' , array_keys ( $this -> to ));
2011-08-27 06:33:46 +00:00
$elo -> email_template_translate_id = $this -> etto -> id ;
2012-01-01 09:41:42 +00:00
$elo -> data = $this -> email_data [ 'variables' ];
2011-08-16 02:27:19 +00:00
$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
}
}
?>