├── .editorconfig ├── .github ├── CONTRIBUTING.md ├── FUNDING.yml └── SECURITY.md ├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json ├── config └── sweetalert.php ├── functions.php ├── src ├── Console │ └── InstallCommand.php ├── ConvertMessages.php ├── LaravelSessionStore.php ├── Notifier.php ├── SessionStore.php ├── Sweetalert.php └── SweetalertServiceProvider.php └── views └── sweetalert.blade.php /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 4 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.{yml,yaml}] 15 | indent_size = 2 16 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-12 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: waveygroup 2 | -------------------------------------------------------------------------------- /.github/SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you discover any security related issues, please email **security@wavey.group** instead of using the issue tracker. 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Wavey Group Limited 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Sweetalerts 4 | 5 |
6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |

25 |

26 | Sweetalerts is an Laravel implementation of the much loved Sweetalerts 2 package. Show your users some beautiful alerts when actions are performed in your Laravel application with some handy helpers. 27 |

28 | 29 | ## ⚡️ Quickstart 30 | 31 | Install Sweetalert through composer and run our installer. 32 | ```shell 33 | composer require wavey/sweetalert && php artisan sweetalert:install 34 | ``` 35 | 36 | Include the Sweetalert2 javascript and then include our sweetalert 2 components. 37 | ```php 38 | 39 | @include('sweetalert::sweetalert') 40 | ``` 41 | 42 | 43 | ## 📖 Usage 44 | 45 | Before using Sweetalerts, you will need to include the sweetalert facade at the top of your controllers: 46 | ```php 47 | use Wavey\Sweetalert\Sweetalert; 48 | ``` 49 | 50 | Now you should have access to the `Sweetalert` facade inside your controllers. Their is many varients of the alerts you can use: 51 | ```php 52 | Sweetalert::basic('Description', 'Title'); 53 | Sweetalert::info('Description', 'Title'); 54 | Sweetalert::success('Description', 'Title'); 55 | Sweetalert::error('Description', 'Title'); 56 | Sweetalert::warning('Description', 'Title'); 57 | Sweetalert::message('Description', 'Title'); 58 | Sweetalert::message('Description

Custom HTML

