This commit was generated by cvs2svn to compensate for changes in r4888,
[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 ) { #editing
75 %
76 %    my( $query ) = $cgi->keywords;
77 %    $query =~ /^(\d+)$/;
78 %    $object = qsearchs( $table, { $pkey => $1 } );
79 %    warn "$table $pkey => $1"
80 %      if $opt{'debug'};
81 %
82 %    &{$opt{'edit_callback'}}($cgi, $object)
83 %      if $opt{'edit_callback'};
84 %
85 %  } else { #adding
86 %
87 %    my $hashref = $opt{'new_hashref_callback'}
88 %                    ? &{$opt{'new_hashref_callback'}}
89 %                    : {};
90 %
91 %    $object = $class->new( $hashref );
92 %
93 %    &{$opt{'new_callback'}}($cgi, $object)
94 %      if $opt{'new_callback'};
95 %
96 %  }
97 %
98 %  my $action = $object->$pkey() ? 'Edit' : 'Add';
99 %
100 %  my $title = "$action $opt{'name'}";
101 %
102 %  my @menubar = ();
103 %  if ( $opt{'menubar'} ) {
104 %    @menubar = @{ $opt{'menubar'} };
105 %  } else {
106 %    @menubar = (
107 %      'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
108 %      #eventually use Lingua::bs to pluralize
109 %      "View all $opt{'name'}s" => $p. ( $opt{'viewall_dir'} || 'search' ).
110 %                                  "/$table.html",
111 %    );
112 %  }
113 %
114 %
115 <% include("/elements/header.html", $title,
116               include( '/elements/menubar.html', @menubar )
117            )
118 %>
119 % if ( $cgi->param('error') ) { 
120
121   <FONT SIZE="+1" COLOR="#ff0000">Error: <% $cgi->param('error') %></FONT>
122   <BR><BR>
123 % } 
124
125
126 <FORM ACTION="<% popurl(1) %>process/<% $table %>.html" METHOD=POST>
127 <INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $object->$pkey() %>">
128 <% ( $opt{labels} && exists $opt{labels}->{$pkey} )
129       ? $opt{labels}->{$pkey}
130       : $pkey
131 %>
132 #<% $object->$pkey() || "(NEW)" %>
133
134 <% ntable("#cccccc",2) %>
135 % foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
136 %                       @$fields
137 %                 ) {
138 %
139 %    &{ $opt{'field_callback'} }( $f )
140 %      if $opt{'field_callback'};
141 %
142 %    my $field = $f->{'field'};
143 %    my $type = $f->{'type'} ||= 'text';
144 %
145 %
146
147
148   <TR>
149
150     <TD ALIGN="right">
151       <% ( $opt{labels} && exists $opt{labels}->{$field} )
152               ? $opt{labels}->{$field}
153               : $field
154       %>
155     </TD>
156 % if ( $type eq 'fixed' ) { 
157
158
159       <TD BGCOLOR="#dddddd"><% $f->{'value'} %></TD>
160       <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $f->{'value'} %>">
161 % } elsif ( $type eq 'checkbox' ) { 
162
163
164       <TD>
165         <INPUT TYPE="checkbox" NAME="<% $field %>" VALUE="<% $f->{'value'} %>" <% $object->$field() eq $f->{'value'} ? ' CHECKED' : '' %>>
166       </TD>
167 % } else { 
168
169
170       <TD>
171         <INPUT TYPE="<% $type %>" NAME="<% $field %>" VALUE="<% $object->$field() %>">
172       <TD>
173 % } 
174
175
176   </TR>
177 % } 
178
179
180 <% ref( $opt{'html_table_bottom'} )
181       ? &{ $opt{'html_table_bottom'} }( $object )
182       : $opt{'html_table_bottom'}
183 %>
184
185 </TABLE>
186
187 <% ref( $opt{'html_bottom'} )
188       ? &{ $opt{'html_bottom'} }( $object )
189       : $opt{'html_bottom'}
190 %>
191
192 <BR>
193
194 <INPUT TYPE="submit" VALUE="<% $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
195
196 </FORM>
197
198 <% include("/elements/footer.html") %>
199