Use new modify_xml_document() utility function.
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 %# $Id: edit.mc,v 1.6 2006-11-01 10:00:21 mike Exp $
2 <%args>
3 $id
4 </%args>
5 <%once>
6 use ZOOM;
7 </%once>
8 <%perl>
9 my $conn = new ZOOM::Connection("localhost:3313/IR-Explain---1", 0,
10                                 user => "admin", password => "fruitbat");
11 $conn->option(elementSetName => "zeerex");
12 my $qid = $id;
13 $qid =~ s/"/\\"/g;
14 my $query = qq[rec.id="$qid"];
15 my $rs = $conn->search(new ZOOM::Query::CQL($query));
16 my $n = $rs->size();
17 if ($n == 0) {
18     $m->comp("/details/error.mc",
19              title => "Error", message => "No such ID '$id'");
20 } else {
21     my $rec = $rs->record(0);
22     my $xc = irspy_xpath_context($rec);
23     my @fields =
24         (
25          [ protocol     => 0, "Protocol", "e:serverInfo/\@protocol" ],
26          [ host         => 0, "Host", "e:serverInfo/e:host" ],
27          [ port         => 0, "Port", "e:serverInfo/e:port" ],
28          [ dbname       => 0, "Database Name", "e:serverInfo/e:database",
29            qw(e:host e:port) ],
30          [ username     => 0, "Username (if needed)", "e:serverInfo/e:authentication/e:user",
31            qw() ],
32          [ password     => 0, "Password (if needed)", "e:serverInfo/e:authentication/e:password",
33            qw(e:user) ],
34          [ title        => 0, "title", "e:databaseInfo/e:title",
35            qw() ],
36          [ description  => 5, "Description", "e:databaseInfo/e:description",
37            qw(e:title) ],
38          [ author       => 0, "Author", "e:databaseInfo/e:author",
39            qw(e:title e:description) ],
40          [ contact      => 0, "Contact", "e:databaseInfo/e:contact",
41            qw(e:title e:description) ],
42          [ extent       => 3, "Extent", "e:databaseInfo/e:extent",
43            qw(e:title e:description) ],
44          [ history      => 5, "History", "e:databaseInfo/e:history",
45            qw(e:title e:description) ],
46          [ language     => 0, "Language of Records", "e:databaseInfo/e:langUsage",
47            qw(e:title e:description) ],
48          [ restrictions => 2, "Restrictions", "e:databaseInfo/e:restrictions",
49            qw(e:title e:description) ],
50          [ subjects     => 2, "Subjects", "e:databaseInfo/e:subjects",
51            qw(e:title e:description) ],
52          );
53
54     my $nchanges = 0;
55     my $update = $r->param("update");
56     if (defined $update) {
57         # Update record with submitted data
58         my %fieldsByKey = map { ( $_->[0], $_) } @fields;
59         my %data;
60         foreach my $key ($r->param()) {
61             next if grep { $key eq $_ } qw(id update);
62             $data{$key} = $r->param($key);
63         }
64
65         $nchanges = modify_xml_document($xc, \%fieldsByKey, \%data);
66         if ($nchanges) {
67             ### Set e:metaInfo/e:dateModified
68         }
69         ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode());
70     }
71 </%perl>
72      <h2><% xml_encode($id) %></h2>
73 % if (defined $update) {
74      <p><b>The record has been updated (nchanges=<% $nchanges %>).</b></p>
75 % }
76 % if ($nchanges) {
77      <p><b>Changed <% $nchanges %> element<% $nchanges == 1 ? "" : "s" %>.</b></p>
78 % }
79      <form method="get" action="">
80       <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
81 <%perl>
82     foreach my $ref (@fields) {
83         my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
84 </%perl>
85        <tr>
86         <th><% $caption %></th>
87         <td>
88 % my $data = xml_encode($xc->find($xpath));
89 % if ($nlines) {
90          <textarea name="<% $name %>" rows="<% $nlines %>" cols="61"><% $data %></textarea>
91 % } else {
92          <input name="<% $name %>" type="text" size="60" value="<% $data %>">
93 % }
94         </td>
95        </tr>
96 %   }
97        <tr>
98         <td align="right" colspan="2">
99          <input type="submit" name="update" value="Update"/>
100          <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
101         </td>
102        </tr>
103       </table>
104      </form>
105 % }