IMPORTANT : biblioitems.editionresponsibility field was sometimes spelled with a 'a'
[koha_gimpoz] / admin / letter.pl
index 1f87dc0..ff522cd 100755 (executable)
@@ -43,17 +43,15 @@ use C4::Date;
 use C4::Auth;
 use C4::Context;
 use C4::Output;
-use C4::Interface::CGI::Output;
-use C4::Search;
-use HTML::Template;
+
 
 sub StringSearch  {
-       my ($env,$searchstring,$type)=@_;
+       my ($searchstring,$type)=@_;
        my $dbh = C4::Context->dbh;
        $searchstring=~ s/\'/\\\'/g;
        my @data=split(' ',$searchstring);
        my $count=@data;
-       my $sth=$dbh->prepare("Select * from letter where (code like ?) order by code");
+       my $sth=$dbh->prepare("Select * from letter where (code like ?) order by module,code");
        $sth->execute("$data[0]%");
        my @results;
        my $cnt=0;
@@ -71,13 +69,14 @@ my $searchfield=$input->param('searchfield');
 my $offset=$input->param('offset');
 my $script_name="/cgi-bin/koha/admin/letter.pl";
 my $code=$input->param('code');
+my $module = $input->param('module');
 my $pagesize=20;
 my $op = $input->param('op');
 $searchfield=~ s/\,//g;
 my $dbh = C4::Context->dbh;
 
 my ($template, $borrowernumber, $cookie)
-    = get_template_and_user({template_name => "parameters/letter.tmpl",
+    = get_template_and_user({template_name => "tools/letter.tmpl",
                             query => $input,
                             type => "intranet",
                             authnotrequired => 0,
@@ -100,20 +99,35 @@ if ($op eq 'add_form') {
        #---- if primkey exists, it's a modify action, so read values to modify...
        my $letter;
        if ($code) {
-               my $sth=$dbh->prepare("select * from letter where code=?");
-               $sth->execute($code);
+               my $sth=$dbh->prepare("select * from letter where module=? and code=?");
+               $sth->execute($module,$code);
                $letter=$sth->fetchrow_hashref;
                $sth->finish;
        }
        # build field list
        my @SQLfieldname;
+       my %line = ('value' => "LibrarianFirstname", 'text' => 'LibrarianFirstname');
+       push @SQLfieldname, \%line;
+       my %line = ('value' => "LibrarianSurname", 'text' => 'LibrarianSurname');
+       push @SQLfieldname, \%line;
+       my %line = ('value' => "LibrarianEmailaddress", 'text' => 'LibrarianEmailaddress');
+       push @SQLfieldname, \%line;
+       my $sth2=$dbh->prepare("SHOW COLUMNS from branches");
+       $sth2->execute;
+       my %line = ('value' => "", 'text' => '---BRANCHES---');
+       push @SQLfieldname, \%line;
+       while ((my $field) = $sth2->fetchrow_array) {
+               my %line = ('value' => "branches.".$field, 'text' => "branches.".$field);
+               push @SQLfieldname, \%line;
+       }
        my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
        $sth2->execute;
        my %line = ('value' => "", 'text' => '---BIBLIO---');
+
        push @SQLfieldname, \%line;
        while ((my $field) = $sth2->fetchrow_array) {
+               # note : %line is redefined, otherwise \%line contains the same value for every entry of the list
                my %line = ('value' => "biblio.".$field, 'text' => "biblio.".$field);
-               warn "$line{value}";
                push @SQLfieldname, \%line;
        }
        my $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
@@ -122,7 +136,6 @@ if ($op eq 'add_form') {
        push @SQLfieldname, \%line;
        while ((my $field) = $sth2->fetchrow_array) {
                my %line = ('value' => "biblioitems.".$field, 'text' => "biblioitems.".$field);
-               warn "$line{value}";
                push @SQLfieldname, \%line;
        }
        my %line = ('value' => "", 'text' => '---ITEMS---');
@@ -136,7 +149,6 @@ if ($op eq 'add_form') {
        push @SQLfieldname, \%line;
        while ((my $field) = $sth2->fetchrow_array) {
                my %line = ('value' => "borrowers.".$field, 'text' => "borrowers.".$field);
-               warn "$line{value}";
                push @SQLfieldname, \%line;
        }
        if ($code) {
@@ -145,16 +157,17 @@ if ($op eq 'add_form') {
        } else {
            $template->param(adding => 1);
        }
-       $template->param(name => $letter->{name},
+       $template->param(name => $letter->{name},title => $letter->{title},
                                        content => $letter->{content},
+                                       $letter->{module} => 1,
                                        SQLfieldname => \@SQLfieldname,);
                                                                                                        # END $OP eq ADD_FORM
 ################## ADD_VALIDATE ##################################
 # called by add_form, used to insert/modify data in DB
 } elsif ($op eq 'add_validate') {
        my $dbh = C4::Context->dbh;
-       my $sth=$dbh->prepare("replace letter (code,name,content) values (?,?,?)");
-       $sth->execute($input->param('code'),$input->param('name'),$input->param('content'));
+       my $sth=$dbh->prepare("replace letter (module,code,name,title,content) values (?,?,?,?,?)");
+       $sth->execute($input->param('module'),$input->param('code'),$input->param('name'),$input->param('title'),$input->param('content'));
        $sth->finish;
         print $input->redirect("letter.pl");
         exit;
@@ -167,6 +180,7 @@ if ($op eq 'add_form') {
        $sth->execute($code);
        my $data=$sth->fetchrow_hashref;
        $sth->finish;
+       $template->param(module => $data->{module});
        $template->param(code => $code);
        $template->param(name => $data->{'name'});
        $template->param(content => $data->{'content'});
@@ -176,8 +190,9 @@ if ($op eq 'add_form') {
 } elsif ($op eq 'delete_confirmed') {
        my $dbh = C4::Context->dbh;
        my $code=uc($input->param('code'));
-       my $sth=$dbh->prepare("delete from letter where code=?");
-       $sth->execute($code);
+       my $module=$input->param('module');
+       my $sth=$dbh->prepare("delete from letter where module=? and code=?");
+       $sth->execute($module,$code);
        $sth->finish;
         print $input->redirect("letter.pl");
         return;
@@ -188,8 +203,7 @@ if ($op eq 'add_form') {
                $template->param(search => 1);
                $template->param(searchfield => $searchfield);
        }
-       my $env;
-       my ($count,$results)=StringSearch($env,$searchfield,'web');
+       my ($count,$results)=StringSearch($searchfield,'web');
        my $toggle="white";
        my @loop_data =();
        for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
@@ -200,6 +214,7 @@ if ($op eq 'add_form') {
                }
           my %row_data;
           $row_data{toggle} = $toggle;
+          $row_data{module} = $results->[$i]{'module'};
           $row_data{code} = $results->[$i]{'code'};
           $row_data{name} = $results->[$i]{'name'};
           push(@loop_data, \%row_data);