+sub send_file {
+ my ($self, $file_name_or_content, %params) = @_;
+
+ my ($file, $size);
+
+ if (!ref $file_name_or_content) {
+ $file = IO::File->new($file_name_or_content, 'r') || croak("Cannot open file '${file_name_or_content}'");
+ $size = -s $file_name_or_content;
+ } else {
+ $size = length $$file_name_or_content;
+ }
+
+ my $content_type = $params{type} || 'application/octet_stream';
+ my $attachment_name = $params{name} || (!ref($file_name_or_content) ? $file_name_or_content : '');
+ $attachment_name =~ s:.*//::g;
+
+ print $::form->create_http_response(content_type => $content_type,
+ content_disposition => 'attachment; filename="' . $attachment_name . '"',
+ content_length => $size);
+
+ if (!ref $file_name_or_content) {
+ $::locale->with_raw_io(\*STDOUT, sub { print while <$file> });
+ $file->close;
+ unlink $file_name_or_content if $params{unlink};
+ } else {
+ $::locale->with_raw_io(\*STDOUT, sub { print $$file_name_or_content });
+ }
+}
+
+sub presenter {
+ return SL::Presenter->get;
+}
+
+sub controller_name {
+ my $class = ref($_[0]) || $_[0];
+ $class =~ s/^SL::Controller:://;
+ return $class;
+}
+
+#
+# Before/after run hooks
+#
+
+sub run_before {
+ _add_hook('before', @_);
+}
+
+sub run_after {
+ _add_hook('after', @_);
+}
+
+my %hooks;
+
+sub _add_hook {
+ my ($when, $class, $sub, %params) = @_;
+
+ foreach my $key (qw(only except)) {
+ $params{$key} = { map { ( $_ => 1 ) } @{ $params{$key} } } if $params{$key};
+ }
+
+ my $idx = "${when}/${class}";
+ $hooks{$idx} ||= [ ];
+ push @{ $hooks{$idx} }, { %params, code => $sub };
+}
+
+sub _run_hooks {
+ my ($self, $when, $action) = @_;
+
+ my $idx = "${when}/" . ref($self);
+
+ foreach my $hook (@{ $hooks{$idx} || [] }) {
+ next if ($hook->{only } && !$hook->{only }->{$action})
+ || ($hook->{except} && $hook->{except}->{$action});
+
+ if (ref($hook->{code}) eq 'CODE') {
+ $hook->{code}->($self, $action);
+ } else {
+ my $sub = $hook->{code};
+ $self->$sub($action);
+ }
+ }
+}
+
+#
+# behaviour. override these
+#
+
+sub delay_flash_on_redirect {
+ 0;
+}
+
+sub get_auth_level {
+ # Ignore the 'action' parameter.
+ return 'user';
+}
+
+sub keep_auth_vars_in_form {
+ return 0;
+}
+