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