Country now chosen from a dropdown.
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 %# $Id: edit.mc,v 1.24 2006-12-06 10:37:23 mike Exp $
2 <%args>
3 $op
4 $id => undef
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:3313/IR-Explain---1", 0,
36                                 user => "admin", password => "fruitbat",
37                                 elementSetName => "zeerex");
38 my $rec = '<explain xmlns="http://explain.z3950.org/dtd/2.0/"/>';
39 if (defined $id && ($op ne "copy" || !$update)) {
40     # Existing record
41     my $query = 'rec.id="' . cql_quote($id) . '"';
42     my $rs = $conn->search(new ZOOM::Query::CQL($query));
43     if ($rs->size() > 0) {
44         $rec = $rs->record(0);
45     } else {
46         ### Is this an error?  I don't think the UI will ever provoke it
47         print qq[<p class="error">(New ID specified.)</p>\n];
48         $id = undef;
49     }
50
51 } else {
52     # No ID supplied -- this is a brand new record
53     my $host = $r->param("host");
54     my $port = $r->param("port");
55     my $dbname = $r->param("dbname");
56     if (!defined $host || $host eq "" ||
57         !defined $port || $port eq "" ||
58         !defined $dbname || $dbname eq "") {
59         print qq[<p class="error">
60 You must specify host, port and database name.</p>\n] if $update;
61         undef $update;
62     } else {
63         my $query = cql_target($host, $port, $dbname);
64         my $rs = $conn->search(new ZOOM::Query::CQL($query));
65         if ($rs->size() > 0) {
66             my $fakeid = xml_encode(uri_escape("$host:$port/$dbname"));
67             print qq[<p class="error">
68 There is already
69 <a href='?op=edit&amp;id=$fakeid'>a record</a>
70 for this host, port and database name.
71 </p>\n];
72             undef $update;
73         }
74     }
75 }
76
77 my $xc = irspy_xpath_context($rec);
78 my @fields =
79     (
80      [ title        => 0, "Name", "e:databaseInfo/e:title",
81        qw() ],
82      [ country      => [
83                         "",
84                         "Afghanistan",
85                         "Albania",
86                         "Algeria",
87                         "American Samoa",
88                         "Andorra",
89                         "Angola",
90                         "Anguilla",
91                         "Antarctica",
92                         "Antigua and Barbuda",
93                         "Argentina",
94                         "Armenia",
95                         "Aruba",
96                         "Australia",
97                         "Austria",
98                         "Azerbaijan",
99                         "Bahamas",
100                         "Bahrain",
101                         "Bangladesh",
102                         "Barbados",
103                         "Belarus",
104                         "Belgium",
105                         "Belize",
106                         "Benin",
107                         "Bermuda",
108                         "Bhutan",
109                         "Bolivia",
110                         "Bosnia and Herzegowina",
111                         "Botswana",
112                         "Bouvet Island",
113                         "Brazil",
114                         "British Indian Ocean Terr.",
115                         "Brunei Darussalam",
116                         "Bulgaria",
117                         "Burkina Faso",
118                         "Burundi",
119                         "Cambodia",
120                         "Cameroon",
121                         "Canada",
122                         "Cape Verde",
123                         "Cayman Islands",
124                         "Central African Republic",
125                         "Chad",
126                         "Chile",
127                         "China",
128                         "Christmas Island",
129                         "Cocos (Keeling) Islands",
130                         "Colombia",
131                         "Comoros",
132                         "Congo",
133                         "Cook Islands",
134                         "Costa Rica",
135                         "Cote d'Ivoire",
136                         "Croatia (Hrvatska)",
137                         "Cuba",
138                         "Cyprus",
139                         "Czech Republic",
140                         "Denmark",
141                         "Djibouti",
142                         "Dominica",
143                         "Dominican Republic",
144                         "East Timor",
145                         "Ecuador",
146                         "Egypt",
147                         "El Salvador",
148                         "Equatorial Guinea",
149                         "Eritrea",
150                         "Estonia",
151                         "Ethiopia",
152                         "Falkland Islands/Malvinas",
153                         "Faroe Islands",
154                         "Fiji",
155                         "Finland",
156                         "France",
157                         "France, Metropolitan",
158                         "French Guiana",
159                         "French Polynesia",
160                         "French Southern Terr.",
161                         "Gabon",
162                         "Gambia",
163                         "Georgia",
164                         "Germany",
165                         "Ghana",
166                         "Gibraltar",
167                         "Greece",
168                         "Greenland",
169                         "Grenada",
170                         "Guadeloupe",
171                         "Guam",
172                         "Guatemala",
173                         "Guinea",
174                         "Guinea-Bissau",
175                         "Guyana",
176                         "Haiti",
177                         "Heard & McDonald Is.",
178                         "Honduras",
179                         "Hong Kong",
180                         "Hungary",
181                         "Iceland",
182                         "India",
183                         "Indonesia",
184                         "Iran",
185                         "Iraq",
186                         "Ireland",
187                         "Israel",
188                         "Italy",
189                         "Jamaica",
190                         "Japan",
191                         "Jordan",
192                         "Kazakhstan",
193                         "Kenya",
194                         "Kiribati",
195                         "Korea, North",
196                         "Korea, South",
197                         "Kuwait",
198                         "Kyrgyzstan",
199                         "Lao People's Dem. Rep.",
200                         "Latvia",
201                         "Lebanon",
202                         "Lesotho",
203                         "Liberia",
204                         "Libyan Arab Jamahiriya",
205                         "Liechtenstein",
206                         "Lithuania",
207                         "Luxembourg",
208                         "Macau",
209                         "Macedonia",
210                         "Madagascar",
211                         "Malawi",
212                         "Malaysia",
213                         "Maldives",
214                         "Mali",
215                         "Malta",
216                         "Marshall Islands",
217                         "Martinique",
218                         "Mauritania",
219                         "Mauritius",
220                         "Mayotte",
221                         "Mexico",
222                         "Micronesia",
223                         "Moldova",
224                         "Monaco",
225                         "Mongolia",
226                         "Montserrat",
227                         "Morocco",
228                         "Mozambique",
229                         "Myanmar",
230                         "Namibia",
231                         "Nauru",
232                         "Nepal",
233                         "Netherlands",
234                         "Netherlands Antilles",
235                         "New Caledonia",
236                         "New Zealand",
237                         "Nicaragua",
238                         "Niger",
239                         "Nigeria",
240                         "Niue",
241                         "Norfolk Island",
242                         "Northern Mariana Is.",
243                         "Norway",
244                         "Oman",
245                         "Pakistan",
246                         "Palau",
247                         "Panama",
248                         "Papua New Guinea",
249                         "Paraguay",
250                         "Peru",
251                         "Philippines",
252                         "Pitcairn",
253                         "Poland",
254                         "Portugal",
255                         "Puerto Rico",
256                         "Qatar",
257                         "Reunion",
258                         "Romania",
259                         "Russian Federation",
260                         "Rwanda",
261                         "S.Georgia & S.Sandwich Is.",
262                         "Saint Kitts and Nevis",
263                         "Saint Lucia",
264                         "Samoa",
265                         "San Marino",
266                         "Sao Tome & Principe",
267                         "Saudi Arabia",
268                         "Senegal",
269                         "Seychelles",
270                         "Sierra Leone",
271                         "Singapore",
272                         "Slovakia (Slovak Republic)",
273                         "Slovenia",
274                         "Solomon Islands",
275                         "Somalia",
276                         "South Africa",
277                         "Spain",
278                         "Sri Lanka",
279                         "St. Helena",
280                         "St. Pierre & Miquelon",
281                         "St. Vincent & Grenadines",
282                         "Sudan",
283                         "Suriname",
284                         "Svalbard & Jan Mayen Is.",
285                         "Swaziland",
286                         "Sweden",
287                         "Switzerland",
288                         "Syrian Arab Republic",
289                         "Taiwan",
290                         "Tajikistan",
291                         "Tanzania",
292                         "Thailand",
293                         "Togo",
294                         "Tokelau",
295                         "Tonga",
296                         "Trinidad and Tobago",
297                         "Tunisia",
298                         "Turkey",
299                         "Turkmenistan",
300                         "Turks & Caicos Islands",
301                         "Tuvalu",
302                         "U.S. Minor Outlying Is.",
303                         "Uganda",
304                         "Ukraine",
305                         "United Arab Emirates",
306                         "United Kingdom",
307                         "United States",
308                         "Uruguay",
309                         "Uzbekistan",
310                         "Vanuatu",
311                         "Vatican (Holy See)",
312                         "Venezuela",
313                         "Viet Nam",
314                         "Virgin Islands (British)",
315                         "Virgin Islands (U.S.)",
316                         "Wallis & Futuna Is.",
317                         "Western Sahara",
318                         "Yemen",
319                         "Yugoslavia",
320                         "Zaire",
321                         "Zambia",
322                         "Zimbabwe",
323                          ],
324        "Country", "i:status/i:country" ],
325      [ protocol     => [ qw(Z39.50 SRW SRU SRW/U) ],
326        "Protocol", "e:serverInfo/\@protocol" ],
327      [ host         => 0, "Host", "e:serverInfo/e:host" ],
328      [ port         => 0, "Port", "e:serverInfo/e:port" ],
329      [ dbname       => 0, "Database Name", "e:serverInfo/e:database",
330        qw(e:host e:port) ],
331      [ type         => [ "", qw(Academic Public Corporate Special National Education Other) ],
332        "Type of Library", "i:status/i:libraryType" ],
333      [ username     => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
334        qw() ],
335      [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
336        qw(e:user) ],
337      [ description  => 5, "Description", "e:databaseInfo/e:description",
338        qw(e:title) ],
339      [ author       => 0, "Author", "e:databaseInfo/e:author",
340        qw(e:title e:description) ],
341      [ hosturl       => 0, "URL to Hosting Organisation", "i:status/i:hostURL" ],
342      [ contact      => 0, "Contact", "e:databaseInfo/e:contact",
343        qw(e:title e:description) ],
344      [ extent       => 3, "Extent", "e:databaseInfo/e:extent",
345        qw(e:title e:description) ],
346      [ history      => 5, "History", "e:databaseInfo/e:history",
347        qw(e:title e:description) ],
348      [ language     => 0, "Language of Records", "e:databaseInfo/e:langUsage",
349        qw(e:title e:description) ],
350      [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
351        qw(e:title e:description) ],
352      [ subjects     => 2, "Subjects", "e:databaseInfo/e:subjects",
353        qw(e:title e:description) ],
354      );
355
356 # Update record with submitted data
357 my %fieldsByKey = map { ( $_->[0], $_) } @fields;
358 my %data;
359 foreach my $key ($r->param()) {
360     next if grep { $key eq $_ } qw(op id update);
361     $data{$key} = $r->param($key);
362 }
363 my @changedFields = modify_xml_document($xc, \%fieldsByKey, \%data);
364 if ($update && @changedFields) {
365     my @x = modify_xml_document($xc, { dateModified =>
366                                            [ dateModified => 0,
367                                              "Data/time modified",
368                                              "e:metaInfo/e:dateModified" ] },
369                                 { dateModified => isodate(time()) });
370     die "Didn't set dateModified!" if !@x;
371     ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode());
372 }
373
374 </%perl>
375  <h2><% xml_encode($xc->find("e:databaseInfo/e:title"), "[Untitled]") %></h2>
376 % if ($update && @changedFields) {
377 %     my $nchanges = @changedFields;
378  <p style="font-weight: bold">
379   The record has been <% $op ne "edit" ? "created" : "updated" %>.<br/>
380   Changed <% $nchanges %> field<% $nchanges == 1 ? "" : "s" %>:
381   <% join(", ", map { xml_encode($_->[2]) } @changedFields) %>.
382  </p>
383 % }
384  <form method="get" action="">
385   <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
386 <%perl>
387 foreach my $ref (@fields) {
388     my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
389 </%perl>
390    <tr>
391     <th><% $caption %></th>
392     <td>
393 % my $rawval = $xc->findvalue($xpath);
394 % my $val = xml_encode($rawval, "");
395 % if (ref $nlines) {
396      <select name="<% $name %>" size="1">
397 %     foreach my $option (@$nlines) {
398       <option value="<% xml_encode($option) %>"<%
399         ($rawval eq $option ? ' selected="selected"' : "")
400         %>><% xml_encode($option) %></option>
401 %     }
402      </select>
403 % } elsif ($nlines) {
404      <textarea name="<% $name %>" rows="<% $nlines %>" cols="51"><% $val %></textarea>
405 % } else {
406      <input name="<% $name %>" type="text" size="60" value="<% $val %>"/>
407 % }
408     </td>
409     <td>
410      <& /help/link.mc, help => "edit/$name" &>
411     </td>
412    </tr>
413 %   }
414    <tr>
415     <td align="right" colspan="2">
416      <input type="submit" name="update" value="Update"/>
417      <input type="hidden" name="op" value="<% xml_encode($op) %>"/>
418 % if (defined $id) {
419      <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
420 % }
421     </td>
422    </tr>
423   </table>
424  </form>
425 <%perl>
426     if (@changedFields && 0) {
427         my $x = $xc->getContextNode()->toString();
428         $x = xml_encode($x);
429         #$x =~ s/$/<br\/>/gm;
430         print "<pre>$x</pre>\n";
431     }
432 </%perl>