kohana alerts

 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
<?php defined('SYSPATH') or die('No direct script access.');
/*** views/alerts.php
<?php if ( ! empty($messages)): ?>
<div class="alert alert-<?php echo $type; ?>">
<button type="button" class="close" data-dismiss="alert">&times;</button>
<?php foreach ($messages as $message): ?>
<p><?php echo $message; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
*/
class Alerts {
const SUCCESS = 'success';
const ERROR = 'error';
const INFO = 'info';
public static $view;
public static $alerts = array();
protected static $_prefix = 'messages';
public static function add($type, $message)
{
$session = Session::instance();
$key = self::_key($type);
$messages = $session->get($key, array());
$messages[] = $message;
$session->set($key, $messages);
}
public static function render($type)
{
$messages = Session::instance()->get_once(self::_key($type));
if (self::$view)
{
return View::factory(self::$view)
->set('type', $type)
->set('messages', $messages);
}
else
{
if ( ! empty($messages))
{
$out = '<div class="alert alert-'.$type.'">'
.'<button type="button" class="close" data-dismiss="alert">&times;</button>';
foreach ($messages as $message)
{
$out .= '<p>'.$message.'</p>';
}
}
return $out.'</div>';
}
}
protected static function _key($type)
{
return self::$_prefix.'_'.$type;
}
public static function success($message)
{
self::add(self::SUCCESS, $message);
}
public static function error($message)
{
self::add(self::ERROR, $message);
}
public static function info($message)
{
self::add(self::INFO, $message);
}
public static function render_success()
{
return self::render(self::SUCCESS);
}
public static function render_errors()
{
return self::render(self::ERROR);
}
public static function render_info()
{
return self::render(self::INFO);
}
}