Code Cleaning : AuthoritiesMARC.
[koha_ffzg] / authorities / authorities-home.pl
index 3b3d78b..c68001a 100755 (executable)
@@ -22,93 +22,32 @@ use strict;
 require Exporter;
 use CGI;
 use C4::Auth;
-use HTML::Template;
+
 use C4::Context;
-use C4::Search;
 use C4::Auth;
 use C4::Output;
 use C4::Interface::CGI::Output;
 use C4::AuthoritiesMarc;
-use C4::SearchMarc;
-use C4::Catalogue;
+use C4::Acquisition;
 use C4::Koha; # XXX subfield_is_koha_internal_p
-
-# Creates the list of active tags using the active MARC configuration
-sub create_marclist {
-       my $dbh = C4::Context->dbh;
-       my $tagslib = AUTHgettagslib($dbh,1);
-       my @marcarray;
-       push @marcarray,"";
-       my $widest_menu_item_width = 0;
-       for (my $pass = 1; $pass <= 2; $pass += 1)
-       {
-               for (my $tabloop = 0; $tabloop<=9;$tabloop++)
-               {
-                       my $separator_inserted_p = 0; # FIXME... should not use!!
-                       foreach my $tag (sort(keys (%{$tagslib})))
-                       {
-                               foreach my $subfield (sort(keys %{$tagslib->{$tag}}))
-                               {
-                                       next if subfield_is_koha_internal_p($subfield);
-                                       next unless ($tagslib->{$tag}->{$subfield}->{tab} eq $tabloop);
-                                       my $menu_item = "$tag$subfield - $tagslib->{$tag}->{$subfield}->{lib}";
-                                       if ($pass == 1)
-                                       {
-                                               $widest_menu_item_width = length $menu_item if($widest_menu_item_width < length $menu_item);
-                                       } else {
-                                               if (!$separator_inserted_p)
-                                               {
-                                                       my $w = int(($widest_menu_item_width - 3 + 0.5)/2);
-                                                       my $s = ('-' x ($w * 4/5));
-                                                       push @marcarray,  "$s $tabloop $s";
-                                                       $separator_inserted_p = 1;
-                                               }
-                                       push @marcarray, $menu_item;
-                                       }
-                               }
-                       }
-               }
-       }
-       return \@marcarray;
-}
-
-# Creates a scrolling list with the associated default value.
-# Using more than one scrolling list in a CGI assigns the same default value to all the
-# scrolling lists on the page !?!? That's why this function was written.
-sub create_scrolling_list {
-       my ($params) = @_;
-       my $scrollist = sprintf("<select name=\"%s\" size=\"%d\" onChange='%s'>\n", $params->{'name'}, $params->{'size'}, $params->{'onChange'});
-
-       foreach my $tag (@{$params->{'values'}})
-       {
-               my $selected = "selected " if($params->{'default'} eq $tag);
-               $scrollist .= sprintf("<option %svalue=\"%s\">%s</option>\n", $selected, $tag, $tag);
-       }
-
-       $scrollist .= "</select>\n";
-
-       return $scrollist;
-}
+use C4::Biblio;
 
 my $query=new CGI;
 my $op = $query->param('op');
 my $authtypecode = $query->param('authtypecode');
 my $dbh = C4::Context->dbh;
 
-my $startfrom=$query->param('startfrom');
-$startfrom=0 if(!defined $startfrom);
+my $authid=$query->param('authid');
 my ($template, $loggedinuser, $cookie);
-my $resultsperpage;
 
 my $authtypes = getauthtypes;
 my @authtypesloop;
