43e520155d3e6864199ae9e44d991327848cc86e
[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 'selfservice_access'
44 %              or $field eq 'comment'
45 %              or $field eq 'invoice_dest'
46 %              or $field eq 'message_dest' ) {
47 %         $value = $X_contact->get($field);
48 %       } else {
49 %         $value = $contact->get($field);
50 %       }
51
52         <TD>
53 %         if ( $field eq 'selfservice_access' ) {
54
55             <SELECT NAME = "<%$name%>_<%$field%>"
56                     ID   = "<%$id%>_<%$field%>"
57                     STYLE = "width: 140px"
58             >
59               <OPTION VALUE="">Disabled
60 %             if ( $value || $self_base_url ) {
61                 <OPTION VALUE="<% $value eq 'Y' ? 'Y' : 'E' %>" <% $value eq 'Y' ? 'SELECTED' : '' %>>Enabled
62 %               if ( $value eq 'Y' && $self_base_url ) {
63                   <OPTION VALUE="R">Re-email
64                   <OPTION VALUE="P"><% $pwd_change_label %>
65 %               }
66 %             }
67             </SELECT>
68                   <& /elements/change_password.html,
69                    'contact_num'         => $curr_value,
70                    'custnum'             => $opt{'custnum'},
71                    'curr_value'          => '',
72                    'no_label_display'    => '1',
73                    'noformtag'           => '1',
74                    'pre_pwd_field_label' => $id.'_',
75                   &>
76             <SCRIPT TYPE="text/javascript">
77                     document.getElementById("<%$id%>_<%$field%>").onchange = function() {
78                       if (this.value == "P" || this.value == "E") { changepw<%$id%>_toggle(true); }
79                       return false
80                     }
81             </SCRIPT>
82 %         } elsif ( $field eq 'invoice_dest' || $field eq 'message_dest' ) {
83 %           my $curr_value = $cgi->param($name . '_' . $field);
84 %           $curr_value = $value if !defined($curr_value);
85             <& select.html,
86                 field         => $name . '_' . $field,
87                 curr_value    => $curr_value,
88                 options       => [ '', 'Y' ],
89                 option_labels => { '' => 'no', 'Y' => 'yes' },
90                 style         => 'width: 100%',
91             &>
92 %         } else {
93             <INPUT TYPE  = "text"
94                    NAME  = "<%$name%>_<%$field%>"
95                    ID    = "<%$id%>_<%$field%>"
96                    SIZE  = "<% $size{$field} || 14 %>"
97                    VALUE = "<% scalar($cgi->param($name . '_' . $field))
98                                || $value |h %>"
99                    <% $onchange %>
100             >
101 %         }
102           <BR>
103           <FONT SIZE="-1"><% $label{$field} %></FONT>
104         </TD>
105 %     }
106     </TR>
107   </TABLE>
108
109 % }
110 <%init>
111
112 my( %opt ) = @_;
113
114 my $conf = new FS::Conf;
115
116 my $self_base_url = $conf->config('selfservice_server-base_url');
117
118 my $name = $opt{'element_name'} || $opt{'field'} || 'contactnum';
119 my $id = $opt{'id'} || 'contactnum';
120
121 my $curr_value = $opt{'curr_value'} || $opt{'value'};
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 my $X_contact;
136 if ( $curr_value ) {
137   $contact = qsearchs('contact', { 'contactnum' => $curr_value } );
138   if ( $opt{'custnum'} ) {
139     $X_contact = qsearchs('cust_contact', {
140                             'contactnum' => $curr_value,
141                             'custnum'    => $opt{'custnum'},
142                  });
143   } elsif ( $opt{'prospectnum'} ) {
144     $X_contact = qsearchs('prospect_contact', {
145                    'contactnum'  => $curr_value,
146                    'prospectnum' => $opt{'prospectnum'},
147                  });
148     $opt{'for_prospect'} = 1;
149   } else {
150     die 'neither custnum nor prospectnum specified';
151   }
152 } else {
153   $contact = new FS::contact {};
154   if ($opt{'for_prospect'}) {
155     $X_contact = new FS::prospect_contact;
156   } else {
157     $X_contact = new FS::cust_contact;
158   }
159 }
160
161 my %size = ( 'title' => 12 );
162
163 tie my %label, 'Tie::IxHash',
164   'first'              => 'First name',
165   'last'               => 'Last name',
166   'title'              => 'Title/Position',
167   'emailaddress'       => 'Email'
168 ;
169
170 unless ($opt{'for_prospect'}) {
171   $label{'invoice_dest'} = 'Send&nbsp;invoices';
172   $label{'message_dest'} = 'Send&nbsp;messages';
173   $label{'selfservice_access'} = 'Self-service';
174 }
175
176 my $first = 0;
177 foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
178   next if $phone_type->typename =~ /^(Home|Fax)$/;
179   my $f = 'phonetypenum'.$phone_type->phonetypenum;
180   $label{$f} = $phone_type->typename. ' phone';
181   $size{$f} = $first++ ? 10 : 15;
182 }
183
184 $label{'comment'} = 'Comment';
185
186 my @fields = $opt{'name_only'} ? qw( first last ) : keys %label;
187
188 my $pwd_change_label = 'Change Password';
189 $pwd_change_label = 'Setup Password' unless $contact->_password;
190
191 </%init>