Skip to content

Commit

Permalink
feat: global polls feature switch
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Mar 7, 2024
1 parent 43627e9 commit c856a9a
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
->default('fof-polls.maxOptions', 10)
->default('fof-polls.optionsColorBlend', true)
->default('fof-polls.directory-default-sort', 'default')
->default('fof-polls.enableGlobalPolls', false)
->serializeToForum('globalPollsEnabled', 'fof-polls.enableGlobalPolls', 'boolval')
->serializeToForum('allowPollOptionImage', 'fof-polls.allowOptionImage', 'boolval')
->serializeToForum('pollMaxOptions', 'fof-polls.maxOptions', 'intval')
->registerLessConfigVar('fof-polls-options-color-blend', 'fof-polls.optionsColorBlend', function ($value) {
Expand Down
6 changes: 6 additions & 0 deletions js/src/admin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ app.initializers.add('fof/polls', () => {
label: app.translator.trans('fof-polls.admin.settings.max_options'),
min: 2,
})
.registerSetting({
setting: 'fof-polls.enableGlobalPolls',
type: 'boolean',
label: app.translator.trans('fof-polls.admin.settings.enable_global_polls'),
help: app.translator.trans('fof-polls.admin.settings.enable_global_polls_help'),
})
.registerPermission(
{
icon: 'fas fa-poll',
Expand Down
4 changes: 4 additions & 0 deletions js/src/forum/addNavItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import LinkButton from 'flarum/common/components/LinkButton';

export default function addNavItem() {
extend(IndexPage.prototype, 'navItems', (items) => {
if (!app.forum.attribute<boolean>('globalPollsEnabled')) {
return;
}

items.add(
'fof-polls-showcase',
LinkButton.component(
Expand Down
5 changes: 5 additions & 0 deletions js/src/forum/components/ComposePollPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export default class ComposePollPage extends Page {
oninit(vnode: Mithril.Vnode) {
super.oninit(vnode);

if (!app.forum.attribute<boolean>('globalPollsEnabled')) {
m.route.set('/');
return;
}

// Get the `edit` parameter from the URL
const editId = m.route.param('id');
if (editId) {
Expand Down
5 changes: 5 additions & 0 deletions js/src/forum/components/PollViewPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export default class PollViewPage extends AbstractPollPage {
oninit(vnode: Mithril.Vnode) {
super.oninit(vnode);

if (!app.forum.attribute<boolean>('globalPollsEnabled')) {
m.route.set('/');
return;
}

const editId = m.route.param('id');
this.poll = app.store.getById<PollModel>('poll', editId);

Expand Down
5 changes: 5 additions & 0 deletions js/src/forum/components/PollsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export default class PollsPage extends AbstractPollPage {
oninit(vnode: Mithril.Vnode) {
super.oninit(vnode);

if (!app.forum.attribute<boolean>('globalPollsEnabled')) {
m.route.set('/');
return;
}

this.state = new PollListState({
sort: m.route.param('sort'),
filter: m.route.param('filter'),
Expand Down
5 changes: 5 additions & 0 deletions js/src/forum/components/PollsShowcasePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ export default class PollsShowcasePage extends AbstractPollPage {
oninit(vnode: Mithril.Vnode<IPageAttrs, PollListState>) {
super.oninit(vnode);

if (!app.forum.attribute<boolean>('globalPollsEnabled')) {
m.route.set('/');
return;
}

this.state = new PollListState({
sort: m.route.param('sort'),
filter: m.route.param('filter'),
Expand Down
2 changes: 2 additions & 0 deletions resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ fof-polls:
admin:
settings:
allow_option_image: Allow an image URL to be provided for each poll option
enable_global_polls: Enable global polls
enable_global_polls_help: Global polls are not tied to a specific post, and may be accessed from a dedicated polls page.
max_options: Maximum number of options per poll
options_color_blend: Color blend text in poll options
options_color_blend_help: Use this to use color mixing to make the poll options more readable. Disable if this feature causes issues with your forum's appearance, reducing readability.
Expand Down
5 changes: 5 additions & 0 deletions src/Commands/CreatePollHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Carbon\Carbon;
use Flarum\Post\PostRepository;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Exception\PermissionDeniedException;
use FoF\Polls\Events\PollWasCreated;
use FoF\Polls\Events\SavingPollAttributes;
use FoF\Polls\Poll;
Expand Down Expand Up @@ -64,6 +65,10 @@ public function handle(CreatePoll $command)
if ($command->post) {
$command->actor->assertCan('startPoll', $command->post);
} else {
if (!$this->settings->get('fof-polls.enableGlobalPolls')) {
throw new PermissionDeniedException('Global polls are not enabled');
}

$command->actor->assertCan('startGlobalPoll');
}

Expand Down

0 comments on commit c856a9a

Please sign in to comment.