root/Mixxi/trunk/lib/Mixxi/Controller/URL.pm

Revision 2070 (checked in by miyagawa, 13 years ago)

don't allow empty default.

Line 
1 package Mixxi::Controller::URL;
2
3 use strict;
4 use warnings;
5 use base 'Catalyst::Controller';
6
7 =head1 NAME
8
9 Mixxi::Controller::URL - Catalyst Controller
10
11 =head1 DESCRIPTION
12
13 Catalyst Controller.
14
15 =head1 METHODS
16
17 =cut
18
19
20 =head2 index
21
22 =cut
23
24 sub index : Private {
25     my ( $self, $c ) = @_;
26
27 }
28
29 sub create : Local {
30     my($self, $c) = @_;
31
32     my $url   = $c->req->param('url');
33     my $alias = $c->req->param('alias');
34
35     if ($alias && !$self->validate_alias($c, $alias)) {
36         $c->stash->{error} = 'alias is taken or invalid';
37         $c->stash->{template} = 'url/index.tt';
38         return;
39     }
40
41     my $rec = $c->model('DBIC::Url')->create({
42         url   => $url,
43         alias => $alias || undef,
44     });
45
46     $c->stash->{url} = $rec;
47 }
48
49 sub validate : Local {
50     my($self, $c) = @_;
51
52     my $alias = $c->req->param('alias');
53     if ($alias && !$self->validate_alias($c, $alias)) {
54         $c->stash->{json_data}->{error} = 'alias is taken or invalid';
55     } else {
56         $c->stash->{json_data}->{error} = 0;
57     }
58
59     $c->forward($c->view('JSON'));
60 }
61
62 my %reserved_alias = map { $_ => 1 } qw( url id );
63
64 sub validate_alias {
65     my($self, $c, $alias) = @_;
66     $alias =~ /^[a-zA-Z0-9\-\_]{2,16}$/ or return;
67     $reserved_alias{$alias} and return;
68
69     my $rs = $c->model('DBIC::Url')->search(alias => $alias);
70     $rs->count and return;
71
72     return 1;
73 }
74
75 =head1 AUTHOR
76
77 Tatsuhiko Miyagawa
78
79 =head1 LICENSE
80
81 This library is free software, you can redistribute it and/or modify
82 it under the same terms as Perl itself.
83
84 =cut
85
86 1;
Note: See TracBrowser for help on using the browser.