-foreach my $thisauthtype (keys %$authtypes) {
+foreach my $thisauthtype (sort { $authtypes->{$a} <=> $authtypes->{$b} } keys %$authtypes) {
        my $selected = 1 if $thisauthtype eq $authtypecode;
        my %row =(value => $thisauthtype,
                                selected => $selected,
                                authtypetext => $authtypes->{$thisauthtype}{'authtypetext'},
                        );
-       warn "X = $authtypes->{$thisauthtype}{'authtypetext'}";
        push @authtypesloop, \%row;
 }
 
@@ -117,103 +56,110 @@ if ($op eq "do_search") {
        my @and_or = $query->param('and_or');
        my @excluding = $query->param('excluding');
        my @operator = $query->param('operator');
-       my @value = $query->param('value');
-
-       $resultsperpage= $query->param('resultsperpage');
-       $resultsperpage = 19 if(!defined $resultsperpage);
        my $orderby = $query->param('orderby');
+       my @value = $query->param('value');
 
-       # builds tag and subfield arrays
-       my @tags;
-
-       foreach my $marc (@marclist) {
-               if ($marc) {
-                       my ($tag,$subfield) = MARCfind_marc_from_kohafield($dbh,$marc);
-                       if ($tag) {
-                               push @tags,$dbh->quote("$tag$subfield");
-                       } else {
-                               push @tags, $dbh->quote(substr($marc,0,4));
-                       }
-               } else {
-                       push @tags, "";
-               }
-       }
-       findseealso($dbh,\@tags);
-       my ($results,$total) = catalogsearch($dbh, \@tags,\@and_or,
-                                                                               \@excluding, \@operator, \@value,
-                                                                               $startfrom*$resultsperpage, $resultsperpage,$orderby);
+    my $startfrom = $query->param('startfrom') || 1;
+    my $resultsperpage = $query->param('resultsperpage') || 19;
+
+       my ($results,$total) = SearchAuthorities(
+        \@marclist,
+        \@and_or,
+        \@excluding,
+        \@operator,
+        \@value,
+        ($startfrom - 1)*$resultsperpage,
+        $resultsperpage,
+        $authtypecode,
+        $orderby
+    );
 
        ($template, $loggedinuser, $cookie)
-               = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
-                               query => $query,
-                               type => 'intranet',
-                               authnotrequired => 0,
-                               flagsrequired => {borrowers => 1},
-                               flagsrequired => {catalogue => 1},
-                               debug => 1,
-                               });
-
-       # multi page display gestion
-       my $displaynext=0;
-       my $displayprev=$startfrom;
-       if(($total - (($startfrom+1)*($resultsperpage))) > 0 ){
-               $displaynext = 1;
-       }
+               = get_template_and_user({
+            template_name => "authorities/searchresultlist.tmpl",
+            query => $query,
+            type => 'intranet',
+            authnotrequired => 0,
+            flagsrequired => {catalogue => 1},
+            debug => 1,
+        });
 
        my @field_data = ();
 
