Merge branch 'master' of ssh://git.indexdata.com/home/git/pub/irspy
[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 title, 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      [ type         => $m->comp("libtype-list.mc"),
125        "Type of Library", "i:status/i:libraryType" ],
126      [ username     => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
127        qw() ],
128      [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
129        qw(e:user) ],
130      [ description  => 5, "Description", "e:databaseInfo/e:description",
131        qw(e:title) ],
132      [ author       => 0, "Author", "e:databaseInfo/e:author",
133        qw(e:title e:description) ],
134      [ hosturl       => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
135      [ contact      => 0, "Contact", "e:databaseInfo/e:contact",
136        qw(e:title e:description) ],
137      [ extent       => 3, "Extent", "e:databaseInfo/e:extent",
138        qw(e:title e:description) ],
139      [ history      => 5, "History", "e:databaseInfo/e:history",
140        qw(e:title e:description) ],
141      [ language     => [
142 # This list was produced by feeding
143 #       http://www.loc.gov/standards/iso639-2/ISO-639-2_values_8bits.txt
144 # through the filter
145 #       awk -F'|' '$3 {print$4}'
146 # and shortening some of the longer names by hand
147                         "",
148                         "English",
149                         "Afar",
150                         "Abkhazian",
151                         "Afrikaans",
152                         "Akan",
153                         "Albanian",
154                         "Amharic",
155                         "Arabic",
156                         "Aragonese",
157                         "Armenian",
158                         "Assamese",
159                         "Avaric",
160                         "Avestan",
161                         "Aymara",
162                         "Azerbaijani",
163                         "Bashkir",
164                         "Bambara",
165                         "Basque",
166                         "Belarusian",
167                         "Bengali",
168                         "Bihari",
169                         "Bislama",
170                         "Bosnian",
171                         "Breton",
172                         "Bulgarian",
173                         "Burmese",
174                         "Catalan; Valencian",
175                         "Chamorro",
176                         "Chechen",
177                         "Chinese",
178                         "Church Slavic; Old Slavonic",
179                         "Chuvash",
180                         "Cornish",
181                         "Corsican",
182                         "Cree",
183                         "Czech",
184                         "Danish",
185                         "Divehi; Dhivehi; Maldivian",
186                         "Dutch; Flemish",
187                         "Dzongkha",
188                         "Esperanto",
189                         "Estonian",
190                         "Ewe",
191                         "Faroese",
192                         "Fijian",
193                         "Finnish",
194                         "French",
195                         "Western Frisian",
196                         "Fulah",
197                         "Georgian",
198                         "German",
199                         "Gaelic; Scottish Gaelic",
200                         "Irish",
201                         "Galician",
202                         "Manx",
203                         "Greek, Modern (1453-)",
204                         "Guarani",
205                         "Gujarati",
206                         "Haitian; Haitian Creole",
207                         "Hausa",
208                         "Hebrew",
209                         "Herero",
210                         "Hindi",
211                         "Hiri Motu",
212                         "Hungarian",
213                         "Igbo",
214                         "Icelandic",
215                         "Ido",
216                         "Sichuan Yi",
217                         "Inuktitut",
218                         "Interlingue",
219                         "Interlingua",
220                         "Indonesian",
221                         "Inupiaq",
222                         "Italian",
223                         "Javanese",
224                         "Japanese",
225                         "Kalaallisut; Greenlandic",
226                         "Kannada",
227                         "Kashmiri",
228                         "Kanuri",
229                         "Kazakh",
230                         "Khmer",
231                         "Kikuyu; Gikuyu",
232                         "Kinyarwanda",
233                         "Kirghiz",
234                         "Komi",
235                         "Kongo",
236                         "Korean",
237                         "Kuanyama; Kwanyama",
238                         "Kurdish",
239                         "Lao",
240                         "Latin",
241                         "Latvian",
242                         "Limburgan; Limburger; Limburgish",
243                         "Lingala",
244                         "Lithuanian",
245                         "Luxembourgish; Letzeburgesch",
246                         "Luba-Katanga",
247                         "Ganda",
248                         "Macedonian",
249                         "Marshallese",
250                         "Malayalam",
251                         "Maori",
252                         "Marathi",
253                         "Malay",
254                         "Malagasy",
255                         "Maltese",
256                         "Moldavian",
257                         "Mongolian",
258                         "Nauru",
259                         "Navajo; Navaho",
260                         "Ndebele, South; South Ndebele",
261                         "Ndebele, North; North Ndebele",
262                         "Ndonga",
263                         "Nepali",
264                         "Norwegian Nynorsk",
265                         "Norwegian Bokmål",
266                         "Norwegian",
267                         "Chichewa; Chewa; Nyanja",
268                         "Occitan (post 1500); Provençal",
269                         "Ojibwa",
270                         "Oriya",
271                         "Oromo",
272                         "Ossetian; Ossetic",
273                         "Panjabi; Punjabi",
274                         "Persian",
275                         "Pali",
276                         "Polish",
277                         "Portuguese",
278                         "Pushto",
279                         "Quechua",
280                         "Raeto-Romance",
281                         "Romanian",
282                         "Rundi",
283                         "Russian",
284                         "Sango",
285                         "Sanskrit",
286                         "Serbian",
287                         "Croatian",
288                         "Sinhala; Sinhalese",
289                         "Slovak",
290                         "Slovenian",
291                         "Northern Sami",
292                         "Samoan",
293                         "Shona",
294                         "Sindhi",
295                         "Somali",
296                         "Sotho, Southern",
297                         "Spanish; Castilian",
298                         "Sardinian",
299                         "Swati",
300                         "Sundanese",
301                         "Swahili",
302                         "Swedish",
303                         "Tahitian",
304                         "Tamil",
305                         "Tatar",
306                         "Telugu",
307                         "Tajik",
308                         "Tagalog",
309                         "Thai",
310                         "Tibetan",
311                         "Tigrinya",
312                         "Tonga (Tonga Islands)",
313                         "Tswana",
314                         "Tsonga",
315                         "Turkmen",
316                         "Turkish",
317                         "Twi",
318                         "Uighur; Uyghur",
319                         "Ukrainian",
320                         "Urdu",
321                         "Uzbek",
322                         "Venda",
323                         "Vietnamese",
324                         "Volapük",
325                         "Welsh",
326                         "Walloon",
327                         "Wolof",
328                         "Xhosa",
329                         "Yiddish",
330                         "Yoruba",
331                         "Zhuang; Chuang",
332                         "Zulu",
333                         ],
334        "Language of Records", "e:databaseInfo/e:langUsage",
335        qw(e:title e:description) ],
336      [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
337        qw(e:title e:description) ],
338      [ subjects     => 2, "Subjects", "e:databaseInfo/e:subjects",
339        qw(e:title e:description) ],
340      );
341
342 # Update record with submitted data
343 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
344 my %data;
345 foreach my $key (&utf8param($r)) {
346     next if grep { $key eq $_ } qw(op id update);
347     $data{$key} = trimField( utf8param($r, $key) );
348 }
349 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
350 if ($update && @changedFields) {
351     my @x = modify_xml_document($xc, { dateModified =>
352                                            [ dateModified => 0,
353                                              "Data/time modified",
354                                              "e:metaInfo/e:dateModified" ] },
355                                 { dateModified => isodate(time()) });
356     die "Didn't set dateModified!" if !@x;
357     ZOOM::IRSpy::_rewrite_zeerex_record($conn, $xc->getContextNode(),
358                                         $op eq "edit" ? $id : undef);
359 }
360
361 </%perl>
362  <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
363 % if ($update && @changedFields) {
364 %     my $nchanges = @changedFields;
365  <p style="font-weight: bold">
366   The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
367   Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
368   <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
369  </p>
370 % return if $op eq "new";
371 % }
372  <p>
373   Although anyone is allowed to add a new target, please note that
374   <b>you will not be able to edit the newly added target unless you
375   have administrator privileges</b>.  So please be sure that the
376   details are correct before submitting them.
377  </p>
378  <form method="get" action="">
379   <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
380 <%perl>
381 foreach my $ref (@fields) {
382     my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
383 </%perl>
384    <tr>
385     <th><% $caption %></th>
386     <td>
387 % my $rawval = $xc->findvalue($xpath);
388 % my $val = xml_encode($rawval, "");
389 % if (ref $nlines) {
390      <select name="<% $name %>" size="1">
391 %     foreach my $option (@$nlines) {
392       <option value="<% xml_encode($option) %>"<%
393         ($rawval eq $option ? ' selected="selected"' : "")
394         %>><% xml_encode($option) %></option>
395 %     }
396      </select>
397 % } elsif ($nlines) {
398      <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
399 % } else {
400      <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
401 % }
402     </td>
403     <td>
404      <& /help/link.mc, help => "edit/$name" &>
405     </td>
406    </tr>
407 %   }
408    <tr>
409     <td align="right" colspan="2">
410      <input type="submit" name="update" value="Update"/>
411 % $op = "edit" if $op eq "new" && defined $update;
412      <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
413 % $id = $newid if defined $newid;
414 % if (defined $id) {
415      <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
416 % }
417     </td>
418    </tr>
419   </table>
420  </form>
421 <%perl>
422     if (@changedFields && 0) {
423         my $x = $xc->getContextNode()->toString();
424         $x = xml_encode($x);
425         #$x =~ s/$/<br\/>/gm;
426         print "<pre>$x</pre>\n";
427     }
428 </%perl>