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