summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-part_pkg.html
blob: 1f7b42f8698b4510f2d9c0e05d6ca041d8fe7d1c (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<%doc>

Example:

  include( '/elements/select-part_pkg.html',

    #strongly recommended (you want your forms to be "sticky" on errors, right?)
    'curr_value' => 'current_value',
  
    #opt
    'part_pkg'   => \@records,
    'showdisabled' => 1, #defaults to 0, shows disabled if true
    'toggle_disabled' => 1, #adds js to toggle display of disabled
                            #currently cannot be used with many other options
                            #currently must be used with multiple or disable_empty

    #select-table.html options
  )

</%doc>

<& /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,
&>

% if ($opt{'toggle_disabled'}) {
%   unless ($toggle_disabled_init) {
%     $toggle_disabled_init = 1;

<% include('/elements/xmlhttp.html',
        'url'  => $fsurl.'misc/xmlhttp-part_pkg.cgi',
        'subs' => [ 'load_part_pkg' ],
   ) %>

<SCRIPT>
var toggle_disabled_part_pkg_list = undefined;
function toggle_disabled_part_pkg (id,showdisabled,noload) {
  // remove link text, only put it back if this works
  var trigger = document.getElementById(id+'_switch');
  trigger.innerHTML = '';
  // if needed, load pkg_list before trying again
  var pkg_list = toggle_disabled_part_pkg_list;
  if (!pkg_list && !noload) {
    load_part_pkg(
      function (result) {
        result = JSON.parse(result);
        if (result) {
          toggle_disabled_part_pkg_list = result;
          toggle_disabled_part_pkg(id,showdisabled,true);
        } else {
          console.log('Error loading pkg list');
        }
      }
    );
    return;
  }
  if (!(pkg_list && pkg_list.length)) {
    console.log('Cannot load pkg list, or pkg list empty');
    return;
  }
  // remove all existing options, tracking which are selected
  var selectbox = document.getElementById(id);
  var selected = {};
  for (var i = selectbox.length - 1; i >= 0; i--) {
    var nextopt = selectbox.options[i];
    selected[nextopt.value] = nextopt.selected;
    nextopt.parentNode.removeChild(nextopt);
  }
  // regenerate options list from pkg list
  for (var i = 0; i < pkg_list.length; i++) {
    var pkginfo = pkg_list[i];
    if (showdisabled || selected[pkginfo.pkgpart] || !pkginfo.disabled) {
      var newopt = document.createElement('option');
      newopt.value = pkginfo.pkgpart;
      newopt.text = pkginfo.label;
      newopt.selected = selected[newopt.value] ? true : false;
      selectbox.appendChild(newopt);
    }
  }
  trigger.innerHTML = showdisabled ? '<% $hidetext %>' : '<% $showtext %>';
  trigger.onclick = function () { toggle_disabled_part_pkg(id,(showdisabled ? 0 : 1)) };
}
</SCRIPT>

%   } # unless $toggle_disabled_init

<BR><A 
  HREF="javascript:void(0)"
  STYLE="font-size: smaller"
  ONCLICK="toggle_disabled_part_pkg('<% $opt{'id'} %>',<% $showdisabled ? '0' : '1' %>)"
  ID="<% $opt{'id'} %>_switch"><% $showdisabled ? $hidetext : $showtext %></A>

% } # if $opt{'toggle_disabled'}

<%shared>
my $toggle_disabled_init = 0;
</%shared>
<%init>
 
my( %opt ) = @_;

# toggle_disabled relies on matching search/label options with xmlhttp-part_pkg
# it would be difficult to handle these in the current xmlhttp implementation
# this is defensive coding, not a guarantee it'll work with options not listed here
if ($opt{'toggle_disabled'}) {
  foreach my $someopt ( qw(
    records
    part_pkg
    table
    agent_virt
    agent_null
    agent_null_right
    name_col
    label_callback
    hashref
    extra_sql
    addl_from
    order_by
    pre_options
    post_options
    select
  ) ) {
    die "Cannot use toggle_disabled with $someopt"
      if exists $opt{$someopt};
  }
}

# this would be fairly easy to implement, but not needed right now
die 'Use of toggle_disabled without multiple or disable_empty has not been implemented'
  if $opt{'toggle_disabled'} && !($opt{'multiple'} || $opt{'disable_empty'});

$opt{'records'} = delete $opt{'part_pkg'}
  if $opt{'part_pkg'};

my $showdisabled = $opt{'showdisabled'};

my %hash = ();
$hash{'disabled'} = '' unless $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
}

# currently can't handle this, either
die 'Use of toggle_disabled with classnum has not been implemented'
  if $opt{'toggle_disabled'} && exists($hash{'classnum'});

# CAUTION: For proper functioning of toggle_disabled,
# please ensure changes to default options are synced with misc/xmlhttp-part_pkg.cgi

$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;

my $showtext = emt("show disabled packages");
my $hidetext = emt("hide disabled packages");

</%init>