summaryrefslogtreecommitdiff
path: root/httemplate/elements/tr-select-contact.html
blob: 4eb8a9879afbea1165b2609cf117f58c6d118c1b (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<%doc>

Example:

  include('/elements/tr-select-contact.html',
            'cgi'       => $cgi,

            'cust_main'     => $cust_main,
            #or
            'prospect_main' => $prospect_main,

            #optional
            'empty_label'   => '(default contact)',
         )

</%doc>

<SCRIPT TYPE="text/javascript">

  function contact_disable(what) {
%   for (@contact_fields) { 
      what.form.<%$_%>.disabled = true;
      var ftype = what.form.<%$_%>.tagName;
      if( ftype == 'SELECT') changeSelect(what.form.<%$_%>, '');
      else what.form.<%$_%>.value = '';
      if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#dddddd';
%   } 
  }

  function contact_clear(what) {
%   for (@contact_fields) { 
      var ftype = what.form.<%$_%>.tagName;
      if( ftype == 'INPUT' ) what.form.<%$_%>.value = '';
%   }
  }

  function contact_enable(what) {
%   for (@contact_fields) { 
      what.form.<%$_%>.disabled = false;
      var ftype = what.form.<%$_%>.tagName;
      if( ftype != 'SELECT') what.form.<%$_%>.style.backgroundColor = '#ffffff';
%   } 
  }

  function contactnum_changed(what) {
    var contactnum = what.options[what.selectedIndex].value;
    if ( contactnum == -1 ) { //Add new contact
      contact_clear(what);

      contact_enable(what);
      return;
    }

%   if ( $editable ) {
      if ( contactnum == 0 ) {
%   }

%       #sleep/wait until dropdowns are updated?
        contact_disable(what);

%   if ( $editable ) {
      } else {

%       #sleep/wait until dropdowns are updated?
        contact_enable(what);

      }
%   }

  }

  function changeSelect(what, value) {
    for ( var i=0; i<what.length; i++) {
      if ( what.options[i].value == value ) {
        what.selectedIndex = i;
      }
    }
  }

</SCRIPT>

<TR>
  <<%$th%> ALIGN="right" VALIGN="top"><% $opt{'label'} || emt('Service contact') %></<%$th%>>
  <TD VALIGN="top" COLSPAN=7>
    <SELECT NAME     = "contactnum"
            ID       = "contactnum"
            STYLE    = "vertical-align:top;margin:3px"
            onchange = "contactnum_changed(this);"
    >
% if ( $cust_main ) {
      <OPTION VALUE=""><% $opt{'empty_label'} || '(customer default)' |h %>
% }
%
%     foreach my $contact ( @contact ) {
        <OPTION VALUE="<% $contact->contactnum %>"
                <% $contactnum == $contact->contactnum ? 'SELECTED' : '' %>
        ><% $contact->line |h %>
%     }
%     if ( $addnew ) {
        <OPTION VALUE="-1"
                <% $contactnum == -1 ? 'SELECTED' : '' %>
        >New contact
%     }
    </SELECT>

<% include('/elements/contact.html',
             'object'       => $contact,
             #'onchange' ?  probably not
             'disabled'     => $disabled,
             'name_only'    => 1,
          )
%>

  </TD>
</TR>

<SCRIPT TYPE="text/javascript">
  contactnum_changed(document.getElementById('contactnum'));
</SCRIPT>
<%init>

#based on / kinda false laziness w/tr-select-cust_contact.html

my $conf = new FS::Conf;

my %opt = @_;
my $cgi           = $opt{'cgi'};
my $cust_pkg      = $opt{'cust_pkg'};
my $cust_main     = $opt{'cust_main'};
my $prospect_main = $opt{'prospect_main'};
die "cust_main or prospect_main required" unless $cust_main or $prospect_main;

my $contactnum = '';
if ( $cgi->param('error') ) {
  $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum";
  $contactnum = $1;
} else {
  if ( length($opt{'curr_value'}) ) {
    $contactnum = $opt{'curr_value'};
  } elsif ($prospect_main) {
    my @prospect_contact = $prospect_main->prospect_contact;
    $contactnum = $prospect_contact[0]->contactnum if scalar(@prospect_contact)==1;
  } else { #$cust_main
    $cgi->param('contactnum') =~ /^(\-?\d*)$/ or die "illegal contactnum";
    $contactnum = $1;
  }
}

##probably could use explicit controls
#my $editable = $cust_main ? 0 : 1; #could use explicit control
my $editable = 0;
my $addnew = $cust_main ? 1 : ( $contactnum>0 ? 0 : 1 );

my @contact_fields = map "contactnum_$_", qw( first last );

my $contact; #the one that shows by default in the contact edit space
if ( $contactnum && $contactnum > 0 ) {
  $contact = qsearchs('contact', { 'contactnum' => $contactnum } )
    or die "unknown contactnum";
} else {
  $contact = new FS::contact;
  if ( $contactnum == -1 ) {
    $contact->$_( $cgi->param($_) ) foreach @contact_fields; #XXX
  } elsif ( $cust_pkg && $cust_pkg->contactnum ) {
    my $pkg_contact = $cust_pkg->contact_obj;
    $contact->$_( $pkg_contact->$_ ) foreach @contact_fields; #XXX why are we making a new one gagain??
    $opt{'empty_label'} ||= 'package contact: '.$pkg_contact->line;
  } elsif ( $cust_main ) {
    $contact = new FS::contact; #I think
  }
}

my $contact_sort = sub {
     lc($a->last)  cmp lc($b->last)
  or lc($a->first) cmp lc($b->first)
};

my @contact;
push @contact, map $_->contact, $cust_main->cust_contact
  if $cust_main;
push @contact, map $_->contact, $prospect_main->prospect_contact
  if $prospect_main;
push @contact, $contact
  if !$cust_main && $contact && $contact->contactnum > 0
  && ! grep { $_->contactnum == $contact->contactnum } @contact;

@contact = sort $contact_sort grep !$_->disabled, @contact;

$contact = $contact[0]
  if ( $prospect_main )
  && !$opt{'is_optional'}
  && @contact;

my $disabled =
  ( $contactnum < 0
    || ( $editable && $contactnum )
    || ( $prospect_main
         && !$opt{'is_optional'} && !@contact && $addnew
       )
  )
    ? ''
    : 'DISABLED';

my $th = $opt{'no_bold'} ? 'TD' : 'TH';

</%init>