4.x style
[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'} || $opt{'object'} ) {
32 %
33 %    $source_obj = $opt{'source_obj'} || $opt{'object'};
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 = $opt{'extra_sql'} || '';
50 %
51 %  if ( $opt{'agent_virt'} ) {
52 %    $extra_sql .= ' AND' . $FS::CurrentUser::CurrentUser->agentnums_sql(
53 %                             'null_right' => $opt{'agent_null_right'}
54 %                           );
55 %  }
56 %
57 %  if ( $opt{'disable-able'} ) {
58 %    $hashref->{'disabled'} = '';
59 %
60 %    $extra_sql .= ( $sourcenum && $source_pkey ) 
61 %                    ? " OR $source_pkey = $sourcenum"
62 %                    : '';
63 %  }
64 %
65 %
66 % foreach my $target_obj (
67 %     qsearch({ 'table'     => $opt{'target_table'},
68 %               'hashref'   => $hashref,
69 %               'select'    => $opt{'target_table'}. '.*',
70 %               'addl_from' => "LEFT JOIN $opt{'link_table'} USING ( $target_pkey )",
71 %               'extra_sql' => $extra_sql,
72 %               'order_by'  => $opt{'order_by'},
73 %            })
74 %   ) {
75 %
76 %     my $targetnum = $target_obj->$target_pkey();
77 %
78 %     my $checked;
79 %     if ( $cgi->param('error') ) {
80 %
81 %       $checked = $cgi->param($target_pkey.$targetnum)
82 %                    ? 'CHECKED'
83 %                    : '';
84 %
85 %     } elsif ( !$sourcenum ) { # newly created object, has no links yet
86 %
87 %       if ( ref($opt{'default'}) ) {
88 %         $checked = $opt{'default'}->{$targetnum} ? 'CHECKED' : '';
89 %       } else {
90 %         $checked = $opt{'default'}               ? 'CHECKED' : ''
91 %       }
92 %
93 %     } else {
94 %
95 %       $checked = qsearchs( $opt{'link_table'}, {
96 %                                                  $source_pkey => $sourcenum,
97 %                                                  $target_pkey => $targetnum,
98 %                                                }                             )
99 %                    ? 'CHECKED'
100 %                    : ''
101 %
102 %     }
103 %
104
105 % ### use name sent to function else use target table and key.
106 % my $checkbox_name;
107 % if ($opt{'field'}) { $checkbox_name = $opt{'field'}; } else { $checkbox_name = "$target_pkey$targetnum"; }
108
109 % ### use values sent to function rather than values in table to determine CHECKED if values are sent.
110 % my $values;
111 % if (ref $opt{'value'} eq ref {} ) { $values = $opt{'value'}; }
112 % else { foreach my $value ( split(/\,/, $opt{'value'}) ) { $values->{$value} = $value; } }
113 % if ($values->{$targetnum}) { $checked = "CHECKED"; }
114
115 % my $color_col = $opt{'color_col'};
116   <INPUT TYPE="checkbox" NAME="<% $checkbox_name %>" <% $checked %> VALUE="<% $targetnum %>"><DIV STYLE="display:inline; background-color: #<% $color_col ? $target_obj->$color_col : '' %>">
117 % if ( $opt{'target_link'} ) { 
118     <A HREF="<% $opt{'target_link'} %><% $targetnum %>">
119 %  }
120 %  
121 % if ( $opt{'name_callback'} ) { 
122
123
124     <% &{ $opt{'name_callback'} }( $target_obj ) %><% $opt{'target_link'} ? '</A>' : '' %>
125 % } else {
126 %       my $name_col = $opt{'name_col'};
127 %  
128
129
130     <% $target_obj->$name_col() %></DIV><% $opt{'target_link'} ? '</A>' : '' %>
131 % } 
132 % if ( $opt{'disable-able'} ) { 
133
134
135     <% $target_obj->disabled =~ /^Y/i ? ' (DISABLED)' : '' %>
136 % } 
137
138
139   <BR>
140 % } 
141
142