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

  # 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

  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 $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
      "View all $opt{'name'}s" => "${p}search/$table.html", #eventually use Lingua::bs to pluralize
    );
  }

%>


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

  <TR>

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

    <%
      #just text in one size for now... eventually more options for
      # uneditable, hidden, <SELECT>, etc. fields
    %>

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

  </TR>

<% } %>

</TABLE>

<BR>

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

</FORM>

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