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