1 %# $Id: edit.mc,v 1.38 2008-10-08 14:50:54 mike Exp $
4 $id => undef ### should be extracted using utf8param()
8 Since this form is used in many different situations, some care is
9 merited in considering the possibilities:
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 ----------------------------------------------------------------------
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
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;
35 my $conn = new ZOOM::Connection("localhost:8018/IR-Explain---1", 0,
36 user => "admin", password => "fruitbat",
37 elementSetName => "zeerex");
39 my $protocol = utf8param($r, "protocol");
40 my $host = utf8param($r, "host");
41 my $port = utf8param($r, "port");
42 my $dbname = utf8param($r, "dbname");
44 if ((!defined $port || $port eq "") &&
45 (defined $protocol && $protocol ne "")) {
46 # Port-guessing based on defaults for each protocol
47 $port = $protocol eq "Z39.50" ? 210 : 80;
48 warn "guessed port $port";
49 $r->param(port => $port);
53 if (defined $protocol && $protocol ne "" &&
54 defined $host && $host ne "" &&
55 defined $port && $port ne "" &&
56 defined $dbname && $dbname ne "") {
57 $newid = irspy_make_identifier($protocol, $host, $port, $dbname);
60 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
64 # About to enter data for a new record
65 # Nothing to do at this stage
66 } elsif (!defined $newid) {
67 # Tried to create new record but data is insufficient
68 print qq[<p class="error">
69 Please specify protocol, host, port and database name.</p>\n];
71 } elsif ($host !~ /^\w+\.[\w.]*\w$/i) {
72 print qq[<p class="error">
73 This host name is not valid.</p>\n];
76 } elsif ($port !~ /^\d*$/i) {
77 print qq[<p class="error">
78 This port number is not valid.</p>\n];
82 # Creating new record, all necessary data is present. Check
83 # that the new record is not a duplicate of an existing one.
84 my $rs = $conn->search(new ZOOM::Query::CQL(cql_target($newid)));
85 if ($rs->size() > 0) {
86 my $qnewid = xml_encode(uri_escape($newid));
87 print qq[<p class="error">
89 <a href='?op=edit&id=$newid'>a record</a>
90 for this protocol, host, port and database name.
96 # assert(defined $id);
97 # Copying or editing an existing record: fetch it for editing
98 my $query = cql_target($id);
99 my $rs = $conn->search(new ZOOM::Query::CQL($query));
100 if ($rs->size() > 0) {
101 $rec = $rs->record(0);
103 ### Is this an error? I don't think the UI will ever provoke it
104 print qq[<p class="error">(New ID specified.)</p>\n];
109 my $xc = irspy_xpath_context($rec);
112 [ title => 0, "Name", "e:databaseInfo/e:title",
114 [ country => $m->comp("country-list.mc"),
115 "Country", "i:status/i:country" ],
116 [ protocol => [ qw(Z39.50 SRW SRU) ],
117 "Protocol", "e:serverInfo/\@protocol" ],
118 [ host => 0, "Host", "e:serverInfo/e:host" ],
119 [ port => 0, "Port", "e:serverInfo/e:port" ],
120 [ dbname => 0, "Database Name", "e:serverInfo/e:database",
122 [ type => $m->comp("libtype-list.mc"),
123 "Type of Library", "i:status/i:libraryType" ],
124 [ username => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
126 [ password => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
128 [ description => 5, "Description", "e:databaseInfo/e:description",
130 [ author => 0, "Author", "e:databaseInfo/e:author",
131 qw(e:title e:description) ],
132 [ hosturl => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
133 [ contact => 0, "Contact", "e:databaseInfo/e:contact",
134 qw(e:title e:description) ],
135 [ extent => 3, "Extent", "e:databaseInfo/e:extent",
136 qw(e:title e:description) ],
137 [ history => 5, "History", "e:databaseInfo/e:history",
138 qw(e:title e:description) ],
140 # This list was produced by feeding
141 # http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
143 # awk -F'|' '$3 {print$4}'
144 # and shortening some of the longer names by hand
172 "Catalan; Valencian",
176 "Church Slavic; Old Slavonic",
183 "Divehi; Dhivehi; Maldivian",
197 "Gaelic; Scottish Gaelic",
201 "Greek, Modern (1453-)",
204 "Haitian; Haitian Creole",
223 "Kalaallisut; Greenlandic",
235 "Kuanyama; Kwanyama",
240 "Limburgan; Limburger; Limburgish",
243 "Luxembourgish; Letzeburgesch",
258 "Ndebele, South; South Ndebele",
259 "Ndebele, North; North Ndebele",
265 "Chichewa; Chewa; Nyanja",
266 "Occitan (post 1500); Provençal",
286 "Sinhala; Sinhalese",
295 "Spanish; Castilian",
310 "Tonga (Tonga Islands)",
332 "Language of Records", "e:databaseInfo/e:langUsage",
333 qw(e:title e:description) ],
334 [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
335 qw(e:title e:description) ],
336 [ subjects => 2, "Subjects", "e:databaseInfo/e:subjects",
337 qw(e:title e:description) ],
340 # Update record with submitted data
341 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
343 foreach my $key ($r->param()) {
344 next if grep { $key eq $_ } qw(op id update);
345 $data{$key} = utf8param($r, $key);
347 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
348 if ($update && @changedFields) {
349 my @x = modify_xml_document($xc, { dateModified =>
351 "Data/time modified",
352 "e:metaInfo/e:dateModified" ] },
353 { dateModified => isodate(time()) });
354 die "Didn't set dateModified!" if !@x;
355 ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode(),
356 $op eq "edit" ? $id : undef);
360 <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
361 % if ($update && @changedFields) {
362 % my $nchanges = @changedFields;
363 <p style="font-weight: bold">
364 The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
365 Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
366 <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
368 % return if $op eq "new";
371 Although anyone is allowed to add a new target, please note that
372 <b>you will not be able to edit the newly added target unless you
373 have administrator privileges</b>. So please be sure that the
374 details are correct before submitting them.
376 <form method="get" action="">
377 <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
379 foreach my $ref (@fields) {
380 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
383 <th><% $caption %></th>
385 % my $rawval = $xc->findvalue($xpath);
386 % my $val = xml_encode($rawval, "");
388 <select name="<% $name %>" size="1">
389 % foreach my $option (@$nlines) {
390 <option value="<% xml_encode($option) %>"<%
391 ($rawval eq $option ? ' selected="selected"' : "")
392 %>><% xml_encode($option) %></option>
395 % } elsif ($nlines) {
396 <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
398 <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
402 <& /help/link.mc, help => "edit/$name" &>
407 <td align="right" colspan="2">
408 <input type="submit" name="update" value="Update"/>
409 % $op = "edit" if $op eq "new" && defined $update;
410 <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
411 % $id = $newid if defined $newid;
413 <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
420 if (@changedFields && 0) {
421 my $x = $xc->getContextNode()->toString();
423 #$x =~ s/$/<br\/>/gm;
424 print "<pre>$x</pre>\n";