summaryrefslogtreecommitdiff
path: root/httemplate/edit/elements/edit.html
blob: c40a00492d2d142f86fde4d204907d20dcd0f02a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<%

  # options example...
  #
  # 'name'  =>
  # 'table' =>
  # #? 'primary_key' => #required when the dbdef doesn't know...???
  # 'labels' => {
  #               'column' => 'Label',
  #             }
  #
  # listref - each item is a literal column name (or method) or hashref
  #                                                          or (notyet) coderef
  # if not specified all columns (except for the primary key) will be editable
  # 'fields' => [
  #               'columname',
  #               { 'field' => 'another_columname',
  #                 'type'  => 'text', #text, fixed, hidden, checkbox
  #                                    #eventually more for <SELECT>, etc.
  #                 'value' => 'Y', #only for checkbox
  #               },
  #             ]
  #
  # 'menubar'     => '', #menubar arrayref
  #
  # #run when re-displaying with an error
  # 'error_callback' => sub { my( $cgi, $object ) = @_; },
  #
  # #run when editing
  # 'edit_callback' => sub { my( $cgi, $object ) = @_; },
  #
  # # returns a hashref for the new object
  # 'new_hashref_callback'
  #
  # #run when adding
  # 'new_callback' => sub { my( $cgi, $object ) = @_; },
  #
  # #XXX describe
  # 'field_callback' => sub { },
  #
  # #string or coderef of additional HTML to add before </TABLE>
  # 'html_table_bottom' => '',
  #
  # 'viewall_dir' => '', #'search' or 'browse', defaults to 'search'
  #
  # 'html_bottom' => '', #string
  # 'html_bottom' => sub {
  #                        my $object = shift;
  #                        # ...
  #                        "html_string";
  #                      },

  my(%opt) = @_;

  #false laziness w/process.html
  my $table = $opt{'table'};
  my $class = "FS::$table";
  my $pkey = dbdef->table($table)->primary_key; #? $opt{'primary_key'} || 
  my $fields = $opt{'fields'}
               #|| [ grep { $_ ne $pkey } dbdef->table($table)->columns ];
               || [ grep { $_ ne $pkey } fields($table) ];
  #my @actualfields = map { ref($_) ? $_->{'field'} : $_ } @$fields;

  my $object;
  if ( $cgi->param('error') ) {

    $object = $class->new( {
      map { $_ => scalar($cgi->param($_)) } fields($table)
    });

    &{$opt{'error_callback'}}($cgi, $object)
      if $opt{'error_callback'};

  } elsif ( $cgi->keywords ) { #editing

    my( $query ) = $cgi->keywords;
    $query =~ /^(\d+)$/;
    $object = qsearchs( $table, { $pkey => $1 } );
    warn "$table $pkey => $1"
      if $opt{'debug'};

    &{$opt{'edit_callback'}}($cgi, $object)
      if $opt{'edit_callback'};

  } else { #adding

    my $hashref = $opt{'new_hashref_callback'}
                    ? &{$opt{'new_hashref_callback'}}
                    : {};

    $object = $class->new( $hashref );

    &{$opt{'new_callback'}}($cgi, $object)
      if $opt{'new_callback'};

  }

  my $action = $object->$pkey() ? 'Edit' : 'Add';

  my $title = "$action $opt{'name'}";

  my @menubar = ();
  if ( $opt{'menubar'} ) {
    @menubar = @{ $opt{'menubar'} };
  } else {
    @menubar = (
      'Main menu' => $p, #eventually get rid of this when the ACL/UI update is done
      #eventually use Lingua::bs to pluralize
      "View all $opt{'name'}s" => $p. ( $opt{'viewall_dir'} || 'search' ).
                                  "/$table.html",
    );
  }

%><%= include("/elements/header.html", $title,
              include( '/elements/menubar.html', @menubar )
           )
%>

<% if ( $cgi->param('error') ) { %>
  <FONT SIZE="+1" COLOR="#ff0000">Error: <%= $cgi->param('error') %></FONT>
  <BR><BR>
<% } %>

<FORM ACTION="<%= popurl(1) %>process/<%= $table %>.html" METHOD=POST>
<INPUT TYPE="hidden" NAME="<%= $pkey %>" VALUE="<%= $object->$pkey() %>">
<%= ( $opt{labels} && exists $opt{labels}->{$pkey} )
      ? $opt{labels}->{$pkey}
      : $pkey
%>
#<%= $object->$pkey() || "(NEW)" %>

<%= ntable("#cccccc",2) %>

<% foreach my $f ( map { ref($_) ? $_ : {'field'=>$_} }
                       @$fields
                 ) {

    &{ $opt{'field_callback'} }( $f )
      if $opt{'field_callback'};

    my $field = $f->{'field'};
    my $type = $f->{'type'} ||= 'text';

%>

  <TR>

    <TD ALIGN="right">
      <%= ( $opt{labels} && exists $opt{labels}->{$field} )
              ? $opt{labels}->{$field}
              : $field
      %>
    </TD>

    <% if ( $type eq 'fixed' ) { %>

      <TD BGCOLOR="#dddddd"><%= $f->{'value'} %></TD>
      <INPUT TYPE="hidden" NAME="<%= $field %>" VALUE="<%= $f->{'value'} %>">

    <% } elsif ( $type eq 'checkbox' ) { %>

      <TD>
        <INPUT TYPE="checkbox" NAME="<%= $field %>" VALUE="<%= $f->{'value'} %>" <%= $object->$field() eq $f->{'value'} ? ' CHECKED' : '' %>>
      </TD>

    <% } else { %>

      <TD>
        <INPUT TYPE="<%= $type %>" NAME="<%= $field %>" VALUE="<%= $object->$field() %>">
      <TD>

    <% } %>

  </TR>

<% } %>

<%= ref( $opt{'html_table_bottom'} )
      ? &{ $opt{'html_table_bottom'} }( $object )
      : $opt{'html_table_bottom'}
%>

</TABLE>

<%= ref( $opt{'html_bottom'} )
      ? &{ $opt{'html_bottom'} }( $object )
      : $opt{'html_bottom'}
%>

<BR>

<INPUT TYPE="submit" VALUE="<%= $object->$pkey() ? "Apply changes" : "Add $opt{'name'}" %>">

</FORM>

<%= include("/elements/footer.html") %>