Towards ability to add new elements when editing.
[irspy-moved-to-github.git] / web / htdocs / details / edit.mc
1 %# $Id: edit.mc,v 1.5 2006-10-31 09:26:59 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          ### Remember to set e:metaInfo/e:dateModified
53          );
54     my %fieldsByKey = map { ( $_->[0], $_) } @fields;
55     my $update = $r->param("update");
56     if (defined $update) {
57         # Update record with submitted data
58         foreach my $key ($r->param()) {
59             next if grep { $key eq $_ } qw(id update);
60             my $value = $r->param($key);
61             my $ref = $fieldsByKey{$key} or die "no field '$key'";
62             my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
63             my @nodes = $xc->findnodes($xpath);
64             if (@nodes) {
65                 warn scalar(@nodes), " nodes match '$xpath'" if @nodes > 1;
66                 my $node = $nodes[0];
67                 if ($node->isa("XML::LibXML::Attr")) {
68                     $node->setValue($value);
69                     #print "Attr $key <- '$value' ($xpath)<br/>\n";
70                 } elsif ($node->isa("XML::LibXML::Element")) {
71                     my $child = $node->firstChild();
72                     die "element child $child is not text"
73                         if !ref $child || !$child->isa("XML::LibXML::Text");
74                     $child->setData($value);
75                     #print "Elem $key <- '$value' ($xpath)<br/>\n";
76                 } else {
77                     warn "unexpected node type $node";
78                 }
79             } else {
80                 next if !$value;
81                 my($ppath, $element) = $xpath =~ /(.*)\/(.*)/;
82                 dom_add_element($xc, $ppath, $element, $value, @addAfter);
83             }
84         }
85         ZOOM::IRSpy::_really_rewrite_record($conn, $xc->getContextNode());
86     }
87 </%perl>
88      <h2><% xml_encode($id) %></h2>
89 % print "     <p><b>The record has been updated.</b></p>\n" if defined $update;
90      <form method="get" action="">
91       <table class="fullrecord" border="1" cellspacing="0" cellpadding="5" width="100%">
92 <%perl>
93     foreach my $ref (@fields) {
94         my($name, $nlines, $caption, $xpath, @addAfter) = @$ref;
95 </%perl>
96        <tr>
97         <th><% $caption %></th>
98         <td>
99 % my $data = xml_encode($xc->find($xpath));
100 % if ($nlines) {
101          <textarea name="<% $name %>" rows="<% $nlines %>" cols="61"><% $data %></textarea>
102 % } else {
103          <input name="<% $name %>" type="text" size="60" value="<% $data %>">
104 % }
105         </td>
106        </tr>
107 %   }
108        <tr>
109         <td align="right" colspan="2">
110          <input type="submit" name="update" value="Update"/>
111          <input type="hidden" name="id" value="<% xml_encode($id) %>"/>
112         </td>
113        </tr>
114       </table>
115      </form>
116 % }