Skip to content

Commit 3e1517a

Browse files
authored
Merge pull request #17 from tattersoftware/v3
Refactor
2 parents d1264fe + 7b374d2 commit 3e1517a

27 files changed

+762
-377
lines changed

README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ Provides out-of-the-box user alerts for CodeIgniter 4
2323

2424
Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities
2525
and always be up-to-date:
26-
* `> composer require tatter/alerts`
26+
```bash
27+
composer require tatter/alerts
28+
```
2729

2830
Or, install manually by downloading the source files and adding the directory to
2931
`app/Config/Autoload.php`.
3032

31-
Note: The default display template expects [Bootstrap4](https://getbootstrap.com) but is
33+
> Note: The default display template expects [Bootstrap](https://getbootstrap.com) but is
3234
easily changed.
3335

3436
## Configuration (optional)
@@ -37,18 +39,18 @@ The library's default behavior can be changed using its config file. Copy
3739
**examples/Alerts.php** to **app/Config/Alerts.php** and follow the instructions in the
3840
comments. If no config file is found the library will use its defaults.
3941

42+
### Styling
43+
44+
By default alerts will be displayed with classes for Bootstrap (not included).
45+
However, styles can be set to use other frameworks (or you may use your own) by changing
46+
the `$template` property in the Config file.
47+
4048
## Usage
4149

4250
If installed correctly CodeIgniter 4 will detect and autoload the library, service, helper,
4351
and config. Initialize the helper if you want the convenience wrapper function:
44-
`helper('tatter\alerts');`
52+
`helper('alerts');`
4553

4654
Then use the helper function `alert($class, $text)` to set an alert for the user's next
4755
view. Use class methods `$alerts->css()` and `$alerts->display()` to retrieve the styling
4856
and HTML for the alerts.
49-
50-
## Styles
51-
52-
By default alerts will be displayed with classes for Bootstrap (not included).
53-
However, styles can be set to use other toolkits (or you may use your own) by changing
54-
`$template` in the config file.

UPGRADING.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Upgrade Guide
2+
3+
## Version 2 to 3
4+
***
5+
6+
> Note: This is a complete refactor! Please be sure to read the docs carefully before upgrading.
7+
8+
* All handling is now routed through `AlertsFilter` - read the docs on setting up the Filter
9+
* Related, the service, exceptions, and language files have been removed
10+
* Alerts no longer use session prefixes but are matched to the Config `$classes` - beware of session collision
11+
* The view templates no longer include a wrapper; if you would like consistent behavior then put your token in an `<aside>` tag:
12+
```html
13+
<aside id="alerts-wrapper">
14+
{alerts}
15+
</aside>
16+
```
17+
* This library no longer provides CSS for the wrapper or positioning; if you would like consistent behavior then include the old CSS:
18+
```css
19+
<!-- CSS for Alerts -->
20+
<style>
21+
#alerts-wrapper {
22+
position: fixed;
23+
top: 10%;
24+
right: 10px;
25+
left: 10px;
26+
z-index: 10;
27+
}
28+
@media screen and (min-width: 768px) {
29+
#alerts-wrapper {
30+
left: 60%;
31+
}
32+
}
33+
</style>
34+
```

examples/Alerts.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,46 @@
22

33
namespace Config;
44

5-
/*
6-
*
5+
/**
76
* This file contains example values to change the default library behavior.
87
* Recommended usage:
98
* 1. Copy the file to app/Config/Alerts.php
109
* 2. Set any override variables
1110
* 3. Remove any lines to fallback to library defaults
12-
*
1311
*/
1412

15-
class Alerts extends \Tatter\Alerts\Config\Alerts
16-
{
17-
// prefix for SESSION variables and HTML classes, to prevent collision
18-
public $prefix = 'alerts-';
13+
use Tatter\Alerts\Config\Alerts as AlertsConfig;
1914

20-
/*
21-
Template to use for HTML output
22-
There must be a corresponding view file for the path/namespace provided
23-
Native support for:
24-
basic - a minimalist layout internal to this library
25-
bootstrap (default) - [https://getbootstrap.com/docs/4.0/components/alerts/#dismissing]
26-
foundation - [https://foundation.zurb.com/sites/docs/callout.html#making-closable]
15+
class Alerts extends AlertsConfig
16+
{
17+
/**
18+
* Template to use for HTML output.
19+
*
20+
* @var string
2721
*/
28-
public $template = 'Tatter\\Alerts\\Views\\bootstrap';
22+
public $template = 'Tatter\Alerts\Views\Bootstrap4';
2923

30-
// Whether to check session flashdata for common alert keys
31-
public $getflash = true;
24+
/**
25+
* Mapping of Session keys to their CSS classes.
26+
* Note: Some templates may add prefixes to the class,
27+
* like Bootstrap "alert-{$class}".
28+
*
29+
* @var array<string,string>
30+
*/
31+
public $classes = [
32+
'primary' => 'primary',
33+
'message' => 'primary',
34+
'secondary' => 'secondary',
35+
'notice' => 'secondary',
36+
'success' => 'success',
37+
'danger' => 'danger',
38+
'error' => 'danger',
39+
'errors' => 'danger',
40+
'critical' => 'danger',
41+
'emergency' => 'danger',
42+
'warning' => 'warning',
43+
'alert' => 'warning',
44+
'info' => 'info',
45+
'debug' => 'info',
46+
];
3247
}

src/Alerts.php

Lines changed: 0 additions & 128 deletions
This file was deleted.

src/Collectors/Alerts.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
namespace Tatter\Alerts\Collectors;
4+
5+
use CodeIgniter\Debug\Toolbar\Collectors\BaseCollector;
6+
use Tatter\Alerts\Filters\AlertsFilter;
7+
8+
class Alerts extends BaseCollector
9+
{
10+
/**
11+
* Whether this collector has data that can
12+
* be displayed in the Timeline.
13+
*
14+
* @var bool
15+
*/
16+
protected $hasTimeline = false;
17+
18+
/**
19+
* Whether this collector needs to display
20+
* content in a tab or not.
21+
*
22+
* @var bool
23+
*/
24+
protected $hasTabContent = true;
25+
26+
/**
27+
* Whether this collector has data that
28+
* should be shown in the Vars tab.
29+
*
30+
* @var bool
31+
*/
32+
protected $hasVarData = false;
33+
34+
/**
35+
* The 'title' of this Collector.
36+
* Used to name things in the toolbar HTML.
37+
*
38+
* @var string
39+
*/
40+
protected $title = 'Alerts';
41+
42+
/**
43+
* Store of alerts.
44+
*
45+
* @var array<string[]>
46+
*/
47+
protected $alerts;
48+
49+
//--------------------------------------------------------------------
50+
51+
/**
52+
* Preloads alerts so they are only gathered once.
53+
*/
54+
public function __construct()
55+
{
56+
$this->alerts = AlertsFilter::gather(config('Alerts'));
57+
}
58+
59+
//--------------------------------------------------------------------
60+
61+
/**
62+
* Returns the data of this collector to be formatted in the toolbar
63+
*/
64+
public function display(): string
65+
{
66+
$html = '';
67+
68+
foreach ($this->alerts as $alert) {
69+
[$class, $content] = $alert;
70+
71+
$html .= '<p><strong>' . ucfirst($class) . '</strong>: ' . esc($content) . '</p>' . PHP_EOL;
72+
}
73+
74+
return $html;
75+
}
76+
77+
//--------------------------------------------------------------------
78+
79+
/**
80+
* Gets the "badge" value for the button.
81+
*/
82+
public function getBadgeValue(): int
83+
{
84+
return count($this->alerts);
85+
}
86+
87+
//--------------------------------------------------------------------
88+
89+
/**
90+
* Display the icon.
91+
*
92+
* Icon from https://icons8.com - 1em package
93+
* https://icons8.com/icon/pack/data/p1em
94+
*/
95+
public function icon(): string
96+
{
97+
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAAjklEQVQ4ja2TwQmAMAxF3ySKDqLbeCjqxKKOoBPowQSlJNKK/xJK819C0oKvAOwSs9UBK9ACEzB+MVdyLnIgsVmVBLHMRyrEq3xEZxMSgBkoDXAMQPJmHtvZgcZqywEg+dtvHcA9g9qpqNIZDNZl6hZM8xOy4L+DV7MHyTKreq5BNRL7HLMqcK3q029M1gn4Ri2RhwmVWQAAAABJRU5ErkJggg==';
98+
}
99+
}

0 commit comments

Comments
 (0)