1 %# $Id: edit.mc,v 1.36 2007-07-17 13:04:45 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];
72 # Creating new record, all necessary data is present. Check
73 # that the new record is not a duplicate of an existing one.
74 my $rs = $conn->search(new ZOOM::Query::CQL(cql_target($newid)));
75 if ($rs->size() > 0) {
76 my $qnewid = xml_encode(uri_escape($newid));
77 print qq[<p class="error">
79 <a href='?op=edit&id=$newid'>a record</a>
80 for this protocol, host, port and database name.
86 # assert(defined $id);
87 # Copying or editing an existing record: fetch it for editing
88 my $query = cql_target($id);
89 my $rs = $conn->search(new ZOOM::Query::CQL($query));
90 if ($rs->size() > 0) {
91 $rec = $rs->record(0);
93 ### Is this an error? I don't think the UI will ever provoke it
94 print qq[<p class="error">(New ID specified.)</p>\n];
99 my $xc = irspy_xpath_context($rec);
102 [ title => 0, "Name", "e:databaseInfo/e:title",
104 [ country => $m->comp("country-list.mc"),
105 "Country", "i:status/i:country" ],
106 [ protocol => [ qw(Z39.50 SRW SRU) ],
107 "Protocol", "e:serverInfo/\@protocol" ],
108 [ host => 0, "Host", "e:serverInfo/e:host" ],
109 [ port => 0, "Port", "e:serverInfo/e:port" ],
110 [ dbname => 0, "Database Name", "e:serverInfo/e:database",
112 [ type => $m->comp("libtype-list.mc"),
113 "Type of Library", "i:status/i:libraryType" ],
114 [ username => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
116 [ password => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
118 [ description => 5, "Description", "e:databaseInfo/e:description",
120 [ author => 0, "Author", "e:databaseInfo/e:author",
121 qw(e:title e:description) ],
122 [ hosturl => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
123 [ contact => 0, "Contact", "e:databaseInfo/e:contact",
124 qw(e:title e:description) ],
125 [ extent => 3, "Extent", "e:databaseInfo/e:extent",
126 qw(e:title e:description) ],
127 [ history => 5, "History", "e:databaseInfo/e:history",
128 qw(e:title e:description) ],
130 # This list was produced by feeding
131 # http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
133 # awk -F'|' '$3 {print$4}'
134 # and shortening some of the longer names by hand
162 "Catalan; Valencian",
166 "Church Slavic; Old Slavonic",
173 "Divehi; Dhivehi; Maldivian",
187 "Gaelic; Scottish Gaelic",
191 "Greek, Modern (1453-)",
194 "Haitian; Haitian Creole",
213 "Kalaallisut; Greenlandic",
225 "Kuanyama; Kwanyama",
230 "Limburgan; Limburger; Limburgish",
233 "Luxembourgish; Letzeburgesch",
248 "Ndebele, South; South Ndebele",
249 "Ndebele, North; North Ndebele",
255 "Chichewa; Chewa; Nyanja",
256 "Occitan (post 1500); Provençal",
276 "Sinhala; Sinhalese",
285 "Spanish; Castilian",
300 "Tonga (Tonga Islands)",
322 "Language of Records", "e:databaseInfo/e:langUsage",
323 qw(e:title e:description) ],
324 [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
325 qw(e:title e:description) ],
326 [ subjects => 2, "Subjects", "e:databaseInfo/e:subjects",
327 qw(e:title e:description) ],
330 # Update record with submitted data
331 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
333 foreach my $key ($r->param()) {
334 next if grep { $key eq $_ } qw(op id update);
335 $data{$key} = utf8param($r, $key);
337 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
338 if ($update && @changedFields) {
339 my @x = modify_xml_document($xc, { dateModified =>
341 "Data/time modified",
342 "e:metaInfo/e:dateModified" ] },
343 { dateModified => isodate(time()) });
344 die "Didn't set dateModified!" if !@x;
345 ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode(),
346 $op eq "edit" ? $id : undef);
350 <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
351 % if ($update && @changedFields) {
352 % my $nchanges = @changedFields;
353 <p style="font-weight: bold">
354 The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
355 Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
356 <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
358 % return if $op eq "new";
361 Although anyone is allowed to add a new target, please note that
362 <b>you will not be able to edit the newly added target unless you
363 have administrator privileges</b>. So please be sure that the
364 details are correct before submitting them.
366 <form method="get" action="">
367 <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
369 foreach my $ref (@fields) {
370 my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
373 <th><% $caption %></th>
375 % my $rawval = $xc->findvalue($xpath);
376 % my $val = xml_encode($rawval, "");
378 <select name="<% $name %>" size="1">
379 % foreach my $option (@$nlines) {
380 <option value="<% xml_encode($option) %>"<%
381 ($rawval eq $option ? ' selected="selected"' : "")
382 %>><% xml_encode($option) %></option>
385 % } elsif ($nlines) {
386 <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
388 <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
392 <& /help/link.mc, help => "edit/$name" &>
397 <td align="right" colspan="2">
398 <input type="submit" name="update" value="Update"/>
399 % $op = "edit" if $op eq "new" && defined $update;
400 <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
401 % $id = $newid if defined $newid;
403 <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
410 if (@changedFields && 0) {
411 my $x = $xc->getContextNode()->toString();
413 #$x =~ s/$/<br\/>/gm;
414 print "<pre>$x</pre>\n";