forked from rmrevin/yii2-postman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewLetter.php
58 lines (49 loc) · 1.68 KB
/
ViewLetter.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
<?php
/**
* ViewLetter.php
* @author Roman Revin http://phptime.ru
*/
namespace rmrevin\yii\postman;
/**
* Class ViewLetter
* @package rmrevin\yii\postman
*/
class ViewLetter extends Letter
{
/**
* @param string $view view file name or alias
* @param array $data
* @param string $viewPath
* @return static
* @throws \rmrevin\yii\postman\LetterException
*/
public function setBodyFromView($view, $data = [], $viewPath = null)
{
$controller = \Yii::$app->controller;
if (!empty($controller) && empty($viewPath)) {
$viewPath = isset($controller->module) && $controller->module !== null
? $controller->module->getViewPath()
: $controller->getViewPath();
}
if (empty($viewPath)) {
$viewPath = \Yii::$app->basePath . '/views';
}
if (strncmp($view, '@', 1) === 0) {
// example $view = '@app/view/email/letter-text.php'
$path = \Yii::getAlias($view);
} else {
// example $view = 'letter-text';
// expand to '/app/views/email/letter-text.php'
// or '/app/modules/ModuleName/views/email/letter-text.php'
$path = \Yii::getAlias($viewPath) . Component::get()->view_path . DIRECTORY_SEPARATOR . $view . '.php';
}
if (!file_exists($path)) {
throw new LetterException(\Yii::t('app', 'View file «{path}» not found.', ['path' => $path]));
} else {
$data['_code'] = $this->code;
$data['_subject'] = $this->raw_subject;
$this->body = \Yii::$app->getView()->renderFile($path, $data);
}
return $this;
}
}