service refactor!
[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 %  # # overrides default popurl(1)."process/$table.html"
58 %  # 'post_url' => popurl(1).'process/something', 
59 %
60 %  my(%opt) = @_;
61 %
62 %  #false laziness w/process.html
63 %  my $table = $opt{'table'};
64 %  my $class = "FS::$table";
65 %  my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
66 %  my $fields = $opt{'fields'}
67 %               #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
68 %               || [ grep { $_ ne $pkey } fields($table) ];
69 %  #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;
70 %
71 %  my $object;
72 %  if ( $cgi->param('error') ) {
73 %
74 %    $object = $class->new( {
75 %      map { $_ => scalar($cgi->param($_)) } fields($table)
76 %    });
77 %
78 %    &{$opt{'error_callback'}}($cgi, $object)
79 %      if $opt{'error_callback'};
80 %
81 %  } elsif ( $cgi->keywords || $cgi->param($pkey) ) { #editing
82 %
83 %    my $value;
84 %    if ( $cgi->param($pkey) ) {
85 %      $value = $cgi->param($pkey)
86 %    } else { 
87 %      my( $query ) = $cgi->keywords;
88 %      $value = $query;
89 %    }
90 %    $value =~ /^(\d+)$/ or die "unparsable $pkey";
91 %    $object = qsearchs( $table, { $pkey => $1 } );
92 %    warn "$table $pkey => $1"
93 %      if $opt{'debug'};
94 %
95 %    &{$opt{'edit_callback'}}($cgi, $object)
96 %      if $opt{'edit_callback'};
97 %
98 %  } else { #adding
99 %
100 %    my $hashref = $opt{'new_hashref_callback'}
101 %                    ? &{$opt{'new_hashref_callback'}}
102 %                    : {};
103 %
104 %    $object = $class->new( $hashref );
105 %
106 %    &{$opt{'new_callback'}}($cgi, $object)
107 %      if $opt{'new_callback'};
108 %
109 %  }
110 %
111 %  my $action = $object->$pkey() ? 'Edit' : 'Add';
112 %
113 %  my $title = "$action $opt{'name'}";
114 %
115 %  my $viewall_url = $p . ( $opt{'viewall_dir'} || 'search' ) . "/$table.html";
116 %  $viewall_url = $opt{'viewall_url'} if $opt{'viewall_url'};  
117 %
118 %  my @menubar = ();
119 %  if ( $opt{'menubar'} ) {
120 %    @menubar = @{ $opt{'menubar'} };
121 %  } else {
122 %    @menubar = (
123 %      'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
124 %      #eventually use Lingua::bs to pluralize
125 %      "View all $opt{'name'}s" => $viewall_url,
126 %    );
127 %  }
128 %
129 %
130 <% include("/elements/header.html", $title,
131               include( '/elements/menubar.html', @menubar )
132            )
133 %>
134 % if ( $cgi->param('error') ) { 
135
136   <FONT SIZE="+1" COLOR="#ff0000">Error: <% $cgi->param('error') %></FONT>
137   <BR><BR>
138 % } 
139
140 % my $url = $opt{'post_url'} || popurl(1)."process/$table.html";
141
142 <FORM ACTION="<% $url %>" METHOD=POST>
143 <INPUT TYPE="hidden" NAME="svcdb" VALUE="<% $table %>">
144 <INPUT TYPE="hidden" NAME="<% $pkey %>" VALUE="<% $object->$pkey() %>">
145 <% ( $opt{labels} && exists $opt{labels}->{$pkey} )
146       ? $opt{labels}->{$pkey}
147       : $pkey
148 %>
149 #<% $object->$pkey() || "(NEW)" %>
150
151 <% ntable("#cccccc",2) %>
152 % foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
153 %                       @$fields
154 %                 ) {
155 %
156 %    &{ $opt{'field_callback'} }( $f )
157 %      if $opt{'field_callback'};
158 %
159 %    my $field = $f->{'field'};
160 %    my $type = $f->{'type'} ||= 'text';
161 %
162 %
163
164
165   <TR>
166
167     <TD ALIGN="right">
168       <% ( $opt{labels} && exists $opt{labels}->{$field} )
169               ? $opt{labels}->{$field}
170               : $field
171       %>
172     </TD>
173
174 % if ( $type eq 'fixed' ) { 
175
176       <TD BGCOLOR="#dddddd"><% $f->{'value'} %></TD>
177       <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $f->{'value'} %>">
178
179 % } elsif ( $type eq 'fixedhidden' ) {
180
181       <INPUT TYPE="hidden" NAME="<% $field %>" VALUE="<% $f->{'value'} %>">
182
183 % } elsif ( $type eq 'checkbox' ) { 
184
185       <TD>
186         <INPUT TYPE="checkbox" NAME="<% $field %>" VALUE="<% $f->{'value'} %>" <% $object->$field() eq $f->{'value'} ? ' CHECKED' : '' %>>
187       </TD>
188
189 % } elsif ( $type eq 'select' ) { 
190
191       <TD>
192         <SELECT NAME="<% $field %>" 
193 %     my $aref = $f->{'value'}{'values'};
194 %     my $vkey = $f->{'value'}{'vcolumn'};
195 %     my $ckey = $f->{'value'}{'ccolumn'};
196 %     foreach my $v (@$aref) {
197           <OPTION <% ($object->$field() eq $v->$vkey) ? 'SELECTED' : '' %>
198             VALUE="<% $v->$vkey %>"><% $v->$ckey %></OPTION>
199 %     }
200         </SELECT>
201       </TD>
202
203 % } else { 
204
205       <TD>
206         <INPUT TYPE="<% $type %>" NAME="<% $field %>" VALUE="<% $object->$field() %>">
207       <TD>
208
209 % } 
210
211   </TR>
212
213 % } 
214
215 <% ref( $opt{'html_table_bottom'} )
216       ? &{ $opt{'html_table_bottom'} }( $object )
217       : $opt{'html_table_bottom'}
218 %>
219
220 </TABLE>
221
222 <% ref( $opt{'html_bottom'} )
223       ? &{ $opt{'html_bottom'} }( $object )
224       : $opt{'html_bottom'}
225 %>
226
227 <BR>
228
229 <INPUT TYPE="submit" VALUE="<% $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">
230
231 </FORM>
232
233 <% include("/elements/footer.html") %>
234