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