1 | package BookmarkApp; |
---|
2 | use strict; |
---|
3 | use warnings; |
---|
4 | use base qw{CGI::Application}; |
---|
5 | |
---|
6 | use CGI::Application::Plugin::TT; |
---|
7 | |
---|
8 | use DBI; |
---|
9 | use Encode; |
---|
10 | use JSON; |
---|
11 | use Bookmarks; |
---|
12 | |
---|
13 | use URI; |
---|
14 | my $base_uri = URI->new; |
---|
15 | $base_uri->scheme('http'); |
---|
16 | $base_uri->host($ENV{SERVER_NAME}); |
---|
17 | $base_uri->port($ENV{SERVER_PORT}); |
---|
18 | $base_uri->path($ENV{SCRIPT_NAME} . '/'); |
---|
19 | |
---|
20 | my $dbname = 'new.db'; |
---|
21 | my $dbh = DBI->connect("dbi:SQLite:dbname=$dbname", "", "", { RaiseError => 1 }); |
---|
22 | my $bookmarks = Bookmarks->new({ |
---|
23 | dbh => $dbh, |
---|
24 | base_uri => $base_uri->canonical, |
---|
25 | }); |
---|
26 | |
---|
27 | sub setup { |
---|
28 | my $self = shift; |
---|
29 | $self->mode_param(path_info => 1); |
---|
30 | $self->run_modes([qw{ |
---|
31 | list |
---|
32 | view |
---|
33 | edit |
---|
34 | }]); |
---|
35 | } |
---|
36 | |
---|
37 | sub list { |
---|
38 | my $self = shift; |
---|
39 | my $q = $self->query; |
---|
40 | |
---|
41 | # check for a uri param, and if there is one present, |
---|
42 | # see if a bookmark for that URI already exists |
---|
43 | if (defined(my $uri = $q->param('uri'))) { |
---|
44 | my $bookmark = $bookmarks->get_bookmark({ uri => $uri }); |
---|
45 | if ($bookmark) { |
---|
46 | # redirect to the view of the existing bookmark |
---|
47 | $self->header_type('redirect'); |
---|
48 | $self->header_props( |
---|
49 | -uri => $q->url . '/' . $bookmark->{id}, |
---|
50 | ); |
---|
51 | return; |
---|
52 | } else { |
---|
53 | # bookmark was not found; show the form to create a new bookmark |
---|
54 | $bookmark->{uri} = $uri; |
---|
55 | $bookmark->{title} = $q->param('title'); |
---|
56 | $self->header_props( |
---|
57 | -type => 'text/html', |
---|
58 | -charset => 'UTF-8', |
---|
59 | -status => 404, |
---|
60 | ); |
---|
61 | return $self->tt_process( |
---|
62 | 'bookmark.tt', |
---|
63 | $bookmark, |
---|
64 | ); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | # list all the bookmarks |
---|
69 | my $format = $q->param('format') || 'html'; |
---|
70 | my $tag = $q->param('tag'); |
---|
71 | my @resources = $bookmarks->get_resources({ tag => $tag }); |
---|
72 | my @all_tags = $bookmarks->get_tags({ selected => $tag }); |
---|
73 | my @cotags = $bookmarks->get_cotags({ tag => $tag }); |
---|
74 | |
---|
75 | if ($format eq 'json') { |
---|
76 | $self->header_props( |
---|
77 | -type => 'application/json', |
---|
78 | -charset => 'UTF-8', |
---|
79 | ); |
---|
80 | return decode_utf8( |
---|
81 | encode_json({ |
---|
82 | bookmarks => \@resources, |
---|
83 | }) |
---|
84 | ); |
---|
85 | } else { |
---|
86 | $self->header_props( |
---|
87 | -type => 'text/html', |
---|
88 | -charset => 'UTF-8', |
---|
89 | ); |
---|
90 | |
---|
91 | # set the base URL, adding a trailing slash if needed |
---|
92 | my $base_url = $q->url; |
---|
93 | $base_url .= '/' if $base_url =~ m{/bookmarks$}; |
---|
94 | |
---|
95 | return $self->tt_process( |
---|
96 | 'list.tt', |
---|
97 | { |
---|
98 | base_url => $base_url, |
---|
99 | selected_tag => $tag, |
---|
100 | tags => \@all_tags, |
---|
101 | cotags => \@cotags, |
---|
102 | resources => \@resources, |
---|
103 | }, |
---|
104 | ); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | sub view { |
---|
109 | my $self = shift; |
---|
110 | my $id = $self->param('id'); |
---|
111 | my $field = $self->param('field'); |
---|
112 | my $format = $self->query->param('format') || 'html'; |
---|
113 | |
---|
114 | my $bookmark = $bookmarks->get_bookmark({ id => $id }); |
---|
115 | if ($bookmark) { |
---|
116 | if ($field) { |
---|
117 | # respond with just the requested field as plain text |
---|
118 | $self->header_props( |
---|
119 | -type => 'text/plain', |
---|
120 | -charset => 'UTF-8', |
---|
121 | ); |
---|
122 | my $value = $bookmark->{$field}; |
---|
123 | return ref $value eq 'ARRAY' ? join(',', @{ $value }) : $value; |
---|
124 | } else { |
---|
125 | if ($format eq 'json') { |
---|
126 | $self->header_props( |
---|
127 | -type => 'application/json', |
---|
128 | -charset => 'UTF-8', |
---|
129 | ); |
---|
130 | return decode_utf8(encode_json($bookmark)); |
---|
131 | } else { |
---|
132 | # display the bookmark form for this bookmark |
---|
133 | $bookmark->{exists} = 1; |
---|
134 | $bookmark->{created} = "Created " . localtime($bookmark->{ctime}); |
---|
135 | $bookmark->{created} .= '; Updated ' . localtime($bookmark->{mtime}) unless $bookmark->{ctime} == $bookmark->{mtime}; |
---|
136 | $self->header_props( |
---|
137 | -type => 'text/html', |
---|
138 | -charset => 'UTF-8', |
---|
139 | ); |
---|
140 | return $self->tt_process( |
---|
141 | 'bookmark.tt', |
---|
142 | $bookmark, |
---|
143 | ); |
---|
144 | } |
---|
145 | } |
---|
146 | } else { |
---|
147 | $self->header_props( |
---|
148 | -type => 'text/html', |
---|
149 | -charset => 'UTF-8', |
---|
150 | -status => 404, |
---|
151 | ); |
---|
152 | return "Bookmark $id Not Found"; |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | #TODO: split this into edit and create methods |
---|
157 | sub edit { |
---|
158 | my $self = shift; |
---|
159 | my $q = $self->query; |
---|
160 | #TODO: get the bookmark based on the id and edit it directly? |
---|
161 | #TODO: deal with changing URIs |
---|
162 | my $uri = $q->param('uri'); |
---|
163 | my $title = $q->param('title'); |
---|
164 | my @tags = split ' ', $q->param('tags'); |
---|
165 | $bookmarks->add({ |
---|
166 | uri => $uri, |
---|
167 | title => $title, |
---|
168 | tags => \@tags, |
---|
169 | }); |
---|
170 | |
---|
171 | =begin |
---|
172 | |
---|
173 | my $location = URI->new($q->url); |
---|
174 | $location->query_form(uri => $uri) if defined $q->url_param('uri'); |
---|
175 | $location->fragment('updated'); |
---|
176 | |
---|
177 | =cut |
---|
178 | |
---|
179 | # return to the form |
---|
180 | $self->header_type('redirect'); |
---|
181 | $self->header_props( |
---|
182 | -uri => $ENV{REQUEST_URI}, |
---|
183 | -status => 303, |
---|
184 | ); |
---|
185 | } |
---|
186 | |
---|
187 | 1; |
---|