-
+       # we must get parameters once again. Because if there is a mainentry, it
+       # has been replaced by something else during the search, thus the links
+       # next/previous would not work anymore
+       my @marclist_ini = $query->param('marclist');
        for(my $i = 0 ; $i <= $#marclist ; $i++)
        {
-               push @field_data, { term => "marclist", val=>$marclist[$i] };
-               push @field_data, { term => "and_or", val=>$and_or[$i] };
-               push @field_data, { term => "excluding", val=>$excluding[$i] };
-               push @field_data, { term => "operator", val=>$operator[$i] };
-               push @field_data, { term => "value", val=>$value[$i] };
-       }
-
-       my @numbers = ();
-
-       if ($total>$resultsperpage)
-       {
-               for (my $i=1; $i<$total/$resultsperpage+1; $i++)
-               {
-                       if ($i<16)
-                       {
-                       my $highlight=0;
-                       ($startfrom==($i-1)) && ($highlight=1);
-                       push @numbers, { number => $i,
-                                       highlight => $highlight ,
-                                       searchdata=> \@field_data,
-                                       startfrom => ($i-1)};
-                       }
-       }
+               push @field_data, { term => "marclist"  , val=>$marclist_ini[$i] };
+               push @field_data, { term => "and_or"    , val=>$and_or[$i] };
+               push @field_data, { term => "excluding" , val=>$excluding[$i] };
+               push @field_data, { term => "operator"  , val=>$operator[$i] };
+               push @field_data, { term => "value"     , val=>$value[$i] };
        }
 
-       my $from = $startfrom*$resultsperpage+1;
+    # construction of the url of each page
+    my $base_url =
+        'authorities-home.pl?'
+        .join(
+            '&amp;',
+            map { $_->{term}.'='.$_->{val} } @field_data
+        )
+        .'&amp;'
+        .join(
+            '&amp;',
+            map { $_->{term}.'='.$_->{val} } (
+                {term => 'resultsperpage', val => $resultsperpage},
+                {term => 'type'          , val => 'intranet'},
+                {term => 'op'            , val => 'do_search'},
+                {term => 'authtypecode'  , val => $authtypecode},
+                {term => 'orderby'       , val => $orderby},
+            )
+        )
+        ;
+
+       my $from = ($startfrom - 1) * $resultsperpage + 1;
        my $to;
 
-       if($total < (($startfrom+1)*$resultsperpage))
-       {
+       if ($total < $startfrom * $resultsperpage) {
                $to = $total;
-       } else {
-               $to = (($startfrom+1)*$resultsperpage);
        }
-       $template->param(result => $results,
-                                                       startfrom=> $startfrom,
-                                                       displaynext=> $displaynext,
-                                                       displayprev=> $displayprev,
-                                                       resultsperpage => $resultsperpage,
-                                                       startfromnext => $startfrom+1,
-                                                       startfromprev => $startfrom-1,
-                                                       searchdata=>\@field_data,
-                                                       total=>$total,
-                                                       from=>$from,
-                                                       to=>$to,
-                                                       numbers=>\@numbers,
-                                                       );
-
-} elsif ($op eq "AddStatement") {
+    else {
+               $to = $startfrom * $resultsperpage;
+       }
+
+       $template->param(result => $results) if $results;
+
+       $template->param(
+        pagination_bar => pagination_bar(
+            $base_url,
+            int($total/$resultsperpage)+1,
+            $startfrom,
+            'startfrom'
+            ),
+        total=>$total,
+        from=>$from,
+        to=>$to,
+        isEDITORS => $authtypecode eq 'EDITORS',
+    );
+
+} elsif ($op eq "delete") {
+
+       &DelAuthority($authid, 1);
+
+       ($template, $loggedinuser, $cookie)
+               = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
+                               query => $query,
+                               type => 'intranet',
+                               authnotrequired => 0,
+                               flagsrequired => {catalogue => 1},
+                               debug => 1,
+                               });
+#      $template->param("statements" => \@statements,
+#                                              "nbstatements" => $nbstatements);
+}
+elsif ($op eq "AddStatement") {
 
        ($template, $loggedinuser, $cookie)
                = get_template_and_user({template_name => "authorities/authorities-home.tmpl",
@@ -247,6 +193,7 @@ if ($op eq "do_search") {
                my $marclist = create_scrolling_list({name=>"marclist",
                                        values=> $marcarray,
                                        size=> 1,
+                                       -tabindex=>'',
                                        default=>$marcfields[$i],
                                        onChange => "sql_update()"}
                                        );
@@ -280,6 +227,7 @@ if ($op eq "do_search") {
        my $marclist = create_scrolling_list({name=>"marclist",
                                values=> $marcarray,
                                size=>1,
+                               -tabindex=>'',
                                onChange => "sql_update()"});
        push @statements, {"marclist" => $marclist };
 
@@ -296,67 +244,14 @@ else {
                                flagsrequired => {catalogue => 1},
                                debug => 1,
                                });
-       #$template->param(loggedinuser => $loggedinuser);
-
-       my $marcarray = create_marclist();
-
-       my $marclist = CGI::scrolling_list(-name=>"marclist",
-                                       -values=> $marcarray,
-                                       -size=>1,
-                                       -multiple=>0,
-                                       -onChange => "sql_update()",
-                                       );
-
-       my @statements = ();
 
-       # Considering initial search with 3 criterias
-       push @statements, { "marclist" => $marclist, "first" => 1 };
-       push @statements, { "marclist" => $marclist, "first" => 0 };
-       push @statements, { "marclist" => $marclist, "first" => 0 };
-       my $sth=$dbh->prepare("Select itemtype,description from itemtypes order by description");
-       $sth->execute;
-       my  @itemtype;
-       my %itemtypes;
-       push @itemtype, "";
-       $itemtypes{''} = "";
-       while (my ($value,$lib) = $sth->fetchrow_array) {
-               push @itemtype, $value;
-               $itemtypes{$value}=$lib;
-       }
-
-       my $CGIitemtype=CGI::scrolling_list( -name     => 'value',
-                               -values   => \@itemtype,
-                               -labels   => \%itemtypes,
-                               -size     => 1,
-                               -multiple => 0 );
-       $sth->finish;
-
-       my @branches;
-       my @select_branch;
-       my %select_branches;
-       my ($count2,@branches)=branches();
-       push @select_branch, "";
-       $select_branches{''} = "";
-       for (my $i=0;$i<$count2;$i++){
-               push @select_branch, $branches[$i]->{'branchcode'};#
-               $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
-       }
-       my $CGIbranch=CGI::scrolling_list( -name     => 'value',
-                               -values   => \@select_branch,
-                               -labels   => \%select_branches,
-                               -size     => 1,
-                               -multiple => 0 );
-       $sth->finish;
-
-
-       $template->param("statements" => \@statements,
-                       "nbstatements" => 3,
-                       CGIitemtype => $CGIitemtype,
-                       CGIbranch => $CGIbranch,
-                       );
 }
 
-$template->param(authtypesloop => \@authtypesloop);
+$template->param(authtypesloop => \@authtypesloop,
+               intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
+               intranetstylesheet => C4::Context->preference("intranetstylesheet"),
+               IntranetNav => C4::Context->preference("IntranetNav"),
+               );
 
 # Print the page
 output_html_with_http_headers $query, $cookie, $template->output;