Skip to content

Commit

Permalink
Merge pull request #149 from iMattPro/locations
Browse files Browse the repository at this point in the history
Allow selecting forums in addition to Index and Everywhere
  • Loading branch information
iMattPro authored Jun 28, 2024
2 parents ea14af2 + 03d6d32 commit 07a637b
Show file tree
Hide file tree
Showing 19 changed files with 564 additions and 58 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ jobs:
db: "mysql:5.7"
- php: '8.3'
db: "mysql:5.7"
- php: '8.4'
db: "mysql:5.7"

name: PHP ${{ matrix.php }} - ${{ matrix.db_alias != '' && matrix.db_alias || matrix.db }}

Expand Down Expand Up @@ -270,6 +272,8 @@ jobs:
db: "postgres:14"
- php: '8.3'
db: "postgres:14"
- php: '8.4'
db: "postgres:14"

name: PHP ${{ matrix.php }} - ${{ matrix.db }}

Expand Down
20 changes: 13 additions & 7 deletions adm/style/settings_add_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ <h3>{{ lang('WARNING') }}</h3>
<label><input type="radio" class="radio" name="board_announcements_enabled" value="0"{% if not BOARD_ANNOUNCEMENTS_ENABLED %} checked="checked"{% endif %}> {{ lang('NO') }}</label>
</dd>
</dl>
<dl>
<dt><label for="board_announcements_index_only">{{ lang('BOARD_ANNOUNCEMENTS_INDEX_ONLY') ~ lang('COLON') }}</label></dt>
<dd>
<label><input type="radio" class="radio" id="board_announcements_index_only" name="board_announcements_index_only" value="1"{% if BOARD_ANNOUNCEMENTS_INDEX_ONLY %} checked="checked"{% endif %}> {{ lang('YES') }}</label>
<label><input type="radio" class="radio" name="board_announcements_index_only" value="0"{% if not BOARD_ANNOUNCEMENTS_INDEX_ONLY %} checked="checked"{% endif %}> {{ lang('NO') }}</label>
</dd>
</dl>
<dl>
<dt><label for="board_announcements_dismiss">{{ lang('BOARD_ANNOUNCEMENTS_DISMISS') ~ lang('COLON') }}</label></dt>
<dd>
Expand All @@ -50,6 +43,19 @@ <h3>{{ lang('WARNING') }}</h3>
<select name="board_announcements_users" id="board_announcements_users">{{ S_BOARD_ANNOUNCEMENTS_USERS }}</select>
</dd>
</dl>
<dl>
<dt><label for="board_announcements_locations">{{ lang('BOARD_ANNOUNCEMENTS_LOCATIONS') ~ lang('COLON') }}</label><br><span>{{ lang('BOARD_ANNOUNCEMENTS_LOCATIONS_EXPLAIN') }}</span></dt>
<dd>
<select id="board_announcements_locations" name="board_announcements_locations[]" size="8" multiple="multiple">
{% for locations in BOARD_ANNOUNCEMENTS_LOCATIONS %}
<option value="{{ locations.forum_id }}"{% if locations.selected %} selected="selected"{% endif %}>
{{ locations.padding ~ locations.forum_name}}
</option>
{% endfor %}
</select>
</dd>
</dl>

</fieldset>

{% if BOARD_ANNOUNCEMENTS_PREVIEW %}
Expand Down
16 changes: 15 additions & 1 deletion adm/style/settings_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ <h1>{{ lang('BOARD_ANNOUNCEMENTS_SETTINGS') }}</h1>
{% for ba in announcements %}
<tr>
<td>{{ ba.DESCRIPTION }}</td>
<td>{{ ba.INDEX_ONLY ? lang('INDEX') : lang('BOARD_ANNOUNCEMENTS_EVERYWHERE') }}</td>
<td>
{% if ba.LOCATIONS is empty %}
{{ lang('BOARD_ANNOUNCEMENTS_EVERYWHERE') }}
{% else %}
{% 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') }}
{% elseif has_index %}
{{ lang('BOARD_ANNOUNCEMENTS_INDEX_PAGE') }}
{% elseif has_forums %}
{{ lang('BOARD_ANNOUNCEMENTS_FORUMS') }}
{% endif %}
{% endif %}
</td>
<td>{% if ba.USERS == constant('\\phpbb\\boardannouncements\\ext::ALL') %}
{{ lang('BOARD_ANNOUNCEMENTS_EVERYONE') }}
{% elseif ba.USERS == constant('\\phpbb\\boardannouncements\\ext::MEMBERS') %}
Expand Down
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
class: phpbb\boardannouncements\event\listener
arguments:
- '@phpbb.boardannouncements.manager'
- '@auth'
- '@config'
- '@controller.helper'
- '@language'
Expand Down
35 changes: 32 additions & 3 deletions controller/acp_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,12 @@ protected function list_announcements()

$this->template->assign_block_vars('announcements' , [
'DESCRIPTION' => $row['announcement_description'],
'INDEX_ONLY' => $row['announcement_indexonly'],
'USERS' => $row['announcement_users'],
'CREATED_DATE' => $row['announcement_timestamp'],
'EXPIRY_DATE' => $row['announcement_expiry'],
'S_EXPIRED' => $expired,
'S_ENABLED' => $enabled,
'LOCATIONS' => $this->manager->decode_json($row['announcement_locations']),
'U_EDIT' => $this->u_action . '&amp;action=add&amp;id=' . $row['announcement_id'],
'U_DELETE' => $this->u_action . '&amp;action=delete&amp;id=' . $row['announcement_id'],
'U_MOVE_UP' => $this->u_action . '&amp;action=move&amp;id=' . $row['announcement_id'] . '&amp;dir=up&amp;hash=' . generate_link_hash('up' . $row['announcement_id']),
Expand Down Expand Up @@ -206,7 +206,7 @@ protected function action_add()
$data['announcement_bgcolor'] = $this->request->variable('board_announcements_bgcolor', '', true);
$data['announcement_enabled'] = $this->request->variable('board_announcements_enabled', true);
$data['announcement_users'] = $this->request->variable('board_announcements_users', ext::ALL);
$data['announcement_indexonly'] = $this->request->variable('board_announcements_index_only', false);
$data['announcement_locations'] = $this->request->variable('board_announcements_locations', [0]);
$data['announcement_dismissable'] = $this->request->variable('board_announcements_dismiss', true);
$data['announcement_expiry'] = $this->request->variable('board_announcements_expiry', '');

Expand All @@ -229,6 +229,9 @@ protected function action_add()
$data['announcement_expiry'] = 0;
}

// Locations array should be json encoded for storage in the DB
$data['announcement_locations'] = json_encode($data['announcement_locations']);

// Prepare announcement text for storage
generate_text_for_storage(
$data['announcement_text'],
Expand Down Expand Up @@ -269,14 +272,15 @@ protected function action_add()
$this->template->assign_vars([
'ERRORS' => implode('<br>', $errors),
'BOARD_ANNOUNCEMENTS_ENABLED' => $data['announcement_enabled'],
'BOARD_ANNOUNCEMENTS_INDEX_ONLY'=> $data['announcement_indexonly'],
'BOARD_ANNOUNCEMENTS_DISMISS' => $data['announcement_dismissable'],
'BOARD_ANNOUNCEMENTS_DESC' => $data['announcement_description'],
'BOARD_ANNOUNCEMENTS_TEXT' => $announcement_text_edit['text'],
'BOARD_ANNOUNCEMENTS_PREVIEW' => $announcement_text_preview,
'BOARD_ANNOUNCEMENTS_EXPIRY' => $data['announcement_expiry'] ? $this->user->format_date($data['announcement_expiry'], ext::DATE_FORMAT) : '',
'BOARD_ANNOUNCEMENTS_BGCOLOR' => $data['announcement_bgcolor'],

'BOARD_ANNOUNCEMENTS_LOCATIONS' => $this->get_location_options($data['announcement_locations']),

'S_BOARD_ANNOUNCEMENTS_USERS' => build_select([
ext::ALL => 'BOARD_ANNOUNCEMENTS_EVERYONE',
ext::MEMBERS => 'G_REGISTERED',
Expand Down Expand Up @@ -443,4 +447,29 @@ protected function log_change($msg, $params)
{
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $msg, time(), [$params]);
}

/**
* Get an array of available locations for the announcement
*
* @param string $locations
* @return array
*/
protected function get_location_options($locations)
{
$selected = !empty($locations) ? $this->manager->decode_json($locations) : [];

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

// Add the index page to the list
$forum_list[ext::INDEX_ONLY] = [
'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);

return $forum_list;
}
}
89 changes: 83 additions & 6 deletions event/listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace phpbb\boardannouncements\event;

use phpbb\auth\auth;
use phpbb\boardannouncements\ext;
use phpbb\boardannouncements\manager\manager;
use phpbb\config\config;
use phpbb\controller\helper;
Expand All @@ -27,6 +29,9 @@ class listener implements EventSubscriberInterface
/** @var manager $manager */
protected $manager;

/** @var auth $auth */
protected $auth;

/** @var config $config */
protected $config;

Expand All @@ -48,10 +53,20 @@ class listener implements EventSubscriberInterface
/** @var string $php_ext */
protected $php_ext;

/** @var array $protected_forums*/
protected $protected_forums;

/** @var int $location */
protected $location;

/** @var bool|mixed $permission */
protected $permission;

/**
* Constructor
*
* @param manager $manager
* @param manager $manager Board announcements manager object
* @param auth $auth Auth object
* @param config $config Config object
* @param helper $controller_helper Controller helper object
* @param language $language Language object
Expand All @@ -61,9 +76,10 @@ class listener implements EventSubscriberInterface
* @param string $php_ext PHP extension
* @access public
*/
public function __construct(manager $manager, config $config, helper $controller_helper, language $language, request $request, template $template, user $user, $php_ext)
public function __construct(manager $manager, auth $auth, config $config, helper $controller_helper, language $language, request $request, template $template, user $user, $php_ext)
{
$this->manager = $manager;
$this->auth = $auth;
$this->config = $config;
$this->controller_helper = $controller_helper;
$this->language = $language;
Expand Down Expand Up @@ -108,14 +124,19 @@ public function display_board_announcements()

foreach ($board_announcements_data as $data)
{
// Do not continue if announcements are only displayed on the board index, and the user is not currently viewing the board index
if ($data['announcement_indexonly'] && $this->user->page['page_name'] !== "index.$this->php_ext")
$locations = $this->manager->decode_json($data['announcement_locations']);

// Do not include announcement if user is in a location where it shouldn't be visible
if (!empty($locations) && ($this->location_not_in($locations) || $this->is_protected() || $this->no_permission()))
{
continue;
}

// Do not continue if announcement has been dismissed
if ($this->request->variable($this->config['cookie_name'] . '_ba_' . $data['announcement_id'], '', true, \phpbb\request\request_interface::COOKIE) == $data['announcement_timestamp'])
$cookie_name = $this->config['cookie_name'] . '_ba_' . $data['announcement_id'];
$announcement_dismissed = $this->request->variable($cookie_name, '', true, \phpbb\request\request_interface::COOKIE) == $data['announcement_timestamp'];

// Do not include announcement if it has been dismissed
if ($announcement_dismissed)
{
continue;
}
Expand All @@ -133,4 +154,60 @@ public function display_board_announcements()
]);
}
}

/**
* Get the current location, board index or a forum_id
*
* @return int
*/
protected function get_current_location()
{
if (!isset($this->location))
{
$this->location = $this->user->page['page_name'] === "index.$this->php_ext" ? ext::INDEX_ONLY : $this->request->variable('f', 0);
}

return $this->location;
}

/**
* Is the current location not in the announcement's array of allowed locations?
*
* @param array|string $locations An array of locations
* @return bool
*/
protected function location_not_in($locations)
{
return !in_array($this->get_current_location(), $locations);
}

/**
* Is the current page a password protected forum?
*
* @return bool
*/
protected function is_protected()
{
if (!isset($this->protected_forums))
{
$this->protected_forums = $this->user->get_passworded_forums();
}

return $this->get_current_location() > 0 && !empty($this->protected_forums) && in_array($this->get_current_location(), $this->protected_forums);
}

/**
* Is the current page a forum not accessible to the current user?
*
* @return bool
*/
protected function no_permission()
{
if (!isset($this->permission))
{
$this->permission = $this->auth->acl_get('f_read', $this->get_current_location());
}

return $this->get_current_location() > 0 && !$this->permission;
}
}
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
17 changes: 10 additions & 7 deletions language/en/boardannouncements_acp.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,23 @@

'BOARD_ANNOUNCEMENTS_ENABLE_ALL' => 'Enable board announcements',

'BOARD_ANNOUNCEMENTS_OPTIONS' => 'Announcement options',
'BOARD_ANNOUNCEMENTS_OPTIONS' => 'Board announcement options',

'BOARD_ANNOUNCEMENTS_DESC' => 'Description',
'BOARD_ANNOUNCEMENTS_DESC_EXPLAIN' => 'A short description for this announcement. This will only be visible here in the ACP to help identify this announcement.',

'BOARD_ANNOUNCEMENTS_ENABLE' => 'Display this board announcement',
'BOARD_ANNOUNCEMENTS_INDEX_ONLY' => 'Display on board index only',
'BOARD_ANNOUNCEMENTS_USERS' => 'Who can view this board announcement',
'BOARD_ANNOUNCEMENTS_DISMISS' => 'Allow users to dismiss this board announcement',
'BOARD_ANNOUNCEMENTS_ENABLE' => 'Display this announcement',
'BOARD_ANNOUNCEMENTS_USERS' => 'Who can view this announcement',
'BOARD_ANNOUNCEMENTS_DISMISS' => 'Allow users to dismiss this announcement',
'BOARD_ANNOUNCEMENTS_LOCATIONS' => 'Limit where this announcement should be displayed',
'BOARD_ANNOUNCEMENTS_LOCATIONS_EXPLAIN' => 'Select one or more locations to display the announcement. To display it everywhere, leave the selection empty. Use Command (Mac) or Control (Windows) click to select multiple locations.',

'BOARD_ANNOUNCEMENTS_EVERYONE' => 'Everyone',

'BOARD_ANNOUNCEMENTS_BGCOLOR' => 'Board announcement background color',
'BOARD_ANNOUNCEMENTS_BGCOLOR' => 'Background color',
'BOARD_ANNOUNCEMENTS_BGCOLOR_EXPLAIN' => 'You can change the background color of the announcement using a hex code (e.g: FFFF80). Leave this field blank to use the default color.',

'BOARD_ANNOUNCEMENTS_EXPIRY' => 'Board announcement expiration date',
'BOARD_ANNOUNCEMENTS_EXPIRY' => 'Expiration date',
'BOARD_ANNOUNCEMENTS_EXPIRY_EXPLAIN' => 'Set the date the announcement will expire and become disabled. Leave this field blank if you do not want the announcement to expire.',
'BOARD_ANNOUNCEMENTS_EXPIRY_INVALID' => 'The expiration date was invalid or has already expired.',

Expand All @@ -77,6 +78,8 @@
'BOARD_ANNOUNCEMENTS_TH_EXPIRED' => 'Expired',

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

'BOARD_ANNOUNCEMENTS_EMPTY' => 'There are no board announcements to display',
'BOARD_ANNOUNCEMENTS_ADD' => 'Create Announcement',
Expand Down
14 changes: 13 additions & 1 deletion manager/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,18 @@ public function close_announcement($id, $user_id)
return (bool) $this->nestedset->insert_tracked_item($id, ['user_id' => (int) $user_id]);
}

/**
* Decode JSON data
*
* @param string $data
* @return mixed decoded json data or original data if not valid json
*/
public function decode_json($data)
{
$result = json_decode($data, true);
return json_last_error() === JSON_ERROR_NONE ? $result : $data;
}

/**
* Filter members only announcements
*
Expand Down Expand Up @@ -245,7 +257,7 @@ public function announcement_columns()
'announcement_description' => '',
'announcement_bgcolor' => '',
'announcement_enabled' => true,
'announcement_indexonly' => false,
'announcement_locations' => '',
'announcement_dismissable' => true,
'announcement_users' => \phpbb\boardannouncements\ext::ALL,
'announcement_timestamp' => 0,
Expand Down
Loading

0 comments on commit 07a637b

Please sign in to comment.