3 use App::IdiotBox::Common qw(@SupportedFormats);
4 use Web::Simple __PACKAGE__;
7 use HTML::Zoom::FilterBuilder::Template;
8 use List::Util qw(first);
10 use App::IdiotBox::Announcement;
11 use App::IdiotBox::Bucket;
12 use App::IdiotBox::Video;
14 has $_ => (is => 'ro') for qw(recent_announcements buckets);
17 template_dir => 'share/html',
19 db_file => 'var/lib/idiotbox.db',
20 base_url => 'http://localhost:3000/',
21 base_dir => do { use FindBin; $FindBin::Bin },
27 ($store = $self->config->{store}) =~ /^(\w+)$/
28 or die "Store config should be just a name, got ${store} instead";
29 my $store_class = "App::IdiotBox::Store::${store}";
30 eval "require ${store_class}; 1"
31 or die "Couldn't load ${store} store: $@";
32 $store_class->bind($self);
35 sub dispatch_request {
37 sub (/) { $self->show_front_page },
41 sub (%new_name=&new_slug=) {
42 my ($self, $name, $slug) = @_;
44 my ($nb, $err) = App::IdiotBox::Bucket->create(
49 return $self->show_admin_page(message => $err) if $err;
51 my $nb = $self->buckets->add($nb);
53 $self->show_admin_page(message => "New bucket created");
56 sub (/admin/) { $self->show_admin_page },
58 sub (/admin/bucket/*/...) {
59 my $bucket = $self->buckets->get({ slug => $_[1] });
63 my $video = $bucket->videos->get({ slug => $_[1] });
65 sub (POST + %edit_video=&*) {
66 my ($self, undef, $args) = @_;
67 delete $args->{edit_video};
69 my ($uv, $err) = $video->update(%$args);
71 return $self->show_edit_video_page($video, message => $err) if $err;
73 $self->videos->replace($video, $uv);
75 $self->show_edit_video_page($video, message => "Video updated");
78 sub (/) { $self->show_edit_video_page($video); }
81 sub (POST + %edit_bucket=&*) {
82 my ($self, $new_name) = @_;
84 my ($ub, $err) = $bucket->update(
88 return $self->show_admin_page(message => $err) if $err;
90 $self->buckets->replace($bucket, $ub);
92 $self->show_bucket_edited_page($bucket);
95 $self->show_edit_bucket_page($bucket);
98 $self->show_confirm_delete_bucket_page($bucket)
101 $self->buckets->remove({ slug => $bucket->slug });
102 $self->show_bucket_deleted_page($bucket->slug);
108 my $bucket = $self->buckets->get({ slug => $_[1] });
110 $self->show_bucket($bucket)
113 $self->show_video($bucket->videos->get({ slug => $_[1] }));
118 sub show_front_page {
120 my $ann = $self->recent_announcements;
121 $self->html_response(
123 $_->select('#announcement-list')
124 ->repeat_content($ann->map(sub {
127 $_->select('.bucket-name')->replace_content($obj->bucket->name)
128 ->select('.made-at')->replace_content($obj->made_at)
129 ->select('.bucket-link')->set_attribute(
130 'href' => $obj->bucket->slug.'/'
132 ->select('.new-videos')->replace_content($obj->video_count)
133 ->select('.total-videos')->replace_content(
134 $obj->bucket->video_count
142 sub show_admin_page {
145 my $message = $opts{message} || '';
147 my $bucket = $self->buckets;
148 $self->html_response(
150 $_->select('#bucket-list')
151 ->repeat_content($bucket->map(sub {
154 $_->select('.bucket-slug')->replace_content($obj->slug)
155 ->select('.bucket-name')->replace_content($obj->name)
156 ->select('.edit-link')->set_attribute(
157 'href' => 'bucket/'.$obj->slug.'/'
159 ->select('.delete-link')->set_attribute(
160 'href' => 'bucket/'.$obj->slug.'/delete/'
164 ->select('.message-text')->replace_content($message)
171 sub show_confirm_delete_bucket_page {
172 my ($self, $bucket) = @_;
173 $self->html_response('delete_bucket' => sub {
174 $_->select('.bucket-name')->replace_content($bucket->name)
175 ->select('.confirm-yes')->set_attribute(
181 sub show_edit_bucket_page {
182 my ($self, $bucket, %opt) = @_;
183 my $message = $opt{message} || '';
184 $self->html_response('edit_bucket' => sub {
185 $_->select('.bucket-name')->replace_content($bucket->name)
186 ->select('.bucket-slug')->replace_content($bucket->slug)
187 ->select('.message-text')->replace_content($message)
188 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
191 $_->select('.video-name')->replace_content($video->name)
192 ->select('.video-author')->replace_content($video->author)
193 ->select('.video-link')->set_attribute(
194 href => 'video/'.$video->slug.'/'
202 sub show_edit_video_page {
203 my ($self, $video, %opt) = @_;
204 my $message = $opt{message} || '';
205 $self->html_response('edit_video' => sub {
206 $_->select('.video-name')->set_attribute(value => $video->name)
207 ->select('.author-name')->set_attribute(value => $video->author)
208 ->select('.bucket-slug')->replace_content($video->bucket->slug)
209 ->select('.slug-name')->replace_content($video->slug)
210 ->select('.message-text')->replace_content($message)
214 sub show_bucket_deleted_page {
215 my ($self, $name) = @_;
216 $self->html_response('deleted_bucket' => sub {
217 $_->select('.bucket-name')->replace_content($name)
221 sub show_bucket_edited_page {
222 my ($self, $name) = @_;
223 $self->html_response('edited_bucket' => sub {
224 $_->select('.bucket-name')->replace_content($name)
229 my ($self, $bucket) = @_;
230 $self->html_response(bucket => sub {
231 $_->select('.bucket-name')->replace_content($bucket->name)
232 ->select('#video-list')->repeat_content($bucket->videos->map(sub {
235 $_->select('.video-name')->replace_content($video->name)
236 ->select('.video-author')->replace_content($video->author)
237 ->select('.video-link')->set_attribute(
238 href => $video->slug.'/'
246 my ($self, $video) = @_;
247 my $video_file = first {
248 -e join('/', $self->config->{base_dir}, $_)
250 join('/', $video->bucket->slug, $video->slug, $video->file_name.".$_")
252 $self->html_response(video => sub {
255 .($video_file||'NO FILE FOUND SORRY');
257 $_->select('.video-name')->replace_content($video->name)
258 ->select('.author-name')->replace_content($video->author)
259 ->select('.bucket-link')->set_attribute(
262 ->select('.bucket-name')->replace_content($video->bucket->name)
263 ->select('.video-details')->replace_content($video->details)
264 ->select('script')->template_text_raw({ video_url => $video_url });
269 my ($self, $template_name, $selectors) = @_;
270 my $io = $self->_zoom_for($template_name => $selectors)->to_fh;
271 return [ 200, [ 'Content-Type' => 'text/html' ], $io ]
274 sub _template_filename_for {
275 my ($self, $name) = @_;
276 $self->{config}{template_dir}.'/'.$name.'.html';
281 $self->{layout_zoom} ||= HTML::Zoom->from_file(
282 $self->_template_filename_for('layout')
287 my ($self, $template_name, $selectors) = @_;
288 ($self->{zoom_for_template}{$template_name} ||= do {
290 HTML::Zoom->from_file(
291 $self->_template_filename_for($template_name)
293 ->select('#main-content')->collect_content({ into => \@body })
296 ->select('#main-content')->replace_content(\@body)
298 })->apply($selectors);
303 $self->{base_url} ||= do {
304 (my $u = $self->config->{base_url}) =~ s/\/$//;
311 unless (@ARGV == 1 && $ARGV[0] eq 'import') {
312 return $self->SUPER::_run_cli(@_);
319 "To import data into your idiotbox install, chdir into a directory\n".
320 "containing video files and run:\n".
324 $self->SUPER::_cli_usage(@_);
329 require App::IdiotBox::Importer;
330 App::IdiotBox::Importer->run($self);