Will things ever be the same again?
[freeside.git] / httemplate / elements / checkboxes-table.html
1 %
2 %
3 %  ##
4 %  # required
5 %  ##
6 %  # 'target_table'    => 'table_name',
7 %  # 'link_table'      => 'table_name',
8 %  #
9 %  # 'name_col' => 'name_column',
10 %  # #or
11 %  # 'name_callback' => sub { },
12 %  #
13 %  ##
14 %  # recommended (required?)
15 %  ##
16 %  # 'source_obj'   => $obj,
17 %  # #or?
18 %  # #'source_table' => 'table_name',
19 %  # #'sourcenum'    => '4', #current value of primary key in source_table
20 %  # #                       # (none is okay, just pass it if you have it)
21 %  ##
22 %  # optional
23 %  ##
24 %  # 'disable-able' => 1,
25 %
26 %  my( %opt ) = @_;
27 %
28 %  my $target_pkey = dbdef->table($opt{'target_table'})->primary_key;
29 %
30 %  my( $source_pkey, $sourcenum, $source_obj );
31 %  if ( $opt{'source_obj'} ) {
32 %
33 %    $source_obj = $opt{'source_obj'};
34 %    #$source_table = $source_obj->dbdef_table->table;
35 %    $source_pkey = $source_obj->dbdef_table->primary_key;
36 %    $sourcenum = $source_obj->$source_pkey();
37 %
38 %  } else {
39 %
40 %    #$source_obj?
41 %    $source_pkey = $opt{'source_table'}
42 %                     ? dbdef->table($opt{'source_table'})->primary_key
43 %                     : '';
44 %    $sourcenum = $opt{'sourcenum'};
45 %  }
46 %
47 %  my $hashref = $opt{'hashref'} || {};
48 %
49 %  my $extra_sql = '';
50 %
51 %  if ( $opt{'disable-able'} ) {
52 %    $hashref->{'disabled'} = '';
53 %
54 %    $extra_sql .= ( $sourcenum && $source_pkey ) 
55 %                    ? "OR $source_pkey = $sourcenum"
56 %                    : '';
57 %  }
58 %
59 %
60 % foreach my $target_obj (
61 %     qsearch({ 'table'     => $opt{'target_table'},
62 %               'hashref'   => $hashref,
63 %               'select'    => $opt{'target_table'}. '.*',
64 %               'addl_from' => "LEFT JOIN $opt{'link_table'} USING ( $target_pkey )",
65 %               'extra_sql' => $extra_sql,
66 %            })
67 %   ) {
68 %
69 %     my $targetnum = $target_obj->$target_pkey();
70 %
71 %     my $checked;
72 %     if ( $cgi->param('error') ) {
73 %
74 %       $checked = $cgi->param($target_pkey.$targetnum)
75 %                    ? 'CHECKED'
76 %                    : '';
77 %
78 %     } else {
79 %
80 %       $checked = qsearchs( $opt{'link_table'}, {
81 %                                                  $source_pkey => $sourcenum,
82 %                                                  $target_pkey => $targetnum,
83 %                                                }                             )
84 %                    ? 'CHECKED'
85 %                    : ''
86 %
87 %     }
88 %
89 %
90
91
92   <INPUT TYPE="checkbox" NAME="<% $target_pkey. $targetnum %>" <% $checked %> VALUE="ON">
93 % if ( $opt{'target_link'} ) { 
94
95
96     <A HREF="<% $opt{'target_link'} %><% $targetnum %>">
97 %
98 %
99 %  }
100 %  
101 <% $targetnum %>: 
102 % if ( $opt{'name_callback'} ) { 
103
104
105     <% &{ $opt{'name_callback'} }( $target_obj ) %><% $opt{'target_link'} ? '</A>' : '' %>
106 % } else {
107 %       my $name_col = $opt{'name_col'};
108 %  
109
110
111     <% $target_obj->$name_col() %><% $opt{'target_link'} ? '</A>' : '' %>
112 % } 
113 % if ( $opt{'disable-able'} ) { 
114
115
116     <% $target_obj->disabled =~ /^Y/i ? ' (DISABLED)' : '' %>
117 % } 
118
119
120   <BR>
121 % } 
122
123