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