├── Alert.php ├── README.md ├── _config.yml └── alert_helper.php /Alert.php: -------------------------------------------------------------------------------- 1 | '; 19 | 20 | public function __construct() 21 | { 22 | // Copy object CI 23 | $this->CI =& get_instance(); 24 | $this->CI->load->library('session'); 25 | 26 | // Variable untuk alert non flashdata 27 | $this->data['_nr'] = array(); 28 | 29 | // Variable untuk alert flashdata 30 | $this->data['_rr'] = array(); 31 | } 32 | 33 | 34 | /* 35 | * 36 | * Method untuk set alert 37 | * Return object 38 | */ 39 | public function set($type, $message = '', $is_nr = false) 40 | { 41 | 42 | if(!$is_nr){ 43 | 44 | // Buat alert dengan menggunakan flashdata 45 | $this->data['_rr'][$type][] = $message; 46 | $this->_set_rr($type, $message); 47 | 48 | }else{ 49 | 50 | // Buat alert dengan variable biasa 51 | $this->data['_nr'][$type][] = $message; 52 | } 53 | 54 | // Return this agar bisa chainable 55 | return $this; 56 | } 57 | 58 | 59 | /* 60 | * 61 | * Method untuk set flashdata 62 | * 63 | */ 64 | private function _set_rr($type, $message) 65 | { 66 | foreach($this->data['_rr'] as $type => $val) 67 | { 68 | // Buat flashdata CI 69 | $this->CI->session->set_flashdata($type, $val); 70 | } 71 | } 72 | 73 | 74 | /* 75 | * 76 | * Method untuk mengumpulkan semua alert 77 | * 78 | */ 79 | private function alert_collection() 80 | { 81 | 82 | // Ambil alert dari masing-masing type 83 | $rrs = $this->CI->session->flashdata(); 84 | $nrs = $this->data['_nr']; 85 | 86 | // Menggabungkan alert 87 | $all_alert = array_merge($rrs, $nrs); 88 | $new_alert = array(); 89 | 90 | foreach($all_alert as $key => $messages){ 91 | 92 | $i = 1; 93 | $count_message = count($messages); 94 | $str_message = ''; 95 | 96 | foreach($messages as $m){ 97 | 98 | $str_message .= $m; 99 | $str_message .= $i < $count_message ? $this->tag_after : ''; 100 | 101 | $i++; 102 | } 103 | 104 | $new_alert[$key] = $str_message; 105 | } 106 | 107 | return $new_alert; 108 | } 109 | 110 | 111 | /* 112 | * 113 | * Method untuk menampilkan semua alert 114 | * Return array 115 | */ 116 | private function show_all() 117 | { 118 | return $this->alert_collection(); 119 | } 120 | 121 | 122 | /* 123 | * 124 | * Method untuk menampilkan alert berdasarkan type 125 | * Return string 126 | */ 127 | private function show($type) 128 | { 129 | $collection = $this->alert_collection(); 130 | 131 | if(array_key_exists($type, $collection)){ 132 | return $collection[$type]; 133 | } 134 | } 135 | 136 | 137 | /* 138 | * 139 | * Method menampilkan alert untuk public 140 | * Return array/string 141 | */ 142 | public function has_alert($type = '') 143 | { 144 | if($type !== ''){ 145 | return $this->show($type); 146 | } 147 | 148 | return $this->show_all(); 149 | } 150 | 151 | } 152 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # codeigniter-alert 2 | An easy flashdata alert for codeigniter 3 | This is just a simple alert.. 4 | 5 | ## How To Use 6 | 7 | ### File 8 | Put the Alert.php in the application/library 9 | Put the alert_helper.php in the application/helper 10 | Load the library automatically.. 11 | 12 | The library is work with the codeigniter 2.2 and 3.0 13 | 14 | ### Set an Alert 15 | * NAME(String): Your alert name/type/class/whatever.. 16 | * MESSAGE(String): Of course this is for the message.. 17 | * NEXT REQUEST(Bool / Default: false): Set TRUE if not for next request.. I think you now about this if you have use a flashdata codeigniter 18 | 19 | `$this->alert->set(NAME, MESSAGE, NEXT REQUEST);` 20 | 21 | ### Display an alert 22 | `echo has_alert(NAME);` 23 | `print_r(has_alert());` 24 | 25 | ## Sample 26 | 27 | ### Controller 28 | 29 | public function index() 30 | { 31 | if(!auth_check('member')){ 32 | 33 | // Alert will show after redirect 34 | $this->alert->set('alert-danger', 'Orang asing dilarang masuk!'); 35 | redirect(base_url('auth/login')); 36 | } 37 | 38 | redirect(base_url()); 39 | } 40 | 41 | public function login() 42 | { 43 | if($this->input->post('do_login') AND !$this->do_login()){ 44 | 45 | // This is not for next request 46 | $this->alert->set('alert-danger', 'Login gagal!', true); 47 | } 48 | 49 | $this->load->view('login'); 50 | } 51 | 52 | ### View 53 | 54 | $message): ?> 56 |