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