e4356243921a7347edc10f56258d05f9d57fc5e4
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 %# $Id: edit.mc,v 1.36 2007-07-17 13:04:45 mike Exp $
2 <%args>
3 $op
4 $id => undef ### should be extracted using utf8param()
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:8018/IR-Explain---1", 0,
36                                 user => "admin", password => "fruitbat",
37                                 elementSetName => "zeerex");
38
39 my $protocol = utf8param($r, "protocol");
40 my $host = utf8param($r, "host");
41 my $port = utf8param($r, "port");
42 my $dbname = utf8param($r, "dbname");
43
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);
50 }
51
52 my $newid;
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);
58 }
59
60 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
61
62 if (!defined $id) {
63     if (!$update) {
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];
70         undef $update;
71     } else {
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">
78                 There is already
79                 <a href='?op=edit&amp;id=$newid'>a record</a>
80                 for this protocol, host, port and database name.
81                 </p>\n];
82             undef $update;
83         }
84     }
85 } else {
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);
92     } else {
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];
95         $id = undef;
96     }
97 }
98
99 my $xc = irspy_xpath_context($rec);
100 my @fields =
101     (
102      [ title        => 0, "Name", "e:databaseInfo/e:title",
103        qw() ],
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",
111        qw(e:host e:port) ],
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",
115        qw() ],
116      [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
117        qw(e:user) ],
118      [ description  => 5, "Description", "e:databaseInfo/e:description",
119        qw(e:title) ],
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) ],
129      [ language     => [
130 # This list was produced by feeding
131 #       http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
132 # through the filter
133 #       awk -F'|' '$3 {print$4}'
134 # and shortening some of the longer names by hand
135                         "",
136                         "English",
137                         "Afar",
138                         "Abkhazian",
139                         "Afrikaans",
140                         "Akan",
141                         "Albanian",
142                         "Amharic",
143                         "Arabic",
144                         "Aragonese",
145                         "Armenian",
146                         "Assamese",
147                         "Avaric",
148                         "Avestan",
149                         "Aymara",
150                         "Azerbaijani",
151                         "Bashkir",
152                         "Bambara",
153                         "Basque",
154                         "Belarusian",
155                         "Bengali",
156                         "Bihari",
157                         "Bislama",
158                         "Bosnian",
159                         "Breton",
160                         "Bulgarian",
161                         "Burmese",
162                         "Catalan; Valencian",
163                         "Chamorro",
164                         "Chechen",
165                         "Chinese",
166                         "Church Slavic; Old Slavonic",
167                         "Chuvash",
168                         "Cornish",
169                         "Corsican",
170                         "Cree",
171                         "Czech",
172                         "Danish",
173                         "Divehi; Dhivehi; Maldivian",
174                         "Dutch; Flemish",
175                         "Dzongkha",
176                         "Esperanto",
177                         "Estonian",
178                         "Ewe",
179                         "Faroese",
180                         "Fijian",
181                         "Finnish",
182                         "French",
183                         "Western Frisian",
184                         "Fulah",
185                         "Georgian",
186                         "German",
187                         "Gaelic; Scottish Gaelic",
188                         "Irish",
189                         "Galician",
190                         "Manx",
191                         "Greek, Modern (1453-)",
192                         "Guarani",
193                         "Gujarati",
194                         "Haitian; Haitian Creole",
195                         "Hausa",
196                         "Hebrew",
197                         "Herero",
198                         "Hindi",
199                         "Hiri Motu",
200                         "Hungarian",
201                         "Igbo",
202                         "Icelandic",
203                         "Ido",
204                         "Sichuan Yi",
205                         "Inuktitut",
206                         "Interlingue",
207                         "Interlingua",
208                         "Indonesian",
209                         "Inupiaq",
210                         "Italian",
211                         "Javanese",
212                         "Japanese",
213                         "Kalaallisut; Greenlandic",
214                         "Kannada",
215                         "Kashmiri",
216                         "Kanuri",
217                         "Kazakh",
218                         "Khmer",
219                         "Kikuyu; Gikuyu",
220                         "Kinyarwanda",
221                         "Kirghiz",
222                         "Komi",
223                         "Kongo",
224                         "Korean",
225                         "Kuanyama; Kwanyama",
226                         "Kurdish",
227                         "Lao",
228                         "Latin",
229                         "Latvian",
230                         "Limburgan; Limburger; Limburgish",
231                         "Lingala",
232                         "Lithuanian",
233                         "Luxembourgish; Letzeburgesch",
234                         "Luba-Katanga",
235                         "Ganda",
236                         "Macedonian",
237                         "Marshallese",
238                         "Malayalam",
239                         "Maori",
240                         "Marathi",
241                         "Malay",
242                         "Malagasy",
243                         "Maltese",
244                         "Moldavian",
245                         "Mongolian",
246                         "Nauru",
247                         "Navajo; Navaho",
248                         "Ndebele, South; South Ndebele",
249                         "Ndebele, North; North Ndebele",
250                         "Ndonga",
251                         "Nepali",
252                         "Norwegian Nynorsk",
253                         "Norwegian Bokmål",
254                         "Norwegian",
255                         "Chichewa; Chewa; Nyanja",
256                         "Occitan (post 1500); Provençal",
257                         "Ojibwa",
258                         "Oriya",
259                         "Oromo",
260                         "Ossetian; Ossetic",
261                         "Panjabi; Punjabi",
262                         "Persian",
263                         "Pali",
264                         "Polish",
265                         "Portuguese",
266                         "Pushto",
267                         "Quechua",
268                         "Raeto-Romance",
269                         "Romanian",
270                         "Rundi",
271                         "Russian",
272                         "Sango",
273                         "Sanskrit",
274                         "Serbian",
275                         "Croatian",
276                         "Sinhala; Sinhalese",
277                         "Slovak",
278                         "Slovenian",
279                         "Northern Sami",
280                         "Samoan",
281                         "Shona",
282                         "Sindhi",
283                         "Somali",
284                         "Sotho, Southern",
285                         "Spanish; Castilian",
286                         "Sardinian",
287                         "Swati",
288                         "Sundanese",
289                         "Swahili",
290                         "Swedish",
291                         "Tahitian",
292                         "Tamil",
293                         "Tatar",
294                         "Telugu",
295                         "Tajik",
296                         "Tagalog",
297                         "Thai",
298                         "Tibetan",
299                         "Tigrinya",
300                         "Tonga (Tonga Islands)",
301                         "Tswana",
302                         "Tsonga",
303                         "Turkmen",
304                         "Turkish",
305                         "Twi",
306                         "Uighur; Uyghur",
307                         "Ukrainian",
308                         "Urdu",
309                         "Uzbek",
310                         "Venda",
311                         "Vietnamese",
312                         "Volapük",
313                         "Welsh",
314                         "Walloon",
315                         "Wolof",
316                         "Xhosa",
317                         "Yiddish",
318                         "Yoruba",
319                         "Zhuang; Chuang",
320                         "Zulu",
321                         ],
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) ],
328      );
329
330 # Update record with submitted data
331 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
332 my %data;
333 foreach my $key ($r->param()) {
334     next if grep { $key eq $_ } qw(op id update);
335     $data{$key} = utf8param($r, $key);
336 }
337 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
338 if ($update && @changedFields) {
339     my @x = modify_xml_document($xc, { dateModified =>
340                                            [ dateModified => 0,
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);
347 }
348
349 </%perl>
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) %>.
357  </p>
358 % return if $op eq "new";
359 % }
360  <p>
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.
365  </p>
366  <form method="get" action="">
367   <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
368 <%perl>
369 foreach my $ref (@fields) {
370     my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
371 </%perl>
372    <tr>
373     <th><% $caption %></th>
374     <td>
375 % my $rawval = $xc->findvalue($xpath);
376 % my $val = xml_encode($rawval, "");
377 % if (ref $nlines) {
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>
383 %     }
384      </select>
385 % } elsif ($nlines) {
386      <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
387 % } else {
388      <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
389 % }
390     </td>
391     <td>
392      <& /help/link.mc, help => "edit/$name" &>
393     </td>
394    </tr>
395 %   }
396    <tr>
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;
402 % if (defined $id) {
403      <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
404 % }
405     </td>
406    </tr>
407   </table>
408  </form>
409 <%perl>
410     if (@changedFields && 0) {
411         my $x = $xc->getContextNode()->toString();
412         $x = xml_encode($x);
413         #$x =~ s/$/<br\/>/gm;
414         print "<pre>$x</pre>\n";
415     }
416 </%perl>