Skip to content

Commit 43e6a58

Browse files
authored
Merge pull request #13530 from niden/T13445-ability-to-change-the-flash-html-template
T13445 ability to change the flash html template
2 parents e865657 + 09798b4 commit 43e6a58

File tree

4 files changed

+213
-115
lines changed

4 files changed

+213
-115
lines changed

CHANGELOG-4.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Added `Phalcon\Mvc\Router\RouteInterface::convert` so that calling `Phalcon\Mvc\Router\Group::add` will return an instance that has `convert` method [#13380](https://github.com/phalcon/cphalcon/issues/13380)
66
- Added `Phalcon\Mvc\ModelInterface::getModelsMetaData` [#13070](https://github.com/phalcon/cphalcon/issues/13402)
77
- Added `paginate()` method as a proxy of `getPaginate`. Added `previous` in the paginator object same as `before`. After 4.0 is released we will deprecate `getPaginate()`, `before` and `total_items` [#13492](https://github.com/phalcon/cphalcon/issues/13492)
8+
- Added ability to set a custom template for the Flash Messenger. [#13445](https://github.com/phalcon/cphalcon/issues/13445)
89

910
## Changed
1011
- By configuring `prefix` and `statsKey` the `Phalcon\Cache\Backend\Redis::queryKeys` no longer returns prefixed keys, now it returns original keys without prefix. [#13456](https://github.com/phalcon/cphalcon/pull/13456)

phalcon/flash.zep

+157-110
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,20 @@ use Phalcon\Di\InjectionAwareInterface;
3535
abstract class Flash implements FlashInterface, InjectionAwareInterface
3636
{
3737

38-
protected _cssClasses;
39-
40-
protected _implicitFlush = true;
38+
protected _autoescape = true;
4139

4240
protected _automaticHtml = true;
4341

44-
protected _escaperService = null;
42+
protected _cssClasses;
4543

46-
protected _autoescape = true;
44+
protected _customTemplate = "";
4745

4846
protected _dependencyInjector = null;
4947

48+
protected _escaperService = null;
49+
50+
protected _implicitFlush = true;
51+
5052
protected _messages;
5153

5254
/**
@@ -65,21 +67,55 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
6567
let this->_cssClasses = cssClasses;
6668
}
6769

70+
/**
71+
* Clears accumulated messages when implicit flush is disabled
72+
*/
73+
public function clear() -> void
74+
{
75+
let this->_messages = [];
76+
}
77+
78+
/**
79+
* Shows a HTML error message
80+
*
81+
*<code>
82+
* $flash->error("This is an error");
83+
*</code>
84+
*/
85+
public function error(var message) -> string
86+
{
87+
return this->{"message"}("error", message);
88+
}
89+
6890
/**
6991
* Returns the autoescape mode in generated html
7092
*/
7193
public function getAutoescape() -> bool
7294
{
73-
return this->_autoescape;
95+
return this->_autoescape;
7496
}
7597

7698
/**
77-
* Set the autoescape mode in generated html
99+
* Returns the custom template set
78100
*/
79-
public function setAutoescape(boolean autoescape) -> <Flash>
101+
public function getCustomTemplate() -> string
80102
{
81-
let this->_autoescape = autoescape;
82-
return this;
103+
return this->_customTemplate;
104+
}
105+
106+
/**
107+
* Returns the internal dependency injector
108+
*/
109+
public function getDI() -> <DiInterface>
110+
{
111+
var di;
112+
let di = this->_dependencyInjector;
113+
114+
if typeof di != "object" {
115+
let di = Di::getDefault();
116+
}
117+
118+
return di;
83119
}
84120

85121
/**
@@ -99,89 +135,79 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
99135

100136
return escaper;
101137
}
102-
103138
/**
104-
* Sets the Escaper Service
139+
* Shows a HTML notice/information message
140+
*
141+
*<code>
142+
* $flash->notice("This is an information");
143+
*</code>
105144
*/
106-
public function setEscaperService(<EscaperInterface> escaperService) -> <Flash>
145+
public function notice(var message) -> string
107146
{
108-
let this->_escaperService = escaperService;
109-
return this;
147+
return this->{"message"}("notice", message);
110148
}
111149

112150
/**
113-
* Sets the dependency injector
151+
* Set the autoescape mode in generated html
114152
*/
115-
public function setDI(<DiInterface> dependencyInjector) -> <Flash>
153+
public function setAutoescape(boolean autoescape) -> <Flash>
116154
{
117-
let this->_dependencyInjector = dependencyInjector;
155+
let this->_autoescape = autoescape;
118156
return this;
119157
}
120158

121159
/**
122-
* Returns the internal dependency injector
160+
* Set if the output must be implicitly formatted with HTML
123161
*/
124-
public function getDI() -> <DiInterface>
162+
public function setAutomaticHtml(boolean automaticHtml) -> <FlashInterface>
125163
{
126-
var di;
127-
let di = this->_dependencyInjector;
128-
129-
if typeof di != "object" {
130-
let di = Di::getDefault();
131-
}
132-
133-
return di;
164+
let this->_automaticHtml = automaticHtml;
165+
return this;
134166
}
135167

136168
/**
137-
* Set whether the output must be implicitly flushed to the output or returned as string
169+
* Set an array with CSS classes to format the messages
138170
*/
139-
public function setImplicitFlush(boolean implicitFlush) -> <FlashInterface>
171+
public function setCssClasses(array! cssClasses) -> <FlashInterface>
140172
{
141-
let this->_implicitFlush = implicitFlush;
173+
let this->_cssClasses = cssClasses;
142174
return this;
143175
}
144176

145177
/**
146-
* Set if the output must be implicitly formatted with HTML
178+
* Set an custom template for showing the messages
147179
*/
148-
public function setAutomaticHtml(boolean automaticHtml) -> <FlashInterface>
180+
public function setCustomTemplate(string! customTemplate) -> <FlashInterface>
149181
{
150-
let this->_automaticHtml = automaticHtml;
182+
let this->_customTemplate = customTemplate;
151183
return this;
152184
}
153185

154186
/**
155-
* Set an array with CSS classes to format the messages
187+
* Sets the dependency injector
156188
*/
157-
public function setCssClasses(array! cssClasses) -> <FlashInterface>
189+
public function setDI(<DiInterface> dependencyInjector) -> <Flash>
158190
{
159-
let this->_cssClasses = cssClasses;
191+
let this->_dependencyInjector = dependencyInjector;
160192
return this;
161193
}
162194

163195
/**
164-
* Shows a HTML error message
165-
*
166-
*<code>
167-
* $flash->error("This is an error");
168-
*</code>
196+
* Sets the Escaper Service
169197
*/
170-
public function error(var message) -> string
198+
public function setEscaperService(<EscaperInterface> escaperService) -> <Flash>
171199
{
172-
return this->{"message"}("error", message);
200+
let this->_escaperService = escaperService;
201+
return this;
173202
}
174203

175204
/**
176-
* Shows a HTML notice/information message
177-
*
178-
*<code>
179-
* $flash->notice("This is an information");
180-
*</code>
205+
* Set whether the output must be implicitly flushed to the output or returned as string
181206
*/
182-
public function notice(var message) -> string
207+
public function setImplicitFlush(boolean implicitFlush) -> <FlashInterface>
183208
{
184-
return this->{"message"}("notice", message);
209+
let this->_implicitFlush = implicitFlush;
210+
return this;
185211
}
186212

187213
/**
@@ -196,18 +222,6 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
196222
return this->{"message"}("success", message);
197223
}
198224

199-
/**
200-
* Shows a HTML warning message
201-
*
202-
*<code>
203-
* $flash->warning("Hey, this is important");
204-
*</code>
205-
*/
206-
public function warning(var message) -> string
207-
{
208-
return this->{"message"}("warning", message);
209-
}
210-
211225
/**
212226
* Outputs a message formatting it with HTML
213227
*
@@ -220,30 +234,9 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
220234
*/
221235
public function outputMessage(string type, var message)
222236
{
223-
boolean automaticHtml, implicitFlush;
224-
var content, cssClasses, classes, typeClasses, eol, msg,
225-
htmlMessage, autoEscape, escaper, preparedMsg;
226-
227-
let automaticHtml = (bool) this->_automaticHtml,
228-
autoEscape = (bool) this->_autoescape;
229-
230-
if automaticHtml === true {
231-
let classes = this->_cssClasses;
232-
if fetch typeClasses, classes[type] {
233-
if typeof typeClasses == "array" {
234-
let cssClasses = " class=\"" . join(" ", typeClasses) . "\"";
235-
} else {
236-
let cssClasses = " class=\"" . typeClasses . "\"";
237-
}
238-
} else {
239-
let cssClasses = "";
240-
}
241-
let eol = PHP_EOL;
242-
}
243-
244-
if autoEscape === true {
245-
let escaper = this->getEscaperService();
246-
}
237+
boolean implicitFlush;
238+
var content, msg,
239+
htmlMessage, preparedMsg;
247240

248241
let implicitFlush = (bool) this->_implicitFlush;
249242
if typeof message == "array" {
@@ -259,20 +252,15 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
259252
* We create the message with implicit flush or other
260253
*/
261254
for msg in message {
262-
if autoEscape === true {
263-
let preparedMsg = escaper->escapeHtml(msg);
264-
} else {
265-
let preparedMsg = msg;
266-
}
255+
/**
256+
* Check if the message needs to be escaped
257+
*/
258+
let preparedMsg = this->prepareEscapedMessage(msg);
267259

268260
/**
269261
* We create the applying formatting or not
270262
*/
271-
if automaticHtml === true {
272-
let htmlMessage = "<div" . cssClasses . ">" . preparedMsg . "</div>" . eol;
273-
} else {
274-
let htmlMessage = preparedMsg;
275-
}
263+
let htmlMessage = this->prepareHtmlMessage(type, preparedMsg);
276264

277265
if implicitFlush === true {
278266
echo htmlMessage;
@@ -290,20 +278,15 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
290278
}
291279

292280
} else {
293-
if autoEscape === true {
294-
let preparedMsg = escaper->escapeHtml(message);
295-
} else {
296-
let preparedMsg = message;
297-
}
281+
/**
282+
* Check if the message needs to be escaped
283+
*/
284+
let preparedMsg = this->prepareEscapedMessage(message);
298285

299286
/**
300287
* We create the applying formatting or not
301288
*/
302-
if automaticHtml === true {
303-
let htmlMessage = "<div" . cssClasses . ">" . preparedMsg . "</div>" . eol;
304-
} else {
305-
let htmlMessage = preparedMsg;
306-
}
289+
let htmlMessage = this->prepareHtmlMessage(type, preparedMsg);
307290

308291
/**
309292
* We return the message as string if the implicit_flush is turned off
@@ -318,10 +301,74 @@ abstract class Flash implements FlashInterface, InjectionAwareInterface
318301
}
319302

320303
/**
321-
* Clears accumulated messages when implicit flush is disabled
304+
* Shows a HTML warning message
305+
*
306+
*<code>
307+
* $flash->warning("Hey, this is important");
308+
*</code>
322309
*/
323-
public function clear() -> void
310+
public function warning(var message) -> string
324311
{
325-
let this->_messages = [];
312+
return this->{"message"}("warning", message);
313+
}
314+
315+
316+
private function getTemplate(string cssClassses) -> string
317+
{
318+
if ("" === this->_customTemplate) {
319+
if ("" === cssClassses) {
320+
return "<div>%message%</div>" . PHP_EOL;
321+
} else {
322+
return "<div class=\"%cssClass%\">%message%</div>" . PHP_EOL;
323+
}
324+
}
325+
326+
return this->_customTemplate;
327+
}
328+
329+
/**
330+
* Returns the message escaped if the autoEscape is true, otherwise the
331+
* original message is returned
332+
*/
333+
private function prepareEscapedMessage(string message) -> string
334+
{
335+
var autoEscape, escaper;
336+
337+
let autoEscape = (bool) this->_autoescape;
338+
339+
if autoEscape === true {
340+
let escaper = this->getEscaperService();
341+
return escaper->escapeHtml(message);
342+
} else {
343+
return message;
344+
}
345+
}
346+
347+
/**
348+
* Prepares the HTML output for the message. If automaticHtml is not set then
349+
* the original message is returned
350+
*/
351+
private function prepareHtmlMessage(string type, string message) -> string
352+
{
353+
var classes, cssClasses, typeClasses, automaticHtml;
354+
355+
let automaticHtml = (bool) this->_automaticHtml;
356+
357+
if automaticHtml === true {
358+
let classes = this->_cssClasses;
359+
if fetch typeClasses, classes[type] {
360+
if typeof typeClasses == "array" {
361+
let cssClasses = join(" ", typeClasses);
362+
} else {
363+
let cssClasses = typeClasses;
364+
}
365+
} else {
366+
let cssClasses = "";
367+
}
368+
369+
return str_replace(["%cssClass%", "%message%"], [cssClasses, message], this->getTemplate(cssClasses));
370+
} else {
371+
return message;
372+
}
326373
}
327374
}

0 commit comments

Comments
 (0)