App-ZofCMS

 view release on metacpan or  search on metacpan

lib/App/ZofCMS/Plugin/BoolSettingsManager.pm  view on Meta::CPAN

sub get_settings {
    my $self = shift;
    my $conf = $self->{CONF};
    my $dbh  = $self->dbh;

    my $user = ($dbh->selectall_arrayref(
        'SELECT * FROM `' . $conf->{table}
            . '` WHERE `' . $conf->{login_col} . '` = ?',
        { Slice => {} },
        $conf->{login},
    ) || [])->[0] || {};

    my @settings;
    my $key;
    for ( 0 .. $#{ $conf->{settings} } ) {
        if ( $_ % 2 == 0 ) {
            $key = $conf->{settings}[$_];
        }
        else {
            push @settings, {
                id         => $key,
                value      => $conf->{settings}[$_],
                is_checked => ( $user->{$key} ? 1 : 0 ),
            };
        }
    }

    return \@settings;
}

sub dbh {
    my $self = shift;

    return $self->{DBH}
        if $self->{DBH};

    $self->{DBH} = DBI->connect_cached(
        @{ $self->{CONF} }{ qw/dsn user pass opt/ },
    );

    return $self->{DBH};
}

sub has_value {
    my $v = shift;

    return 1
        if defined $v and length $v;

    return 0;
}

sub form_html_template {
    return <<'END_HTML';
<tmpl_if name='save_success'>
    <p class="success-message">Successfully saved</p>
</tmpl_if>
<form action="" method="POST" id="plug_bool_settings_manager_form">
<div>
    <input type="hidden" name="page"
        value="<tmpl_var escape='html' name='page'>">
    <input type="hidden" name="pbsm_save_settings" value="1">
    <ul>
    <tmpl_loop name='settings'>
        <li id="pbsm_container_<tmpl_var escape='html' name='id'>">
            <input type="checkbox"
                id="pbsm_<tmpl_var escape='html' name='id'>"
                name="<tmpl_var escape='html' name='id'>"
                <tmpl_if name='is_checked'>checked</tmpl_if>
                ><label for="pbsm_<tmpl_var escape='html' name='id'>"
                    class="checkbox_label"> <tmpl_var escape='html' name='value'></label>
        </li>
    </tmpl_loop>
    </ul>

    <tmpl_var name="submit_button">
</div>
</form>
END_HTML
}

1;
__END__

=encoding utf8

=head1 NAME

App::ZofCMS::Plugin::BoolSettingsManager - Plugin to let individual users manage boolean settings

=head1 SYNOPSIS

In L<HTML::Template> template:

    <tmpl_var name='plug_bool_settings_manager_form'>

In ZofCMS Template:

    plugins => [
        qw/BoolSettingsManager/,
    ],

    plug_bool_settings_manager => {
        settings => [
            notice_forum         => q|new forum posts|,
            notice_flyers        => q|new flyer uploads|,
            notice_photo_library => q|new images added to Photo Library|,
        ],

        # everything below is optional; default values are shown
        dsn           => "DBI:mysql:database=test;host=localhost",
        user          => '',
        pass          => undef,
        opt           => { RaiseError => 1, AutoCommit => 1 },
        table         => 'users',
        login_col     => 'login',
        login         => sub { $_[0]->{d}{user}{login} },
        submit_button => q|<input type="submit" class="input_submit"|
                            . q| value="Save">|,
    },

=head1 DESCRIPTION

The module is a plugin for L<App::ZofCMS> that provides means to present
a user a form with a number of checkboxes that control boolean settings,
which are stored in a SQL database.

This documentation assumes you've read L<App::ZofCMS>,
L<App::ZofCMS::Config> and L<App::ZofCMS::Template>.

=head1 FIRST-LEVEL ZofCMS TEMPLATE AND MAIN CONFIG FILE KEYS



( run in 0.613 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )