Dominus77 /
yii2-sweetalert2-widget
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace dominus77\sweetalert2; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\bootstrap\Widget; |
||
| 7 | use yii\helpers\Json; |
||
| 8 | use yii\helpers\ArrayHelper; |
||
| 9 | use dominus77\sweetalert2\assets\SweetAlert2Asset; |
||
| 10 | use dominus77\sweetalert2\assets\AnimateCssAsset; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Alert widget renders a message from session flash or custom messages. |
||
| 14 | * @package dominus77\sweetalert2 |
||
| 15 | */ |
||
| 16 | class Alert extends Widget |
||
| 17 | { |
||
| 18 | //modal type |
||
| 19 | const TYPE_INFO = 'info'; |
||
| 20 | const TYPE_ERROR = 'error'; |
||
| 21 | const TYPE_SUCCESS = 'success'; |
||
| 22 | const TYPE_WARNING = 'warning'; |
||
| 23 | const TYPE_QUESTION = 'question'; |
||
| 24 | //input type |
||
| 25 | const INPUT_TYPE_TEXT = 'text'; |
||
| 26 | const INPUT_TYPE_EMAIL = 'email'; |
||
| 27 | const INPUT_TYPE_PASSWORD = 'password'; |
||
| 28 | const INPUT_TYPE_NUMBER = 'number'; |
||
| 29 | const INPUT_TYPE_RANGE = 'range'; |
||
| 30 | const INPUT_TYPE_TEXTAREA = 'textarea'; |
||
| 31 | const INPUT_TYPE_SELECT = 'select'; |
||
| 32 | const INPUT_TYPE_RADIO = 'radio'; |
||
| 33 | const INPUT_TYPE_CHECKBOX = 'checkbox'; |
||
| 34 | const INPUT_TYPE_FILE = 'file'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * All the flash messages stored for the session are displayed and removed from the session |
||
| 38 | * Defaults to false. |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | public $useSessionFlash = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string alert callback |
||
| 45 | */ |
||
| 46 | public $callback = 'function() {}'; |
||
| 47 | |||
| 48 | private $_session; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Initializes the widget |
||
| 52 | */ |
||
| 53 | public function init() |
||
| 54 | { |
||
| 55 | parent::init(); |
||
| 56 | $this->_session = Yii::$app->getSession(); |
||
|
0 ignored issues
–
show
|
|||
| 57 | $this->registerAssets(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @return string|void |
||
| 62 | */ |
||
| 63 | public function run() |
||
| 64 | { |
||
| 65 | if ($this->useSessionFlash) { |
||
| 66 | $flashes = $this->_session->getAllFlashes(); |
||
| 67 | $steps = []; |
||
| 68 | foreach ($flashes as $type => $data) { |
||
| 69 | $data = (array)$data; |
||
| 70 | foreach ($data as $message) { |
||
| 71 | array_push($steps, ['type' => $type, 'text' => $message]); |
||
| 72 | } |
||
| 73 | $this->_session->removeFlash($type); |
||
| 74 | } |
||
| 75 | if (!empty($steps)) { |
||
| 76 | if (!is_array($steps[0]['text'])) { |
||
| 77 | $this->registerSwalQueue($steps); |
||
| 78 | } else { |
||
| 79 | $steps[0]['text']['type'] = isset($steps[0]['text']['type']) ? $steps[0]['text']['type'] : $steps[0]['type']; |
||
| 80 | if (isset($steps[0]['text']['animation']) && $steps[0]['text']['animation'] == false) { |
||
| 81 | if (isset($steps[0]['text']['customClass'])) { |
||
| 82 | $this->registerAnimate(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | $this->options = $steps[0]['text']; |
||
| 86 | $this->callback = isset($steps[1]['text']['callback']) ? $steps[1]['text']['callback'] : $this->callback; |
||
| 87 | $this->registerSwal($this->getOptions(), $this->callback); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } else { |
||
| 91 | $this->registerSwal($this->getOptions(), $this->callback); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get widget options |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public function getOptions() |
||
| 101 | { |
||
| 102 | if (isset($this->options['id'])) |
||
| 103 | unset($this->options['id']); |
||
| 104 | |||
| 105 | if (ArrayHelper::isIndexed($this->options)) { |
||
| 106 | $str = ''; |
||
| 107 | foreach ($this->options as $value) { |
||
| 108 | $str .= '"' . $value . '",'; |
||
| 109 | } |
||
| 110 | return chop($str, ' ,'); |
||
| 111 | } |
||
| 112 | return Json::encode($this->options); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param array $steps |
||
| 117 | */ |
||
| 118 | protected function registerSwalQueue($steps = []) |
||
| 119 | { |
||
| 120 | $view = $this->getView(); |
||
| 121 | $js = "swal.queue(" . Json::encode($steps) . ");"; |
||
| 122 | $this->view->registerJs($js, $view::POS_END); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $options |
||
| 127 | * @param string $callback |
||
| 128 | */ |
||
| 129 | protected function registerSwal($options = '', $callback = '') |
||
| 130 | { |
||
| 131 | $view = $this->getView(); |
||
| 132 | $js = "swal({$options}).then({$callback}).catch(swal.noop);"; |
||
| 133 | $this->view->registerJs($js, $view::POS_END); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Register Animate Assets |
||
| 138 | */ |
||
| 139 | protected function registerAnimate() |
||
| 140 | { |
||
| 141 | AnimateCssAsset::register($this->view); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Register client assets |
||
| 146 | */ |
||
| 147 | protected function registerAssets() |
||
| 148 | { |
||
| 149 | SweetAlert2Asset::register($this->view); |
||
| 150 | if (isset($this->options['animation']) && $this->options['animation'] == false) { |
||
| 151 | if (isset($this->options['customClass'])) { |
||
| 152 | $this->registerAnimate(); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: