]> ruin.nu Git - ndwebbie.git/commitdiff
Converted settings to catalyst
authorMichael Andreen <harv@ruin.nu>
Fri, 20 Jun 2008 14:55:05 +0000 (16:55 +0200)
committerMichael Andreen <harv@ruin.nu>
Fri, 20 Jun 2008 14:55:05 +0000 (16:55 +0200)
NDWeb/Pages/Settings.pm [deleted file]
lib/NDWeb/Controller/Settings.pm [new file with mode: 0644]
root/src/settings/index.tt2 [new file with mode: 0644]
t/controller_Settings.t [new file with mode: 0644]
templates/settings.tmpl [deleted file]

diff --git a/NDWeb/Pages/Settings.pm b/NDWeb/Pages/Settings.pm
deleted file mode 100644 (file)
index 962035e..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-#**************************************************************************
-#   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
-#                                                                         *
-#   This program is free software; you can redistribute it and/or modify  *
-#   it under the terms of the GNU General Public License as published by  *
-#   the Free Software Foundation; either version 2 of the License, or     *
-#   (at your option) any later version.                                   *
-#                                                                         *
-#   This program is distributed in the hope that it will be useful,       *
-#   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
-#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
-#   GNU General Public License for more details.                          *
-#                                                                         *
-#   You should have received a copy of the GNU General Public License     *
-#   along with this program; if not, write to the                         *
-#   Free Software Foundation, Inc.,                                       *
-#   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
-#**************************************************************************/
-
-package NDWeb::Pages::Settings;
-use strict;
-use warnings FATAL => 'all';
-use ND::Include;
-use CGI qw/:standard/;
-use NDWeb::Include;
-
-use base qw/NDWeb::XMLPage/;
-
-$NDWeb::Page::PAGES{settings} = __PACKAGE__;
-
-sub render_body {
-       my $self = shift;
-       my ($BODY) = @_;
-       $self->{TITLE} = 'Edit site preferences';
-       my $DBH = $self->{DBH};
-
-       if (defined param 'cmd'){
-               if(param('cmd') eq 'stylesheet'){
-                       my $query = $DBH->prepare(q{UPDATE users SET css = NULLIF($2,'Default') WHERE uid = $1});
-                       $query->execute($ND::UID,escapeHTML(param 'stylesheet')) or $ND::ERROR .= p $DBH->errstr;
-               }
-       }
-       if(param('oldpass') && param('pass')){
-               my $query = $DBH->prepare('UPDATE users SET password = MD5(?) WHERE password = MD5(?) AND uid = ?');
-               $query->execute(param('pass'),param('oldpass'),$ND::UID);
-       }
-       my ($css) = $DBH->selectrow_array(q{SELECT css FROM users WHERE uid = $1},undef,$ND::UID);
-       my @stylesheets = ({Style => 'Default'});
-       $css = '' unless defined $css;
-       while (<htdocs/stylesheets/*.css>){
-               if(m{stylesheets/(\w+)\.css}){
-                       push @stylesheets,{Style => $1, Selected => $1 eq $css ? 1 : 0};
-               }
-       }
-       $BODY->param(StyleSheets => \@stylesheets);
-       return $BODY;
-}
-
-1;
diff --git a/lib/NDWeb/Controller/Settings.pm b/lib/NDWeb/Controller/Settings.pm
new file mode 100644 (file)
index 0000000..79c66ed
--- /dev/null
@@ -0,0 +1,76 @@
+package NDWeb::Controller::Settings;
+
+use strict;
+use warnings;
+use parent 'Catalyst::Controller';
+
+use NDWeb::Include;
+
+=head1 NAME
+
+NDWeb::Controller::Settings - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index 
+
+=cut
+
+sub index :Path :Args(0) {
+       my ( $self, $c ) = @_;
+               
+       my @stylesheets = ('Default');
+       my $dir = $c->path_to('root/static/css/black.css')->dir;
+       while (my $file = $dir->next){
+               if(!$file->is_dir && $file->basename =~ m{^(\w+)\.css$}){
+                       push @stylesheets,$1;
+               }
+       }
+       $c->stash(stylesheets => \@stylesheets);
+
+}
+
+sub changeStylesheet : Local {
+       my ( $self, $c ) = @_;
+       my $dbh = $c->model;
+
+       my $query = $dbh->prepare(q{UPDATE users SET css = NULLIF($2,'Default')
+               WHERE uid = $1
+       });
+       $query->execute($c->user->id,html_escape $c->req->param('stylesheet'));
+
+       $c->res->redirect($c->uri_for(''));
+}
+
+
+sub changePassword : Local {
+       my ( $self, $c ) = @_;
+       my $dbh = $c->model;
+
+       my $query = $dbh->prepare(q{UPDATE users SET password = MD5($1)
+               WHERE password = MD5($2) AND uid = $3
+               });
+       $query->execute($c->req->param('pass'),$c->req->param('oldpass'),$c->user->id);
+
+       $c->res->redirect($c->uri_for(''));
+}
+
+
+=head1 AUTHOR
+
+Michael Andreen (harv@ruin.nu)
+
+=head1 LICENSE
+
+GPL 2.0, or later.
+
+=cut
+
+1;
diff --git a/root/src/settings/index.tt2 b/root/src/settings/index.tt2
new file mode 100644 (file)
index 0000000..62478b1
--- /dev/null
@@ -0,0 +1,20 @@
+[% META title = 'Site preferences' %]
+
+<form action="[% c.uri_for('changeStylesheet') %]" method="post"><fieldset> <legend>Style</legend>
+       Stylesheet: <select name="stylesheet">
+[% FOR style IN stylesheets %]
+               <option value="[% style %]" [% IF style == user.css %]selected[% END %]>[% style %]</option>
+[% END %]
+       </select>
+       <br>
+       <input type="submit" value="Change">
+</fieldset></form>
+<form action="[% c.uri_for('changePassword') %]" method="post"> <fieldset> <legend>Change password</legend>
+       <p>Old password: 
+       <input type="password" name="oldpass" value="">
+       New password: 
+       <input type="password" name="pass" value="">
+       <input type="submit" value="Submit">
+       </p>
+</fieldset>
+</form>
diff --git a/t/controller_Settings.t b/t/controller_Settings.t
new file mode 100644 (file)
index 0000000..add70bd
--- /dev/null
@@ -0,0 +1,10 @@
+use strict;
+use warnings;
+use Test::More tests => 3;
+
+BEGIN { use_ok 'Catalyst::Test', 'NDWeb' }
+BEGIN { use_ok 'NDWeb::Controller::Settings' }
+
+ok( request('/settings')->is_success, 'Request should succeed' );
+
+
diff --git a/templates/settings.tmpl b/templates/settings.tmpl
deleted file mode 100644 (file)
index b95ba5c..0000000
+++ /dev/null
@@ -1,21 +0,0 @@
-<form action="/<TMPL_VAR NAME=PAGE>" method="post"><fieldset> <legend>Style</legend>
-       <input type="hidden" name="page" value="<TMPL_VAR NAME=PAGE>"/>
-       <input type="hidden" name="cmd" value="stylesheet"/>
-       Stylesheet: <select name="stylesheet">
-                               <TMPL_LOOP StyleSheets>
-                                       <option value="<TMPL_VAR NAME=Style>" <TMPL_IF NAME=Selected>selected="selected"</TMPL_IF>><TMPL_VAR NAME=Style></option>
-                               </TMPL_LOOP>
-                               </select>
-       <br/><input type="submit" value="Change"/>
-</fieldset></form>
-<form action="/<TMPL_VAR NAME=PAGE>" method="post"> <fieldset> <legend>Change password</legend>
-       <input type="hidden" name="cmd" value="password"/>
-       <input type="hidden" name="page" value="<TMPL_VAR NAME=PAGE>"/>
-       <p>Old password: 
-       <input type="password" name="oldpass" value=""/>
-       New password: 
-       <input type="password" name="pass" value=""/>
-       <input type="submit" value="Submit"/>
-       </p>
-</fieldset>
-</form>