forked from thelia/thelia
/
Message.php
177 lines (150 loc) · 5.33 KB
/
Message.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Thelia\Model;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Core\Event\Message\MessageEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\Template\Exception\ResourceNotFoundException;
use Thelia\Core\Template\ParserInterface;
use Thelia\Model\Base\Message as BaseMessage;
class Message extends BaseMessage
{
use \Thelia\Model\Tools\ModelEventDispatcherTrait;
/**
* {@inheritDoc}
*/
public function preInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_CREATEMESSAGE, new MessageEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postInsert(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_CREATEMESSAGE, new MessageEvent($this));
}
/**
* {@inheritDoc}
*/
public function preUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_UPDATEMESSAGE, new MessageEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postUpdate(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_UPDATEMESSAGE, new MessageEvent($this));
}
/**
* {@inheritDoc}
*/
public function preDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::BEFORE_DELETEMESSAGE, new MessageEvent($this));
return true;
}
/**
* {@inheritDoc}
*/
public function postDelete(ConnectionInterface $con = null)
{
$this->dispatchEvent(TheliaEvents::AFTER_DELETEMESSAGE, new MessageEvent($this));
}
/**
* Calculate the message body, given the HTML entered in the back-office, the message layout, and the message template
* @param ParserInterface $parser
* @param $message
* @param $layout
* @param $template
* @return bool
*/
protected function getMessageBody($parser, $message, $layout, $template, $compressOutput = true)
{
$body = false;
// Try to get the body from template file, if a file is defined
if (! empty($template)) {
try {
$body = $parser->render($template, [], $compressOutput);
} catch (ResourceNotFoundException $ex) {
// Ignore this.
}
}
// We did not get it ? Use the message entered in the back-office
if ($body === false) {
$body = $parser->renderString($message, [], $compressOutput);
}
// Do we have a layout ?
if (! empty($layout)) {
// Populate the message body variable
$parser->assign('message_body', $body);
// Render the layout file
$body = $parser->render($layout, [], $compressOutput);
}
return $body;
}
/**
* Get the HTML message body
*/
public function getHtmlMessageBody(ParserInterface $parser)
{
return $this->getMessageBody(
$parser,
$this->getHtmlMessage(),
$this->getHtmlLayoutFileName(),
$this->getHtmlTemplateFileName()
);
}
/**
* Get the TEXT message body
*/
public function getTextMessageBody(ParserInterface $parser)
{
return $this->getMessageBody(
$parser,
$this->getTextMessage(),
$this->getTextLayoutFileName(),
$this->getTextTemplateFileName(),
true // Do not compress the output, and keep empty lines.
);
}
/**
* Add a subject and a body (TEXT, HTML or both, depending on the message
* configuration.
*
* @param ParserInterface $parser
* @param \Swift_Message $messageInstance
* @param bool $useFallbackTemplate When we send mail from a module and don't use the `default` email
* template, if the file (html/txt) is not found in the template then
* the template file located in the module under
* `templates/email/default/' directory is used if
* `$useFallbackTemplate` is set to `true`.
*/
public function buildMessage(ParserInterface $parser, \Swift_Message $messageInstance, $useFallbackTemplate = true)
{
$parser->setTemplateDefinition(
$parser->getTemplateHelper()->getActiveMailTemplate(),
$useFallbackTemplate
);
$subject = $parser->fetch(sprintf("string:%s", $this->getSubject()));
$htmlMessage = $this->getHtmlMessageBody($parser);
$textMessage = $this->getTextMessageBody($parser);
$messageInstance->setSubject($subject);
// If we do not have an HTML message
if (empty($htmlMessage)) {
// Message body is the text message
$messageInstance->setBody($textMessage, 'text/plain');
} else {
// The main body is the HTML messahe
$messageInstance->setBody($htmlMessage, 'text/html');
// Use the text as a message part, if we have one.
if (! empty($textMessage)) {
$messageInstance->addPart($textMessage, 'text/plain');
}
}
return $messageInstance;
}
}