Bug 21290: Updating documentation for ModItem
[koha.git] / C4 / NewsChannels.pm
index b9aa8ad..a01d279 100644 (file)
@@ -20,12 +20,11 @@ package C4::NewsChannels;
 
 use Modern::Perl;
 use C4::Context;
-use C4::Dates qw(format_date);
+use Koha::DateUtils;
 
-use vars qw($VERSION @ISA @EXPORT);
+use vars qw(@ISA @EXPORT);
 
 BEGIN { 
-    $VERSION = 3.07.00.049;    # set the version for version checking
     @ISA = qw(Exporter);
     @EXPORT = qw(
         &GetNewsToDisplay
@@ -61,9 +60,9 @@ sub add_opac_new {
     if ($href_entry) {
         my @fields = keys %{$href_entry};
         my @values = values %{$href_entry};
-        my $field_string = join ',',@fields;
+        my $field_string = join ',', @fields;
         $field_string = $field_string // q{};
-        my $values_string = '?,' x ($#fields) . '?';
+        my $values_string = join(',', map { '?' } @fields);
         my $dbh = C4::Context->dbh;
         my $sth = $dbh->prepare("INSERT INTO opac_news ( $field_string ) VALUES ( $values_string )");
         $sth->execute(@values);
@@ -125,25 +124,44 @@ sub del_opac_new {
 sub get_opac_new {
     my ($idnew) = @_;
     my $dbh = C4::Context->dbh;
-    my $query = q{ SELECT * FROM opac_news WHERE idnew = ? };
+    my $query = q{
+                  SELECT opac_news.*,branches.branchname
+                  FROM opac_news LEFT JOIN branches
+                      ON opac_news.branchcode=branches.branchcode
+                  WHERE opac_news.idnew = ?;
+                };
     my $sth = $dbh->prepare($query);
     $sth->execute($idnew);
     my $data = $sth->fetchrow_hashref;
     $data->{$data->{'lang'}} = 1 if defined $data->{lang};
-    $data->{expirationdate} = format_date($data->{expirationdate});
-    $data->{timestamp}      = format_date($data->{timestamp});
+    $data->{expirationdate} = output_pref({ dt => dt_from_string( $data->{expirationdate} ), dateonly => 1 }) if ( $data->{expirationdate} );
+    $data->{timestamp}      = output_pref({ dt => dt_from_string( $data->{timestamp} ), dateonly => 1 }) ;
     return $data;
 }
 
 sub get_opac_news {
-    my ($limit, $lang) = @_;
+    my ($limit, $lang, $branchcode) = @_;
     my @values;
     my $dbh = C4::Context->dbh;
-    my $query = q{ SELECT *, timestamp AS newdate FROM opac_news };
+    my $query = q{
+                  SELECT opac_news.*, branches.branchname,
+                         timestamp AS newdate,
+                         borrowers.title AS author_title,
+                         borrowers.firstname AS author_firstname,
+                         borrowers.surname AS author_surname
+                  FROM opac_news LEFT JOIN branches
+                      ON opac_news.branchcode=branches.branchcode
+                  LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
+                };
+    $query .= ' WHERE 1';
     if ($lang) {
-        $query.= " WHERE (lang='' OR lang=?)";
+        $query .= " AND (opac_news.lang='' OR opac_news.lang=?)";
         push @values,$lang;
     }
+    if ($branchcode) {
+        $query .= ' AND (opac_news.branchcode IS NULL OR opac_news.branchcode=?)';
+        push @values,$branchcode;
+    }
     $query.= ' ORDER BY timestamp DESC ';
     #if ($limit) {
     #    $query.= 'LIMIT 0, ' . $limit;
@@ -163,36 +181,42 @@ sub get_opac_news {
 
 =head2 GetNewsToDisplay
 
-    $news = &GetNewsToDisplay($lang);
-    C<$news> is a ref to an array which containts
-    all news with expirationdate > today or expirationdate is null.
+    $news = &GetNewsToDisplay($lang,$branch);
+    C<$news> is a ref to an array which contains
+    all news with expirationdate > today or expirationdate is null
+    that is applicable for a given branch.
 
 =cut
 
 sub GetNewsToDisplay {
-    my ($lang) = @_;
+    my ($lang,$branch) = @_;
     my $dbh = C4::Context->dbh;
     # SELECT *,DATE_FORMAT(timestamp, '%d/%m/%Y') AS newdate
     my $query = q{
-     SELECT *,timestamp AS newdate
+     SELECT opac_news.*,timestamp AS newdate,
+     borrowers.title AS author_title,
+     borrowers.firstname AS author_firstname,
+     borrowers.surname AS author_surname
      FROM   opac_news
+     LEFT JOIN borrowers on borrowers.borrowernumber = opac_news.borrowernumber
      WHERE   (
         expirationdate >= CURRENT_DATE()
         OR    expirationdate IS NULL
         OR    expirationdate = '00-00-0000'
      )
-     AND   `timestamp` < CURRENT_DATE()+1
-     AND   (lang = '' OR lang = ?)
+     AND   DATE(timestamp) < DATE_ADD(CURDATE(), INTERVAL 1 DAY)
+     AND   (opac_news.lang = '' OR opac_news.lang = ?)
+     AND   (opac_news.branchcode IS NULL OR opac_news.branchcode = ?)
      ORDER BY number
     }; # expirationdate field is NOT in ISO format?
        # timestamp has HH:mm:ss, CURRENT_DATE generates 00:00:00
        #           by adding 1, that captures today correctly.
     my $sth = $dbh->prepare($query);
     $lang = $lang // q{};
-    $sth->execute($lang);
+    $sth->execute($lang,$branch);
     my @results;
     while ( my $row = $sth->fetchrow_hashref ){
-        $row->{newdate} = format_date($row->{newdate});
+        $row->{newdate} = output_pref({ dt => dt_from_string( $row->{newdate} ), dateonly => 1 });
         push @results, $row;
     }
     return \@results;