Alert::run()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 20
rs 9.8333
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view the LICENSE.md
5
 * file that was distributed with this source code.
6
 *
7
 *        @link: https://github.com/terabytesoft/app-basic
8
 *      @author: Wilmer Arámbula <[email protected]>
9
 *   @copyright: (c) TERABYTE SOFTWARE SA
10
 *      @widget: [Alert]
11
 *       @since: 0.0.1
12
 *         @yii: 3.0
13
 **/
14
15
namespace app\widgets;
16
17
/**
18
 * Alert widget renders a message from session flash. All flash messages are displayed
19
 * in the sequence they were assigned using setFlash. You can set message as following:.
20
 *
21
 * @author Kartik Visweswaran <[email protected]>
22
 * @author Alexander Makarov <[email protected]>
23
 */
24
class Alert extends \yii\bootstrap4\Widget
25
{
26
	/**
27
	 * @var array the alert types configuration for the flash messages.
28
	 * This array is setup as $key => $value, where:
29
	 * - $key is the name of the session flash variable
30
	 * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
31
	 **/
32
	public $alertTypes = [
33
		'danger' => 'alert-danger',
34
		'dark' => 'alert-dark',
35
		'info' => 'alert-info',
36
		'light' => 'alert-light',
37
		'primary' => 'alert-primary',
38
		'secondary' => 'alert-secondary',
39
		'success' => 'alert-success',
40
		'warning' => 'alert-warning',
41
	];
42
43
	/**
44
	 * @var array the options for rendering the close button tag.
45
	 **/
46
	public $closeButton = [];
47
48
	public function run()
49
	{
50
		$session = $this->app->getSession();
51
		$flashes = $session->getAllFlashes();
52
53
		foreach ($flashes as $type => $data) {
54
			if (isset($this->alertTypes[$type])) {
55
				$data = (array) $data;
56
				foreach ($data as $message) {
57
					/* initialize css class for each alert box */
58
					$this->options['class'] = $this->alertTypes[$type];
59
					/* assign unique id to each alert box */
60
					$this->options['id'] = $this->getId() . '-' . $type;
61
					echo \yii\bootstrap4\Alert::widget([
62
						'body' => $message,
63
						'closeButton' => $this->closeButton,
64
						'options' => $this->options,
65
					]);
66
				}
67
				$session->removeFlash($type);
68
			}
69
		}
70
	}
71
}
72