Big, big rewrite. Not only does it now work properly in all three
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 %# $Id: edit.mc,v 1.21 2006-11-29 18:22:08 mike Exp $
2 <%args>
3 $op
4 $id => undef
5 $update => undef
6 </%args>
7 <%doc>
8 Since this form is used in many different situations, some care is
9 merited in considering the possibilities:
10
11 Situation                                       Op      ID      Update
12 ----------------------------------------------------------------------
13 Blank form for adding a new target              new
14 New target rejected, changes required           new             X
15 New target accepted and added                   new             X
16 ---------------------------------------------------------------------
17 Existing target to be edited                    edit    X
18 Edit rejected, changes required                 edit    X       X
19 Target successfully updated                     edit    X       X
20 ----------------------------------------------------------------------
21 Existing target to be copied                    copy    X
22 New target rejected, changes required           copy    X       X
23 New target accepted and added                   copy    X       X
24 ----------------------------------------------------------------------
25
26 Submissions, whether of new targets, edits or copies, may be rejected
27 due either to missing mandatory fields or host/name/port that form a
28 duplicate ID.
29 </%doc>
30 <%perl>
31 # Sanity checking
32 die "op = new but id defined" if $op eq "new" && defined $id;
33 die "op != new but id undefined" if $op ne "new" && !defined $id;
34
35 my $conn = new ZOOM::Connection("localhost:3313/IR-Explain---1", 0,
36                                 user => "admin", password => "fruitbat",
37                                 elementSetName => "zeerex");
38 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
39 if (defined $id && ($op ne "copy" || !$update)) {
40     # Existing record
41     my $query = 'rec.id="' . cql_quote($id) . '"';
42     my $rs = $conn->search(new ZOOM::Query::CQL($query));
43     if ($rs->size() > 0) {
44         $rec = $rs->record(0);
45     } else {
46         ### Is this an error?  I don't think the UI will ever provoke it
47         print qq[<p class="error">(New ID specified.)</p>\n];
48         $id = undef;
49     }
50
51 } else {
52     # No ID supplied -- this is a brand new record
53     my $host = $r->param("host");
54     my $port = $r->param("port");
55     my $dbname = $r->param("dbname");
56     if (!defined $host || $host eq "" ||
57         !defined $port || $port eq "" ||
58         !defined $dbname || $dbname eq "") {
59         print qq[<p class="error">
60 You must specify host, port and database name.</p>\n] if $update;
61         undef $update;
62     } else {
63         my $query = cql_target($host, $port, $dbname);
64         my $rs = $conn->search(new ZOOM::Query::CQL($query));
65         if ($rs->size() > 0) {
66             my $fakeid = xml_encode(uri_escape("$host:$port/$dbname"));
67             print qq[<p class="error">
68 There is already
69 <a href='?op=edit&amp;id=$fakeid'>a record</a>
70 for this host, port and database name.
71 </p>\n];
72             undef $update;
73         }
74     }
75 }
76
77 my $xc = irspy_xpath_context($rec);
78 my @fields =
79     (
80      [ protocol     => [ qw(Z39.50 SRW SRU SRW/U) ],
81        "Protocol", "e:serverInfo/\@protocol" ],
82      [ host         => 0, "Host", "e:serverInfo/e:host" ],
83      [ port         => 0, "Port", "e:serverInfo/e:port" ],
84      [ dbname       => 0, "Database Name", "e:serverInfo/e:database",
85        qw(e:host e:port) ],
86      [ type         => [ qw(Academic Public Corporate Special National Education Other) ],
87        "Type of Library", "i:status/i:libraryType" ],
88      [ country      => 0, "Country", "i:status/i:country" ],
89      [ username     => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
90        qw() ],
91      [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
92        qw(e:user) ],
93      [ title        => 0, "Title", "e:databaseInfo/e:title",
94        qw() ],
95      [ description  => 5, "Description", "e:databaseInfo/e:description",
96        qw(e:title) ],
97      [ author       => 0, "Author", "e:databaseInfo/e:author",
98        qw(e:title e:description) ],
99      [ hosturl       => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
100      [ contact      => 0, "Contact", "e:databaseInfo/e:contact",
101        qw(e:title e:description) ],
102      [ extent       => 3, "Extent", "e:databaseInfo/e:extent",
103        qw(e:title e:description) ],
104      [ history      => 5, "History", "e:databaseInfo/e:history",
105        qw(e:title e:description) ],
106      [ language     => 0, "Language of Records", "e:databaseInfo/e:langUsage",
107        qw(e:title e:description) ],
108      [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
109        qw(e:title e:description) ],
110      [ subjects     => 2, "Subjects", "e:databaseInfo/e:subjects",
111        qw(e:title e:description) ],
112      );
113
114 # Update record with submitted data
115 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
116 my %data;
117 foreach my $key ($r->param()) {
118     next if grep { $key eq $_ } qw(op id update);
119     $data{$key} = $r->param($key);
120 }
121 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
122 if ($update && @changedFields) {
123     my @x = modify_xml_document($xc, { dateModified =>
124                                            [ dateModified => 0,
125                                              "Data/time modified",
126                                              "e:metaInfo/e:dateModified" ] },
127                                 { dateModified => isodate(time()) });
128     die "Didn't set dateModified!" if !@x;
129     ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode());
130 }
131
132 </%perl>
133  <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
134 % if ($update && @changedFields) {
135 %     my $nchanges = @changedFields;
136  <p style="font-weight: bold">
137   The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
138   Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
139   <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
140  </p>
141 % }
142  <form method="get" action="">
143   <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
144 <%perl>
145 foreach my $ref (@fields) {
146     my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
147 </%perl>
148    <tr>
149     <th><% $caption %></th>
150     <td>
151 % my $rawval = $xc->findvalue($xpath);
152 % my $val = xml_encode($rawval, "");
153 % if (ref $nlines) {
154      <select name="<% $name %>" size="1">
155 %     foreach my $option (@$nlines) {
156       <option value="<% xml_encode($option) %>"<%
157         ($rawval eq $option ? ' selected="selected"' : "")
158         %>><% xml_encode($option) %></option>
159 %     }
160      </select>
161 % } elsif ($nlines) {
162      <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
163 % } else {
164      <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
165 % }
166     </td>
167    </tr>
168 %   }
169    <tr>
170     <td align="right" colspan="2">
171      <input type="submit" name="update" value="Update"/>
172      <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
173 % if (defined $id) {
174      <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
175 % }
176     </td>
177    </tr>
178   </table>
179  </form>
180 <%perl>
181     if (@changedFields && 0) {
182         my $x = $xc->getContextNode()->toString();
183         $x = xml_encode($x);
184         #$x =~ s/$/<br\/>/gm;
185         print "<pre>$x</pre>\n";
186     }
187 </%perl>