Instantly share code, notes, and snippets.
Created
August 9, 2013 11:33
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save phcostabh/6192956 to your computer and use it in GitHub Desktop.
Implementation of flash messages using cookies instead of session.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Adm\Library; | |
/** | |
* | |
*/ | |
class CookieFlashMessage implements \Phalcon\FlashInterface | |
{ | |
/** | |
* Chave do cookie. | |
*/ | |
const COOKIE_KEY = "cookie_flash_message"; | |
/** | |
* Path do cookie. | |
*/ | |
const PATH = "/adm"; | |
/** | |
* Classes das divs de mensagens que serâo geradas. | |
*/ | |
private $_classes = array(); | |
/** | |
* Armazena as mensagens registradas. | |
*/ | |
protected static $_messages = array(); | |
/** | |
* Template HTML para exibição das mensagens. | |
* | |
* @var bool | |
*/ | |
private $_template = '<div class=":classes:">:message:</div>'; | |
/** | |
* __construct | |
* | |
* @param array $classes | |
* @access public | |
* @return void | |
*/ | |
public function __construct(array $classes = array()) | |
{ | |
$this->_classes = $classes; | |
} | |
/** | |
* Registra uma mensagem de erro. | |
* | |
* @param string $message | |
* @access public | |
* @return void | |
*/ | |
public function error($message) | |
{ | |
$this->message("error", $message); | |
} | |
/** | |
* Registra uma mensagem de sucesso. | |
* | |
* @param string $message | |
* @access public | |
* @return void | |
*/ | |
public function notice($message) | |
{ | |
$this->message("notice", $message); | |
} | |
/** | |
* Registra uma mensagem de sucesso. | |
* | |
* @param string $message | |
* @access public | |
* @return void | |
*/ | |
public function success($message) | |
{ | |
$this->message("success", $message); | |
} | |
/** | |
* Registra uma mensagem de warning. | |
* | |
* @param string $message | |
* @access public | |
* @return void | |
*/ | |
public function warning($message) | |
{ | |
$this->message("warning", $message); | |
} | |
/** | |
* Registra uma mensagem para ser exibida. | |
* | |
* @param string $type | |
* @param string $message | |
* @access public | |
* @return void | |
*/ | |
public function message($type, $message) | |
{ | |
static::$_messages[$type][] = $message; | |
$index = count(static::$_messages[$type]) - 1; | |
setcookie(static::COOKIE_KEY . "[$type][$index]", $message, time() + 30, static::PATH); | |
} | |
/** | |
* Exibe a mensagem em HTML. | |
* | |
* @param string $type | |
* @param bool $remove | |
* @access public | |
* @return void | |
*/ | |
public function outputMessage($type, $remove = true) | |
{ | |
$messages = $this->getMessages($type, $remove); | |
if ($messages !== false) { | |
$top = 20; | |
foreach ($messages as $message) { | |
$this->toHTML($type, $message); | |
$top += 50; | |
} | |
} | |
} | |
/** | |
* Retorna todas as mensagens de um tipo. | |
* | |
* @param string $type | |
* @param bool $remove | |
* @access public | |
* @return void | |
*/ | |
public function getMessages($type, $remove = true) | |
{ | |
if (!isset($_COOKIE[static::COOKIE_KEY])) { | |
return false; | |
} | |
$messages = $_COOKIE[static::COOKIE_KEY]; | |
$messages = isset($messages[$type]) ? $messages[$type] : false; | |
if ($remove) { | |
for ($i = 0; $i < count($messages); $i++) { | |
setcookie(static::COOKIE_KEY . "[$type][$i]", "", time() - 3600, static::PATH); | |
} | |
} | |
return $messages; | |
} | |
/** | |
* Printa a mensagem em HTML. | |
* | |
* @param string $type | |
* @param string $message | |
* @access public | |
* @return void | |
*/ | |
public function toHTML($type, $message) | |
{ | |
$search = array( | |
':classes:', | |
':message:' | |
); | |
$classes = isset($this->_classes[$type]) ? $this->_classes[$type] : $type; | |
$replace = array( | |
$classes, | |
$message | |
); | |
echo str_replace($search, $replace, $this->_template); | |
} | |
/** | |
* Define o template a ser utilizado para exibição da mensagem. | |
* | |
* @param mixed $template | |
* @return void | |
*/ | |
public function setTemplate($template) | |
{ | |
$this->_template = $template; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment