suspension and cancellation reasons
[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 hashref
13 %  #                                                          or (notyet) coderef
14 %  # if not specified all columns (except for the primary key) will be editable
15 %  # 'fields' => [
16 %  #               'columname',
17 %  #               { 'field' => 'another_columname',
18 %  #                 'type'  => 'text', #text, fixed, hidden, checkbox
19 %  #                                    #eventually more for <SELECT>, etc.
20 %  #                 'value' => 'Y', #only for checkbox
21 %  #               },
22 %  #             ]
23 %  #
24 %  # 'menubar'     => '', #menubar arrayref
25 %  #
26 %  # #run when re-displaying with an error
27 %  # 'error_callback' => sub { my( $cgi, $object ) = @_; },
28 %  #
29 %  # #run when editing
30 %  # 'edit_callback' => sub { my( $cgi, $object ) = @_; },
31 %  #
32 %  # # returns a hashref for the new object
33 %  # 'new_hashref_callback'
34 %  #
35 %  # #run when adding
36 %  # 'new_callback' => sub { my( $cgi, $object ) = @_; },
37 %  #
38 %  # #XXX describe
39 %  # 'field_callback' => sub { },
40 %  #
41 %  # #string or coderef of additional HTML to add before </TABLE>
42 %  # 'html_table_bottom' => '',
43 %  #
44 %  # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
45 %  #
46 %  # 'html_bottom' => '', #string
47 %  # 'html_bottom' => sub {
48 %  #                        my $object = shift;
49 %  #                        # ...
50 %  #                        "html_string";
51 %  #                      },
52 %
53 %  my(%opt) = @_;
54 %
55 %  #false laziness w/process.html
56 %  my $table = $opt{'table'};
57 %  my $class = "FS::$table";
58 %  my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
59 %  my $fields = $opt{'fields'}
60 %               #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
61 %               || [ grep { $_ ne $pkey } fields($table) ];
62 %  #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
63 %
64 %  my $object;
65 %  if ( $cgi->param('error') ) {
66 %
67 %    $object = $class->new( {
68 %      map { $_ => scalar($cgi->param($_)) } fields($table)
69 %    });
70 %
71 %    &{$opt{'error_callback'}}($cgi, $object)
72 %      if $opt{'error_callback'};
73 %
74 %  } elsif ( $cgi->keywords || $cgi->param($pkey) ) { #editing
75 %
76 %    my( $query ) = $cgi->keywords;
77 %    $query = $cgi->param($pkey) unless $query;
78 %    $query =~ /^(\d+)$/;
79 %    $object = qsearchs( $table, { $pkey => $1 } );
80 %    warn "$table $pkey => $1"
81 %      if $opt{'debug'};
82 %
83 %    &{$opt{'edit_callback'}}($cgi, $object)
84 %      if $opt{'edit_callback'};
85 %
86 %  } else { #adding
87 %
88 %    my $hashref = $opt{'new_hashref_callback'}
89 %                    ? &{$opt{'new_hashref_callback'}}
90 %                    : {};
91 %
92 %    $object = $class->new( $hashref );
93 %
94 %    &{$opt{'new_callback'}}($cgi, $object)
95 %      if $opt{'new_callback'};
96 %
97 %  }
98 %
99 %  my $action = $object->$pkey() ? 'Edit' : 'Add';
100 %
101 %  my $title = "$action $opt{'name'}";
102 %
103 %  my $viewall_url = $p . ( $opt{'viewall_dir'} || 'search' ) . "/$table.html";
104 %  $viewall_url = $opt{'viewall_url'} if $opt{'viewall_url'};  
105 %
106 %  my @menubar = ();
107 %  if ( $opt{'menubar'} ) {
108 %    @menubar = @{ $opt{'menubar'} };
109 %  } else {
110 %    @menubar = (
111 %      'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
112 %      #eventually use Lingua::bs to pluralize
113 %      "View all $opt{'name'}s" => $viewall_url,
114 %    );
115 %  }
116 %
117 %
118 <% include("/elements/header.html", $title,
119               include( '/elements/menubar.html', @menubar )
120            )
121 %>
122 % if ( $cgi->param('error') ) { 
123
124   <FONT SIZE="+1" COLOR="#ff0000">Error: <% $cgi->param('error') %></FONT>
125   <BR><BR>
126 % } 
127
128
129 <FORM ACTION="<% popurl(1) %>process/<% $table %>.html" METHOD=POST>
130 <INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $object->$pkey() %>">
131 <% ( $opt{labels} && exists $opt{labels}->{$pkey} )
132       ? $opt{labels}->{$pkey}
133       : $pkey
134 %>
135 #<% $object->$pkey() || "(NEW)" %>
136
137 <% ntable("#cccccc",2) %>
138 % foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
139 %                       @$fields
140 %                 ) {
141 %
142 %    &{ $opt{'field_callback'} }( $f )
143 %      if $opt{'field_callback'};
144 %
145 %    my $field = $f->{'field'};
146 %    my $type = $f->{'type'} ||= 'text';
147 %
148 %
149
150
151   <TR>
152
153     <TD ALIGN="right">
154       <% ( $opt{labels} && exists $opt{labels}->{$field} )
155               ? $opt{labels}->{$field}
156               : $field
157       %>
158     </TD>
159 % if ( $type eq 'fixed' ) { 
160
161
162       <TD BGCOLOR="#dddddd"><% $f->{'value'} %></TD>
163       <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $f->{'value'} %>">
164 % } elsif ( $type eq 'fixedhidden' ) { 
165
166
167       <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $f->{'value'} %>">
168 % } elsif ( $type eq 'checkbox' ) { 
169
170
171       <TD>
172         <INPUT TYPE="checkbox" NAME="<% $field %>" VALUE="<% $f->{'value'} %>" <% $object->$field() eq $f->{'value'} ? ' CHECKED' : '' %>>
173       </TD>
174 % } elsif ( $type eq 'select' ) { 
175       <TD>
176         <SELECT NAME="<% $field %>" 
177 %     my $aref = $f->{'value'}{'values'};
178 %     my $vkey = $f->{'value'}{'vcolumn'};
179 %     my $ckey = $f->{'value'}{'ccolumn'};
180 %     foreach my $v (@$aref) {
181           <OPTION <% ($object->$field() eq $v->$vkey) ? 'SELECTED' : '' %>
182             VALUE="<% $v->$vkey %>"><% $v->$ckey %></OPTION>
183 %     }
184         </SELECT>
185       </TD>
186 % } else { 
187
188
189       <TD>
190         <INPUT TYPE="<% $type %>" NAME="<% $field %>" VALUE="<% $object->$field() %>">
191       <TD>
192 % } 
193
194
195   </TR>
196 % } 
197
198
199 <% ref( $opt{'html_table_bottom'} )
200       ? &{ $opt{'html_table_bottom'} }( $object )
201       : $opt{'html_table_bottom'}
202 %>
203
204 </TABLE>
205
206 <% ref( $opt{'html_bottom'} )
207       ? &{ $opt{'html_bottom'} }( $object )
208       : $opt{'html_bottom'}
209 %>
210
211 <BR>
212
213 <INPUT TYPE="submit" VALUE="<% $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
214
215 </FORM>
216
217 <% include("/elements/footer.html") %>
218