]> ruin.nu Git - ndwebbie.git/commitdiff
Allow users to store their time zone
authorMichael Andreen <harv@ruin.nu>
Wed, 31 Dec 2008 14:22:03 +0000 (15:22 +0100)
committerMichael Andreen <harv@ruin.nu>
Wed, 31 Dec 2008 14:22:03 +0000 (15:22 +0100)
database/timezone.sql [new file with mode: 0644]
lib/NDWeb/Controller/JSRPC.pm
lib/NDWeb/Controller/Settings.pm
root/src/jsrpc/tzcountries.tt2 [new file with mode: 0644]
root/src/settings/index.tt2

diff --git a/database/timezone.sql b/database/timezone.sql
new file mode 100644 (file)
index 0000000..b196e9b
--- /dev/null
@@ -0,0 +1 @@
+ALTER TABLE users ADD COLUMN timezone TEXT NOT NULL DEFAULT 'GMT';
index 5515a2aecfa824fec3c5e4703b8855ce1be477f8..3bea69a5405e5c14983503767410d1eaf2a28ea0 100644 (file)
@@ -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');
index 9277c7fadc2d2519cc82465981d165693907642b..d6743fb1f6afebcc985d0ac201f775cd8574a07c 100644 (file)
@@ -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 (file)
index 0000000..aeffb0f
--- /dev/null
@@ -0,0 +1,6 @@
+
+<countries>
+[% FOR country IN tzcountries %]
+       <country>[% country | html %]</country>
+[% END %]
+</countries>
index d52509a1f49466f07d8496a15e7d558c15ae8157..6291e33cddcf3684b067b4da5f943167687e9a85 100644 (file)
        </p>
 </fieldset>
 </form>
+<form action="[% c.uri_for('changeTimezone') %]" method="post"><fieldset> <legend>Timezone</legend>
+       <select name="category" id="tzcategory">
+[% FOR cat IN tzcategories %]
+               <option value="[% cat %]" [% IF cat == timezone.0 %]selected[% END %]>[% cat %]</option>
+[% END %]
+       </select>
+       <select name="country" id="tzcountry">
+[% FOR country IN tzcountries %]
+               <option value="[% country %]" [% IF country == timezone.1 %]selected[% END %]>[% country %]</option>
+[% END %]
+       </select>
+       <br><input type="submit" value="Change">
+       <p>Remember that GMT stays the same all year, so if you're in the UK you should pick Europe/London.</p>
+</fieldset></form>
 [%- site.stylesheets = ['ui/ui.datepicker.css'] %]
 <script type="text/javascript" src="/static/ui/ui.core.min.js"></script>
 <script type="text/javascript" src="/static/ui/ui.datepicker.min.js"></script>
@@ -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('<option value="'+$(this).text()+'">'+$(this).text()+'</option>');
+                       });
+               });
        });
 });
 </script>