Skip to content

Commit 8d468a7

Browse files
authored
Merge pull request #159 from iMattPro/notifications
Add notifications for status changes
2 parents 016e983 + 3c65585 commit 8d468a7

File tree

18 files changed

+931
-56
lines changed

18 files changed

+931
-56
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ The official Ideas Centre used at [phpBB.com](https://www.phpbb.com/ideas/). Thi
44

55
[![Build Status](https://github.com/phpbb/ideas/actions/workflows/tests.yml/badge.svg)](https://github.com/phpbb/ideas/actions)
66
[![codecov](https://codecov.io/gh/phpbb/ideas/graph/badge.svg?token=74AITS9CPZ)](https://codecov.io/gh/phpbb/ideas)
7-
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/phpbb/ideas/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/phpbb/ideas/?branch=master)
87

98
## Contribute
109

config/services.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ services:
8282
- '@config'
8383
- '@dbal.conn'
8484
- '@language'
85+
- '@notification_manager'
8586
- '@user'
8687
- '%tables.ideas_ideas%'
8788
- '%tables.ideas_votes%'
@@ -122,3 +123,13 @@ services:
122123
- [set_name, [cron.task.prune_orphaned_ideas]]
123124
tags:
124125
- { name: cron.task }
126+
127+
# ----- Notifications -----
128+
phpbb.ideas.notification.type.status:
129+
class: phpbb\ideas\notification\type\status
130+
parent: notification.type.base
131+
shared: false
132+
calls:
133+
- [set_additional_services, ['@config', '@controller.helper', '@user_loader']]
134+
tags:
135+
- { name: notification.type }

ext.php

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
namespace phpbb\ideas;
1212

1313
/**
14-
* This ext class is optional and can be omitted if left empty.
15-
* However, you can add special (un)installation commands in the
16-
* methods enable_step(), disable_step() and purge_step(). As it is,
17-
* these methods are defined in \phpbb\extension\base, which this
18-
* class extends, but you can overwrite them to give special
19-
* instructions for those cases.
20-
*/
14+
* This ext class is optional and can be omitted if left empty.
15+
* However, you can add special (un)installation commands in the
16+
* methods enable_step(), disable_step() and purge_step(). As it is,
17+
* these methods are defined in \phpbb\extension\base, which this
18+
* class extends, but you can overwrite them to give special
19+
* instructions for those cases.
20+
*/
2121
class ext extends \phpbb\extension\base
2222
{
2323
public const SORT_AUTHOR = 'author';
@@ -30,44 +30,93 @@ class ext extends \phpbb\extension\base
3030
public const SORT_MYIDEAS = 'egosearch';
3131
public const SUBJECT_LENGTH = 120;
3232
public const NUM_IDEAS = 5;
33+
public const NOTIFICATION_TYPE_STATUS = 'phpbb.ideas.notification.type.status';
3334

3435
/** @var array Idea status names and IDs */
35-
public static $statuses = array(
36+
public static $statuses = [
3637
'NEW' => 1,
3738
'IN_PROGRESS' => 2,
3839
'IMPLEMENTED' => 3,
3940
'DUPLICATE' => 4,
4041
'INVALID' => 5,
41-
);
42+
];
43+
44+
/** @var array Cached flipped statuses array */
45+
private static $status_names;
4246

4347
/**
4448
* Return the status name from the status ID.
4549
*
4650
* @param int $id ID of the status.
47-
*
4851
* @return string The status name.
49-
* @static
50-
* @access public
5152
*/
5253
public static function status_name($id)
5354
{
54-
return array_flip(self::$statuses)[$id];
55+
if (self::$status_names === null)
56+
{
57+
self::$status_names = array_flip(self::$statuses);
58+
}
59+
60+
return self::$status_names[$id];
5561
}
5662

5763
/**
5864
* Check whether the extension can be enabled.
5965
*
60-
* Requires phpBB >= 3.2.3 due to removal of deprecated Twig functions (ie Twig_SimpleFunction)
6166
* Requires phpBB >= 3.3.0 due to use of PHP 7 features
62-
* Requires PHP >= 7.1.0
67+
* Requires PHP >= 7.2.0
6368
*
6469
* @return bool
65-
* @access public
6670
*/
6771
public function is_enableable()
6872
{
69-
return !(PHP_VERSION_ID < 70100 ||
70-
phpbb_version_compare(PHPBB_VERSION, '3.3.0', '<') ||
71-
phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '>='));
73+
return PHP_VERSION_ID >= 70200
74+
&& phpbb_version_compare(PHPBB_VERSION, '3.3.0', '>=')
75+
&& phpbb_version_compare(PHPBB_VERSION, '4.0.0-dev', '<');
76+
}
77+
78+
/**
79+
* Handle notification management for extension lifecycle
80+
*
81+
* @param string $method The notification manager method to call
82+
* @return string
83+
*/
84+
private function handle_notifications($method)
85+
{
86+
$this->container->get('notification_manager')->$method(self::NOTIFICATION_TYPE_STATUS);
87+
return 'notification';
88+
}
89+
90+
/**
91+
* Enable notifications for the extension
92+
*
93+
* @param mixed $old_state
94+
* @return bool|string
95+
*/
96+
public function enable_step($old_state)
97+
{
98+
return $old_state === false ? $this->handle_notifications('enable_notifications') : parent::enable_step($old_state);
99+
}
100+
101+
/**
102+
* Disable notifications for the extension
103+
*
104+
* @param mixed $old_state
105+
* @return bool|string
106+
*/
107+
public function disable_step($old_state)
108+
{
109+
return $old_state === false ? $this->handle_notifications('disable_notifications') : parent::disable_step($old_state);
110+
}
111+
112+
/**
113+
* Purge notifications for the extension
114+
*
115+
* @param mixed $old_state
116+
* @return bool|string
117+
*/
118+
public function purge_step($old_state)
119+
{
120+
return $old_state === false ? $this->handle_notifications('purge_notifications') : parent::purge_step($old_state);
72121
}
73122
}

factory/base.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use phpbb\config\config;
1515
use phpbb\db\driver\driver_interface;
1616
use phpbb\language\language;
17+
use phpbb\notification\manager as notification_manager;
1718
use phpbb\user;
1819

1920
/**
@@ -33,6 +34,9 @@ class base
3334
/** @var language */
3435
protected $language;
3536

37+
/** @var notification_manager */
38+
protected $notification_manager;
39+
3640
/* @var user */
3741
protected $user;
3842

@@ -51,22 +55,24 @@ class base
5155
/**
5256
* Constructor
5357
*
54-
* @param auth $auth
55-
* @param config $config
58+
* @param auth $auth
59+
* @param config $config
5660
* @param driver_interface $db
57-
* @param language $language
58-
* @param user $user
59-
* @param string $table_ideas
60-
* @param string $table_votes
61-
* @param string $table_topics
62-
* @param string $phpEx
61+
* @param language $language
62+
* @param notification_manager $notification_manager
63+
* @param user $user
64+
* @param string $table_ideas
65+
* @param string $table_votes
66+
* @param string $table_topics
67+
* @param string $phpEx
6368
*/
64-
public function __construct(auth $auth, config $config, driver_interface $db, language $language, user $user, $table_ideas, $table_votes, $table_topics, $phpEx)
69+
public function __construct(auth $auth, config $config, driver_interface $db, language $language, notification_manager $notification_manager, user $user, $table_ideas, $table_votes, $table_topics, $phpEx)
6570
{
6671
$this->auth = $auth;
6772
$this->config = $config;
6873
$this->db = $db;
6974
$this->language = $language;
75+
$this->notification_manager = $notification_manager;
7076
$this->user = $user;
7177

7278
$this->php_ext = $phpEx;

factory/idea.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ public function set_status($idea_id, $status)
7070
);
7171

7272
$this->update_idea_data($sql_ary, $idea_id, $this->table_ideas);
73+
74+
// Send a notification
75+
$idea = $this->get_idea($idea_id);
76+
$notifications = $this->notification_manager->get_notified_users(ext::NOTIFICATION_TYPE_STATUS, ['item_id' => (int) $idea_id]);
77+
$this->notification_manager->{empty($notifications) ? 'add_notifications' : 'update_notifications'}(
78+
ext::NOTIFICATION_TYPE_STATUS,
79+
[
80+
'idea_id' => (int) $idea_id,
81+
'status' => (int) $status,
82+
'user_id' => (int) $this->user->data['user_id'],
83+
'idea_author' => (int) $idea['idea_author'],
84+
'idea_title' => $idea['idea_title'],
85+
]
86+
);
7387
}
7488

7589
/**
@@ -262,8 +276,14 @@ public function delete($id, $topic_id = 0)
262276
// Delete idea
263277
$deleted = $this->delete_idea_data($id, $this->table_ideas);
264278

265-
// Delete votes
266-
$this->delete_idea_data($id, $this->table_votes);
279+
if ($deleted)
280+
{
281+
// Delete votes
282+
$this->delete_idea_data($id, $this->table_votes);
283+
284+
// Delete notifications
285+
$this->notification_manager->delete_notifications(ext::NOTIFICATION_TYPE_STATUS, $id);
286+
}
267287

268288
return $deleted;
269289
}

language/en/common.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'IDEA_DELETED' => 'Idea successfully deleted.',
3939
'IDEA_LIST' => 'Idea List',
4040
'IDEA_NOT_FOUND' => 'Idea not found',
41+
'IDEA_STATUS_CHANGE' => '<strong>Idea status changed</strong> by %s:',
4142
'IDEA_STORED_MOD' => 'Your idea has been submitted successfully, but it will need to be approved by a moderator before it is publicly viewable. You will be notified when your idea has been approved.<br /><br /><a href="%s">Return to Ideas</a>.',
4243
'IDEAS_TITLE' => 'phpBB Ideas',
4344
'IDEAS_NOT_AVAILABLE' => 'Ideas is not available at this time.',
@@ -66,6 +67,7 @@
6667
'NEW' => 'New',
6768
'NEW_IDEA' => 'New Idea',
6869
'NO_IDEAS_DISPLAY' => 'There are no ideas to display.',
70+
'NOTIFICATION_STATUS' => '<em>Status: <strong>%s</strong></em>',
6971

7072
'OPEN_IDEAS' => 'Open ideas',
7173

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Subject: Idea status - "{IDEA_TITLE}"
2+
3+
Hello {USERNAME},
4+
5+
The status of your Idea topic "{IDEA_TITLE}" at "{SITENAME}" has been updated by {UPDATED_BY} to {STATUS}.
6+
7+
If you want to view the Idea, click the following link:
8+
{U_VIEW_IDEA}
9+
10+
{EMAIL_SIG}

language/en/info_ucp_ideas.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
*
4+
* Ideas extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
if (!defined('IN_PHPBB'))
12+
{
13+
exit;
14+
}
15+
16+
if (empty($lang) || !is_array($lang))
17+
{
18+
$lang = [];
19+
}
20+
21+
// DEVELOPERS PLEASE NOTE
22+
//
23+
// All language files should use UTF-8 as their encoding and the files must not contain a BOM.
24+
//
25+
// Placeholders can now contain order information, e.g. instead of
26+
// 'Page %s of %s' you can (and should) write 'Page %1$s of %2$s', this allows
27+
// translators to re-order the output of data while ensuring it remains correct
28+
//
29+
// You do not need this where single placeholders are used, e.g. 'Message %d' is fine
30+
// equally where a string contains only two placeholders which are used to wrap text
31+
// in a url you again do not need to specify an order e.g., 'Click %sHERE%s' is fine
32+
//
33+
// Some characters you may want to copy&paste:
34+
// ’ » “ ” …
35+
//
36+
37+
$lang = array_merge($lang, [
38+
'NOTIFICATION_TYPE_IDEAS' => 'Your Idea in the Ideas forum has a status change',
39+
]);

0 commit comments

Comments
 (0)