Skip to content

Commit c7a58bd

Browse files
authored
Merge pull request #3 from Digitalist-Open-Cloud/add_commands
add some commands
2 parents 0dc6473 + fe0d853 commit c7a58bd

File tree

8 files changed

+207
-26
lines changed

8 files changed

+207
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## [5.0.4] - 2025-01-03
4+
5+
### Added
6+
7+
- Console commands to create and list notifications.
8+
- Improved usability on the manage page for notifications.
9+
310
## [5.0.3] - 2025-01-02
411

512
### Added

Commands/CreateNotification.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function configure()
4444
To run:
4545
<info>%command.name%</info>';
4646
$this->setHelp($HelpText);
47-
$this->setName('rebelnotifications:create-notification');
47+
$this->setName('rebelnotifications:create');
4848
$this->setDescription('CreateNotification');
4949
$this->addNoValueOption(
5050
'enabled',
@@ -100,54 +100,38 @@ protected function doExecute(): int
100100
{
101101
$input = $this->getInput();
102102
$output = $this->getOutput();
103-
104-
// Retrieve and check 'active' option
105103
if (!$input->hasOption('enabled') || $input->getOption('enabled') === null) {
106104
$enabled = 0;
107105
} else {
108106
$enabled = 1;
109107
}
110-
111-
// Retrieve and check 'raw' option
112108
if (!$input->hasOption('raw') || $input->getOption('raw') === null) {
113109
$raw = 0;
114110
} else {
115111
$raw = 1;
116112
}
117-
118-
// Retrieve and check 'type' option
119113
if (!$input->hasOption('type') || $input->getOption('type') === null) {
120114
throw new \InvalidArgumentException("The 'type' option is required.");
121115
}
122116
$type = $input->getOption('type');
123-
124-
// Retrieve and check 'title' option
125117
if (!$input->hasOption('title') || $input->getOption('title') === null) {
126118
throw new \InvalidArgumentException("The 'title' option is required.");
127119
}
128120
$title = $input->getOption('title');
129-
130-
// Retrieve and check 'message' option
131121
if (!$input->hasOption('message') || $input->getOption('message') === null) {
132122
throw new \InvalidArgumentException("The 'message' option is required.");
133123
}
134124
$message = $input->getOption('message');
135-
136-
// Retrieve and check 'context' option
137125
if (!$input->hasOption('context') || $input->getOption('context') === null) {
138126
throw new \InvalidArgumentException("The 'context' option is required.");
139127
}
140128
$context = $input->getOption('context');
141-
142-
// Retrieve and check 'priority' option
143129
if (!$input->hasOption('priority') || $input->getOption('priority') === null) {
144130
throw new \InvalidArgumentException("The 'priority' option is required.");
145131
}
146132
$priority = $input->getOption('priority');
147-
148-
$api = new API;
133+
$api = new API();
149134
$addNotification = $api->insertNotification($enabled, $title, $message, $context, $priority, $type, $raw);
150-
151135
$message = sprintf('<info>Created notification: %s</info>', $title);
152136

153137
$output->writeln($message);

Commands/ListNotifications.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/**
4+
* The Rebel Notification plugin for Matomo.
5+
*
6+
* Copyright (C) Digitalist Open Cloud <cloud@digitalist.com>
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
namespace Piwik\Plugins\RebelNotifications\Commands;
23+
24+
use Piwik\Plugin\ConsoleCommand;
25+
use Piwik\Plugins\RebelNotifications\API;
26+
27+
/**
28+
* This class lets you define a new command. To read more about commands have a look at our Matomo Console guide on
29+
* https://developer.matomo.org/guides/piwik-on-the-command-line
30+
*
31+
* As Matomo Console is based on the Symfony Console you might also want to have a look at
32+
* http://symfony.com/doc/current/components/console/index.html
33+
*/
34+
class ListNotifications extends ConsoleCommand
35+
{
36+
/**
37+
* This method allows you to configure your command. Here you can define the name and description of your command
38+
* as well as all options and arguments you expect when executing it.
39+
*/
40+
protected function configure()
41+
{
42+
$HelpText = 'The <info>%command.name%</info> will create a notification.
43+
<comment>Samples:</comment>
44+
To run:
45+
<info>%command.name%</info>';
46+
$this->setHelp($HelpText);
47+
$this->setName('rebelnotifications:list');
48+
$this->setDescription('List notifications');
49+
$this->addNoValueOption(
50+
'enabled',
51+
null,
52+
'Set to enabled',
53+
null
54+
);
55+
}
56+
57+
protected function doExecute(): int
58+
{
59+
$input = $this->getInput();
60+
$output = $this->getOutput();
61+
$enabled = $input->getOption('enabled') ? true : false;
62+
63+
$api = new API();
64+
if ($enabled === false) {
65+
$listNotifications = $api->getAllNotifications();
66+
} else {
67+
$listNotifications = $api->getEnabledNotifications();
68+
}
69+
70+
foreach ($listNotifications as $notification) {
71+
if ($notification['enabled'] == 1) {
72+
$enabled = 'yes';
73+
}
74+
if ($notification['raw'] == 1) {
75+
$raw = 'yes';
76+
}
77+
78+
$out = "ID: <comment>{$notification['id']}</comment>\n";
79+
$out .= "Enabled: <comment>{$enabled}</comment>\n";
80+
$out .= "Title: <comment>{$notification['title']}</comment>\n";
81+
$out .= "Message: <comment>{$notification['message']}</comment>\n";
82+
$out .= "Context: <comment>{$notification['context']}</comment>\n";
83+
$out .= "Priority: <comment>{$notification['priority']}</comment>\n";
84+
$out .= "Type: <comment>{$notification['type']}</comment>\n";
85+
$out .= "Raw (HTML allowed): <comment>{$raw}</comment>\n";
86+
$out .= "-----------------------------------";
87+
$output->writeln("<info>$out</info>");
88+
}
89+
90+
91+
return self::SUCCESS;
92+
}
93+
}

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,24 @@ When you add or change a notification, nothing is changed until you logout and l
4343
### Create a notification
4444

4545
```sh
46-
./console rebelnotifications:create-notification --raw --enabled --title="My title" --me
46+
./console rebelnotifications:create --raw --enabled --title="My title" --me
4747
ssage="This is the message in <strong>bold</strong>" --context=warning --priority=50 --type=persistent
4848
```
4949

50+
### List notifications
51+
52+
#### All notifications
53+
54+
```sh
55+
./console rebelnotifications:list
56+
```
57+
58+
#### Enabled notifications
59+
60+
```sh
61+
./console rebelnotifications:list --enabled
62+
```
63+
5064
## Using RebelNotifications with Matomo API
5165

5266
Examples with curl.

docs/index.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ At "Manage" you can add, edit and delete notifications.
3838

3939
When you add or change a notification, nothing is changed until you logout and login. The triggering event for the notifications is `Login.authenticate.successful` - which means that nothing updates until you login.
4040

41+
## Using Rebel Notifications with the console
42+
43+
### Create a notification
44+
45+
```sh
46+
./console rebelnotifications:create --raw --enabled --title="My title" --me
47+
ssage="This is the message in <strong>bold</strong>" --context=warning --priority=50 --type=persistent
48+
```
49+
50+
### List notifications
51+
52+
#### All notifications
53+
54+
```sh
55+
./console rebelnotifications:list
56+
```
57+
58+
#### Enabled notifications
59+
60+
```sh
61+
./console rebelnotifications:list --enabled
62+
```
63+
4164
## Using RebelNotifications with Matomo API
4265

4366
Examples with curl.

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "RebelNotifications",
33
"description": "Show Notifications to your users.",
4-
"version": "5.0.3",
4+
"version": "5.0.4",
55
"theme": false,
66
"require": {
77
"matomo": ">=5.0.0-stable,<6.0.0-b1"

templates/manageNotifications.twig

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,24 @@
4444
<tbody>
4545
{% for notification in messages %}
4646
<tr>
47-
<td>{{ notification.enabled }}</td>
47+
<td>{% if notification.enabled == "1" %}
48+
<span class="icon-show" title="Enabled"></span>
49+
{% elseif notification.enabled == "0" %}
50+
<span class="icon-hide" title="Disabled"></span>
51+
{% endif %}
52+
</td>
4853
<td>{{ notification.title }}</td>
4954
<td>{{ notification.message | raw }}</td>
5055
<td>{{ notification.context }}</td>
5156
<td>{{ notification.priority }}</td>
5257
<td>{{ notification.type }}</td>
5358
<td>{{ notification.raw }}</td>
5459
<td>
55-
<a href="{{ linkTo({'module':'RebelNotifications','action':'editNotification', 'id': notification.id}) }}">
60+
<a href="{{ linkTo({'module':'RebelNotifications','action':'editNotification', 'id': notification.id}) }}" title="Edit notification">
5661
<span class="icon-edit"></span>
5762
</a>
5863
</td>
59-
<td><a href='{{ linkTo({'module':'RebelNotifications','action':'deleteNotification', 'id': notification.id}) }}'><span class="icon-delete"></span></a></td>
64+
<td><a href='{{ linkTo({'module':'RebelNotifications','action':'deleteNotification', 'id': notification.id}) }}' title="Delete notification"><span class="icon-delete"></span></a></td>
6065
</tr>
6166
{% else %}
6267
<tr>

tests/Integration/CommandsTest.php

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Piwik\Tests\Framework\TestCase\ConsoleCommandTestCase;
66
use Piwik\Tests\Framework\Fixture;
7+
use Piwik\Plugins\RebelNotifications\API;
78

89
/**
910
* @group RebelNotifications
@@ -12,31 +13,37 @@
1213
*/
1314
class CommandsTest extends ConsoleCommandTestCase
1415
{
16+
/**
17+
* @var API
18+
*/
19+
private $api;
20+
1521
public function setUp(): void
1622
{
1723
parent::setUp();
1824
Fixture::createSuperUser();
1925
Fixture::createWebsite('2025-01-01 00:00:00');
26+
$this->api = API::getInstance();
2027
}
2128

2229
public function testHelpCreateNotification()
2330
{
2431
$code = $this->applicationTester->run([
25-
'command' => 'rebelnotifications:create-notification',
32+
'command' => 'rebelnotifications:create',
2633
'--help' => true,
2734
'-vvv' => true,
2835
]);
2936
$this->assertEquals(0, $code);
3037
$this->assertStringContainsStringIgnoringCase(
31-
"rebelnotifications:create-notification [options]",
38+
"rebelnotifications:create [options]",
3239
$this->applicationTester->getDisplay()
3340
);
3441
}
3542

3643
public function testCreateNotification()
3744
{
3845
$code = $this->applicationTester->run([
39-
'command' => 'rebelnotifications:create-notification',
46+
'command' => 'rebelnotifications:create',
4047
'--raw' => true,
4148
'--enabled' => true,
4249
'--title' => 'test title',
@@ -52,4 +59,52 @@ public function testCreateNotification()
5259
$this->applicationTester->getDisplay()
5360
);
5461
}
62+
63+
public function testHelpListNotifications()
64+
{
65+
$code = $this->applicationTester->run([
66+
'command' => 'rebelnotifications:list',
67+
'--help' => true,
68+
'-vvv' => true,
69+
]);
70+
$this->assertEquals(0, $code);
71+
$this->assertStringContainsStringIgnoringCase(
72+
"rebelnotifications:list [options]",
73+
$this->applicationTester->getDisplay()
74+
);
75+
}
76+
77+
public function testListEnabledNotifications()
78+
{
79+
$this->api->insertNotification('1', 'this should show', 'bar', 'warning', '25', 'persistent', '0');
80+
$this->api->insertNotification('0', 'this should not show', 'bar', 'warning', '25', 'persistent', '0');
81+
$code = $this->applicationTester->run([
82+
'command' => 'rebelnotifications:list',
83+
'--enabled' => true,
84+
]);
85+
86+
$this->assertEquals(0, $code);
87+
$this->assertStringContainsStringIgnoringCase(
88+
"this should show",
89+
$this->applicationTester->getDisplay()
90+
);
91+
$this->assertStringNotContainsStringIgnoringCase(
92+
"this should not show",
93+
$this->applicationTester->getDisplay()
94+
);
95+
}
96+
97+
public function testListAllNotifications()
98+
{
99+
$this->api->insertNotification('0', 'this one is disabled', 'bar', 'warning', '25', 'persistent', '0');
100+
$code = $this->applicationTester->run([
101+
'command' => 'rebelnotifications:list',
102+
]);
103+
104+
$this->assertEquals(0, $code);
105+
$this->assertStringContainsStringIgnoringCase(
106+
"this one is disabled",
107+
$this->applicationTester->getDisplay()
108+
);
109+
}
55110
}

0 commit comments

Comments
 (0)