]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Settings.pm
Allow users to store their time zone
[ndwebbie.git] / lib / NDWeb / Controller / Settings.pm
1 package NDWeb::Controller::Settings;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use NDWeb::Include;
8
9 use DateTime::TimeZone;
10
11 =head1 NAME
12
13 NDWeb::Controller::Settings - Catalyst Controller
14
15 =head1 DESCRIPTION
16
17 Catalyst Controller.
18
19 =head1 METHODS
20
21 =cut
22
23
24 =head2 index 
25
26 =cut
27
28 sub index :Path :Args(0) {
29         my ( $self, $c ) = @_;
30         my $dbh = $c->model;
31
32         $c->stash(error => $c->flash->{error});
33
34         my @stylesheets = ('Default');
35         my $dir = $c->path_to('root/static/css/black.css')->dir;
36         while (my $file = $dir->next){
37                 if(!$file->is_dir && $file->basename =~ m{^(\w+)\.css$}){
38                         push @stylesheets,$1;
39                 }
40         }
41         $c->stash(stylesheets => \@stylesheets);
42
43         my ($birthday,$timezone) = $dbh->selectrow_array(q{
44 SELECT birthday,timezone FROM users WHERE uid = $1
45                 },undef,$c->user->id);
46         $c->stash(birthday => $birthday);
47
48         my @timezone = split m{/},$timezone,2;
49         $c->stash(timezone => \@timezone);
50
51         my @cat = DateTime::TimeZone->categories;
52         unshift @cat, 'GMT';
53         $c->stash(tzcategories => \@cat);
54
55         my @countries = DateTime::TimeZone->names_in_category($timezone[0]);
56         $c->stash(tzcountries => \@countries);
57 }
58
59 sub changeStylesheet : Local {
60         my ( $self, $c ) = @_;
61         my $dbh = $c->model;
62
63         my $query = $dbh->prepare(q{UPDATE users SET css = NULLIF($2,'Default')
64                 WHERE uid = $1
65         });
66         $query->execute($c->user->id,html_escape $c->req->param('stylesheet'));
67
68         $c->res->redirect($c->uri_for(''));
69 }
70
71 sub changeBirthday : Local {
72         my ( $self, $c ) = @_;
73         my $dbh = $c->model;
74
75         my $query = $dbh->prepare(q{UPDATE users SET birthday = NULLIF($2,'')::date
76                 WHERE uid = $1
77                 });
78         eval{
79                 $query->execute($c->user->id,html_escape $c->req->param('birthday'));
80         };
81         if ($@){
82                 if ($@ =~ /invalid input syntax for type date/){
83                         $c->flash(error => 'Bad syntax for day, use YYYY-MM-DD.');
84                 }else{
85                         $c->flash(error => $@);
86                 }
87         }
88         $c->res->redirect($c->uri_for(''));
89 }
90
91 sub changeTimezone : Local {
92         my ( $self, $c ) = @_;
93         my $dbh = $c->model;
94
95         my $timezone = $c->req->param('category');
96         $timezone .= '/' . $c->req->param('country') if $c->req->param('country');
97         my $query = $dbh->prepare(q{UPDATE users SET timezone = $2 WHERE uid = $1});
98         eval{
99                 $dbh->selectrow_array(q{SELECT NOW() AT TIME ZONE $1},undef,$timezone);
100                 $query->execute($c->user->id,$timezone );
101         };
102         if ($@){
103                 $c->flash(error => $@);
104         }
105         $c->res->redirect($c->uri_for(''));
106 }
107
108 sub changePassword : Local {
109         my ( $self, $c ) = @_;
110         my $dbh = $c->model;
111
112         my $query = $dbh->prepare(q{UPDATE users SET password = MD5($1)
113                 WHERE password = MD5($2) AND uid = $3
114                 });
115         $query->execute($c->req->param('pass'),$c->req->param('oldpass'),$c->user->id);
116
117         $c->res->redirect($c->uri_for(''));
118 }
119
120
121 =head1 AUTHOR
122
123 Michael Andreen (harv@ruin.nu)
124
125 =head1 LICENSE
126
127 GPL 2.0, or later.
128
129 =cut
130
131 1;