2014-02-05 14:03:04 +02:00
< ? php defined ( 'SYSPATH' ) OR die ( 'No direct access allowed.' );
/**
* Electronic instant letter model . This is a strict one - time letter .
* Stores HTML to be placed in e - mail template .
* @ package Models
* @ author Oreolek
**/
class Model_Instant extends ORM {
protected $belongs_to = array (
'subscription'
);
2016-10-05 06:44:39 +03:00
2014-02-05 14:03:04 +02:00
/**
* @ return array validation rules
**/
public function rules ()
2016-10-05 06:44:39 +03:00
{
return array (
2014-02-05 14:03:04 +02:00
'subject' => array (
2016-10-05 06:44:39 +03:00
array ( 'not_empty' ),
array ( 'max_length' , array ( ':value' , 128 )),
2014-02-05 14:03:04 +02:00
),
'text' => array (
2016-10-05 06:44:39 +03:00
array ( 'not_empty' ),
array ( 'min_length' , array ( ':value' , 20 )),
2014-02-05 14:03:04 +02:00
),
2016-10-05 06:44:39 +03:00
);
}
2014-02-05 14:03:04 +02:00
/**
* Array of field labels .
* Used in forms .
**/
protected $_labels = array (
'text' => 'Message text' ,
'subject' => 'Message subject' ,
2014-02-18 10:11:50 +02:00
'sent' => 'Is sent?'
2014-02-05 14:03:04 +02:00
);
/**
* Function to send a email to a specified address .
* Not suitable for a large - scale use .
* @ param email $address email address
**/
public function send ( $address , $token = '' )
{
2014-02-18 10:11:50 +02:00
$this -> sent = TRUE ;
2014-02-05 14:03:04 +02:00
return self :: _send ( $address , $this -> text , $this -> subject , $token );
}
2016-10-05 06:44:39 +03:00
2014-02-05 14:03:04 +02:00
/**
* @ param $address string or array of strings - email addresses
* @ param $text message body
* @ param $subject message subject
* @ param $token user course token
**/
public static function _send ( $address , $text , $subject , $token = '' )
{
2016-10-11 08:37:54 +03:00
Log :: instance () -> add ( Log :: NOTICE , I18n :: translate ( 'Sending letter with subject "%subject" to address %address' , array ( '%subject' => $subject , '%address' => $address )));
2014-02-05 14:03:04 +02:00
$sender = Kohana :: $config -> load ( 'email' ) -> get ( 'sender' );
2014-02-21 08:57:10 +02:00
$template = new View_Letter_View ;
$template -> content = $text ;
$template -> token = $token ;
$renderer = Kostache_Layout :: factory ( $template -> _layout );
2014-03-02 11:35:30 +02:00
$email = Email :: factory ( $subject , $renderer -> render ( $template , $template -> _view ), 'text/html' ) -> from ( $sender [ 0 ], $sender [ 1 ]);
2014-02-05 14:03:04 +02:00
if ( is_array ( $address ))
{
$email -> bcc ( $address );
}
else
{
$email -> to ( $address );
}
return $email -> send ();
}
}