Refactor to include form.mc inline.
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 %# $Id: edit.mc,v 1.13 2006-11-16 11:53:33 mike Exp $
2 <%args>
3 $id => undef
4 </%args>
5 <%perl>
6 my $conn = new ZOOM::Connection("localhost:3313/IR-Explain---1", 0,
7                                 user => "admin", password => "fruitbat");
8 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
9 if (defined $id && $id ne "") {
10     $conn->option(elementSetName => "zeerex");
11     my $qid = $id;
12     $qid =~ s/"/\\"/g;
13     my $query = qq[rec.id="$qid"];
14     my $rs = $conn->search(new ZOOM::Query::CQL($query));
15     my $n = $rs->size();
16     if ($n == 0) {
17         $id = undef;
18     } else {
19         $rec = $rs->record(0);
20     }
21 }
22
23 my $xc = irspy_xpath_context($rec);
24 my @fields =
25     (
26      [ protocol     => [ qw(Z39.50 SRW SRU SRW/U) ],
27        "Protocol", "e:serverInfo/\@protocol" ],
28      [ host         => 0, "Host", "e:serverInfo/e:host" ],
29      [ port         => 0, "Port", "e:serverInfo/e:port" ],
30      [ dbname       => 0, "Database Name", "e:serverInfo/e:database",
31        qw(e:host e:port) ],
32      [ type         => [ qw(Academic Public Corporate Special National Education Other) ],
33        "Type of Library", "i:status/i:libraryType" ],
34      [ country      => 0, "Country", "i:status/i:country" ],
35      [ username     => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
36        qw() ],
37      [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
38        qw(e:user) ],
39      [ title        => 0, "Title", "e:databaseInfo/e:title",
40        qw() ],
41      [ description  => 5, "Description", "e:databaseInfo/e:description",
42        qw(e:title) ],
43      [ author       => 0, "Author", "e:databaseInfo/e:author",
44        qw(e:title e:description) ],
45      [ hosturl       => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
46      [ contact      => 0, "Contact", "e:databaseInfo/e:contact",
47        qw(e:title e:description) ],
48      [ extent       => 3, "Extent", "e:databaseInfo/e:extent",
49        qw(e:title e:description) ],
50      [ history      => 5, "History", "e:databaseInfo/e:history",
51        qw(e:title e:description) ],
52      [ language     => 0, "Language of Records", "e:databaseInfo/e:langUsage",
53        qw(e:title e:description) ],
54      [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
55        qw(e:title e:description) ],
56      [ subjects     => 2, "Subjects", "e:databaseInfo/e:subjects",
57        qw(e:title e:description) ],
58      );
59
60 my $nchanges = 0;
61 my $update = $r->param("update");
62 if (defined $update) {
63     # Update record with submitted data
64     my %fieldsByKey = map { ( $_->[0], $_) } @fields;
65     my %data;
66     foreach my $key ($r->param()) {
67         next if grep { $key eq $_ } qw(id update new copy);
68         $data{$key} = $r->param($key);
69     }
70
71     $nchanges = modify_xml_document($xc, \%fieldsByKey, \%data);
72     if ($nchanges) {
73         ### Set e:metaInfo/e:dateModified
74     }
75     ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode());
76 }
77 </%perl>
78  <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
79 % if ($nchanges) {
80  <p style="font-weight: bold">
81   The record has been <% $r->param("new") ? "created" : "updated" %>.<br/>
82   Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>.
83  </p>
84 % }
85  <form method="get" action="">
86   <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
87 <%perl>
88 foreach my $ref (@fields) {
89     my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
90 </%perl>
91    <tr>
92     <th><% $caption %></th>
93     <td>
94 % my $rawdata = $xc->findvalue($xpath);
95 % my $data = xml_encode($rawdata, "");
96 % if (ref $nlines) {
97      <select name="<% $name %>" size="1">
98 %     foreach my $val (@$nlines) {
99       <option value="<% $val %>"
100 % print ' selected="selected"' if $rawdata eq $val;
101         ><% $val %></option>
102 %     }
103      </select>
104 % } elsif ($nlines) {
105      <textarea name="<% $name %>" rows="<% $nlines %>" cols="55"><% $data %></textarea>
106 % } else {
107      <input name="<% $name %>" type="text" size="60" value="<% $data %>">
108 % }
109     </td>
110    </tr>
111 %   }
112    <tr>
113     <td align="right" colspan="2">
114      <input type="submit" name="update" value="Update"/>
115 % if (defined $id) {
116      <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
117 % } else {
118      <input type="hidden" name="new" value="1"/>
119 % }
120     </td>
121    </tr>
122   </table>
123  </form>
124 <%perl>
125     if ($nchanges && 0) {
126         my $x = $xc->getContextNode()->toString();
127         $x = xml_encode($x);
128         #$x =~ s/$/<br\/>/gm;
129         print "<pre>$x</pre>\n";
130     }
131 </%perl>