375e3efee758d7f88bf795367f510de951ea7435
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 <%args>
2 $op
3 $id => undef ### should be extracted using utf8param()
4 $update => undef
5 </%args>
6 <%doc>
7 Since this form is used in many different situations, some care is
8 merited in considering the possibilities:
9
10 Situation                                       Op      ID      Update
11 ----------------------------------------------------------------------
12 Blank form for adding a new target              new
13 New target rejected, changes required           new             X
14 New target accepted and added                   new             X
15 ---------------------------------------------------------------------
16 Existing target to be edited                    edit    X
17 Edit rejected, changes required                 edit    X       X
18 Target successfully updated                     edit    X       X
19 ----------------------------------------------------------------------
20 Existing target to be copied                    copy    X
21 New target rejected, changes required           copy    X       X
22 New target accepted and added                   copy    X       X
23 ----------------------------------------------------------------------
24
25 Submissions, whether of new targets, edits or copies, may be rejected
26 due either to missing mandatory fields or host/name/port that form a
27 duplicate ID.
28 </%doc>
29 <%perl>
30 # Sanity checking
31 die "op = new but id defined" if $op eq "new" && defined $id;
32 die "op != new but id undefined" if $op ne "new" && !defined $id;
33
34 my $db = ZOOM::IRSpy::connect_to_registry();
35 my $conn = new ZOOM::Connection($db, 0,
36                                 user => "admin", password => "fruitbat",
37                                 elementSetName => "zeerex");
38
39 my $protocol = utf8paramTrim($r, "protocol");
40 my $host = utf8paramTrim($r, "host");
41 my $port = utf8paramTrim($r, "port");
42 my $dbname = utf8paramTrim($r, "dbname");
43 my $title = utf8paramTrim($r, "title");
44
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);
51 }
52
53 my $newid;
54 if (defined $protocol && $protocol ne "" &&
55     defined $host && $host ne "" &&
56     defined $port && $port ne "" &&
57     defined $title && $title ne "" &&
58     defined $dbname && $dbname ne "") {
59     $newid = irspy_make_identifier($protocol, $host, $port, $dbname);
60 }
61
62 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
63
64 if (!defined $id) {
65     if (!$update) {
66         # About to enter data for a new record
67         # Nothing to do at this stage
68     } elsif (!defined $newid) {
69         # Tried to create new record but data is insufficient
70         print qq[<p class="error">
71                 Please specify name, protocol, host, port and database name.</p>\n];
72         undef $update;
73     } elsif ($host !~ /^[\w-]+\.[\w.-]*\w$/i) {
74         print qq[<p class="error">
75                 This host name is not valid.</p>\n];
76         undef $update;
77         sleep 25;
78     } elsif ($port !~ /^\d*$/i) {
79         print qq[<p class="error">
80                 This port number is not valid.</p>\n];
81         undef $update;
82         sleep 25;
83     } else {
84         # Creating new record, all necessary data is present.  Check
85         # that the new record is not a duplicate of an existing one.
86         my $rs = $conn->search(new ZOOM::Query::CQL(cql_target($newid)));
87         if ($rs->size() > 0) {
88             my $qnewid = xml_encode(uri_escape_utf8($newid));
89             print qq[<p class="error">
90                 There is already
91                 <a href='?op=edit&amp;id=$newid'>a record</a>
92                 for this protocol, host, port and database name.
93                 </p>\n];
94             undef $update;
95         }
96     }
97 } else {
98     # assert(defined $id);
99     # Copying or editing an existing record: fetch it for editing
100     my $query = cql_target($id);
101     my $rs = $conn->search(new ZOOM::Query::CQL($query));
102     if ($rs->size() > 0) {
103         $rec = $rs->record(0);
104     } else {
105         ### Is this an error?  I don't think the UI will ever provoke it
106         print qq[<p class="error">(New ID specified.)</p>\n];
107         $id = undef;
108     }
109 }
110
111 my $xc = irspy_xpath_context($rec);
112 my @fields =
113     (
114      [ title        => 0, "Name", "e:databaseInfo/e:title",
115        qw() ],
116      [ country      => $m->comp("country-list.mc"),
117        "Country", "i:status/i:country" ],
118      [ protocol     => [ qw(Z39.50 SRW SRU) ],
119        "Protocol", "e:serverInfo/\@protocol" ],
120      [ host         => 0, "Host", "e:serverInfo/e:host" ],
121      [ port         => 0, "Port", "e:serverInfo/e:port" ],
122      [ dbname       => 0, "Database Name", "e:serverInfo/e:database",
123        qw(e:host e:port) ],
124      [ udb          => 0, "Access Name (Unique DB)", "e:databaseInfo/i:udb", qw(e:host e:port e:database) ],
125      [ type         => $m->comp("libtype-list.mc"),
126        "Type of Library", "i:status/i:libraryType" ],
127      [ username     => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
128        qw() ],
129      [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
130        qw(e:user) ],
131      [ description  => 5, "Description", "e:databaseInfo/e:description",
132        qw(e:title) ],
133      [ author       => 0, "Author", "e:databaseInfo/e:author",
134        qw(e:title e:description) ],
135      [ hosturl       => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
136      [ contact      => 0, "Contact", "e:databaseInfo/e:contact",
137        qw(e:title e:description) ],
138      [ extent       => 3, "Extent", "e:databaseInfo/e:extent",
139        qw(e:title e:description) ],
140      [ history      => 5, "History", "e:databaseInfo/e:history",
141        qw(e:title e:description) ],
142      [ language     => [
143 # This list was produced by feeding
144 #       http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
145 # through the filter
146 #       awk -F'|' '$3 {print$4}'
147 # and shortening some of the longer names by hand
148                         "",
149                         "English",
150                         "Afar",
151                         "Abkhazian",
152                         "Afrikaans",
153                         "Akan",
154                         "Albanian",
155                         "Amharic",
156                         "Arabic",
157                         "Aragonese",
158                         "Armenian",
159                         "Assamese",
160                         "Avaric",
161                         "Avestan",
162                         "Aymara",
163                         "Azerbaijani",
164                         "Bashkir",
165                         "Bambara",
166                         "Basque",
167                         "Belarusian",
168                         "Bengali",
169                         "Bihari",
170                         "Bislama",
171                         "Bosnian",
172                         "Breton",
173                         "Bulgarian",
174                         "Burmese",
175                         "Catalan; Valencian",
176                         "Chamorro",
177                         "Chechen",
178                         "Chinese",
179                         "Church Slavic; Old Slavonic",
180                         "Chuvash",
181                         "Cornish",
182                         "Corsican",
183                         "Cree",
184                         "Czech",
185                         "Danish",
186                         "Divehi; Dhivehi; Maldivian",
187                         "Dutch; Flemish",
188                         "Dzongkha",
189                         "Esperanto",
190                         "Estonian",
191                         "Ewe",
192                         "Faroese",
193                         "Fijian",
194                         "Finnish",
195                         "French",
196                         "Western Frisian",
197                         "Fulah",
198                         "Georgian",
199                         "German",
200                         "Gaelic; Scottish Gaelic",
201                         "Irish",
202                         "Galician",
203                         "Manx",
204                         "Greek, Modern (1453-)",
205                         "Guarani",
206                         "Gujarati",
207                         "Haitian; Haitian Creole",
208                         "Hausa",
209                         "Hebrew",
210                         "Herero",
211                         "Hindi",
212                         "Hiri Motu",
213                         "Hungarian",
214                         "Igbo",
215                         "Icelandic",
216                         "Ido",
217                         "Sichuan Yi",
218                         "Inuktitut",
219                         "Interlingue",
220                         "Interlingua",
221                         "Indonesian",
222                         "Inupiaq",
223                         "Italian",
224                         "Javanese",
225                         "Japanese",
226                         "Kalaallisut; Greenlandic",
227                         "Kannada",
228                         "Kashmiri",
229                         "Kanuri",
230                         "Kazakh",
231                         "Khmer",
232                         "Kikuyu; Gikuyu",
233                         "Kinyarwanda",
234                         "Kirghiz",
235                         "Komi",
236                         "Kongo",
237                         "Korean",
238                         "Kuanyama; Kwanyama",
239                         "Kurdish",
240                         "Lao",
241                         "Latin",
242                         "Latvian",
243                         "Limburgan; Limburger; Limburgish",
244                         "Lingala",
245                         "Lithuanian",
246                         "Luxembourgish; Letzeburgesch",
247                         "Luba-Katanga",
248                         "Ganda",
249                         "Macedonian",
250                         "Marshallese",
251                         "Malayalam",
252                         "Maori",
253                         "Marathi",
254                         "Malay",
255                         "Malagasy",
256                         "Maltese",
257                         "Moldavian",
258                         "Mongolian",
259                         "Nauru",
260                         "Navajo; Navaho",
261                         "Ndebele, South; South Ndebele",
262                         "Ndebele, North; North Ndebele",
263                         "Ndonga",
264                         "Nepali",
265                         "Norwegian Nynorsk",
266                         "Norwegian Bokmål",
267                         "Norwegian",
268                         "Chichewa; Chewa; Nyanja",
269                         "Occitan (post 1500); Provençal",
270                         "Ojibwa",
271                         "Oriya",
272                         "Oromo",
273                         "Ossetian; Ossetic",
274                         "Panjabi; Punjabi",
275                         "Persian",
276                         "Pali",
277                         "Polish",
278                         "Portuguese",
279                         "Pushto",
280                         "Quechua",
281                         "Raeto-Romance",
282                         "Romanian",
283                         "Rundi",
284                         "Russian",
285                         "Sango",
286                         "Sanskrit",
287                         "Serbian",
288                         "Croatian",
289                         "Sinhala; Sinhalese",
290                         "Slovak",
291                         "Slovenian",
292                         "Northern Sami",
293                         "Samoan",
294                         "Shona",
295                         "Sindhi",
296                         "Somali",
297                         "Sotho, Southern",
298                         "Spanish; Castilian",
299                         "Sardinian",
300                         "Swati",
301                         "Sundanese",
302                         "Swahili",
303                         "Swedish",
304                         "Tahitian",
305                         "Tamil",
306                         "Tatar",
307                         "Telugu",
308                         "Tajik",
309                         "Tagalog",
310                         "Thai",
311                         "Tibetan",
312                         "Tigrinya",
313                         "Tonga (Tonga Islands)",
314                         "Tswana",
315                         "Tsonga",
316                         "Turkmen",
317                         "Turkish",
318                         "Twi",
319                         "Uighur; Uyghur",
320                         "Ukrainian",
321                         "Urdu",
322                         "Uzbek",
323                         "Venda",
324                         "Vietnamese",
325                         "Volapük",
326                         "Welsh",
327                         "Walloon",
328                         "Wolof",
329                         "Xhosa",
330                         "Yiddish",
331                         "Yoruba",
332                         "Zhuang; Chuang",
333                         "Zulu",
334                         ],
335        "Language of Records", "e:databaseInfo/e:langUsage",
336        qw(e:title e:description) ],
337      [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
338        qw(e:title e:description) ],
339      [ subjects     => 2, "Subjects", "e:databaseInfo/e:subjects",
340        qw(e:title e:description) ],
341      [ disabled     => [ qw(0 1) ],
342        "Target Test Disabled", "i:status/i:disabled" ],
343      );
344
345 # Update record with submitted data
346 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
347 my %data;
348 foreach my $key (&utf8param($r)) {
349     next if grep { $key eq $_ } qw(op id update);
350     $data{$key} = trimField( utf8param($r, $key) );
351 }
352 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
353 if ($update && @changedFields) {
354     my @x = modify_xml_document($xc, { dateModified =>
355                                            [ dateModified => 0,
356                                              "Data/time modified",
357                                              "e:metaInfo/e:dateModified" ] },
358                                 { dateModified => isodate(time()) });
359     die "Didn't set dateModified!" if !@x;
360     ZOOM::IRSpy::_rewrite_zeerex_record($conn, $xc->getContextNode(),
361                                         $op eq "edit" ? $id : undef);
362 }
363
364 </%perl>
365  <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
366 % if ($update && @changedFields) {
367 %     my $nchanges = @changedFields;
368  <p style="font-weight: bold">
369   The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
370   Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
371   <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
372  </p>
373 % return if $op eq "new";
374 % }
375  <p>
376   Although anyone is allowed to add a new target, please note that
377   <b>you will not be able to edit the newly added target unless you
378   have administrator privileges</b>.  So please be sure that the
379   details are correct before submitting them.
380  </p>
381  <form method="get" action="">
382   <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
383 <%perl>
384 foreach my $ref (@fields) {
385     my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
386 </%perl>
387    <tr>
388     <th><% $caption %></th>
389     <td>
390 % my $rawval = $xc->findvalue($xpath);
391 % my $val = xml_encode($rawval, "");
392 % if (ref $nlines) {
393      <select name="<% $name %>" size="1">
394 %     foreach my $option (@$nlines) {
395       <option value="<% xml_encode($option) %>"<%
396         ($rawval eq $option ? ' selected="selected"' : "")
397         %>><% xml_encode($option) %></option>
398 %     }
399      </select>
400 % } elsif ($nlines) {
401      <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
402 % } else {
403      <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
404 % }
405     </td>
406     <td>
407      <& /help/link.mc, help => "edit/$name" &>
408     </td>
409    </tr>
410 %   }
411    <tr>
412     <td align="right" colspan="2">
413      <input type="submit" name="update" value="Update"/>
414 % $op = "edit" if $op eq "new" && defined $update;
415      <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
416 % $id = $newid if defined $newid;
417 % if (defined $id) {
418      <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
419 % }
420     </td>
421    </tr>
422   </table>
423  </form>
424 <%perl>
425     if (@changedFields && 0) {
426         my $x = $xc->getContextNode()->toString();
427         $x = xml_encode($x);
428         #$x =~ s/$/<br\/>/gm;
429         print "<pre>$x</pre>\n";
430     }
431 </%perl>