| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace backend\widgets;
- use Yii;
- use common\helpers\StringHelper;
- /**
- * 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>
- */
- class Alert extends \yii\bootstrap4\Widget
- {
- /**
- * @var array the alert types configuration for the flash messages.
- * This array is setup as $key => $value, where:
- * - key: the name of the session flash variable
- * - value: 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.
- * Array will be passed to [[\yii\bootstrap\Alert::closeButton]].
- */
- public $closeButton = [];
- /**
- * {@inheritdoc}
- */
- public function run()
- {
- $session = Yii::$app->session;
- $flashes = $session->getAllFlashes();
- $appendClass = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
- foreach ($flashes as $type => $flash) {
- if (!isset($this->alertTypes[$type])) {
- continue;
- }
- $data = (array) $flash;
- foreach ($data as $i => $message) {
- /* initialize css class for each alert box */
- $this->options['class'] = $this->alertTypes[$type] . $appendClass;
- /* assign unique id to each alert box */
- $this->options['id'] = $this->getId() . '-' . $type . '-' . $i;
- $message = StringHelper::replace("'", '"', $message);
- $message = StringHelper::replace("\r", '', $message);
- $message = StringHelper::replace("\n", '', $message);
- // 调用函数
- $this->view->registerJs(<<<Js
- toastr.{$type}('{$message}')
- Js
- );
- }
- $session->removeFlash($type);
- }
- }
- }
|