ACLs: finish group edit (agents + rights) & browse
[freeside.git] / httemplate / edit / elements / edit.html
1 <%
2
3   # options example...
4   #
5   # 'name'  =>
6   # 'table' =>
7   # #? 'primary_key' => #required when the dbdef doesn't know...???
8   # 'labels' => {
9   #               'column' => 'Label',
10   #             }
11   #
12   # listref - each item is a literal column name (or method) or (notyet) coderef
13   # if not specified all columns (except for the primary key) will be editable
14   # 'fields' => [
15   #             ]
16   #
17   # 'menubar'     => '', #menubar arrayref
18   #
19   # #run when re-displaying with an error
20   # 'error_callback' => sub { my $cgi, $object = @_; },
21   #
22   # #run when editing
23   # 'edit_callback' => sub { my $cgi, $object = @_; },
24   #
25   # #run when adding
26   # 'new_callback' => sub { my $cgi, $object = @_; },
27   #
28   # #broken'html_table_bottom' => '', #string or listref of additinal HTML to
29   #                            #add before </TABLE>
30   #
31   # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
32   #
33   # 'html_bottom' => '', #string
34   # 'html_bottom' => sub {
35   #                        my $object = shift;
36   #                        # ...
37   #                        "html_string";
38   #                      },
39
40   my(%opt) = @_;
41
42   #false laziness w/process.html
43   my $table = $opt{'table'};
44   my $class = "FS::$table";
45   my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
46   my $fields = $opt{'fields'}
47                #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
48                || [ grep { $_ ne $pkey } fields($table) ];
49   #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
50
51   my $object;
52   if ( $cgi->param('error') ) {
53
54     $object = $class->new( {
55       map { $_ => scalar($cgi->param($_)) } fields($table)
56     });
57
58     &{$opt{'error_callback'}}($cgi, $object)
59       if $opt{'error_callback'};
60
61   } elsif ( $cgi->keywords ) { #editing
62
63     my( $query ) = $cgi->keywords;
64     $query =~ /^(\d+)$/;
65     $object = qsearchs( $table, { $pkey => $1 } );
66
67     &{$opt{'edit_callback'}}($cgi, $object)
68       if $opt{'edit_callback'};
69
70   } else { #adding
71
72     $object = $class->new( {} );
73
74     &{$opt{'new_callback'}}($cgi, $object)
75       if $opt{'new_callback'};
76
77   }
78
79   my $action = $object->$pkey() ? 'Edit' : 'Add';
80
81   my $title = "$action $opt{'name'}";
82
83   my @menubar = ();
84   if ( $opt{'menubar'} ) {
85     @menubar = @{ $opt{'menubar'} };
86   } else {
87     @menubar = (
88       'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
89       #eventually use Lingua::bs to pluralize
90       "View all $opt{'name'}s" => $p. ( $opt{'viewall_dir'} || 'search' ).
91                                   "/$table.html",
92     );
93   }
94
95 %><%= include("/elements/header.html", $title,
96               include( '/elements/menubar.html', @menubar )
97            )
98 %>
99
100 <% if ( $cgi->param('error') ) { %>
101   <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
102   <BR><BR>
103 <% } %>
104
105 <FORM ACTION="<%= popurl(1) %>process/<%= $table %>.html" METHOD=POST>
106 <INPUT TYPE="hidden" NAME="<%= $pkey %>" VALUE="<%= $object->$pkey() %>">
107 <%= ( $opt{labels} && exists $opt{labels}->{$pkey} )
108       ? $opt{labels}->{$pkey}
109       : $pkey
110 %>
111 #<%= $object->$pkey() || "(NEW)" %>
112
113 <%= ntable("#cccccc",2) %>
114
115 <% foreach my $f ( @$fields ) {
116
117     my( $field, $type);
118     if ( ref($f) ) {
119       $field = $f->{'field'},
120       $type  = $f->{'type'} || 'text',
121     } else {
122       $field = $f;
123       $type = 'text';
124     }
125
126 %>
127
128   <TR>
129
130     <TD ALIGN="right">
131       <%= ( $opt{labels} && exists $opt{labels}->{$field} )
132               ? $opt{labels}->{$field}
133               : $field
134       %>
135     </TD>
136
137     <%
138       #eventually more options for <SELECT>, etc. fields
139     %>
140
141     <TD>
142       <INPUT TYPE="<%= $type %>" NAME="<%= $field %>" VALUE="<%= $object->$field() %>">
143     <TD>
144
145   </TR>
146
147 <% } %>
148
149 </TABLE>
150
151 <%= ref( $opt{'html_bottom'} )
152       ? &{ $opt{'html_bottom'} }( $object )
153       : $opt{'html_bottom'}
154 %>
155
156 <BR>
157
158 <INPUT TYPE="submit" VALUE="<%= $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
159
160 </FORM>
161
162 <%= include("/elements/footer.html") %>
163