Blame view

common/widgets/Alert.php 2.79 KB
d8c1a2e0   Yarik   Big commit artbox
1
  <?php

5c2eb7c8   Yarik   Big commit almost...
2
3
4
5
6
      namespace common\widgets;

      

      use Yii;

      use yii\bootstrap\Widget;

      

d8c1a2e0   Yarik   Big commit artbox
7
      /**

5c2eb7c8   Yarik   Big commit almost...
8
9
10
11
12
13
14
15
16
17
18
19
20
       * Alert widget renders a message from session flash. All flash messages are displayed

       * in the sequence they were assigned using setFlash. You can set message as following:

       * ```php

       * Yii::$app->session->setFlash('error', 'This is the message');

       * Yii::$app->session->setFlash('success', 'This is the message');

       * Yii::$app->session->setFlash('info', 'This is the message');

       * ```

       * Multiple messages could be set as follows:

       * ```php

       * Yii::$app->session->setFlash('error', ['Error 1', 'Error 2']);

       * ```

       * @author Kartik Visweswaran <kartikv2@gmail.com>

       * @author Alexander Makarov <sam@rmcreative.ru>

d8c1a2e0   Yarik   Big commit artbox
21
       */

5c2eb7c8   Yarik   Big commit almost...
22
      class Alert extends Widget

d8c1a2e0   Yarik   Big commit artbox
23
      {

5c2eb7c8   Yarik   Big commit almost...
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
          

          /**

           * @var array the alert types configuration for the flash messages.

           * This array is setup as $key => $value, where:

           * - $key is the name of the session flash variable

           * - $value is the bootstrap alert type (i.e. danger, success, info, warning)

           */

          public $alertTypes = [

              'error'   => 'alert-danger',

              'danger'  => 'alert-danger',

              'success' => 'alert-success',

              'info'    => 'alert-info',

              'warning' => 'alert-warning',

          ];

          

          /**

           * @var array the options for rendering the close button tag.

           */

          public $closeButton = [];

          

          public function init()

          {

              parent::init();

              

              $session = Yii::$app->session;

              $flashes = $session->getAllFlashes();

              $appendCss = isset( $this->options[ 'class' ] ) ? ' ' . $this->options[ 'class' ] : '';

              

              foreach($flashes as $type => $data) {

                  if(isset( $this->alertTypes[ $type ] )) {

                      $data = (array) $data;

                      foreach($data as $i => $message) {

                          /* initialize css class for each alert box */

                          $this->options[ 'class' ] = $this->alertTypes[ $type ] . $appendCss;

                          

                          /* assign unique id to each alert box */

                          $this->options[ 'id' ] = $this->getId() . '-' . $type . '-' . $i;

                          

                          echo \yii\bootstrap\Alert::widget([

                              'body'        => $message,

                              'closeButton' => $this->closeButton,

                              'options'     => $this->options,

                          ]);

                      }

                      

                      $session->removeFlash($type);

d8c1a2e0   Yarik   Big commit artbox
70
                  }

d8c1a2e0   Yarik   Big commit artbox
71
72
73
              }

          }

      }