]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/cgi-bin/lib-setup.cgi
config updates...
[Evergreen.git] / Open-ILS / src / cgi-bin / lib-setup.cgi
1 #!/usr/bin/perl
2 use strict;
3
4 use OpenILS::Application::Storage;
5 use OpenILS::Application::Storage::CDBI;
6
7 # I need to abstract the driver loading away...
8 use OpenILS::Application::Storage::Driver::Pg;
9
10 use CGI qw/:standard start_*/;
11
12 OpenILS::Application::Storage::CDBI->connection('dbi:Pg:host=10.0.0.2;dbname=open-ils-dev', 'postgres');
13 OpenILS::Application::Storage::CDBI->db_Main->{ AutoCommit } = 1;
14
15 my $cgi = new CGI;
16
17 #-------------------------------------------------------------------------------
18 # setup part
19 #-------------------------------------------------------------------------------
20
21 my %org_cols = ( qw/id SysID name Name parent_ou Parent ou_type OrgUnitType shortname ShortName/ );
22
23 my @col_display_order = ( qw/id name shortname ou_type parent_ou/ );
24
25 if (my $action = $cgi->param('action')) {
26         if ( $action eq 'Update' ) {
27                 for my $id ( ($cgi->param('id')) ) {
28                         my $u = actor::org_unit->retrieve($id);
29                         for my $col ( keys %org_cols ) {
30                                 $u->$col( $cgi->param($col."_$id") );
31                         }
32                         $u->update;
33                 }
34         } elsif ( $action eq 'Add New' ) {
35                 actor::org_unit->create( { map { defined($cgi->param($_)) ? ($_ => $cgi->param($_)) : () } keys %org_cols } );
36         }
37 }
38
39 #-------------------------------------------------------------------------------
40 # HTML part
41 #-------------------------------------------------------------------------------
42
43 print <<HEADER;
44 Content-type: text/html
45
46 <html>
47
48 <head>
49         <style>
50                 table.table_class {
51                         border: dashed lightgrey 1px;
52                         background-color: #EEE;
53                         border-collapse: collapse;
54                 }
55
56                 deactivated {
57                         color: lightgrey;
58                 }
59
60                 tr.row_class td {
61                         border: solid lightgrey 1px;
62                 }
63                 
64                 tr.header_class th {
65                         background-color: lightblue;
66                 }
67
68         </style>
69         <script language='javascript' src='/js/widgets/xtree.js'></script>
70 </head>
71
72 <body style='padding: 25px;'>
73
74 <h1>Library Hierarchy Setup</h1>
75 <hr/>
76 HEADER
77
78 my $top;
79 for my $lib ( actor::org_unit->search( {parent_ou=>undef} ) ) {
80         my $name = $lib->name;
81         $name =~ s/'/\\'/og;
82         print <<"       HEADER";
83 <div style="float: left;">
84         <script language='javascript'>
85
86                 function getById (id) { return document.getElementById(id); }
87                 function createAppElement (el) { return document.createElement(el); }
88                 function createAppTextNode (txt) { return document.createTextNode(txt); }
89         
90                 var node_$lib = new WebFXTree('$name');
91         HEADER
92         $top = $lib->id;
93         last;
94 }
95
96 for my $lib ( actor::org_unit->search_like( {parent_ou => '%'}, {order_by => 'id'} ) ) {
97         my $name = $lib->name;
98         $name =~ s/'/\\'/og;
99         my $parent = $lib->parent_ou;
100         my $uri = $cgi->url(-relative=>1);
101         print <<"       JS"
102                 var node_$lib = new WebFXTreeItem('$name','$uri?action=child&id=$lib');
103         JS
104 }for my $lib ( sort {$a->name cmp $b->name} actor::org_unit->retrieve_all ) {
105         my $parent = $lib->parent_ou;
106         next unless $parent;
107         print <<"       JS"
108                 node_$parent.add(node_$lib);
109         JS
110 }
111
112 print <<HEADER;
113                 document.write(node_$top);
114         </script>
115 </div>
116 <div>
117
118 HEADER
119
120 #-------------------------------------------------------------------------------
121 # Logic part
122 #-------------------------------------------------------------------------------
123
124 if (my $action = $cgi->param('action')) {
125         if ( $action eq 'child' ) {
126                 my $id = $cgi->param('id');
127                 if ($id) {
128                         my $node = actor::org_unit->retrieve($id);
129                         #-----------------------------------------------------------------------
130                         # child form
131                         #-----------------------------------------------------------------------
132
133                         print "<h2>Edit ".$node->name."</h2>";
134                         print   "<form method='POST'>".
135                                 "<table class='table_class'><tr class='header_class'>\n";
136         
137                         print Tr(
138                                 th($org_cols{id}),
139                                 td( $node->id() ),
140                         );
141                         print Tr(
142                                 th($org_cols{name}),
143                                 td("<input type='text' name='name_$node' value='". $node->name() ."'>"),
144                         );
145                         print Tr(
146                                 th($org_cols{shortname}),
147                                 td("<input type='text' name='shortname_$node' value='". $node->shortname() ."'>"),
148                         );
149                         print Tr(
150                                 th($org_cols{ou_type}),
151                                 td("<select name='ou_type_$node'>".do{
152                                                         my $out = '';
153                                                         for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
154                                                                 $out .= "<option value='$type' ".do {
155                                                                         if ($node->ou_type == $type->id) {
156                                                                                 "selected";
157                                                                         }}.'>'.$type->name.'</option>'
158                                                         }
159                                                         $out;
160                                                 }."</select>"),
161                         );
162                         print Tr(
163                                 th($org_cols{parent_ou}),
164                                 td("<select name='parent_ou_$node'>".do{
165                                                 my $out = '';
166                                                 for my $org ( sort {$a->id <=> $b->id} actor::org_unit->retrieve_all) {
167                                                         $out .= "<option value='$org' ".do {
168                                                                 if ($node->parent_ou == $org->id) {
169                                                                         "selected";
170                                                                 }}.'>'.do{'&nbsp;&nbsp;'x$org->ou_type->depth}.$org->name.'</option>'
171                                                 }
172                                                 $out;
173                                         }."</select><input type='hidden' value='$node' name='id'>"),
174                         );
175
176                         print Tr( "<td colspan='2'><input type='submit' name='action' value='Update'/></td>" );
177
178                         print   "</table></form><hr/>";
179
180
181                         print "<h2>New Child</h2>";
182         
183                         print   "<form method='POST'>".
184                                 "<table class='table_class'>\n";
185
186                         print Tr(
187                                 th($org_cols{name}),
188                                 td("<input type='text' name='name'>"),
189                         );
190                         print Tr(
191                                 th($org_cols{shortname}),
192                                 td("<input type='text' name='shortname'>"),
193                         );
194                         print Tr(
195                                 th($org_cols{ou_type}),
196                                 td("<select name='ou_type_$node'>".do{
197                                                 my $out = '';
198                                                 for my $type ( sort {$a->depth <=> $b->depth} actor::org_unit_type->retrieve_all) {
199                                                         $out .= "<option value='$type'>".$type->name.'</option>'
200                                                 }
201                                                 $out;
202                                         }."</select>"),
203                         );
204                         print Tr( "<td colspan='2'><input type='hidden' value='$node' name='parent_ou'>",
205                                   "<input type='submit' name='action' value='Add New'/></td>" );
206                         print   "</table></form><hr/>";
207                 }
208         }
209 }
210         
211 print "</div></body></html>";
212
213