1 %# $Id: edit.mc,v 1.40 2009-04-15 18:16:46 wosch 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 $db = ZOOM::IRSpy::connect_to_registry();
36 my $conn = new ZOOM::Connection($db, 0,
37 user => "admin", password => "fruitbat",
38 elementSetName => "zeerex");
40 my $protocol = utf8param($r, "protocol");
41 my $host = utf8param($r, "host");
42 my $port = utf8param($r, "port");
43 my $dbname = utf8param($r, "dbname");
45 if ((!defined $port || $port eq "") &&
46 (defined $protocol && $protocol ne "")) {
47 # Port-guessing based on defaults for each protocol
48 $port = $protocol eq "Z39.50" ? 210 : 80;
49 warn "guessed port $port";
50 &utf8param($r, port => $port);
54 if (defined $protocol && $protocol ne "" &&
55 defined $host && $host ne "" &&
56 defined $port && $port ne "" &&
57 defined $dbname && $dbname ne "") {
58 $newid = irspy_make_identifier($protocol, $host, $port, $dbname);
61 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
65 # About to enter data for a new record
66 # Nothing to do at this stage
67 } elsif (!defined $newid) {
68 # Tried to create new record but data is insufficient
69 print qq[<p class="error">
70 Please specify protocol, host, port and database name.</p>\n];
72 } elsif ($host !~ /^\w+\.[\w.]*\w$/i) {
73 print qq[<p class="error">
74 This host name is not valid.</p>\n];
77 } elsif ($port !~ /^\d*$/i) {
78 print qq[<p class="error">
79 This port number is not valid.</p>\n];
83 # Creating new record, all necessary data is present. Check
84 # that the new record is not a duplicate of an existing one.
85 my $rs = $conn->search(new ZOOM::Query::CQL(cql_target($newid)));
86 if ($rs->size() > 0) {
87 my $qnewid = xml_encode(uri_escape($newid));
88 print qq[<p class="error">
90 <a href='?op=edit&id=$newid'>a record</a>
91 for this protocol, host, port and database name.
97 # assert(defined $id);
98 # Copying or editing an existing record: fetch it for editing
99 my $query = cql_target($id);
100 my $rs = $conn->search(new ZOOM::Query::CQL($query));
101 if ($rs->size() > 0) {
102 $rec = $rs->record(0);
104 ### Is this an error? I don't think the UI will ever provoke it
105 print qq[<p class="error">(New ID specified.)</p>\n];
110 my $xc = irspy_xpath_context($rec);
113 [ title => 0, "Name", "e:databaseInfo/e:title",
115 [ country => $m->comp("country-list.mc"),
116 "Country", "i:status/i:country" ],
117 [ protocol => [ qw(Z39.50 SRW SRU) ],
118 "Protocol", "e:serverInfo/\@protocol" ],
119 [ host => 0, "Host", "e:serverInfo/e:host" ],
120 [ port => 0, "Port", "e:serverInfo/e:port" ],
121 [ dbname => 0, "Database Name", "e:serverInfo/e:database",
123 [ type => $m->comp("libtype-list.mc"),
124 "Type of Library", "i:status/i:libraryType" ],
125 [ username => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
127 [ password => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
129 [ description => 5, "Description", "e:databaseInfo/e:description",
131 [ author => 0, "Author", "e:databaseInfo/e:author",
132 qw(e:title e:description) ],
133 [ hosturl => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
134 [ contact => 0, "Contact", "e:databaseInfo/e:contact",
135 qw(e:title e:description) ],
136 [ extent => 3, "Extent", "e:databaseInfo/e:extent",
137 qw(e:title e:description) ],
138 [ history => 5, "History", "e:databaseInfo/e:history",
139 qw(e:title e:description) ],
141 # This list was produced by feeding
142 # http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
144 # awk -F'|' '$3 {print$4}'
145 # and shortening some of the longer names by hand
173 "Catalan; Valencian",
177 "Church Slavic; Old Slavonic",
184 "Divehi; Dhivehi; Maldivian",
198 "Gaelic; Scottish Gaelic",
202 "Greek, Modern (1453-)",
205 "Haitian; Haitian Creole",
224 "Kalaallisut; Greenlandic",
236 "Kuanyama; Kwanyama",
241 "Limburgan; Limburger; Limburgish",
244 "Luxembourgish; Letzeburgesch",
259 "Ndebele, South; South Ndebele",
260 "Ndebele, North; North Ndebele",
266 "Chichewa; Chewa; Nyanja",
267 "Occitan (post 1500); Provençal",
287 "Sinhala; Sinhalese",
296 "Spanish; Castilian",
311 "Tonga (Tonga Islands)",
333 "Language of Records", "e:databaseInfo/e:langUsage",
334 qw(e:title e:description) ],
335 [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
336 qw(e:title e:description) ],
337 [ subjects => 2, "Subjects", "e:databaseInfo/e:subjects",
338 qw(e:title e:description) ],
341 # Update record with submitted data
342 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
344 foreach my $key (&utf8param($r)) {
345 next if grep { $key eq $_ } qw(op id update);
346 $data{$key} = utf8param($r, $key);
348 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
349 if ($update && @changedFields) {
350 my @x = modify_xml_document($xc, { dateModified =>
352 "Data/time modified",
353 "e:metaInfo/e:dateModified" ] },
354 { dateModified => isodate(time()) });
355 die "Didn't set dateModified!" if !@x;
356 ZOOM::IRSpy::_rewrite_zeerex_record($conn, $xc->getContextNode(),
357 $op eq "edit" ? $id : undef);
361 <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
362 % if ($update && @changedFields) {
363 % my $nchanges = @changedFields;
364 <p style="font-weight: bold">
365 The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
366 Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
367 <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
369 % return if $op eq "new";
372 Although anyone is allowed to add a new target, please note that
373 <b>you will not be able to edit the newly added target unless you
374 have administrator privileges</b>. So please be sure that the
375 details are correct before submitting them.
377 <form method="get" action="">
378 <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
380 foreach my $ref (@fields) {
381 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
384 <th><% $caption %></th>
386 % my $rawval = $xc->findvalue($xpath);
387 % my $val = xml_encode($rawval, "");
389 <select name="<% $name %>" size="1">
390 % foreach my $option (@$nlines) {
391 <option value="<% xml_encode($option) %>"<%
392 ($rawval eq $option ? ' selected="selected"' : "")
393 %>><% xml_encode($option) %></option>
396 % } elsif ($nlines) {
397 <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
399 <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
403 <& /help/link.mc, help => "edit/$name" &>
408 <td align="right" colspan="2">
409 <input type="submit" name="update" value="Update"/>
410 % $op = "edit" if $op eq "new" && defined $update;
411 <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
412 % $id = $newid if defined $newid;
414 <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
421 if (@changedFields && 0) {
422 my $x = $xc->getContextNode()->toString();
424 #$x =~ s/$/<br\/>/gm;
425 print "<pre>$x</pre>\n";