RT#21463: Option to show disabled package definitions [does not work on IE]
[freeside.git] / httemplate / elements / select-part_pkg.html
index cd6d24c..9743780 100644 (file)
@@ -9,44 +9,87 @@ Example:
   
     #opt
     'part_pkg'   => \@records,
+    'showdisabled' => 1, #defaults to 0, shows disabled if true
+    'toggle_disabled' => 1, #implies showdisabled, adds js to toggle display of disabled
 
     #select-table.html options
   )
 
 </%doc>
 
-<% include( '/elements/select-table.html',
-              'table'          => 'part_pkg',
-              'agent_virt'     => 1,
-              'agent_null'     => 1,
-              'name_col'       => 'pkg',
-              'empty_label'    => 'Select package', #should this be the default?
-              'label_callback' => sub { shift->pkg_comment },
-              'hashref'        => { 'disabled' => '' },
-              %opt,
-          )
-%>
-<%init>
+<& /elements/select-table.html,
+     'table'          => 'part_pkg',
+     'agent_virt'     => 1,
+     'agent_null'     => 1,
+     'name_col'       => 'pkg',
+     'empty_label'    => 'Select package', #should this be the default?
+     'label_callback' => $opt{'label_callback'} || sub { shift->pkg_comment_only },
+     'hashref'        => \%hash,
+     $opt{'toggle_disabled'} ? (
+       'extra_option_attributes' => [ 'disabled' ],
+       'hidden_sub' => sub { my $rec = shift; $rec->disabled; },
+     ) : (),
+     %opt,
+&>
+
+% if ($opt{'toggle_disabled'}) {
+%   unless ($toggle_disabled_init) {
+%     $toggle_disabled_init = 1;
+
+<SCRIPT>
+function toggle_disabled (id, disabled_on) {
+  var selectbox = document.getElementById(id);
+  for (var i = 0; i < selectbox.length; i++) {
+    var optionbox = selectbox.options[i];
+    if (optionbox.getAttribute('data-disabled')) {
+      optionbox.hidden = (disabled_on || optionbox.selected) ? false : true;
+    }
+  }
+  var switchlink = document.getElementById(id+'_switch');
+  switchlink.innerHTML = disabled_on ? '<% emt("hide disabled packages") %>' : '<% emt("show disabled packages") %>';
+  switchlink.onclick = function () { toggle_disabled(id, disabled_on ? 0 : 1) };
+}
+</SCRIPT>
+
+%   } # unless $toggle_disabled_init
+
+<BR><A 
+  HREF="javascript:void(0)"
+  STYLE="font-size: smaller"
+  ONCLICK="toggle_disabled('<% $opt{'id'} %>',1)"
+  ID="<% $opt{'id'} %>_switch"><% emt("show disabled packages") %></A>
+
+% } # if $opt{'toggle_disabled'}
 
+<%shared>
+my $toggle_disabled_init = 0;
+</%shared>
+<%init>
 my( %opt ) = @_;
 
 $opt{'records'} = delete $opt{'part_pkg'}
   if $opt{'part_pkg'};
 
-#false laziness w/browse/part_pkg.cgi
-my $agentnums = join(',', $FS::CurrentUser::CurrentUser->agentnums);
-
-$opt{'extra_sql'} .=
-  " AND ( agentnum IS NOT NULL
-          OR 0 < ( SELECT COUNT(*)
-                     FROM type_pkgs
-                       LEFT JOIN agent_type USING ( typenum )
-                       LEFT JOIN agent AS typeagent USING ( typenum )
-                     WHERE type_pkgs.pkgpart = part_pkg.pkgpart
-                       AND typeagent.agentnum IN ($agentnums)
-                 )
-        )
-  ";
-#eofalse
+$opt{'showdisabled'} = 1 if $opt{'toggle_disabled'};
+
+my %hash = ();
+$hash{'disabled'} = '' unless $opt{'showdisabled'};
+
+if ( exists($opt{'classnum'}) && defined($opt{'classnum'}) ) {
+  if ( $opt{'classnum'} > 0 ) {
+    $hash{'classnum'} = $opt{'classnum'};
+  } elsif ( $opt{'classnum'} eq '' || $opt{'classnum'} == 0 ) {
+    $hash{'classnum'} = '';
+  } #else -1 or not specified, all classes, so don't set classnum
+}
+
+$opt{'select'} = 'part_pkg.*, setup_option.optionvalue AS _setup_fee,
+                              recur_option.optionvalue AS _recur_fee'
+  unless $opt{'select'};
+$opt{'addl_from'} .= FS::part_pkg->join_options_sql;
+
+$opt{'extra_sql'} .= ( keys(%hash) ? ' AND ' : ' WHERE ' ).
+                     FS::part_pkg->curuser_pkgs_sql;
 
 </%init>