Skip to content

Commit 5537380

Browse files
committed
Fixes and housekeeping
Signed-off-by: Matt Friedman <maf675@gmail.com>
1 parent fef66f0 commit 5537380

7 files changed

Lines changed: 50 additions & 55 deletions

File tree

ext.php

Lines changed: 39 additions & 47 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,45 +30,61 @@ 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';
7288
}
7389

7490
/**
@@ -79,15 +95,7 @@ public function is_enableable()
7995
*/
8096
public function enable_step($old_state)
8197
{
82-
if ($old_state === false)
83-
{
84-
$this->container->get('notification_manager')
85-
->enable_notifications('phpbb.ideas.notification.type.status');
86-
87-
return 'notification';
88-
}
89-
90-
return parent::enable_step($old_state);
98+
return $old_state === false ? $this->handle_notifications('enable_notifications') : parent::enable_step($old_state);
9199
}
92100

93101
/**
@@ -98,33 +106,17 @@ public function enable_step($old_state)
98106
*/
99107
public function disable_step($old_state)
100108
{
101-
if ($old_state === false)
102-
{
103-
$this->container->get('notification_manager')
104-
->disable_notifications('phpbb.ideas.notification.type.status');
105-
106-
return 'notification';
107-
}
108-
109-
return parent::disable_step($old_state);
109+
return $old_state === false ? $this->handle_notifications('disable_notifications') : parent::disable_step($old_state);
110110
}
111111

112112
/**
113113
* Purge notifications for the extension
114114
*
115-
* @param mixed $old_state
115+
* @param mixed $old_state
116116
* @return bool|string
117117
*/
118118
public function purge_step($old_state)
119119
{
120-
if ($old_state === false)
121-
{
122-
$this->container->get('notification_manager')
123-
->purge_notifications('phpbb.ideas.notification.type.status');
124-
125-
return 'notification';
126-
}
127-
128-
return parent::purge_step($old_state);
120+
return $old_state === false ? $this->handle_notifications('purge_notifications') : parent::purge_step($old_state);
129121
}
130122
}

factory/idea.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function set_status($idea_id, $status)
7373

7474
// Send a notification
7575
$method = $this->notification_exists($idea_id) ? 'update_notifications' : 'add_notifications';
76-
$this->notification_manager->$method('phpbb.ideas.notification.type.status', [
76+
$this->notification_manager->$method(ext::NOTIFICATION_TYPE_STATUS, [
7777
'idea_id' => (int) $idea_id,
7878
'status' => (int) $status,
7979
]);
@@ -275,7 +275,7 @@ public function delete($id, $topic_id = 0)
275275
$this->delete_idea_data($id, $this->table_votes);
276276

277277
// Delete notifications
278-
$this->notification_manager->delete_notifications('phpbb.ideas.notification.type.status', $id);
278+
$this->notification_manager->delete_notifications(ext::NOTIFICATION_TYPE_STATUS, $id);
279279
}
280280

281281
return $deleted;
@@ -465,7 +465,7 @@ protected function notification_exists($item_id)
465465
$sql = 'SELECT notification_id
466466
FROM ' . NOTIFICATIONS_TABLE . '
467467
WHERE item_id = ' . (int) $item_id . '
468-
AND notification_type_id = ' . $this->notification_manager->get_notification_type_id('phpbb.ideas.notification.type.status');
468+
AND notification_type_id = ' . $this->notification_manager->get_notification_type_id(ext::NOTIFICATION_TYPE_STATUS);
469469

470470
$result = $this->db->sql_query_limit($sql, 1);
471471
$row = $this->db->sql_fetchrow($result);

language/en/common.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
'IDEA_DELETED' => 'Idea successfully deleted.',
3939
'IDEA_LIST' => 'Idea List',
4040
'IDEA_NOT_FOUND' => 'Idea not found',
41-
'IDEA_STATUS_CHANGE' => 'Your idea <strong>“%1$s”</strong> has been changed to:',
41+
'IDEA_STATUS_CHANGE' => 'Your idea <strong>“%s”</strong> has been changed to:',
4242
'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>.',
4343
'IDEAS_TITLE' => 'phpBB Ideas',
4444
'IDEAS_NOT_AVAILABLE' => 'Ideas is not available at this time.',

notification/type/status.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function set_additional_services(config $config, helper $helper, idea $id
8484
*/
8585
public function get_type()
8686
{
87-
return 'phpbb.ideas.notification.type.status';
87+
return ext::NOTIFICATION_TYPE_STATUS;
8888
}
8989

9090
/**

tests/ext_test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private function setup_notification_manager(string $method): void
6969

7070
$this->notification_manager->expects($this->once())
7171
->method($method)
72-
->with('phpbb.ideas.notification.type.status');
72+
->with(ext::NOTIFICATION_TYPE_STATUS);
7373
}
7474

7575
public function test_is_enableable(): void

tests/ideas/idea_attributes_test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace phpbb\ideas\tests\ideas;
1212

13+
use phpbb\ideas\ext;
14+
1315
class idea_attributes_test extends ideas_base
1416
{
1517
/**
@@ -63,7 +65,7 @@ public function test_set_status($idea_id, $status)
6365
{
6466
$this->notification_manager->expects($this->once())
6567
->method('get_notification_type_id')
66-
->with('phpbb.ideas.notification.type.status')
68+
->with(ext::NOTIFICATION_TYPE_STATUS)
6769
->willReturn(1);
6870

6971
$object = $this->get_idea_object();

tests/notification/status_test.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace phpbb\ideas\tests\notification\type;
1212

13+
use phpbb\ideas\ext;
1314
use phpbb\ideas\notification\type\status;
1415

1516
class status_test extends \phpbb_test_case
@@ -89,7 +90,7 @@ protected function setNotificationData(array $data)
8990

9091
public function test_get_type()
9192
{
92-
$this->assertEquals('phpbb.ideas.notification.type.status', $this->notification_type->get_type());
93+
$this->assertEquals(ext::NOTIFICATION_TYPE_STATUS, $this->notification_type->get_type());
9394
}
9495

9596
public function test_is_available_with_permission()

0 commit comments

Comments
 (0)