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