Commit 6674f619 authored by travis%sedsystems.ca's avatar travis%sedsystems.ca

Bug 98123 : Create a user preferences infrastructure (became 'General Settings')

Patch by Shane H. W. Travis <travis@sedsystems.ca> r=jouni, mkanat a=myk
parent 9ffc6eb5
......@@ -913,6 +913,55 @@ use constant ABSTRACT_SCHEMA => {
],
},
# SETTINGS
# --------
# setting - each global setting will have exactly one entry
# in this table.
# setting_value - stores the list of acceptable values for each
# setting, and a sort index that controls the order
# in which the values are displayed.
# profile_setting - If a user has chosen to use a value other than the
# global default for a given setting, it will be
# stored in this table. Note: even if a setting is
# later changed so is_enabled = false, the stored
# value will remain in case it is ever enabled again.
#
setting => {
FIELDS => [
name => {TYPE => 'varchar(32)', NOTNULL => 1,
PRIMARYKEY => 1},
default_value => {TYPE => 'varchar(32)', NOTNULL => 1},
is_enabled => {TYPE => 'BOOLEAN', NOTNULL => 1,
DEFAULT => 'TRUE'},
],
},
setting_value => {
FIELDS => [
name => {TYPE => 'varchar(32)', NOTNULL => 1},
value => {TYPE => 'varchar(32)', NOTNULL => 1},
sortindex => {TYPE => 'INT2', NOTNULL => 1},
],
INDEXES => [
setting_value_nv_unique_idx => {FIELDS => [qw(name value)],
TYPE => 'UNIQUE'},
setting_value_ns_unique_idx => {FIELDS => [qw(name sortindex)],
TYPE => 'UNIQUE'},
],
},
profile_setting => {
FIELDS => [
user_id => {TYPE => 'INT3', NOTNULL => 1},
setting_name => {TYPE => 'varchar(32)', NOTNULL => 1},
setting_value => {TYPE => 'varchar(32)', NOTNULL => 1},
],
INDEXES => [
profile_setting_value_unique_idx => {FIELDS => [qw(user_id setting_name)],
TYPE => 'UNIQUE'},
],
},
};
#--------------------------------------------------------------------------
......
......@@ -22,6 +22,7 @@
# Bradley Baetz <bbaetz@acm.org>
# Joel Peshkin <bugreport@peshkin.net>
# Byron Jones <bugzilla@glob.com.au>
# Shane H. W. Travis <travis@sedsystems.ca>
# Max Kanat-Alexander <mkanat@kerio.com>
################################################################################
......@@ -38,6 +39,7 @@ use Bugzilla::Config;
use Bugzilla::Error;
use Bugzilla::Util;
use Bugzilla::Constants;
use Bugzilla::User::Setting;
use Bugzilla::Auth;
use base qw(Exporter);
......@@ -224,6 +226,23 @@ sub queries {
return $self->{queries};
}
sub settings {
my ($self) = @_;
return $self->{'settings'} if (defined $self->{'settings'});
# IF the user is logged in
# THEN get the user's settings
# ELSE get default settings
if ($self->id) {
$self->{'settings'} = get_all_settings($self->id);
} else {
$self->{'settings'} = get_defaults();
}
return $self->{'settings'};
}
sub flush_queries_cache {
my $self = shift;
......@@ -1148,6 +1167,20 @@ linkinfooter - Whether or not the query should be displayed in the footer.
Returns the disable text of the user, if any.
=item C<settings>
Returns a hash of hashes which holds the user's settings. The first key is
the name of the setting, as found in setting.name. The second key is one of:
is_enabled - true if the user is allowed to set the preference themselves;
false to force the site defaults
for themselves or must accept the global site default value
default_value - the global site default for this setting
value - the value of this setting for this user. Will be the same
as the default_value if the user is not logged in, or if
is_default is true.
is_default - a boolean to indicate whether the user has chosen to make
a preference for themself or use the site default.
=item C<flush_queries_cache>
Some code modifies the set of stored queries. Because C<Bugzilla::User> does
......
......@@ -1479,6 +1479,10 @@ if ($^O !~ /MSWin32/i) {
require Bugzilla::Auth;
import Bugzilla::Auth 'bz_crypt';
# This is done so we can add new settings as developers need them.
require Bugzilla::User::Setting;
import Bugzilla::User::Setting qw(add_setting);
# globals.pl clears the PATH, but File::Find uses Cwd::cwd() instead of
# Cwd::getcwd(), which we need to do because `pwd` isn't in the path - see
# http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2001-09/msg00115.html
......
#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Shane H. W. Travis <travis@sedsystems.ca>
#
use strict;
use lib qw(.);
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::User::Setting;
require "CGI.pl";
# Use global template variables.
use vars qw($template $vars);
###############################
### Subroutine Definitions ###
###############################
sub LoadSettings {
$vars->{'settings'} = Bugzilla::User::Setting::get_defaults();
my @setting_list = keys %{$vars->{'settings'}};
$vars->{'setting_names'} = \@setting_list;
}
sub SaveSettings{
my $cgi = Bugzilla->cgi;
$vars->{'settings'} = Bugzilla::User::Setting::get_defaults();
my @setting_list = keys %{$vars->{'settings'}};
foreach my $name (@setting_list) {
my $changed = 0;
my $old_enabled = $vars->{'settings'}->{$name}->{'is_enabled'};
my $old_value = $vars->{'settings'}->{$name}->{'default_value'};
my $enabled = defined $cgi->param("${name}-enabled") || 0;
my $value = $cgi->param("${name}");
# remove taint
if ($value =~ /^(\w+)$/ ) {
$value = $1;
}
if ( ($old_enabled != $enabled) ||
($old_value ne $value) ) {
Bugzilla::User::Setting::set_default($name, $value, $enabled);
}
}
}
###################
### Live code ###
###################
Bugzilla->login(LOGIN_REQUIRED);
my $cgi = Bugzilla->cgi;
print $cgi->header;
UserInGroup("tweakparams")
|| ThrowUserError("auth_failure", {group => "tweakparams",
action => "modify",
object => "settings"});
my $action = trim($cgi->param('action') || 'load');
if ($action eq 'update') {
SaveSettings();
$vars->{'changes_saved'} = 1;
$template->process("admin/settings/updated.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
if ($action eq 'load') {
LoadSettings();
$template->process("admin/settings/edit.html.tmpl", $vars)
|| ThrowTemplateError($template->error());
exit;
}
#
# No valid action found
#
ThrowUserError('no_valid_action', {'field' => "settings"});
......@@ -52,6 +52,8 @@
[% tabs = [ { name => "account", description => "Account settings",
saveable => "1" },
{ name => "settings", description => "General Settings",
saveable => "1" },
{ name => "email", description => "Email settings",
saveable => "1" },
{ name => "saved-searches", description => "Saved searches",
......
[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Shane H. W. Travis <travis@sedsystems.ca>
#
#%]
[%# INTERFACE:
# setting_names: an array of strings
# settings: a hash of hashes, keyed by setting_name.
# Each hash contains:
# is_enabled - boolean
# default_value - string (global default for this setting)
# value - string (user-defined preference)
# is_default - boolean (true if user has no preference)
#%]
[% PROCESS "global/setting-descs.none.tmpl" %]
[% IF settings.size %]
<table border="0" cellpadding="8">
<tr>
[% FOREACH name = setting_names %]
[% IF settings.${name}.is_enabled %]
[% default_name = name _ '-isdefault' %]
[% default_val = settings.${name}.default_value %]
<tr>
<td align="right">
[% setting_descs.$name OR name FILTER html %]
</td>
<td>
<select name="[% name %]" id="[% name %]">
<option value="[% default_name %]"
[% ' selected="selected"' IF settings.${name}.is_default %]>
Site Default ([% setting_descs.${default_val} OR default_val FILTER html %])
</option>
[% FOREACH x = settings.${name}.legal_values %]
<option value="[% x FILTER html %]"
[% ' selected="selected"'
IF x == settings.${name}.value
AND NOT settings.${name}.is_default %]>
[% setting_descs.${x} OR x FILTER html %]
</option>
[% END %]
</select>
</td>
</tr>
[% END %]
[% END %]
</table>
[% END %]
<br>
[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Shane H. W. Travis <travis@sedsystems.ca>
#
#%]
[%# INTERFACE:
# setting_names: an array of strings
# settings: a hash of hashes, keyed by setting_name.
# Each hash contains:
# is_enabled - boolean
# default_value - string (global default for this setting)
# value - string (user-defined preference)
# is_default - boolean (true if user has no preference)
#%]
[% PROCESS global/header.html.tmpl
title = "Edit Global Settings"
%]
[% PROCESS "global/setting-descs.none.tmpl" %]
<p>
This lets you edit the global settings values.
</p>
<p>
The Default Value displayed for each setting will apply to all users who
do not choose their own value, and to anyone who is not logged in.
</p>
<p>
The 'Enabled' checkbox controls whether or not this setting is available
to users.<br>
If it is checked, users will see this setting on their User Preferences page,
and will be allowed to choose their own value if they desire.<br>
If it is not checked, this setting will not apppear on the User Preference
page, and the Default Value will automatically apply to everyone.
</p>
<hr>
[% IF settings.size %]
<form name="adminsettingform" method="post" action="editsettings.cgi">
<table border="1" cellpadding="4">
<tr>
<th>Setting Text</th>
<th>Default Value</th>
<th>Enabled</th>
<tr>
[% FOREACH name = setting_names %]
[% checkbox_name = name _ '-enabled' %]
<tr>
<td align="right">
[% setting_descs.$name OR name FILTER html %]
</td>
<td>
<select name="[% name %]" id="[% name %]">
[% FOREACH x = settings.${name}.legal_values %]
<option value="[% x FILTER html %]"
[% " selected=\"selected\"" IF x == settings.${name}.default_value %]>
[% setting_descs.${x} OR x FILTER html %]
</option>
[% END %]
</select>
</td>
<td align="center">
<input type="checkbox"
name="[% checkbox_name %]"
id="[% checkbox_name %]"
[% " checked=\"checked\"" IF settings.${name}.is_enabled %]>
<br>
</td>
</tr>
[% END %]
</table>
<input type="hidden" name="action" value="update">
<table>
<tr>
<td width="150"></td>
<td>
<input type="submit" value="Submit Changes">
</td>
</tr>
</table>
</form>
[% ELSE %]
There are no settings to edit.
[% END %]
[% PROCESS global/footer.html.tmpl %]
[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Shane H. W. Travis <travis@sedsystems.ca>
#
#%]
[% PROCESS global/header.html.tmpl
title = "Settings Updated"
%]
Your changes to the Global Settings have been saved.<br>
<br>
Return to the <a
href="editsettings.cgi?action=load">Global Settings</a> page.
[% PROCESS global/footer.html.tmpl %]
......@@ -634,6 +634,11 @@
'deleted_bug_count'
],
'admin/settings/edit.html.tmpl' => [
'name',
'checkbox_name'
],
'account/login.html.tmpl' => [
'target',
],
......@@ -656,4 +661,9 @@
'current_tab.description',
],
'account/prefs/settings.html.tmpl' => [
'name',
'default_name'
],
);
......@@ -216,6 +216,15 @@
[%+ ELSIF fld == "z" %]the multiple tables/images
[%+ ELSE %]a report axis[% END %] field.
[% ELSIF error == "setting_info_invalid" %]
To create a new setting, you must supply a setting name, a list of
value/sortindex pairs, and the devault value.
[% ELSIF error == "setting_name_invalid" %]
The setting name <em>[% name FILTER html %]</em> is not a valid
option. Setting names must begin with a letter, and contain only
letters, digits, or the symbols '_', '-', '.', or ':'.
[% ELSIF error == "token_generation_error" %]
Something is seriously wrong with the token generation system.
......
......@@ -59,6 +59,8 @@
"reporter" => "Reporter",
"reporter_accessible" => "Reporter accessible?",
"resolution" => "Resolution",
"setting" => "Setting",
"settings" => "Settings",
"short_desc" => "Summary",
"status_whiteboard" => "Whiteboard",
"target_milestone" => "Target Milestone",
......
[%# 1.0@bugzilla.org %]
[%# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
#
# Contributor(s): Shane H. W. Travis <travis@sedsystems.ca>
#
#%]
[%# Remember to PROCESS rather than INCLUDE this template. %]
[% PROCESS global/variables.none.tmpl %]
[% setting_descs = {
"off" => "Off",
"on" => "On"
}
%]
......@@ -74,7 +74,8 @@
<div class="links">
<a href="userprefs.cgi">Prefs</a>
[% ' | <a href="editparams.cgi">Parameters</a>'
[% ' | <a href="editparams.cgi">Parameters</a> ' _
' | <a href="editsettings.cgi">User Settings</a>'
IF user.groups.tweakparams %]
[% ' | <a href="editusers.cgi">Users</a>' IF user.groups.editusers
|| user.can_bless %]
......
......@@ -20,6 +20,7 @@
# Christopher Aillon <christopher@aillon.com>
# Gervase Markham <gerv@gerv.net>
# Vlad Dascalu <jocuri@softhome.net>
# Shane H. W. Travis <travis@sedsystems.ca>
use strict;
......@@ -139,6 +140,39 @@ sub SaveAccount {
}
sub DoSettings {
$vars->{'settings'} = Bugzilla->user->settings;
my @setting_list = keys %{Bugzilla->user->settings};
$vars->{'setting_names'} = \@setting_list;
}
sub SaveSettings {
my $cgi = Bugzilla->cgi;
my $settings = Bugzilla->user->settings;
my @setting_list = keys %{Bugzilla->user->settings};
foreach my $name (@setting_list) {
next if ! ($settings->{$name}->{'is_enabled'});
my $value = $cgi->param($name);
# de-taint the value.
if ($value =~ /^([-\w]+)$/ ) {
$value = $1;
}
if ($value eq "${name}-isdefault" ) {
if (! $settings->{$name}->{'is_default'}) {
$settings->{$name}->reset_to_default;
}
}
else {
$settings->{$name}->set($value);
}
}
$vars->{'settings'} = Bugzilla->user->settings(1);
}
sub DoEmail {
my $dbh = Bugzilla->dbh;
......@@ -367,6 +401,11 @@ SWITCH: for ($current_tab_name) {
DoAccount();
last SWITCH;
};
/^settings$/ && do {
SaveSettings() if $cgi->param('dosave');
DoSettings();
last SWITCH;
};
/^email$/ && do {
SaveEmail() if $cgi->param('dosave');
DoEmail();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment