-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathAlert.php
151 lines (116 loc) · 2.46 KB
/
Alert.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
<?php
/**
* Alert Class
*
* @package CodeIgniter
* @subpackage Libraries
* @category Alert / Notification
* @author Twitter: @d_nizh / Facebook: /dna1993
* @link https://github.com/dnizh
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Alert {
private $CI;
private $data;
private $tag_after = '<br/>';
public function __construct()
{
// Copy object CI
$this->CI =& get_instance();
$this->CI->load->library('session');
// Variable untuk alert non flashdata
$this->data['_nr'] = array();
// Variable untuk alert flashdata
$this->data['_rr'] = array();
}
/*
*
* Method untuk set alert
* Return object
*/
public function set($type, $message = '', $is_nr = false)
{
if(!$is_nr){
// Buat alert dengan menggunakan flashdata
$this->data['_rr'][$type][] = $message;
$this->_set_rr($type, $message);
}else{
// Buat alert dengan variable biasa
$this->data['_nr'][$type][] = $message;
}
// Return this agar bisa chainable
return $this;
}
/*
*
* Method untuk set flashdata
*
*/
private function _set_rr($type, $message)
{
foreach($this->data['_rr'] as $type => $val)
{
// Buat flashdata CI
$this->CI->session->set_flashdata($type, $val);
}
}
/*
*
* Method untuk mengumpulkan semua alert
*
*/
private function alert_collection()
{
// Ambil alert dari masing-masing type
$rrs = $this->CI->session->flashdata();
$nrs = $this->data['_nr'];
// Menggabungkan alert
$all_alert = array_merge($rrs, $nrs);
$new_alert = array();
foreach($all_alert as $key => $messages){
$i = 1;
$count_message = count($messages);
$str_message = '';
foreach($messages as $m){
$str_message .= $m;
$str_message .= $i < $count_message ? $this->tag_after : '';
$i++;
}
$new_alert[$key] = $str_message;
}
return $new_alert;
}
/*
*
* Method untuk menampilkan semua alert
* Return array
*/
private function show_all()
{
return $this->alert_collection();
}
/*
*
* Method untuk menampilkan alert berdasarkan type
* Return string
*/
private function show($type)
{
$collection = $this->alert_collection();
if(array_key_exists($type, $collection)){
return $collection[$type];
}
}
/*
*
* Method menampilkan alert untuk public
* Return array/string
*/
public function has_alert($type = '')
{
if($type !== ''){
return $this->show($type);
}
return $this->show_all();
}
}