Skip to content

Commit

Permalink
Use a class constant for index only value of -1
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Friedman <[email protected]>
  • Loading branch information
iMattPro committed Jun 26, 2024
1 parent d8b55cf commit 4564be4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion adm/style/settings_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ <h1>{{ lang('BOARD_ANNOUNCEMENTS_SETTINGS') }}</h1>
{% if ba.LOCATIONS is empty %}
{{ lang('BOARD_ANNOUNCEMENTS_EVERYWHERE') }}
{% else %}
{% set has_index = -1 in ba.LOCATIONS %}
{% set has_index = constant('\\phpbb\\boardannouncements\\ext::INDEX_ONLY') in ba.LOCATIONS %}
{% set has_forums = ba.LOCATIONS|filter(v => v > 0)|length > 0 %}
{% if has_index and has_forums %}
{{ lang('BOARD_ANNOUNCEMENTS_INDEX_PAGE') }}, {{ lang('BOARD_ANNOUNCEMENTS_FORUMS') }}
Expand Down
11 changes: 8 additions & 3 deletions controller/acp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ protected function log_change($msg, $params)
/**
* Get an array of available locations for the announcement
*
* @param string $selected
* @param string $locations
* @return array
*/
protected function get_location_options($locations)
Expand All @@ -459,8 +459,13 @@ protected function get_location_options($locations)

$forum_list = make_forum_select($selected, false, false, false, false, false, true);

// Add the index page
$forum_list[0] = array_merge(['padding' => '', 'selected' => in_array(-1, $selected)], ['forum_id' => -1, 'forum_name' => $this->language->lang('BOARD_ANNOUNCEMENTS_INDEX_PAGE')]);
// Add the index page to the list
$forum_list[0] = [
'padding' => '',
'selected' => in_array(ext::INDEX_ONLY, $selected),
'forum_id' => ext::INDEX_ONLY,
'forum_name' => $this->language->lang('BOARD_ANNOUNCEMENTS_INDEX_PAGE')
];

ksort($forum_list);

Expand Down
3 changes: 2 additions & 1 deletion event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace phpbb\boardannouncements\event;

use phpbb\boardannouncements\ext;
use phpbb\boardannouncements\manager\manager;
use phpbb\config\config;
use phpbb\controller\helper;
Expand Down Expand Up @@ -109,7 +110,7 @@ public function display_board_announcements()
$board_announcements_data = array_filter($board_announcements_data, function ($data) {
$locations = json_decode($data['announcement_locations'], true);
$is_index = $this->user->page['page_name'] === "index.$this->php_ext";
$current_page = $is_index ? -1 : $this->request->variable('f', 0);
$current_page = $is_index ? ext::INDEX_ONLY : $this->request->variable('f', 0);

// Check if announcement has locations specified, and user is at that location
if (!empty($locations) && !in_array($current_page, $locations))
Expand Down
1 change: 1 addition & 0 deletions ext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
class ext extends \phpbb\extension\base
{
public const INDEX_ONLY = -1;
public const ALL = 0;
public const MEMBERS = 1;
public const GUESTS = 2;
Expand Down
2 changes: 1 addition & 1 deletion language/en/boardannouncements_acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
'BOARD_ANNOUNCEMENTS_TH_EXPIRED' => 'Expired',

'BOARD_ANNOUNCEMENTS_EVERYWHERE' => 'Everywhere',
'BOARD_ANNOUNCEMENTS_INDEX_PAGE' => 'Index Page',
'BOARD_ANNOUNCEMENTS_INDEX_PAGE' => 'Board Index',
'BOARD_ANNOUNCEMENTS_FORUMS' => 'Selected Forums',

'BOARD_ANNOUNCEMENTS_EMPTY' => 'There are no board announcements to display',
Expand Down
3 changes: 2 additions & 1 deletion migrations/v10x/m13_locations_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace phpbb\boardannouncements\migrations\v10x;

use phpbb\boardannouncements\ext;
use phpbb\boardannouncements\manager\nestedset;

/**
Expand Down Expand Up @@ -69,7 +70,7 @@ public function copy_locations()
while ($row = $this->db->sql_fetchrow($result))
{
$this->get_nestedset()->update_item($row['announcement_id'], [
'announcement_locations' => ($row['announcement_indexonly'] ? json_encode([-1]) : '')
'announcement_locations' => ($row['announcement_indexonly'] ? json_encode([ext::INDEX_ONLY]) : '')
]);
}

Expand Down
8 changes: 5 additions & 3 deletions tests/functional/announcement_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace phpbb\boardannouncements\tests\functional;

use phpbb\boardannouncements\ext;

/**
* @group functional
*/
Expand Down Expand Up @@ -66,7 +68,7 @@ public function test_set_acp_settings()
'board_announcements_enabled' => true,
'board_announcements_users' => 0,
'board_announcements_dismiss' => true,
'board_announcements_locations' => [-1],
'board_announcements_locations' => [ext::INDEX_ONLY],
'board_announcements_bgcolor' => 'ff0000',
'board_announcements_description' => 'Test announcement',
'board_announcements_text' => 'This is a board announcement test.',
Expand Down Expand Up @@ -196,7 +198,7 @@ public function test_locations()

// show index only
$index_id = $this->create_announcement([
'board_announcements_locations' => [-1],
'board_announcements_locations' => [ext::INDEX_ONLY],
'board_announcements_description' => 'Board index announcement',
'board_announcements_text' => 'Board index announcement',
]);
Expand All @@ -210,7 +212,7 @@ public function test_locations()

// show in forum 2 and index
$index_forum_id = $this->create_announcement([
'board_announcements_locations' => [-1,2],
'board_announcements_locations' => [ext::INDEX_ONLY, 2],
'board_announcements_description' => 'Forum and Index announcement',
'board_announcements_text' => 'Forum and Index announcement',
]);
Expand Down

0 comments on commit 4564be4

Please sign in to comment.