|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
* @author [email protected] |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Core\Mail; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\Mail\Header; |
|
14
|
|
|
use Zend\Mail\Headers; |
|
15
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
16
|
|
|
use Zend\View\Model\ViewModel; |
|
17
|
|
|
use Zend\Stdlib\Response; |
|
18
|
|
|
use Zend\View\Variables as ViewVariables; |
|
19
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class HTMLTemplateMessage. |
|
23
|
|
|
* uses methods alike ViewModel |
|
24
|
|
|
* @package Core\Mail |
|
25
|
|
|
*/ |
|
26
|
|
|
class HTMLTemplateMessage extends TranslatorAwareMessage |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ServiceLocatorInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $serviceManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* View variables |
|
35
|
|
|
* @var array|\ArrayAccess|\Traversable |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $variables = array(); |
|
38
|
|
|
|
|
39
|
|
|
protected $renderedBody; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param ServiceLocatorInterface $serviceManager |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(ServiceLocatorInterface $serviceManager, array $options = array()) |
|
46
|
|
|
{ |
|
47
|
|
|
// @TODO make this multipart |
|
48
|
|
|
parent::__construct($options); |
|
49
|
|
|
$this->serviceManager = $serviceManager; |
|
50
|
|
|
$this->getHeaders()->addHeader(Header\ContentType::fromString('Content-Type: text/html; charset=UTF-8')); |
|
51
|
|
|
$this->setEncoding('UTF-8'); |
|
52
|
|
|
$this->variables = new ViewVariables(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Property overloading: set variable value |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __set($name, $value) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->setVariable($name, $value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Property overloading: get variable value |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function __get($name) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->__isset($name)) { |
|
76
|
|
|
return null; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$variables = $this->getVariables(); |
|
80
|
|
|
return $variables[$name]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Property overloading: do we have the requested variable value? |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $name |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function __isset($name) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
$variables = $this->getVariables(); |
|
92
|
|
|
return isset($variables[$name]); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Property overloading: unset the requested variable |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name |
|
99
|
|
|
* @return void |
|
100
|
|
|
*/ |
|
101
|
|
|
public function __unset($name) |
|
102
|
|
|
{ |
|
103
|
|
|
if ($this->__isset($name)) { |
|
104
|
|
|
unset($this->variables[$name]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get a single view variable |
|
110
|
|
|
* |
|
111
|
|
|
* @param string $name |
|
112
|
|
|
* @param mixed|null $default (optional) default value if the variable is not present. |
|
113
|
|
|
* @return mixed |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getVariable($name, $default = null) |
|
116
|
|
|
{ |
|
117
|
|
|
$name = (string) $name; |
|
118
|
|
|
if (array_key_exists($name, $this->variables)) { |
|
119
|
|
|
return $this->variables[$name]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $default; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set view variable |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $name |
|
129
|
|
|
* @param mixed $value |
|
130
|
|
|
* @return ViewModel |
|
|
|
|
|
|
131
|
|
|
*/ |
|
132
|
|
|
public function setVariable($name, $value) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->variables[(string) $name] = $value; |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set view variables en masse |
|
140
|
|
|
* |
|
141
|
|
|
* Can be an array or a Traversable + ArrayAccess object. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array|\ArrayAccess|\Traversable $variables |
|
144
|
|
|
* @param bool $overwrite Whether or not to overwrite the internal container with $variables |
|
145
|
|
|
* @throws \InvalidArgumentException |
|
146
|
|
|
* @return self |
|
147
|
|
|
*/ |
|
148
|
|
|
public function setVariables($variables, $overwrite = false) |
|
149
|
|
|
{ |
|
150
|
|
View Code Duplication |
if (!is_array($variables) && !$variables instanceof \Traversable) { |
|
|
|
|
|
|
151
|
|
|
throw new \InvalidArgumentException( |
|
152
|
|
|
sprintf( |
|
153
|
|
|
'%s: expects an array, or Traversable argument; received "%s"', |
|
154
|
|
|
__METHOD__, |
|
155
|
|
|
(is_object($variables) ? get_class($variables) : gettype($variables)) |
|
156
|
|
|
) |
|
157
|
|
|
); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if ($overwrite) { |
|
161
|
|
|
if (is_object($variables) && !$variables instanceof \ArrayAccess) { |
|
162
|
|
|
$variables = ArrayUtils::iteratorToArray($variables); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$this->variables = $variables; |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
foreach ($variables as $key => $value) { |
|
170
|
|
|
$this->setVariable($key, $value); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $this; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Get view variables |
|
178
|
|
|
* |
|
179
|
|
|
* @return array|\ArrayAccess|\Traversable |
|
180
|
|
|
*/ |
|
181
|
|
|
public function getVariables() |
|
182
|
|
|
{ |
|
183
|
|
|
return $this->variables; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Clear all variables |
|
188
|
|
|
* |
|
189
|
|
|
* Resets the internal variable container to an empty container. |
|
190
|
|
|
* |
|
191
|
|
|
* @return ViewModel |
|
|
|
|
|
|
192
|
|
|
*/ |
|
193
|
|
|
public function clearVariables() |
|
194
|
|
|
{ |
|
195
|
|
|
$this->variables = new ViewVariables(); |
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* |
|
201
|
|
|
* @param $template |
|
202
|
|
|
* |
|
203
|
|
|
* @return self |
|
204
|
|
|
*/ |
|
205
|
|
|
public function setTemplate($template) |
|
206
|
|
|
{ |
|
207
|
|
|
$this->template = $template; |
|
|
|
|
|
|
208
|
|
|
return $this; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function getTemplate() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->template; |
|
|
|
|
|
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @example /module/Jobs/src/Jobs/Listener/PortalListener.php |
|
218
|
|
|
* @return string |
|
|
|
|
|
|
219
|
|
|
* @throws \InvalidArgumentException the mail body must completely be provided by the template, any other attempt is a misconception that may leave the coder in an quagmire |
|
220
|
|
|
*/ |
|
221
|
|
|
public function renderBodyText($force = true) |
|
222
|
|
|
{ |
|
223
|
|
|
if (!$this->renderedBody || $force ) { |
|
224
|
|
|
$viewModel = new ViewModel(); |
|
225
|
|
|
$response = new Response(); |
|
226
|
|
|
$body = parent::getBodyText(); |
|
|
|
|
|
|
227
|
|
|
if (!empty($body)) { |
|
228
|
|
|
throw new \InvalidArgumentException('mail body shall come from Template.'); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/* @var \Zend\Mvc\View\Http\ViewManager $viewManager */ |
|
232
|
|
|
$viewManager = $this->serviceManager->get('viewManager'); |
|
233
|
|
|
$resolver = $this->serviceManager->get('viewResolver'); |
|
234
|
|
|
|
|
235
|
|
|
/* @var \Zend\Mvc\MvcEvent $event */ |
|
236
|
|
|
$event = $this->serviceManager->get('application')->getMvcEvent(); |
|
237
|
|
|
$lang = $event->getRouteMatch()->getParam('lang'); |
|
238
|
|
|
|
|
239
|
|
|
|
|
240
|
|
|
if ($resolver->resolve($this->getTemplate() . '.' . $lang)) { |
|
241
|
|
|
$viewModel->setTemplate($this->getTemplate() . '.' . $lang); |
|
242
|
|
|
}else{ |
|
243
|
|
|
$viewModel->setTemplate($this->getTemplate()); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
$view = $viewManager->getView(); |
|
247
|
|
|
|
|
248
|
|
|
$viewModel->setVariables($this->getVariables()); |
|
249
|
|
|
$viewModel->setVariable('mail', $this); |
|
250
|
|
|
$view->setResponse($response); |
|
251
|
|
|
$view->render($viewModel); |
|
252
|
|
|
$body = $response->getContent(); |
|
253
|
|
|
|
|
254
|
|
|
$this->renderedBody = $body; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
public function getBodyText() |
|
259
|
|
|
{ |
|
260
|
|
|
$this->renderBodyText(); |
|
261
|
|
|
return $this->renderedBody; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param MailService $mailService |
|
266
|
|
|
* @return \Core\Mail\HTMLTemplateMessage |
|
267
|
|
|
*/ |
|
268
|
|
|
public static function factory(MailService $mailService) |
|
269
|
|
|
{ |
|
270
|
|
|
return new static($mailService->getServiceLocator()); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.