(bug #2040) This prevent users to post empty suggestions, and post the same suggestio...
[koha.git] / C4 / Suggestions.pm
index afb548d..1f9daef 100644 (file)
@@ -72,7 +72,7 @@ Suggestions done by other borrowers can be seen when not "AVAILABLE"
 
 =head2 SearchSuggestion
 
-(\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme)
+(\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme,$branchcode)
 
 searches for a suggestion
 
@@ -85,13 +85,15 @@ Note the status is stored twice :
 =cut
 
 sub SearchSuggestion  {
-    my ($user,$author,$title,$publishercode,$status,$suggestedbyme)=@_;
+    my ($user,$author,$title,$publishercode,$status,$suggestedbyme,$branchcode)=@_;
     my $dbh = C4::Context->dbh;
     my $query = "
     SELECT suggestions.*,
+        U1.branchcode   AS branchcodesuggestedby,
         U1.surname   AS surnamesuggestedby,
         U1.firstname AS firstnamesuggestedby,
         U1.borrowernumber AS borrnumsuggestedby,
+        U1.branchcode AS branchcodesuggestedby,
         U2.surname   AS surnamemanagedby,
         U2.firstname AS firstnamemanagedby,
         U2.borrowernumber AS borrnummanagedby
@@ -113,14 +115,18 @@ sub SearchSuggestion  {
         push @sql_params,"%".$publishercode."%";
         $query .= " and publishercode like ?";
     }
-    if (C4::Context->preference("IndependantBranches")) {
+    if (C4::Context->preference("IndependantBranches") || $branchcode) {
         my $userenv = C4::Context->userenv;
         if ($userenv) {
-            unless ($userenv->{flags} == 1){
+            unless ($userenv->{flags} % 2 == 1){
                 push @sql_params,$userenv->{branch};
                 $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
             }
         }
+        if ($branchcode) {
+            push @sql_params,$branchcode;
+            $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
+        }
     }
     if ($status) {
         push @sql_params,$status;
@@ -146,6 +152,7 @@ sub SearchSuggestion  {
         } else {
             $even=1;
         }
+#         $data->{date} = format_date($data->{date});
         push(@results,$data);
     }
     return (\@results);
@@ -201,7 +208,7 @@ sub GetSuggestionFromBiblionumber {
 
 =head2 GetSuggestionByStatus
 
-$suggestions = &GetSuggestionByStatus($status)
+$suggestions = &GetSuggestionByStatus($status,[$branchcode])
 
 Get a suggestion from it's status
 
@@ -212,10 +219,13 @@ all the suggestion with C<$status>
 
 sub GetSuggestionByStatus {
     my $status = shift;
+    my $branchcode = shift;
     my $dbh = C4::Context->dbh;
-    my $query = "SELECT suggestions.*,
+    my @sql_params=($status);  
+    my $query = qq(SELECT suggestions.*,
                         U1.surname   AS surnamesuggestedby,
                         U1.firstname AS firstnamesuggestedby,
+            U1.branchcode AS branchcodesuggestedby,
                                                U1.borrowernumber AS borrnumsuggestedby,
                         U2.surname   AS surnamemanagedby,
                         U2.firstname AS firstnamemanagedby,
@@ -223,17 +233,28 @@ sub GetSuggestionByStatus {
                         FROM suggestions
                         LEFT JOIN borrowers AS U1 ON suggestedby=U1.borrowernumber
                         LEFT JOIN borrowers AS U2 ON managedby=U2.borrowernumber
-                        WHERE status = ?
-                        ";
+                        WHERE status = ?);
+    if (C4::Context->preference("IndependantBranches") || $branchcode) {
+        my $userenv = C4::Context->userenv;
+        if ($userenv) {
+            unless ($userenv->{flags} % 2 == 1){
+                push @sql_params,$userenv->{branch};
+                $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
+            }
+        }
+        if ($branchcode) {
+            push @sql_params,$branchcode;
+            $query .= " and (U1.branchcode = ? or U1.branchcode ='')";
+        }
+    }
+    
     my $sth = $dbh->prepare($query);
-    $sth->execute($status);
+    $sth->execute(@sql_params);
     
-    my @results;
-    while(my $data = $sth->fetchrow_hashref){
-        $data->{date} = format_date($data->{date});
-        push @results,$data;
-    }
-    return \@results;
+    my $results;
+    $results=  $sth->fetchall_arrayref({});
+#     map{$_->{date} = format_date($_->{date})} @$results;
+    return $results;
 }
 
 =head2 CountSuggestion
@@ -266,7 +287,7 @@ sub CountSuggestion {
     my $sth;
     if (C4::Context->preference("IndependantBranches")){
         my $userenv = C4::Context->userenv;
-        if ($userenv->{flags} == 1){
+        if ($userenv->{flags} % 2 == 1){
             my $query = qq |
                 SELECT count(*)
                 FROM   suggestions
@@ -310,15 +331,17 @@ Insert a new suggestion on database with value given on input arg.
 
 sub NewSuggestion {
     my ($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason) = @_;
-    my $dbh = C4::Context->dbh;
-    my $query = qq |
-        INSERT INTO suggestions
-            (status,suggestedby,title,author,publishercode,note,copyrightdate,
-            volumedesc,publicationyear,place,isbn,biblionumber,reason)
-        VALUES ('ASKED',?,?,?,?,?,?,?,?,?,?,?,?)
-    |;
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason);
+    if ($title) {
+        my $dbh = C4::Context->dbh;
+        my $query = qq |
+            INSERT INTO suggestions
+                (status,suggestedby,title,author,publishercode,note,copyrightdate,
+                volumedesc,publicationyear,place,isbn,biblionumber,reason)
+            VALUES ('ASKED',?,?,?,?,?,?,?,?,?,?,?,?)
+        |;
+        my $sth = $dbh->prepare($query);
+        $sth->execute($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber,$reason);
+    }
 }
 
 =head2 ModStatus
@@ -409,7 +432,8 @@ sub ModStatus {
         To => $emailinfo->{byemail},
         From => $emailinfo->{libemail},
         Subject => 'Koha suggestion',
-        Message => "".$template->output
+        Message => "".$template->output,
+        'Content-Type' => 'text/plain; charset="utf8"',
     );
     sendmail(%mail);
 }
@@ -443,7 +467,7 @@ Delete a suggestion. A borrower can delete a suggestion only if he is its owner.
 =cut
 
 sub DelSuggestion {
-    my ($borrowernumber,$suggestionid) = @_;
+    my ($borrowernumber,$suggestionid,$type) = @_;
     my $dbh = C4::Context->dbh;
     # check that the suggestion comes from the suggestor
     my $query = "
@@ -454,13 +478,14 @@ sub DelSuggestion {
     my $sth = $dbh->prepare($query);
     $sth->execute($suggestionid);
     my ($suggestedby) = $sth->fetchrow;
-    if ($suggestedby eq $borrowernumber) {
+    if ($type eq "intranet" || $suggestedby eq $borrowernumber ) {
         my $queryDelete = "
             DELETE FROM suggestions
             WHERE suggestionid=?
         ";
         $sth = $dbh->prepare($queryDelete);
-        $sth->execute($suggestionid);
+        my $suggestiondeleted=$sth->execute($suggestionid);
+        return $suggestiondeleted;  
     }
 }