', 'Title')->html(); 59 | ``` 60 | 61 | ## ⚠️ Limitations 62 | * Currently this package doesn't work as expected from inside Livewire components, we are working on a fix for this and will be inside a later release. | 63 | 64 | 65 | ## 👍 Contribute 66 | 67 | If you want to say **thank you** and/or support the active development of `Sweetalerts`: 68 | 69 | 1. Add a [GitHub Star](https://github.com/waveygroup/sweetalert/stargazers) to the project. 70 | 2. Tweet about the project on your twitter. 71 | 3. Write a review or tutorial on [Medium](https://medium.com/), [Dev.to](https://dev.to/) or personal blog. 72 | 4. Support the project by pleding through [GitHub](https://github.com/sponsors/waveygroup). 73 | 74 | ## 🛡️ Security Vulnerabilities 75 | 76 | Please review our [security policy](https://github.com/waveygroup/sweetalert/security/policy) on how to report security vulnerabilities. 77 | 78 | ## ⚠️ License 79 | 80 | Copyright (c) 2022-present [Wavey Group](https://github.com/waveygroup) and [Contributors](https://github.com/waveygroup/sweetalert/graphs/contributors). `Sweetalert` is free and open-source software licensed under the [MIT License](https://github.com/waveygroup/sweetalert/blob/master/LICENSE.md). -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wavey/sweetalert", 3 | "description": "SweetAlert2 implementation for Laravel", 4 | "keywords": [ 5 | "sweetalert", 6 | "laravel", 7 | "sweet", 8 | "alert", 9 | "alerts" 10 | ], 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Kieran Heron", 15 | "email": "kieran@wavey.group" 16 | } 17 | ], 18 | "support": { 19 | "issues": "https://github.com/waveygroup/sweetalert/issues", 20 | "source": "https://github.com/waveygroup/sweetalert" 21 | }, 22 | "require": { 23 | "php": "^7.3|^8.0" 24 | }, 25 | "require-dev": { 26 | "mockery/mockery": "^1.0", 27 | "phpunit/phpunit": "^9.3" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Wavey\\Sweetalert\\": "src/" 32 | } 33 | }, 34 | "autoload-dev": { 35 | "psr-4": { 36 | "Wavey\\Sweetalert\\Tests\\": "tests/" 37 | } 38 | }, 39 | "extra": { 40 | "branch-alias": { 41 | "dev-master": "1.x-dev" 42 | }, 43 | "laravel": { 44 | "providers": [ 45 | "Wavey\\Sweetalert\\SweetalertServiceProvider" 46 | ] 47 | } 48 | }, 49 | "config": { 50 | "sort-packages": true 51 | }, 52 | "minimum-stability": "dev", 53 | "prefer-stable": true 54 | } 55 | -------------------------------------------------------------------------------- /config/sweetalert.php: -------------------------------------------------------------------------------- 1 | autoclose(milliseconds) on the end. 7 | */ 8 | 'autoclose' => 2500, 9 | ]; 10 | -------------------------------------------------------------------------------- /functions.php: -------------------------------------------------------------------------------- 1 | message($message, $title); 16 | } 17 | 18 | return $notifier; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Console/InstallCommand.php: -------------------------------------------------------------------------------- 1 | callSilent('vendor:publish', ['--tag' => 'sweetalert-config', '--force' => true]); 30 | 31 | // Install NPM Package. 32 | if ($this->option('npm')) { 33 | $this->installNPM(); 34 | } 35 | 36 | // Policies... 37 | ( new Filesystem() )->copyDirectory(__DIR__.'/../../views/', resource_path( 38 | 'views/vendor/wavey/sweetalert' 39 | )); 40 | } 41 | 42 | protected function installNPM() 43 | { 44 | // Install the NPM Package. 45 | ( new Process(['npm', 'install', '&&', 'npm', 'install', 'sweetalert2', '--save'], base_path()) ) 46 | ->setTimeout(null) 47 | ->run(function ($type, $output) { 48 | $this->output->write($output); 49 | }); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/ConvertMessages.php: -------------------------------------------------------------------------------- 1 | session()->has('success')) { 19 | alert()->success($request->session()->get('success'))->persistent(); 20 | } 21 | 22 | if ($request->session()->has('warning')) { 23 | alert()->warning($request->session()->get('warning'))->persistent(); 24 | } 25 | 26 | if ($request->session()->has('info')) { 27 | alert()->info($request->session()->get('info'))->persistent(); 28 | } 29 | 30 | if ($request->session()->has('message')) { 31 | alert()->message($request->session()->get('message'))->persistent(); 32 | } 33 | 34 | if ($request->session()->has('basic')) { 35 | alert()->basic($request->session()->get('basic')); 36 | } 37 | 38 | if ($request->session()->has('errors')) { 39 | $message = $request->session()->get('errors'); 40 | 41 | if (!is_string($message)) { 42 | $message = $this->prepareErrors($message->getMessages()); 43 | } 44 | 45 | alert()->error($message)->html()->persistent(); 46 | } 47 | 48 | return $next($request); 49 | } 50 | 51 | /** 52 | * Prepare the errors. 53 | * 54 | * @param $errors 55 | * 56 | * @return string 57 | */ 58 | private function prepareErrors($errors): string 59 | { 60 | $errors = collect($errors); 61 | 62 | return $errors->flatten()->implode('
'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/LaravelSessionStore.php: -------------------------------------------------------------------------------- 1 | session = $session; 14 | } 15 | 16 | /** 17 | * Flash the given message. 18 | * 19 | * @param string $key 20 | * 21 | * @return void 22 | */ 23 | public function flash(string $key, $value = true) 24 | { 25 | $this->session->flash($key, $value); 26 | } 27 | 28 | public function remove($keys) 29 | { 30 | $this->session->forget($keys); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Notifier.php: -------------------------------------------------------------------------------- 1 | '', 16 | 'visible' => false, 17 | 'value' => null, 18 | 'className' => '', 19 | 'closeModal' => true, 20 | ]; 21 | 22 | /** 23 | * Create a new Notifier instance. 24 | * 25 | * @param SessionStore SessionStore $session 26 | */ 27 | public function __construct(SessionStore $session) 28 | { 29 | $this->session = $session; 30 | 31 | $this->setDefaultConfig(); 32 | } 33 | 34 | /** 35 | * Sets all default config options for an alert. 36 | * 37 | * @return void 38 | */ 39 | protected function setDefaultConfig() 40 | { 41 | $this->setConfig([ 42 | 'timer' => config('sweetalert.autoclose'), 43 | 'text' => '', 44 | 'buttons' => [ 45 | 'cancel' => false, 46 | 'confirm' => false, 47 | ], 48 | ]); 49 | } 50 | 51 | /** 52 | * Create the basic alert. 53 | * 54 | * @param $text 55 | * @param $title 56 | * 57 | * @return $this 58 | */ 59 | public function basic($text, $title): Notifier 60 | { 61 | $this->message($text, $title); 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * Add the text for the alert. 68 | * 69 | * @param string $text 70 | * @param string|null $title 71 | * @param $icon 72 | * 73 | * @return $this 74 | */ 75 | public function message(string $text = '', string $title = null, $icon = null): Notifier 76 | { 77 | $this->config['text'] = $text; 78 | 79 | if (!is_null($title)) { 80 | $this->config['title'] = $title; 81 | } 82 | 83 | if (!is_null($icon)) { 84 | $this->config['icon'] = $icon; 85 | } 86 | $this->flashConfig(); 87 | 88 | return $this; 89 | } 90 | 91 | /** 92 | * Flash the config. 93 | * 94 | * @return void 95 | */ 96 | protected function flashConfig() 97 | { 98 | $this->session->remove('sweetalert'); 99 | 100 | foreach ($this->config as $key => $value) { 101 | $this->session->flash("sweetalert.{$key}", $value); 102 | } 103 | 104 | $this->session->flash('sweetalert.alert', $this->buildJsonConfig()); 105 | } 106 | 107 | /** 108 | * Encode the config. 109 | */ 110 | protected function buildJsonConfig() 111 | { 112 | return json_encode($this->config); 113 | } 114 | 115 | /** 116 | * Get the config. 117 | * 118 | * @param $key 119 | * 120 | * @return array|mixed|void 121 | */ 122 | public function getConfig($key = null) 123 | { 124 | if (is_null($key)) { 125 | return $this->config; 126 | } 127 | 128 | if (array_key_exists($key, $this->config)) { 129 | return $this->config[$key]; 130 | } 131 | } 132 | 133 | /** 134 | * Set the config. 135 | * 136 | * @param array $config 137 | * 138 | * @return $this 139 | */ 140 | public function setConfig(array $config = []) 141 | { 142 | $this->config = array_merge($this->config, $config); 143 | 144 | return $this; 145 | } 146 | 147 | /** 148 | * Get the config in JSON format. 149 | */ 150 | public function getJsonConfig() 151 | { 152 | return $this->buildJsonConfig(); 153 | } 154 | 155 | /** 156 | * Flash the config. 157 | */ 158 | public function __destruct() 159 | { 160 | $this->flashConfig(); 161 | } 162 | 163 | /** 164 | * Create the info alert. 165 | * 166 | * @param $text 167 | * @param string $title 168 | * 169 | * @return $this 170 | */ 171 | public function info($text, string $title = ''): Notifier 172 | { 173 | $this->message($text, $title, 'info'); 174 | 175 | return $this; 176 | } 177 | 178 | /** 179 | * Create the success alert. 180 | * 181 | * @param $text 182 | * @param string $title 183 | * 184 | * @return $this 185 | */ 186 | public function success($text, string $title = ''): Notifier 187 | { 188 | $this->message($text, $title, 'success'); 189 | 190 | return $this; 191 | } 192 | 193 | /** 194 | * Create the error alert. 195 | * 196 | * @param $text 197 | * @param string $title 198 | * 199 | * @return $this 200 | */ 201 | public function error($text, string $title = ''): Notifier 202 | { 203 | $this->message($text, $title, 'error'); 204 | 205 | return $this; 206 | } 207 | 208 | /** 209 | * Create the warning alert. 210 | * 211 | * @param $text 212 | * @param string $title 213 | * 214 | * @return $this 215 | */ 216 | public function warning($text, string $title = ''): Notifier 217 | { 218 | $this->message($text, $title, 'warning'); 219 | 220 | return $this; 221 | } 222 | 223 | /** 224 | * Set the autoclose value in milliseconds. 225 | * 226 | * @param $milliseconds 227 | * 228 | * @return $this 229 | */ 230 | public function autoclose($milliseconds = null): Notifier 231 | { 232 | if (!is_null($milliseconds)) { 233 | $this->config['timer'] = $milliseconds; 234 | } 235 | 236 | return $this; 237 | } 238 | 239 | /** 240 | * Create the confirm button. 241 | * 242 | * @param string $buttonText 243 | * @param array $overrides 244 | * 245 | * @return $this 246 | */ 247 | public function confirmButton(string $buttonText = 'OK', array $overrides = []): Notifier 248 | { 249 | $this->addButton('confirm', $buttonText, $overrides); 250 | 251 | return $this; 252 | } 253 | 254 | /** 255 | * Add the button. 256 | * 257 | * @param $key 258 | * @param $buttonText 259 | * @param array $overrides 260 | * 261 | * @return $this 262 | */ 263 | public function addButton($key, $buttonText, array $overrides = []): Notifier 264 | { 265 | $this->config['buttons'][$key] = array_merge( 266 | $this->buttonConfig, 267 | [ 268 | 'text' => $buttonText, 269 | 'visible' => true, 270 | ], 271 | $overrides 272 | ); 273 | 274 | $this->closeOnClickOutside(false); 275 | $this->removeTimer(); 276 | 277 | return $this; 278 | } 279 | 280 | /** 281 | * Close the button when clicking outside the alert. 282 | * 283 | * @param bool $value 284 | * 285 | * @return $this 286 | */ 287 | public function closeOnClickOutside(bool $value = true) 288 | { 289 | $this->config['closeOnClickOutside'] = $value; 290 | 291 | return $this; 292 | } 293 | 294 | /** 295 | * Remove the timer. 296 | * 297 | * @return void 298 | */ 299 | protected function removeTimer() 300 | { 301 | if (array_key_exists('timer', $this->config)) { 302 | unset($this->config['timer']); 303 | } 304 | } 305 | 306 | /** 307 | * Create the HTML alert. 308 | */ 309 | public function html() 310 | { 311 | $this->config['content'] = $this->config['text']; 312 | 313 | unset($this->config['text']); 314 | 315 | return $this; 316 | } 317 | 318 | /** 319 | * Create the persistant alert. 320 | * 321 | * @param string $buttonText 322 | * 323 | * @return $this 324 | */ 325 | public function persistent(string $buttonText = 'OK') 326 | { 327 | $this->addButton('confirm', $buttonText); 328 | $this->closeOnClickOutside(false); 329 | $this->removeTimer(); 330 | 331 | return $this; 332 | } 333 | 334 | /** 335 | * Create the close button. 336 | * 337 | * @param string $buttonText 338 | * @param array $overrides 339 | * 340 | * @return $this 341 | */ 342 | public function cancelButton(string $buttonText = 'Cancel', array $overrides = []): Notifier 343 | { 344 | $this->addButton('cancel', $buttonText, $overrides); 345 | 346 | return $this; 347 | } 348 | } 349 | -------------------------------------------------------------------------------- /src/SessionStore.php: -------------------------------------------------------------------------------- 1 | loadViewsFrom(__DIR__.'/../views', 'sweetalert'); 12 | 13 | $this->publishes([ 14 | __DIR__.'/../config/sweetalert.php' => config_path('sweetalert.php'), 15 | ], 'sweetalert-config'); 16 | 17 | $this->publishes([ 18 | __DIR__.'/../views' => base_path('resources/views/vendor/wavey/sweetalert'), 19 | ], 'sweetalert-views'); 20 | 21 | $this->configureCommands(); 22 | } 23 | 24 | /** 25 | * Configure the commands offered by the application. 26 | * 27 | * @return void 28 | */ 29 | protected function configureCommands() 30 | { 31 | if (!$this->app->runningInConsole()) { 32 | return; 33 | } 34 | 35 | $this->commands([ 36 | Console\InstallCommand::class, 37 | ]); 38 | } 39 | 40 | public function register() 41 | { 42 | $this->mergeConfigFrom( 43 | __DIR__.'/../config/sweetalert.php', 44 | 'sweetalert' 45 | ); 46 | 47 | $this->app->bind( 48 | 'Wavey\Sweetalert\SessionStore', 49 | 'Wavey\Sweetalert\LaravelSessionStore' 50 | ); 51 | 52 | $this->app->bind('wavey.sweetalert', function () { 53 | return $this->app->make('Wavey\Sweetalert\Notifier'); 54 | }); 55 | } 56 | 57 | public function provides(): array 58 | { 59 | return [ 60 | 'Wavey\Sweetalert\SessionStore', 61 | 'wavey.sweetalert', 62 | ]; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /views/sweetalert.blade.php: -------------------------------------------------------------------------------- 1 | @if (Session::has('sweetalert.alert')) 2 | 6 | @endif 7 | --------------------------------------------------------------------------------