Bug 18736: Calculate tax depending on rounding
[koha.git] / C4 / HTML5Media.pm
index 89620d3..c77e8a4 100644 (file)
@@ -1,28 +1,28 @@
 package C4::HTML5Media;
 
-# Copyright 2012 Mirko Tietgen
+# Copyright 2012/2015 Mirko Tietgen
 #
 # This file is part of Koha.
 #
-# Koha 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.
+# Koha 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 3 of the License, or
+# (at your option) any later version.
 #
-# Koha 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.
+# Koha 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 Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use strict;
 use warnings;
 
 use C4::Context;
 use MARC::Field;
-
+use Koha::UploadedFiles;
 
 =head1 HTML5Media
 
@@ -48,8 +48,10 @@ sub gethtml5media {
     my $HTML5MediaParent;
     my $HTML5MediaWidth;
     my @HTML5MediaExtensions = split( /\|/, C4::Context->preference("HTML5MediaExtensions") );
+    my $HTML5MediaYouTube    = C4::Context->preference("HTML5MediaYouTube");
     my $marcflavour          = C4::Context->preference("marcflavour");
     foreach my $HTML5Media_field (@HTML5Media_fields) {
+    my $isyoutube            = 0;
         my %HTML5Media;
         # protocol
         if ( $HTML5Media_field->indicator(1) eq '1' ) {
@@ -96,12 +98,31 @@ sub gethtml5media {
         # src
         if ( $HTML5Media_field->subfield('u') ) {
             $HTML5Media{srcblock} = $HTML5Media_field->subfield('u');
+            if (grep /youtu\.?be/, $HTML5Media_field->subfield('u') ) {
+                if ($HTML5MediaYouTube == 1) {
+                    require WWW::YouTube::Download;
+                    import  WWW::YouTube::Download qw(playback_url);
+                    my $youtube           = WWW::YouTube::Download->new;
+                    eval {
+                        $HTML5Media{srcblock} = $youtube->playback_url(
+                            $HTML5Media_field->subfield('u'), {
+                                'fmt' => '43' #webm is the only format compatible to all modern browsers. maybe check for available qualities
+                            }
+                        );
+                    };
+                    if ($@) { warn $@; }
+                    else  { $isyoutube = 1;}
+                }
+               else {
+                   next; # do not embed youtube videos
+               }
+            }
         }
         elsif ( $HTML5Media_field->subfield('a') && $HTML5Media_field->subfield('d') && $HTML5Media_field->subfield('f') ) {
             $HTML5Media{host}        = $HTML5Media_field->subfield('a');
             $HTML5Media{host}        =~ s/(^\/|\/$)//g;
             $HTML5Media{path}        = $HTML5Media_field->subfield('d');
-            $HTML5Media{path}        =~ s/(^\/|\/$)//g;
+            $HTML5Media{path}        =~ s/(^\/|\/$)//g; # TODO we could check for youtube here too, but nobody uses these fields anyway…
             $HTML5Media{file}        = $HTML5Media_field->subfield('f');
             $HTML5Media{srcblock}    = $HTML5Media{protocol} . '://' . $HTML5Media{loginblock} . $HTML5Media{host} . $HTML5Media{portblock} . '/' . $HTML5Media{path} . '/' . $HTML5Media{file};
         }
@@ -109,10 +130,28 @@ sub gethtml5media {
             next; # no file to play
         }
         # extension
-        $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
-        if ( !grep /$HTML5Media{extension}/, @HTML5MediaExtensions ) {
+        # check uploaded files
+        if ( $HTML5Media{srcblock} =~ /\Qopac-retrieve-file.pl\E/ ) {
+            my ( undef, $id ) = split /id=/, $HTML5Media{srcblock};
+            next if !$id;
+            my %public = ( ( caller )[1] =~ /opac/ ) ? ( public => 1 ): ();
+            my $upload = Koha::UploadedFiles->search({
+                hashvalue => $id, %public,
+            })->next;
+            next if !$upload || $upload->filename !~ /\./;
+            $HTML5Media{extension} = ( $upload->filename =~ m/([^.]+)$/ )[0];
+        }
+        # check remote files
+        else {
+            $HTML5Media{extension} = ($HTML5Media{srcblock} =~ m/([^.]+)$/)[0];
+        }
+        if ( ( !grep /\Q$HTML5Media{extension}\E/, @HTML5MediaExtensions ) && ( $isyoutube != 1) ) {
             next; # not a specified media file
         }
+        # youtube
+        if ($isyoutube == 1) {
+                $HTML5Media{mime} = 'video/webm';
+        }
         # mime
         if ( $HTML5Media_field->subfield('c') ) {
             $HTML5Media{codecs} = $HTML5Media_field->subfield('c');
@@ -144,7 +183,7 @@ sub gethtml5media {
             }
             if ( $HTML5Media{extension} eq 'oga' ) {
                 $HTML5Media{mime} = 'audio/ogg';
-              $HTML5Media{codecs} = 'vorbis';
+                $HTML5Media{codecs} = 'vorbis';
             }
             elsif ( $HTML5Media{extension} eq 'spx' ) {
                 $HTML5Media{mime} = 'audio/ogg';