-
Notifications
You must be signed in to change notification settings - Fork 1
/
Core.php
131 lines (109 loc) · 3.31 KB
/
Core.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace helpers\EmailManager;
use phptoolcase\HandyMan as HandyMan;
use phptoolcase\View as View;
use helpers\Translator\Core as Translator;
class Core
{
const BASE_PATH = '/views/email-templates';
public function __construct( $folder , $translator = null )
{
$path = HandyMan::getAppPaths( 'app' ) . static::BASE_PATH . $folder;
$this->_basePath = realpath( $path );
if ( !$this->_basePath )
{
trigger_error( 'EmailManager helper could not find folder ' . $path .
' for email templates!' , E_USER_ERROR );
}
if ( $translator && !$this->_xml( $translator ) ){ return false; }
}
public function data( $data )
{
$this->_data = $data;
return $this;
}
public function compile( $template )
{
if ( $this->_translator ){ $this->_data[ '_lang' ] = $this->_translator; }
$tpl = $this->_basePath . '/views/' . $template;
$this->_template = View::make( $tpl , $this->_data )->compile( );
return $this;
}
public function getTemplate( ){ return $this->_template; }
public function getSubject( ){ return $this->_compileSubject( ); }
public function subject( $template , $compileSubject = false )
{
$this->_compileSubject = $compileSubject;
if ( false !== strpos( $template , 'xml:' ) )
{
$this->_subject = $this->_translator->val( str_replace( 'xml:' , '' , $template ) );
return $this;
}
$this->_subject = $template;
return $this;
}
public function from( $address )
{
$this->_from = $address;
return $this;
}
public function send( $address , $type = 'html' )
{
$this->_compileSubject( );
$headers = array( );
$headers .= 'MIME-Version: 1.0' . "\r\n";
if ( 'html' === $type ){ $headers .= "Content-type: text/html\r\n"; }
if ( $this->_from )
{
$headers .= 'From: ' . $this->_from . "\r\n";
$headers .= 'Reply-To: ' . $this->_from . "\r\n";
}
$headers .= 'X-Mailer: PHP/' . phpversion( );
mail( $address , $this->_subject , $this->_template , $headers );
$this->reset( );
}
public function reset( )
{
$this->_data = array( );
$this->_template = null;
$this->_subject = null;
$this->_from = null;
$this->_compileSubject = false;
}
protected function _xml( $file )
{
if ( !class_exists( '\helpers\Translator\Core' ) )
{
trigger_error( 'Cannot use xml language files, ' .
'helper class must be present!' , E_USER_ERROR );
return false;
}
$path = $this->_basePath . '/languages/';
if ( is_array( $file ) )
{
$this->_translator = new Translator( $path . $file[ 0 ] , $path . $file[ 1 ] );
}
else{ $this->_translator = new Translator( $path . $file ); }
if ( !$this->_translator ){ return false; }
}
protected function _compileSubject( )
{
if ( $this->_compileSubject && !empty( $this->_data ) )
{
foreach ( $this->_data as $key => $val )
{
if ( '_lang' === $key || !is_string( $val ) ){ continue; }
$this->_subject = str_replace( '{' . $key . '}' , $val , $this->_subject );
}
}
return $this->_subject;
}
protected $_compileSubject = false;
protected $_from = null;
protected $_data = array( );
protected $_folder = null;
protected $_basePath = null;
protected $_template = null;
protected $_translator = null;
protected $_subject = null;
}