1 %# $Id: edit.mc,v 1.37 2007-08-23 14:29:18 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];
75 } elsif ($port !~ /^\d*$/i) {
76 print qq[<p class="error">
77 This port number is not valid.</p>\n];
80 # Creating new record, all necessary data is present. Check
81 # that the new record is not a duplicate of an existing one.
82 my $rs = $conn->search(new ZOOM::Query::CQL(cql_target($newid)));
83 if ($rs->size() > 0) {
84 my $qnewid = xml_encode(uri_escape($newid));
85 print qq[<p class="error">
87 <a href='?op=edit&id=$newid'>a record</a>
88 for this protocol, host, port and database name.
94 # assert(defined $id);
95 # Copying or editing an existing record: fetch it for editing
96 my $query = cql_target($id);
97 my $rs = $conn->search(new ZOOM::Query::CQL($query));
98 if ($rs->size() > 0) {
99 $rec = $rs->record(0);
101 ### Is this an error? I don't think the UI will ever provoke it
102 print qq[<p class="error">(New ID specified.)</p>\n];
107 my $xc = irspy_xpath_context($rec);
110 [ title => 0, "Name", "e:databaseInfo/e:title",
112 [ country => $m->comp("country-list.mc"),
113 "Country", "i:status/i:country" ],
114 [ protocol => [ qw(Z39.50 SRW SRU) ],
115 "Protocol", "e:serverInfo/\@protocol" ],
116 [ host => 0, "Host", "e:serverInfo/e:host" ],
117 [ port => 0, "Port", "e:serverInfo/e:port" ],
118 [ dbname => 0, "Database Name", "e:serverInfo/e:database",
120 [ type => $m->comp("libtype-list.mc"),
121 "Type of Library", "i:status/i:libraryType" ],
122 [ username => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
124 [ password => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
126 [ description => 5, "Description", "e:databaseInfo/e:description",
128 [ author => 0, "Author", "e:databaseInfo/e:author",
129 qw(e:title e:description) ],
130 [ hosturl => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
131 [ contact => 0, "Contact", "e:databaseInfo/e:contact",
132 qw(e:title e:description) ],
133 [ extent => 3, "Extent", "e:databaseInfo/e:extent",
134 qw(e:title e:description) ],
135 [ history => 5, "History", "e:databaseInfo/e:history",
136 qw(e:title e:description) ],
138 # This list was produced by feeding
139 # http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
141 # awk -F'|' '$3 {print$4}'
142 # and shortening some of the longer names by hand
170 "Catalan; Valencian",
174 "Church Slavic; Old Slavonic",
181 "Divehi; Dhivehi; Maldivian",
195 "Gaelic; Scottish Gaelic",
199 "Greek, Modern (1453-)",
202 "Haitian; Haitian Creole",
221 "Kalaallisut; Greenlandic",
233 "Kuanyama; Kwanyama",
238 "Limburgan; Limburger; Limburgish",
241 "Luxembourgish; Letzeburgesch",
256 "Ndebele, South; South Ndebele",
257 "Ndebele, North; North Ndebele",
263 "Chichewa; Chewa; Nyanja",
264 "Occitan (post 1500); Provençal",
284 "Sinhala; Sinhalese",
293 "Spanish; Castilian",
308 "Tonga (Tonga Islands)",
330 "Language of Records", "e:databaseInfo/e:langUsage",
331 qw(e:title e:description) ],
332 [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
333 qw(e:title e:description) ],
334 [ subjects => 2, "Subjects", "e:databaseInfo/e:subjects",
335 qw(e:title e:description) ],
338 # Update record with submitted data
339 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
341 foreach my $key ($r->param()) {
342 next if grep { $key eq $_ } qw(op id update);
343 $data{$key} = utf8param($r, $key);
345 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
346 if ($update && @changedFields) {
347 my @x = modify_xml_document($xc, { dateModified =>
349 "Data/time modified",
350 "e:metaInfo/e:dateModified" ] },
351 { dateModified => isodate(time()) });
352 die "Didn't set dateModified!" if !@x;
353 ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode(),
354 $op eq "edit" ? $id : undef);
358 <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
359 % if ($update && @changedFields) {
360 % my $nchanges = @changedFields;
361 <p style="font-weight: bold">
362 The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
363 Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
364 <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
366 % return if $op eq "new";
369 Although anyone is allowed to add a new target, please note that
370 <b>you will not be able to edit the newly added target unless you
371 have administrator privileges</b>. So please be sure that the
372 details are correct before submitting them.
374 <form method="get" action="">
375 <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
377 foreach my $ref (@fields) {
378 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
381 <th><% $caption %></th>
383 % my $rawval = $xc->findvalue($xpath);
384 % my $val = xml_encode($rawval, "");
386 <select name="<% $name %>" size="1">
387 % foreach my $option (@$nlines) {
388 <option value="<% xml_encode($option) %>"<%
389 ($rawval eq $option ? ' selected="selected"' : "")
390 %>><% xml_encode($option) %></option>
393 % } elsif ($nlines) {
394 <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
396 <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
400 <& /help/link.mc, help => "edit/$name" &>
405 <td align="right" colspan="2">
406 <input type="submit" name="update" value="Update"/>
407 % $op = "edit" if $op eq "new" && defined $update;
408 <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
409 % $id = $newid if defined $newid;
411 <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
418 if (@changedFields && 0) {
419 my $x = $xc->getContextNode()->toString();
421 #$x =~ s/$/<br\/>/gm;
422 print "<pre>$x</pre>\n";