From: Michael Andreen
Date: Wed, 31 Dec 2008 14:22:03 +0000 (+0100)
Subject: Allow users to store their time zone
X-Git-Url: https://ruin.nu/git/?p=ndwebbie.git;a=commitdiff_plain;h=eb3bda37d084c0e6b13c97f88f06d5563c40d2fb
Allow users to store their time zone
---
diff --git a/database/timezone.sql b/database/timezone.sql
new file mode 100644
index 0000000..b196e9b
--- /dev/null
+++ b/database/timezone.sql
@@ -0,0 +1 @@
+ALTER TABLE users ADD COLUMN timezone TEXT NOT NULL DEFAULT 'GMT';
diff --git a/lib/NDWeb/Controller/JSRPC.pm b/lib/NDWeb/Controller/JSRPC.pm
index 5515a2a..3bea69a 100644
--- a/lib/NDWeb/Controller/JSRPC.pm
+++ b/lib/NDWeb/Controller/JSRPC.pm
@@ -4,6 +4,8 @@ use strict;
use warnings;
use parent 'Catalyst::Controller';
+use DateTime::TimeZone;
+
=head1 NAME
NDWeb::Controller::JSRPC - Catalyst Controller
@@ -187,6 +189,13 @@ sub listTargets : Local {
$c->forward('/listTargets');
}
+sub tzcountries : Local {
+ my ($self, $c, $cat) = @_;
+
+ my @countries = DateTime::TimeZone->names_in_category($cat);
+ $c->stash(tzcountries => \@countries);
+}
+
sub access_denied : Private {
my ($self, $c) = @_;
$c->stash(template => 'jsrpc/access_denied.tt2');
diff --git a/lib/NDWeb/Controller/Settings.pm b/lib/NDWeb/Controller/Settings.pm
index 9277c7f..d6743fb 100644
--- a/lib/NDWeb/Controller/Settings.pm
+++ b/lib/NDWeb/Controller/Settings.pm
@@ -6,6 +6,8 @@ use parent 'Catalyst::Controller';
use NDWeb::Include;
+use DateTime::TimeZone;
+
=head1 NAME
NDWeb::Controller::Settings - Catalyst Controller
@@ -37,10 +39,21 @@ sub index :Path :Args(0) {
}
}
$c->stash(stylesheets => \@stylesheets);
- $c->stash(birthday => $dbh->selectrow_array(q{
- SELECT birthday FROM users WHERE uid = $1
- },undef,$c->user->id)
- );
+
+ my ($birthday,$timezone) = $dbh->selectrow_array(q{
+SELECT birthday,timezone FROM users WHERE uid = $1
+ },undef,$c->user->id);
+ $c->stash(birthday => $birthday);
+
+ my @timezone = split m{/},$timezone,2;
+ $c->stash(timezone => \@timezone);
+
+ my @cat = DateTime::TimeZone->categories;
+ unshift @cat, 'GMT';
+ $c->stash(tzcategories => \@cat);
+
+ my @countries = DateTime::TimeZone->names_in_category($timezone[0]);
+ $c->stash(tzcountries => \@countries);
}
sub changeStylesheet : Local {
@@ -75,6 +88,22 @@ sub changeBirthday : Local {
$c->res->redirect($c->uri_for(''));
}
+sub changeTimezone : Local {
+ my ( $self, $c ) = @_;
+ my $dbh = $c->model;
+
+ my $timezone = $c->req->param('category');
+ $timezone .= '/' . $c->req->param('country') if $c->req->param('country');
+ my $query = $dbh->prepare(q{UPDATE users SET timezone = $2 WHERE uid = $1});
+ eval{
+ $dbh->selectrow_array(q{SELECT NOW() AT TIME ZONE $1},undef,$timezone);
+ $query->execute($c->user->id,$timezone );
+ };
+ if ($@){
+ $c->flash(error => $@);
+ }
+ $c->res->redirect($c->uri_for(''));
+}
sub changePassword : Local {
my ( $self, $c ) = @_;
diff --git a/root/src/jsrpc/tzcountries.tt2 b/root/src/jsrpc/tzcountries.tt2
new file mode 100644
index 0000000..aeffb0f
--- /dev/null
+++ b/root/src/jsrpc/tzcountries.tt2
@@ -0,0 +1,6 @@
+
+
+[% FOR country IN tzcountries %]
+ [% country | html %]
+[% END %]
+
diff --git a/root/src/settings/index.tt2 b/root/src/settings/index.tt2
index d52509a..6291e33 100644
--- a/root/src/settings/index.tt2
+++ b/root/src/settings/index.tt2
@@ -23,6 +23,20 @@
+
[%- site.stylesheets = ['ui/ui.datepicker.css'] %]
@@ -33,7 +47,15 @@ $(document).ready(function(){
yearRange: "-80:+0",
dateFormat: $.datepicker.ATOM,
firstDay: 1,
- showOn: "both",
+ showOn: "both"
+ });
+ $("#tzcategory").change(function(){
+ $.get("/jsrpc/tzcountries/" + $(this).val(),function(xml){
+ $("#tzcountry").empty();
+ $("country",xml).each(function(i){
+ $("#tzcountry").append('');
+ });
+ });
});
});