summaryrefslogtreecommitdiff
path: root/httemplate/edit/elements/edit.html
blob: 120c03a3c5c83581feae6d0ca9882d4168891884 (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
<%

  # 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 (notyet) coderef
  # if not specified all columns (except for the primary key) will be editable
  # 'fields' => [
  #             ]
  #
  # 'menubar'     => '', #menubar arrayref
  #
  # '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)
    });

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

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

  } else { #adding

    $object = $class->new( {} );

  }

  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 ( @$fields ) {

    my( $field, $type);
    if ( ref($f) ) {
      $field = $f->{'field'},
      $type  = $f->{'type'} || 'text',
    } else {
      $field = $f;
      $type = 'text';
    }

%>

  <TR>

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

    <%
      #eventually more options for <SELECT>, etc. fields
    %>

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

  </TR>

<% } %>

</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") %>