From 8802bbf5fe6c0b35ad4d6e00293eb102a48b92b4 Mon Sep 17 00:00:00 2001 From: Henri-Damien LAURENT Date: Tue, 25 Aug 2009 23:56:16 +0200 Subject: [PATCH] Editing C4::Suggestions.pm Enhancing C4::SearchSuggestion And changing its API Changing C4::Suggestions API taking a hashref for NewSuggestion and ModSuggestion Signed-off-by: Galen Charlton --- C4/Letters.pm | 1 + C4/Suggestions.pm | 225 +++++--------- acqui/acqui-home.pl | 2 +- acqui/newordersuggestion.pl | 8 +- .../en/modules/suggestion/suggestion.tmpl | 283 ++++++++++++++++++ .../prog/en/modules/opac-suggestions.tmpl | 26 +- opac/opac-suggestions.pl | 94 +++--- suggestion/suggestion.pl | 269 +++++++++++++++++ t/db_dependent/Suggestions.t | 24 ++ 9 files changed, 718 insertions(+), 214 deletions(-) create mode 100755 koha-tmpl/intranet-tmpl/prog/en/modules/suggestion/suggestion.tmpl create mode 100755 suggestion/suggestion.pl create mode 100644 t/db_dependent/Suggestions.t diff --git a/C4/Letters.pm b/C4/Letters.pm index 6d33b3f7fe..535e87d363 100644 --- a/C4/Letters.pm +++ b/C4/Letters.pm @@ -486,6 +486,7 @@ sub parseletter_sth { ($table eq 'reserves' ) ? "SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" : ($table eq 'borrowers' ) ? "SELECT * FROM $table WHERE borrowernumber = ?" : ($table eq 'branches' ) ? "SELECT * FROM $table WHERE branchcode = ?" : + ($table eq 'suggestions' ) ? "SELECT * FROM $table WHERE borrowernumber = ? and biblionumber = ?" : ($table eq 'aqbooksellers') ? "SELECT * FROM $table WHERE id = ?" : undef ; unless ($query) { warn "ERROR: No parseletter_sth query for table '$table'"; diff --git a/C4/Suggestions.pm b/C4/Suggestions.pm index 491739c1f9..b31772f0ee 100644 --- a/C4/Suggestions.pm +++ b/C4/Suggestions.pm @@ -20,11 +20,14 @@ package C4::Suggestions; use strict; use CGI; -use Mail::Sendmail; use C4::Context; use C4::Output; use C4::Dates qw(format_date); +use C4::SQLHelper qw(:all); +use C4::Debug; +use C4::Letters; +use List::MoreUtils qw(any); use vars qw($VERSION @ISA @EXPORT); BEGIN { @@ -39,7 +42,7 @@ BEGIN { &GetSuggestionByStatus &DelSuggestion &CountSuggestion - &ModStatus + &ModSuggestion &ConnectSuggestionAndBiblio &GetSuggestionFromBiblionumber ); @@ -72,7 +75,7 @@ Suggestions done by other borrowers can be seen when not "AVAILABLE" =head2 SearchSuggestion -(\@array) = &SearchSuggestion($user,$author,$title,$publishercode,$status,$suggestedbyme,$branchcode) +(\@array) = &SearchSuggestion($suggestionhashref_to_search) searches for a suggestion @@ -85,9 +88,10 @@ Note the status is stored twice : =cut sub SearchSuggestion { - my ($user,$author,$title,$publishercode,$status,$suggestedbyme,$branchcode)=@_; + my ($suggestion)=@_; my $dbh = C4::Context->dbh; - my $query = " + my @sql_params; + my @query =(q{ SELECT suggestions.*, U1.branchcode AS branchcodesuggestedby, B1.branchname AS branchnamesuggestedby, @@ -107,62 +111,44 @@ sub SearchSuggestion { LEFT JOIN categories AS C1 ON C1.categorycode = U1.categorycode LEFT JOIN branches AS B1 ON B1.branchcode = U1.branchcode LEFT JOIN branches AS B2 ON B2.branchcode = U2.branchcode - WHERE 1=1 "; + WHERE status NOT IN ('CLAIMED') + } , map { + if ( my $s = $$suggestion{$_} ) { + push @sql_params,'%'.$s.'%'; + " and suggestions.$_ like ? "; + } else { () } + } qw( title author isbn publishercode collectiontitle ) + ); - my @sql_params; - if ($author) { - push @sql_params,"%".$author."%"; - $query .= " and author like ?"; - } - if ($title) { - push @sql_params,"%".$title."%"; - $query .= " and suggestions.title like ?"; + my $userenv = C4::Context->userenv; + if (C4::Context->preference('IndependantBranches')) { + if ($userenv) { + if (($userenv->{flags} % 2) != 1 && !$$suggestion{branchcode}){ + push @sql_params,$$userenv{branch}; + push @query,q{ and (branchcode = ? or branchcode ='')}; + } + } } - if ($publishercode) { - push @sql_params,"%".$publishercode."%"; - $query .= " and publishercode like ?"; - } - 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 ='')"; - } - } - if ($status) { - push @sql_params,$status; - $query .= " and status=?"; - } - if ($suggestedbyme) { - unless ($suggestedbyme eq -1) { - push @sql_params,$user; - $query .= " and suggestedby=?"; - } - } else { - $query .= " and managedby is NULL"; + + foreach my $field (grep { my $fieldname=$_; + any {$fieldname eq $_ } qw< + status branchcode itemtype suggestedby managedby acceptedby + bookfundid biblionumber + >} keys %$suggestion + ) { + if ($$suggestion{$field}){ + push @sql_params,$$suggestion{$field}; + push @query, " and suggestions.$field=?"; + } + else { + push @query, " and (suggestions.$field='' OR suggestions.$field IS NULL)"; + } } - my $sth=$dbh->prepare($query); + + $debug && warn "@query"; + my $sth=$dbh->prepare("@query"); $sth->execute(@sql_params); - my @results; - my $even=1; # the even variable is used to set even / odd lines, for highlighting - while (my $data=$sth->fetchrow_hashref){ - $data->{$data->{STATUS}} = 1; - if ($even) { - $even=0; - $data->{even}=1; - } else { - $even=1; - } -# $data->{date} = format_date($data->{date}); - push(@results,$data); - } - return (\@results); + return ($sth->fetchall_arrayref({})); } =head2 GetSuggestion @@ -234,8 +220,8 @@ sub GetSuggestionByStatus { U1.firstname AS firstnamesuggestedby, U1.branchcode AS branchcodesuggestedby, B1.branchname AS branchnamesuggestedby, - U1.borrowernumber AS borrnumsuggestedby, - U1.categorycode AS categorycodesuggestedby, + U1.borrowernumber AS borrnumsuggestedby, + U1.categorycode AS categorycodesuggestedby, C1.description AS categorydescriptionsuggestedby, U2.surname AS surnamemanagedby, U2.firstname AS firstnamemanagedby, @@ -265,7 +251,6 @@ sub GetSuggestionByStatus { my $results; $results= $sth->fetchall_arrayref({}); -# map{$_->{date} = format_date($_->{date})} @$results; return $results; } @@ -335,117 +320,47 @@ sub CountSuggestion { =head2 NewSuggestion -&NewSuggestion($borrowernumber,$title,$author,$publishercode,$note,$copyrightdate,$volumedesc,$publicationyear,$place,$isbn,$biblionumber) +&NewSuggestion($suggestion) Insert a new suggestion on database with value given on input arg. =cut 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); + my ($suggestion) = @_; + return InsertInTable("suggestions",$suggestion); } -=head2 ModStatus +=head2 ModSuggestion -&ModStatus($suggestionid,$status,$managedby,$biblionumber) +&ModSuggestion($suggestion) -Modify the status (status can be 'ASKED', 'ACCEPTED', 'REJECTED', 'ORDERED') -and send a mail to notify the user that did the suggestion. +Modify the suggestion according to the hash passed by ref. +The hash HAS to contain suggestionid +Data not defined is not updated unless it is a note or sort1 +Send a mail to notify the user that did the suggestion. -Note that there is no function to modify a suggestion : only the status can be modified, thus the name of the function. +Note that there is no function to modify a suggestion. =cut -sub ModStatus { - my ($suggestionid,$status,$managedby,$biblionumber,$reason) = @_; - my $dbh = C4::Context->dbh; - my $sth; - if ($managedby>0) { - if ($biblionumber) { - my $query = qq| - UPDATE suggestions - SET status=?,managedby=?,biblionumber=?,reason=? - WHERE suggestionid=? - |; - $sth = $dbh->prepare($query); - $sth->execute($status,$managedby,$biblionumber,$reason,$suggestionid); - } else { - my $query = qq| - UPDATE suggestions - SET status=?,managedby=?,reason=? - WHERE suggestionid=? - |; - $sth = $dbh->prepare($query); - $sth->execute($status,$managedby,$reason,$suggestionid); - } - } else { - if ($biblionumber) { - my $query = qq| - UPDATE suggestions - SET status=?,biblionumber=?,reason=? - WHERE suggestionid=? - |; - $sth = $dbh->prepare($query); - $sth->execute($status,$biblionumber,$reason,$suggestionid); - } - else { - my $query = qq| - UPDATE suggestions - SET status=?,reason=? - WHERE suggestionid=? - |; - $sth = $dbh->prepare($query); - $sth->execute($status,$reason,$suggestionid); +sub ModSuggestion { + my ($suggestion)=@_; + my $status_update_table=UpdateInTable("suggestions", $suggestion); + # check mail sending. + if ($$suggestion{STATUS}){ + my $letter=C4::Letters::getletter('suggestions',$$suggestion{STATUS}); + if ($letter){ + my $enqueued = C4::Letters::EnqueueLetter({ + letter=>$letter, + borrowernumber=>$$suggestion{suggestedby}, + suggestionid=>$$suggestion{suggestionid}, + msg_transport_type=>'email' + }); + if (!$enqueued){warn "can't enqueue letter $letter";} } } - # check mail sending. - my $queryMail = " - SELECT suggestions.*, - boby.surname AS bysurname, - boby.firstname AS byfirstname, - boby.email AS byemail, - lib.surname AS libsurname, - lib.firstname AS libfirstname, - lib.email AS libemail - FROM suggestions - LEFT JOIN borrowers AS boby ON boby.borrowernumber=suggestedby - LEFT JOIN borrowers AS lib ON lib.borrowernumber=managedby - WHERE suggestionid=? - "; - $sth = $dbh->prepare($queryMail); - $sth->execute($suggestionid); - my $emailinfo = $sth->fetchrow_hashref; - my $template = gettemplate("suggestion/mail_suggestion_$status.tmpl", "intranet", CGI->new()); - - $template->param( - byemail => $emailinfo->{byemail}, - libemail => $emailinfo->{libemail}, - status => $emailinfo->{status}, - title => $emailinfo->{title}, - author =>$emailinfo->{author}, - libsurname => $emailinfo->{libsurname}, - libfirstname => $emailinfo->{libfirstname}, - byfirstname => $emailinfo->{byfirstname}, - bysurname => $emailinfo->{bysurname}, - reason => $emailinfo->{reason} - ); - my %mail = ( - To => $emailinfo->{byemail}, - From => $emailinfo->{libemail}, - Subject => 'Koha suggestion', - Message => "".$template->output, - 'Content-Type' => 'text/plain; charset="utf8"', - ); - sendmail(%mail); + return $status_update_table; } =head2 ConnectSuggestionAndBiblio diff --git a/acqui/acqui-home.pl b/acqui/acqui-home.pl index 339885d370..22ee900b16 100755 --- a/acqui/acqui-home.pl +++ b/acqui/acqui-home.pl @@ -111,7 +111,7 @@ for ( my $i = 0 ; $i < $count ; $i++ ) { # suggestions my $status = $query->param('status') || "ASKED"; my $suggestion = CountSuggestion($status); -my $suggestions_loop = &SearchSuggestion( '', '', '', '', $status, '' ); +my $suggestions_loop = &SearchSuggestion( {status=> $status} ); $template->param( classlist => $classlist, diff --git a/acqui/newordersuggestion.pl b/acqui/newordersuggestion.pl index e56e52197d..7d53bc2635 100755 --- a/acqui/newordersuggestion.pl +++ b/acqui/newordersuggestion.pl @@ -126,8 +126,12 @@ if ( $op eq 'connectDuplicate' ) { # getting all suggestions. my $suggestions_loop = - &SearchSuggestion( $borrowernumber, $author, $title, $publishercode,'ACCEPTED', - -1 ); + &SearchSuggestion( + { suggestedby => $borrowernumber, + author => $author, + title => $title, + publishercode => $publishercode, + status => 'ACCEPTED'}); my $vendor = GetBookSellerFromId($supplierid); $template->param( suggestions_loop => $suggestions_loop, diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/suggestion/suggestion.tmpl b/koha-tmpl/intranet-tmpl/prog/en/modules/suggestion/suggestion.tmpl new file mode 100755 index 0000000000..1784a29b9e --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/suggestion/suggestion.tmpl @@ -0,0 +1,283 @@ +<!-- TMPL_IF NAME="LibraryNameTitle" --><!-- TMPL_VAR NAME="LibraryNameTitle" --><!-- TMPL_ELSE -->Koha Online<!-- /TMPL_IF --> Catalog › +<!-- TMPL_IF name="op_save" --><!-- TMPL_IF name="suttesionid" -->Edit purchase suggestion #<!-- TMPL_VAR name="suggestionid" --><!--TMPL_ELSE-->Enter a new purchase suggestion<!-- /TMPL_IF --><!--TMPL_ELSE-->Suggestions<!-- /TMPL_IF --> + + + + + + + + + + + +
+
+
+
+ +
+ +

Edit purchase suggestion #

+ "/> + +

Enter a new purchase suggestion

+ +
Bibliographic information
    +
  1. "/>
  2. +
  3. "/>
  4. +
  5. " />
  6. +
  7. "/>
  8. +
  9. "/>
  10. +
  11. "/>
  12. +
  13. "/>
  14. +
  15. +
  16. +
  17. +
+
Suggestion management
    +
  1. + + + + + + + + + + + +
     dateby
    Suggestion creation "/>"/> + +
    Suggestion management " />"/>
    Suggestion Accepted " />"/>
    +
+
Acquisition information
    +
  1. + +
  2. +
  3. + +
  4. +
+
" />">CancelCancel +
+
+ + + + + +
+Filters +
+
Bibliographic information : + " /> + " /> + " />
+ " /> + " /> + " /> +
+
Suggestion information: +
+ +
+ +
+ + +
+
Acquistion information : + + +
+
+ +
+ +
+ +
Sorting...
+
Loading tab...
+
+ +
+ + + +
"> +
" method="post" action="/cgi-bin/koha/suggestion/suggestion.pl#"> + + + t" > + + + + + + + + + + + + + + + + + + + + +
" >Check All tab SuggestionSuggested by /onManaged by /onForBudgetStatus
+ " /> + &op=edit" title="suggestion" > + , by
+ © + ; Volume: + ; ISBN : ; Published by in in ; ;
; +
+ ">, +
+
+ ">, +
+
+ + + + + Requested Accepted Ordered Rejected Checked
() +
+
"> + +
+
"> + + " name="other_reason" value="please note your reason here..." /> ">Cancel +
+
"> + Accept + Check + Reject +
+
"> + .op.value='delete';}else{document.f.op.value='change'}"/> Delete +
+ + "/> + +
+
+
+
+ + No Results. + +
+ +
+
+ +
+ diff --git a/koha-tmpl/opac-tmpl/prog/en/modules/opac-suggestions.tmpl b/koha-tmpl/opac-tmpl/prog/en/modules/opac-suggestions.tmpl index aaba1eb130..b14bf764c1 100644 --- a/koha-tmpl/opac-tmpl/prog/en/modules/opac-suggestions.tmpl +++ b/koha-tmpl/opac-tmpl/prog/en/modules/opac-suggestions.tmpl @@ -63,10 +63,20 @@ $.tablesorter.addParser({
  • +
  • +
  • +
  • +
  • +
  • -
    Cancel
    +
    " /> Cancel
    @@ -101,8 +111,8 @@ $.tablesorter.addParser({
    -
    Select All - Clear All | New purchase suggestionNew purchase suggestion + @@ -116,28 +126,30 @@ $.tablesorter.addParser({
    - - " /> - + " />

    , - , - + () + , + -

    -   +   ,   Requested + Checked by the library Accepted by the library Ordered by the library Suggestion declined diff --git a/opac/opac-suggestions.pl b/opac/opac-suggestions.pl index a12826aa61..cd512de079 100755 --- a/opac/opac-suggestions.pl +++ b/opac/opac-suggestions.pl @@ -21,27 +21,21 @@ use warnings; use CGI; use C4::Auth; # get_template_and_user use C4::Branch; +use C4::Koha; use C4::Output; use C4::Suggestions; +use C4::Koha; +use C4::Dates; my $input = new CGI; -my $title = $input->param('title'); -my $author = $input->param('author'); -my $note = $input->param('note'); -my $copyrightdate = $input->param('copyrightdate'); -my $publishercode = $input->param('publishercode'); -my $volumedesc = $input->param('volumedesc'); -my $publicationyear = $input->param('publicationyear'); -my $place = $input->param('place'); -my $isbn = $input->param('isbn'); -my $status = $input->param('status'); -my $suggestedbyme = (defined $input->param('suggestedby')? $input->param('suggestedby'):1); +my $allsuggestions = $input->param('showall'); my $op = $input->param('op'); +my $suggestion = $input->Vars; +delete $$suggestion{$_} foreach qw; $op = 'else' unless $op; my ( $template, $borrowernumber, $cookie ); -my $dbh = C4::Context->dbh; if ( C4::Context->preference("AnonSuggestions") ) { ( $template, $borrowernumber, $cookie ) = get_template_and_user( @@ -52,8 +46,8 @@ if ( C4::Context->preference("AnonSuggestions") ) { authnotrequired => 1, } ); - if ( !$borrowernumber ) { - $borrowernumber = C4::Context->preference("AnonSuggestions"); + if ( !$$suggestion{suggestedby} ) { + $$suggestion{suggestedby} = C4::Context->preference("AnonSuggestions"); } } else { @@ -66,24 +60,30 @@ else { } ); } - +if ($allsuggestions){ + delete $$suggestion{suggestedby}; +} +else { + $$suggestion{suggestedby} ||= $borrowernumber unless ($allsuggestions); +} +warn "bornum:",$borrowernumber; +use YAML; +my $suggestions_loop = + &SearchSuggestion( $suggestion); if ( $op eq "add_confirm" ) { - &NewSuggestion( - $borrowernumber, $title, $author, $publishercode, - $note, $copyrightdate, $volumedesc, $publicationyear, - $place, $isbn, '' - ); - - # empty fields, to avoid filter in "SearchSuggestion" - $title = ''; - $author = ''; - $publishercode = ''; - $copyrightdate = ''; - $volumedesc = ''; - $publicationyear = ''; - $place = ''; - $isbn = ''; - $op = 'else'; + if (@$suggestions_loop>=1){ + #some suggestion are answering the request Donot Add + } + else { + $$suggestion{'suggestioncreatedon'}=C4::Dates->today; + $$suggestion{'branchcode'}=C4::Context->userenv->{"branch"}; + &NewSuggestion($suggestion); + # empty fields, to avoid filter in "SearchSuggestion" + $$suggestion{$_}='' foreach qw; + $suggestions_loop = + &SearchSuggestion( $suggestion ); + } + $op = 'else'; } if ( $op eq "delete_confirm" ) { @@ -93,28 +93,24 @@ if ( $op eq "delete_confirm" ) { } $op = 'else'; } - -my $suggestions_loop = - &SearchSuggestion( $borrowernumber, $author, $title, $publishercode, $status, - $suggestedbyme ); - -foreach my $suggestion(@$suggestions_loop) { - if($suggestion->{'suggestedby'} == $borrowernumber) { - $suggestion->{'showcheckbox'} = $borrowernumber; - } else { - $suggestion->{'showcheckbox'} = 0; - } +map{ $_->{'branchcodesuggestedby'}=GetBranchInfo($_->{'branchcodesuggestedby'})->[0]->{'branchname'}} @$suggestions_loop; +my $supportlist=GetSupportList(); +foreach my $support(@$supportlist){ + if ($$support{'imageurl'}){ + $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} ); + } + else { + delete $$support{'imageurl'} + } } - $template->param( + %$suggestion, + itemtypeloop=> $supportlist, suggestions_loop => $suggestions_loop, - title => $title, - author => $author, - publishercode => $publishercode, - status => $status, - suggestedbyme => $suggestedbyme, + showall => $allsuggestions, "op_$op" => 1, - suggestionsview => 1 + suggestionsview => 1 ); output_html_with_http_headers $input, $cookie, $template->output; + diff --git a/suggestion/suggestion.pl b/suggestion/suggestion.pl new file mode 100755 index 0000000000..c455e3f786 --- /dev/null +++ b/suggestion/suggestion.pl @@ -0,0 +1,269 @@ +#!/usr/bin/perl + +# 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 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., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA + +use strict; +require Exporter; +use CGI; +use C4::Auth; # get_template_and_user +use C4::Output; +use C4::Suggestions; +use C4::Koha; #GetItemTypes +use C4::Branch; +use C4::Budgets; +use C4::Search; +use C4::Dates qw(format_date); +use C4::Members; +use C4::Debug; + +sub Init{ + my $suggestion= shift @_; + foreach my $date qw(createdon managedon){ + $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")? + $suggestion->{$date}=C4::Dates->today: + format_date($suggestion->{$date}) + ); + } + $suggestion->{'acceptedon'}=(($suggestion->{'acceptedon'} eq "0000-00-00" ||$suggestion->{'acceptedon'} eq "")? + "": + format_date($suggestion->{'acceptedon'}) + ); + $suggestion->{'managedby'}=C4::Context->userenv->{"id"} unless ($suggestion->{'managedby'}); + $suggestion->{'createdby'}=C4::Context->userenv->{"id"} unless ($suggestion->{'createdby'}); + $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'}); +} + +sub GetCriteriumDesc{ + my ($criteriumvalue,$displayby)=@_; + return ($criteriumvalue eq 'ASKED'?"pending":lc $criteriumvalue) if ($displayby =~/status/i); + return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/); + return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/); + if ($displayby =~/managedby/||$displayby =~/acceptedby/){ + my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue); + return "" unless $borr; +# warn '$borr : ',Data::Dumper::Dumper($borr); + return $$borr{firstname}.", ".$$borr{surname}; + } +} + +my $input = CGI->new; +my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1); +my $op = $input->param('op')||'else'; +my @editsuggestions = $input->param('edit_field'); +my $branchfilter = $input->param('branchcode'); +my $suggestedby = $input->param('suggestedby'); +my $managedby = $input->param('managedby'); +my $displayby = $input->param('displayby'); +my $tabcode = $input->param('tabcode'); + +# filter informations which are not suggestion related. +my $suggestion_ref = $input->Vars; +delete $$suggestion_ref{$_} foreach qw<suggestedbyme op displayby tabcode edit_field>; +foreach (keys %$suggestion_ref){ + delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change')); +} +my ( $template, $borrowernumber, $cookie ) = get_template_and_user( + { + template_name => "suggestion/suggestion.tmpl", + query => $input, + type => "intranet", + flagsrequired => { catalogue => 1 }, + } + ); + +######################################### +## Operations +## +if ($op =~/save/i){ + if ($$suggestion_ref{'suggestionid'}>0){ + &ModSuggestion($suggestion_ref); + } + else { + ###FIXME:Search here if suggestion already exists. + my $suggestions_loop = + SearchSuggestion( $suggestion_ref ); + if (@$suggestions_loop>=1){ + #some suggestion are answering the request Donot Add + } + else { + ## Adding some informations related to suggestion + &NewSuggestion($suggestion_ref); + } + # empty fields, to avoid filter in "SearchSuggestion" + } + map{delete $$suggestion_ref{$_}} keys %$suggestion_ref; + $op = 'else'; +} +elsif ($op=~/add/) { + #Adds suggestion + Init($suggestion_ref); + $op ='save'; +} +elsif ($op=~/edit/) { + #Edit suggestion + $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'}); + Init($suggestion_ref); + $op ='save'; +} +elsif ($op eq "change" ) { + if ($$suggestion_ref{"STATUS"}){ + my $tmpstatus=($$suggestion_ref{"STATUS"} eq "ACCEPTED"?"accepted":"managed"); + $$suggestion_ref{"$tmpstatus"."on"}=C4::Dates->today; + $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number}; + } + if ( my $reason = $$suggestion_ref{"reason$tabcode"}){ + if ( $reason eq "other" ) { + $reason = $$suggestion_ref{"other_reason$tabcode"}; + } + $$suggestion_ref{'reason'}=$reason; + } + delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode"); + foreach (keys %$suggestion_ref){ + delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_}); + } + foreach my $suggestionid (@editsuggestions) { + next unless $suggestionid; + $$suggestion_ref{'suggestionid'}=$suggestionid; + &ModSuggestion($suggestion_ref); + } + $op = 'else'; +}elsif ($op eq "delete" ) { + foreach my $delete_field (@editsuggestions) { + &DelSuggestion( $borrowernumber, $delete_field,'intranet' ); + } + $op = 'else'; +} +if ($op=~/else/) { + $op='else'; + + $displayby||="STATUS"; + my $criteria_list=GetDistinctValues("suggestions.".$displayby); + my @allsuggestions; + foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){ + my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne ""; + + next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue); + $$suggestion_ref{$displayby}=$criteriumvalue; + warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby"; + + my $suggestions = &SearchSuggestion($suggestion_ref); + foreach (@$suggestions){ + foreach my $date qw(createdon managedon acceptedon){ + if ($_->{$date} ne "0000-00-00" && $_->{$date} ne "" ){ + $_->{$date}=format_date($_->{$date}) ; + } else { + $_->{$date}="" ; + } + } + } + push @allsuggestions,{ + "suggestiontype"=>$criteriumvalue||"suggest", + "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"", + "suggestionscount"=>scalar(@$suggestions), + 'suggestions_loop'=>$suggestions, + }; + + delete $$suggestion_ref{$displayby} unless $definedvalue; + } + my $reasonsloop = GetAuthorisedValues("SUGGEST"); + $template->param( + "displayby"=> $displayby, + "notabs"=> $displayby eq "", + suggestions => \@allsuggestions, + reasonsloop => $reasonsloop, + ); +} + +foreach my $element qw<managedby createdby suggestedby rejectedby>{ + warn $$suggestion_ref{$element}; + if ($$suggestion_ref{$element}){ + my $member=GetMember(borrowernumber=>$$suggestion_ref{$element}); + my $presentation_string=$$member{firstname}." ".$$member{surname}." ".GetBranchName($$member{branchcode})." ".$$member{description}." ".$$member{category_type}; + warn $presentation_string; + $template->param($element."information"=>$presentation_string); + } +} +$template->param( + %$suggestion_ref, + "op_$op" => 1, + dateformat => C4::Context->preference("dateformat"), + "op" =>$op, +); + + +#################### +## Initializing selection lists + +#branch display management +my $onlymine=C4::Context->preference('IndependantBranches') && + C4::Context->userenv && + C4::Context->userenv->{flags}!=1 && + C4::Context->userenv->{branch}; +my $branches = GetBranches($onlymine); +my @branchloop; + +foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) { + my %row = ( + value => $thisbranch, + branchname => $branches->{$thisbranch}->{'branchname'}, + selected => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter) + ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'}) + ); + push @branchloop, \%row; +} +$branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter); + +$template->param( branchloop => \@branchloop, + branchfilter => $branchfilter); + +# the index parameter is different for item-level itemtypes +my $supportlist=GetSupportList(); +foreach my $support(@$supportlist){ + $$support{'selected'}= $$support{'code'} eq $$suggestion_ref{'itemtype'}; + if ($$support{'imageurl'}){ + $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} ); + } + else { + delete $$support{'imageurl'} + } +} +$template->param(itemtypeloop=>$supportlist); + +#Budgets management +my $searchbudgets={ budget_branchcode=>$branchfilter} if $branchfilter; +my $budgets = GetBudgets($searchbudgets); + +foreach (@$budgets){ + $_->{'selected'}=1 if ($$suggestion_ref{'budget_id'} && $_{'budget_id'} eq $$suggestion_ref{'budget_id'}) +}; + +$template->param( budgetsloop => $budgets); + +my %hashlists; +foreach my $field qw(managedby acceptedby suggestedby STATUS){ + my $values_list; + $values_list=GetDistinctValues("suggestions.".$field) ; + my @codes_list = map{ + { 'code'=>$$_{'value'}, + 'desc'=>GetCriteriumDesc($$_{'value'},$field), + 'selected'=> $$_{'value'} eq $$suggestion_ref{$field} + } + } @$values_list; + $hashlists{lc($field)."_loop"}=\@codes_list; +} +$template->param(%hashlists); + +output_html_with_http_headers $input, $cookie, $template->output; diff --git a/t/db_dependent/Suggestions.t b/t/db_dependent/Suggestions.t new file mode 100644 index 0000000000..001dc245d4 --- /dev/null +++ b/t/db_dependent/Suggestions.t @@ -0,0 +1,24 @@ +#!/usr/bin/perl +# +# This Koha test module is a stub! +# Add more tests here!!! + +use strict; +use warnings; +use Data::Dumper; + +use C4::Suggestions; + +use Test::More tests =>6; + +BEGIN { + use_ok('C4::Suggestions'); +} + +my ($suggestionid, $suggestion, $status); +ok($suggestionid= NewSuggestion( {title=>'Petit traité de philosohpie',author=>'Hubert de Chardassé',publishercode=>'Albin Michel'} ), "NewSuggestion OK"); +ok($suggestion= GetSuggestion( $suggestionid), "GetSuggestion OK"); +ok($status= ModSuggestion( {title=>'test Modif Simple', suggestionid=>$suggestionid} ), "ModSuggestion Simple OK"); +ok($status= ModSuggestion( {STATUS=>'STALLED', suggestionid=>$suggestionid} ), "ModSuggestion Status OK"); +ok(@{SearchSuggestion( {STATUS=>'STALLED'} )}>0, "SearchSuggestion Status OK"); + -- 2.20.1