default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / httemplate / view / elements / svc_Common.html
1 <%doc>
2
3 #Example:
4
5   include( 'elements/svc_Common.html, 
6
7              'table' => 'svc_something'
8
9              'labels' => {
10                            'column' => 'Label',
11                          },
12
13              #listref - each item is a literal column name (or method) or
14              # (notyet) coderef.  if not specified all columns (except for the
15              #primary key) will be viewable
16              'fields' => [
17                          ]
18
19              # defaults to "edit/$table.cgi?", will have svcnum appended
20              'edit_url' => 
21
22              #at the very bottom (well, as low as you can go from here)
23              'html_foot'  => '',
24
25              #only needed for svc_broadband Manage Devices link
26              'manage_link' => scalar($conf->config('svc_broadband-manage_link')),
27              'manage_link_text' => scalar($conf->config('svc_broadband-manage_link_text')),
28              'manage_link-new_window' => $conf->exists('svc_broadband-manage_link-new_window')
29
30          )
31
32 </%doc>
33 <SCRIPT>
34 function areyousure(href) {
35 % my $delmsg = emt("Permanently delete this [_1]?", $label);
36   if (confirm(<% $delmsg |js_string %>) == true)
37     window.location.href = href;
38 }
39 </SCRIPT>
40 <STYLE>
41   td.content {
42     background-color: #ffffff;
43   }
44   .error {
45     color: #ff0000;
46     font-weight: bold;
47   }
48 </STYLE>
49
50 % if ( $custnum ) { 
51
52   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value) &>
53
54   <& /elements/small_custview.html, $custnum, '', 1,
55      "${p}view/cust_main.cgi" &>
56   <BR>
57
58 % } else { 
59
60   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value), menubar(
61       emt("Cancel this (unaudited) [_1]",$label) =>
62             "javascript:areyousure(\'${p}misc/cancel-unaudited.cgi?$svcnum\')"
63   ) &>
64
65 % } 
66
67 % if ( $opt{radius_usage} ) {
68     <& svc_radius_usage.html,
69               'svc'      => $svc_x,
70               'part_svc' => $part_svc,
71               'cust_pkg' => $cust_pkg,
72     &>
73 % }
74
75 <% mt('Service #') |h %><B><% $svcnum %></B>
76 % if ( $custnum ) {
77 %   my $url = $opt{'edit_url'} || $p. 'edit/'. $opt{'table'}. '.cgi?';
78 <& /view/elements/svc_edit_link.html, 'svc' => $svc_x, 'part_svc' => $part_svc, 'edit_url' => $url,
79   'manage_link'               => $opt{'manage_link'},
80   'manage_link_text'          => $opt{'manage_link_text'},
81   'manage_link-new_window'    => $opt{'manage_link-new_window'}
82 &>
83 % }
84 <BR>
85
86 <% ntable("#cccccc") %><TR><TD><% ntable("#cccccc",2) %>
87
88 % foreach my $f ( @$fields ) {
89 %   my ($field, $label, $value) = &{ $format_field }($f);
90 %   next if !$field; 
91       <TR>
92         <TD ALIGN="right">
93           <% $label %>
94         </TD>
95
96         <TD CLASS="content">
97           <% $value %>
98         </TD>
99       </TR>
100
101 % }
102
103 % foreach (sort { $a cmp $b } $svc_x->virtual_fields) { 
104   <% $svc_x->pvf($_)->widget('HTML', 'view', $svc_x->getfield($_)) %>
105 % } 
106
107
108 </TABLE></TD></TR></TABLE>
109
110 <BR>
111
112 <& svc_devices.html,
113      'svc_x' => $svc_x,
114      'table' => $svc_x->device_table,
115 &>
116
117 % if ( defined($opt{'html_foot'}) ) {
118
119   <% ref($opt{'html_foot'})
120        ? &{ $opt{'html_foot'} }($svc_x)
121        : $opt{'html_foot'}
122   %>
123   <BR>
124
125 % }
126
127 % if ( $cust_svc ) {
128 <& /elements/table-tickets.html, object => $cust_svc &>
129 % }
130
131 <% joblisting({'svcnum'=>$svcnum}, 1) %>
132
133 <% include('/elements/footer.html') %>
134 <%init>
135
136 die "access denied"
137   unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
138
139 my(%opt) = @_;
140
141 my $conf = new FS::Conf;
142 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
143
144 my $table = $opt{'table'};
145
146 my $fields = $opt{'fields'}
147              #|| [ grep { $_ ne 'svcnum' } dbdef->table($table)->columns ];
148              || [ grep { $_ ne 'svcnum' } fields($table) ];
149
150 my $svcnum;
151 if ( $cgi->param('svcnum') ) {
152   $cgi->param('svcnum') =~ /^(\d+)$/ or die "unparseable svcnum";
153   $svcnum = $1;
154 } else {
155   my($query) = $cgi->keywords;
156   $query =~ /^(\d+)$/ or die "no svcnum";
157   $svcnum = $1;
158 }
159 my $svc_x = qsearchs({
160   'select'    => $opt{'table'}.'.*',
161   'table'     => $opt{'table'},
162   'addl_from' => ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
163                  ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
164                  ' LEFT JOIN cust_main USING ( custnum ) ',
165   'hashref'   => { 'svcnum' => $svcnum },
166   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql(
167                             'null_right' => 'View/link unlinked services'
168                           ),
169 }) or die "Unknown svcnum $svcnum in ". $opt{'table'}. " table\n";
170
171 my $cust_svc = $svc_x->cust_svc;
172 my ($label, $value, $svcdb, $part_svc );
173 my $labels = $opt{labels} || {};
174
175 if ( $cust_svc ) {
176   ($label, $value, $svcdb) = $cust_svc->label;
177
178   $part_svc = $cust_svc->part_svc;
179
180   #false laziness w/edit/svc_Common.html
181   #override default labels with service-definition labels if applicable
182   foreach my $field ( keys %$labels ) {
183     my $col = $part_svc->part_svc_column($field);
184     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
185   }
186 } else {
187   $label = "Unlinked $table";
188   $value = $svc_x->label;
189   $svcdb = $table;
190   # just to satisfy callbacks
191   $part_svc = FS::part_svc->new({ svcpart => 0, svcdb => $table });
192 }
193
194 my $pkgnum = $cust_svc->pkgnum if $cust_svc;
195
196 my($cust_pkg, $custnum);
197 if ($pkgnum) {
198   $cust_pkg = $cust_svc->cust_pkg;
199   $custnum = $cust_pkg->custnum;
200 } else {
201   $cust_pkg = '';
202   $custnum = '';
203 }
204
205 # attached routers
206 if ( my $router = qsearchs('router', { svcnum => $svc_x->svcnum }) ) {
207   push @$fields,
208     'router_routername',
209     'router_block';
210
211   $labels->{'router_routername'} = 'Attached router';
212   $labels->{'router_block'} = 'Attached address block';
213   $svc_x->set('router_routername', $router->routername);
214   my $block = qsearchs('addr_block', { routernum => $router->routernum });
215   if ( $block ) {
216     $svc_x->set('router_block', $block->cidr);
217   } else {
218     $svc_x->set('router_block', '(none)');
219   }
220 }
221
222 my @inventory_items = $svc_x->inventory_item;
223
224 my $format_field = sub {
225   my $f = shift;
226   my($field, $type, $value);
227   if ( ref($f) ) {
228     $field = $f->{'field'};
229     $type  = $f->{'type'} || 'text';
230   } else {
231     $field = $f;
232     $type = 'text';
233   }
234
235   my $columndef = $part_svc->part_svc_column($field);
236   # skip fields that are fixed and empty
237   if ( $columndef->columnflag eq 'F'
238        and length($columndef->columnvalue) == 0 ) {
239     return;
240   }
241
242   # things that override the column value: value_callback, select
243   if ( ref($f) and $f->{'value_callback'} ) {
244
245     my $hack_strict_refs = \&{ $f->{'value_callback'} };
246     $value = &$hack_strict_refs($svc_x);
247
248   } elsif ( $type eq 'select-table' ) {
249     # imitates the /elements/select-table interface
250     $value = $svc_x->$field;
251
252     my $value_col = $f->{'value_col'} ||
253                     dbdef->table($f->{'table'})->primary_key;
254     my $name_col = $f->{'name_col'} or die 'name_col required';
255     # we don't yet support multiple-valued fields here
256     my $obj = qsearchs($f->{'table'}, { $value_col => $value });
257     if ( $obj ) {
258       $value = $obj->$name_col; # can be any method of the object
259     } else {
260       # show the raw value, but mark it as an error
261       $value = '<SPAN CLASS="error">' . $f->{'table'} . ' ' .
262                 encode_entities($value) . '</SPAN>';
263     }
264
265   } else {
266     $value = encode_entities($svc_x->$field);
267   }
268
269   # inventory-select field with multiple classes
270   # show the class name to disambiguate
271   if ( $columndef->columnflag =~ /^[MA]$/ && $columndef->columnvalue =~ /,/ )
272   {
273     my ($item) = grep { $_->svc_field eq $field } @inventory_items;
274     my $class = qsearchs('inventory_class', { classnum => $item->classnum });
275     $value .= ' <i>('. $class->classname . ')</i>' if $class;
276   }
277
278   # formatting tweaks
279   if ( $type eq 'date' and $value ) {
280     $value = time2str($date_format,$value)
281   } elsif ( $type eq 'datetime' and $value ) {
282     $value = time2str("$date_format %H:%M",$value)
283   } elsif ( $type eq 'checkbox' ) {
284     $value = $value eq 'Y' ? emt('Yes') : emt('No');
285   } elsif ( $type =~ /(input-)?mac_addr/ and $value =~ /\w/) {
286     my $vendor = Net::MAC::Vendor::lookup($value);
287     $value .= ' ('. $vendor->[0]. ')' if $vendor;
288     $value = $m->scomp('/elements/mac_addr.html', $value);
289   }
290
291   # 'link' option
292   my $href;
293   if ( ref($f) and exists $f->{'link'} ) {
294     my $link = $f->{'link'};
295     if ( ref($link) eq 'CODE' ) {
296       $link = &{$link}($svc_x);
297     }
298     if ( ref($link) eq 'ARRAY' ) {
299       my ($base, $method) = @$link;
300       $href = $base . $svc_x->$method();
301     } elsif ( !ref($link) ) {
302       $href = $link;
303     }
304
305     if ( $href ) {
306       $value = qq!<A HREF="$href">$value</A>!;
307     }
308   }
309
310   my $label = $opt{labels}->{$field} || $field;
311   return ($field, $label, $value);
312 };
313
314 &{ $opt{'svc_callback'} }( $cgi, $svc_x, $part_svc, $cust_pkg, $fields, \%opt ) 
315     if $opt{'svc_callback'};
316 </%init>