Alert.php 1008 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace common\widgets;
  3. class Alert extends \yii\bootstrap\Widget
  4. {
  5. /**
  6. * @var array the alert types configuration for the flash messages.
  7. * This array is setup as $key => $value, where:
  8. * - $key is the name of the session flash variable
  9. * - $value is the bootstrap alert type (i.e. danger, success, info, warning)
  10. */
  11. public $alertTypes = [
  12. 'success' => 1,
  13. 'error' => 0
  14. ];
  15. public function init()
  16. {
  17. parent::init();
  18. $session = \Yii::$app->session;
  19. $flashes = $session->getAllFlashes();
  20. foreach ($flashes as $type => $data) {
  21. if (isset($this->alertTypes[$type])) {
  22. $data = (array) $data;
  23. foreach ($data as $i => $message) {
  24. $this->view->registerJs(<<<js
  25. $.modal.{$type}('{$message}');
  26. js
  27. );
  28. }
  29. $session->removeFlash($type);
  30. }
  31. }
  32. }
  33. }