RT# 75817 - fixed issue with contact edit on edit customer page
[freeside.git] / httemplate / elements / contact.html
1 % if ( $opt{'js_only'} ) {
2 <% $js %>
3 % } else {
4
5   <INPUT TYPE="hidden" NAME="<%$name%>" ID="<%$id%>" VALUE="<% $curr_value %>">
6
7   <TABLE STYLE="display:inline">
8     <TR>
9 %     if ( @contact_class && ! $opt{name_only} ) {
10         <TD>
11           <SELECT NAME="<%$name%>_classnum" <% $onchange %>>
12             <OPTION VALUE="">
13 %           my $classnum = scalar($cgi->param($name.'_classnum'))
14 %                            || $contact->classnum;
15 %           foreach my $contact_class (@contact_class) {
16               <OPTION VALUE="<% $contact_class->classnum %>"
17                  <% ($contact_class->classnum == $classnum) ? 'SELECTED' : '' %>
18               ><% $contact_class->classname |h %>
19 %           }
20           </SELECT><BR>
21           <FONT SIZE="-1">Type</FONT>
22         </TD>
23 %     } else {
24         <INPUT TYPE="hidden" NAME="<%$name%>_classnum" VALUE="">
25 %     }
26 %
27 %     foreach my $field ( @fields ) {
28 %
29 %       my $value = '';
30 %       if ( $field =~ /^phonetypenum(\d+)$/ ) {
31 %         my $contact_phone = qsearchs('contact_phone', {
32 %           'contactnum'   => $curr_value,
33 %           'phonetypenum' => $1,
34 %         });
35 %         if ( $contact_phone ) {
36 %           $value = $contact_phone->phonenum_pretty;
37 %           $value .= 'x'.$contact_phone->extension
38 %             if $contact_phone->extension;
39 %           $value = '+'. $contact_phone->countrycode. " $value"
40 %             if $contact_phone->countrycode
41 %             && $contact_phone->countrycode ne '1';
42 %         }
43 %       } elsif ( $field eq 'emailaddress' ) {
44 %         $value = join(', ', map $_->emailaddress, $contact->contact_email);
45 %       } elsif ( $field eq 'password' ) {
46 %          $value = $contact->get('_password') ? '********' : '';
47 %       } else {
48 %         $value = $contact->get($field);
49 %       }
50
51         <TD>
52 %         if ( $field eq 'selfservice_access' ) {
53
54             <SELECT NAME = "<%$name%>_<%$field%>"
55                     ID   = "<%$id%>_<%$field%>"
56                     STYLE = "width: 140px"
57             >
58               <OPTION VALUE="" <% !$value ? 'SELECTED' : '' %>>Disabled
59 %             if ( $value || $self_base_url ) {
60                 <OPTION VALUE="<% $value eq 'Y' ? 'Y' : 'E' %>" <% $value eq 'Y' ? 'SELECTED' : '' %>>Enabled
61 %               if ( $value eq 'Y' && $self_base_url ) {
62                   <OPTION VALUE="R">Re-email
63 %               }
64 %             }
65             </SELECT>
66 % #password form
67 %         } elsif ( $field eq 'password') {
68             <INPUT TYPE  = "text"
69                    NAME  = "<%$name%>_<%$field%>"
70                    ID    = "changepw<%$id%>_<%$field%>"
71                    SIZE  = "<% $size{$field} || 14 %>"
72                    VALUE = ""
73                    placeholder = "<% $value |h %>"
74             >
75             <SCRIPT>
76               <% $js %>
77             </SCRIPT>
78 %         } elsif ( $field eq 'invoice_dest' ) {
79 %           my $curr_value = $cgi->param($name . '_' . $field);
80 %           $curr_value = $value if !defined($curr_value);
81             <& select.html,
82                 field         => $name . '_' . $field,
83                 curr_value    => $curr_value,
84                 options       => [ '', 'Y' ],
85                 option_labels => { '' => 'no', 'Y' => 'yes' },
86                 style         => 'width: 100%',
87             &>
88 %         } else {
89             <INPUT TYPE  = "text"
90                    NAME  = "<%$name%>_<%$field%>"
91                    ID    = "<%$id%>_<%$field%>"
92                    SIZE  = "<% $size{$field} || 14 %>"
93                    VALUE = "<% scalar($cgi->param($name."_$field"))
94                                || $value |h %>"
95                    <% $onchange %>
96             >
97 %         }
98           <BR>
99           <FONT SIZE="-1"><% $label{$field} %></FONT>
100 %       if ( $field eq 'password' ) {
101           <DIV ID="changepw<%$id%>_<%$field%>_result" STYLE="font-size: smaller"></DIV>
102 %       }
103         </TD>
104 %     }
105     </TR>
106   </TABLE>
107
108 % }
109 <%init>
110
111 my( %opt ) = @_;
112
113 my $conf = new FS::Conf;
114
115 my $self_base_url = $conf->config('selfservice_server-base_url');
116
117 my $name = $opt{'element_name'} || $opt{'field'} || 'contactnum';
118 my $id = $opt{'id'} || 'contactnum';
119
120 my $curr_value = $opt{'curr_value'} || $opt{'value'};
121 my $contactnum = $curr_value ? $curr_value : '0';
122
123 my $onchange = '';
124 if ( $opt{'onchange'} ) {
125   $onchange = $opt{'onchange'};
126   $onchange .= '(this)' unless $onchange =~ /\(\w*\);?$/;
127   $onchange =~ s/\(what\);/\(this\);/g; #ugh, terrible hack.  all onchange
128                                         #callbacks should act the same
129   $onchange = 'onChange="'. $onchange. '"';
130 }
131
132 my @contact_class = qsearch('contact_class', { 'disabled' => '' });
133
134 my $contact;
135 if ( $curr_value ) {
136   $contact = qsearchs('contact', { 'contactnum' => $curr_value } );
137 } else {
138   $contact = new FS::contact {};
139 }
140
141 my %size = ( 'title' => 12 );
142
143 tie my %label, 'Tie::IxHash',
144   'first'              => 'First name',
145   'last'               => 'Last name',
146   'title'              => 'Title/Position',
147   'emailaddress'       => 'Email',
148   'selfservice_access' => 'Self-service'
149 ;
150
151 unless ($opt{'for_prospect'}) {
152   $label{'selfservice_access'} = 'Self-service';
153   $label{'password'} = 'Password';
154 }
155
156 my $first = 0;
157 foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
158   next if $phone_type->typename =~ /^(Home|Fax)$/;
159   my $f = 'phonetypenum'.$phone_type->phonetypenum;
160   $label{$f} = $phone_type->typename. ' phone';
161   $size{$f} = $first++ ? 10 : 15;
162 }
163
164 $label{'comment'} = 'Comment';
165
166 my @fields = $opt{'name_only'} ? qw( first last ) : keys %label;
167
168 my $submitid = $opt{'submit_id'} ? $opt{'submit_id'} : 'submit';
169
170 my $js = qq(
171     add_password_validation('changepw$id\_password', '$submitid', '', '$contactnum');
172
173     var selfService = document.getElementById("$id\_selfservice_access").value;
174
175     if (selfService !== "Y") { document.getElementById("changepw$id\_password").disabled = 'true'; }
176     document.getElementById("$id\_selfservice_access").onchange = function() {
177       if (this.value == "P" || this.value == "E" || this.value =="Y") {
178         document.getElementById("changepw$id\_password").disabled = '';
179       }
180       else { document.getElementById("changepw$id\_password").disabled = 'true'; }
181       return false;
182     }
183 );
184
185 </%init>