merge NG auth, RT#21563
[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          )
26
27 </%doc>
28 <SCRIPT>
29 function areyousure(href) {
30 % my $delmsg = emt("Permanently delete this [_1]?", $label);
31   if (confirm(<% $delmsg |js_string %>) == true)
32     window.location.href = href;
33 }
34 </SCRIPT>
35
36 % if ( $custnum ) { 
37
38   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value) &>
39
40   <& /elements/small_custview.html, $custnum, '', 1,
41      "${p}view/cust_main.cgi" &>
42   <BR>
43
44 % } else { 
45
46   <& /elements/header.html, mt("View [_1]: [_2]",$label,$value), menubar(
47       emt("Cancel this (unaudited) [_1]",$label) =>
48             "javascript:areyousure(\'${p}misc/cancel-unaudited.cgi?$svcnum\')"
49   ) &>
50
51 % } 
52
53 <% mt('Service #') |h %><B><% $svcnum %></B>
54 % if ( $custnum ) {
55 %   my $url = $opt{'edit_url'} || $p. 'edit/'. $opt{'table'}. '.cgi?';
56 <& /view/elements/svc_edit_link.html, 'svc' => $svc_x, 'edit_url' => $url &>
57 % }
58 <BR>
59
60 <% ntable("#cccccc") %><TR><TD><% ntable("#cccccc",2) %>
61
62 % my @inventory_items = $svc_x->inventory_item;
63 % foreach my $f ( @$fields ) {
64 %
65 %   my($field, $type, $value);
66 %   if ( ref($f) ) {
67 %     $field = $f->{'field'};
68 %     $type  = $f->{'type'} || 'text';
69 %     if ( $f->{'value_callback'} ) {
70 %       my $hack_strict_refs = \&{ $f->{'value_callback'} };
71 %       $value = &$hack_strict_refs($svc_x);
72 %     } else {
73 %       $value = exists($f->{'value'}) ? $f->{'value'} : $svc_x->$field;
74 %     }
75 %   } else {
76 %     $field = $f;
77 %     $type = 'text';
78 %     $value = $svc_x->$field;
79 %   }
80 %
81 %   my $columndef = $part_svc->part_svc_column($field);
82 %   if ( $columndef->columnflag =~ /^[MA]$/ && $columndef->columnvalue =~ /,/ )
83 %   {
84 %     # inventory-select field with multiple classes
85 %     # show the class name to disambiguate
86 %     my ($item) = grep { $_->svc_field eq $field } @inventory_items;
87 %     my $class = qsearchs('inventory_class', { classnum => $item->classnum });
88 %     $value .= ' <i>('. $class->classname . ')</i>' if $class;
89 %   }
90 %   unless ($columndef->columnflag eq 'F' && !length($columndef->columnvalue)) {
91
92       <TR>
93         <TD ALIGN="right">
94           <% ( $opt{labels} && exists $opt{labels}->{$field} )
95                   ? $opt{labels}->{$field}
96                   : $field
97           %>
98         </TD>
99
100 %       $value = time2str($date_format,$value) if ( $type eq 'date' && $value );
101 %       $value = time2str("$date_format %H:%M",$value) if ( $type eq 'datetime' && $value );
102 %       $value = $value eq 'Y' ? emt('Yes') : emt('No') if ( $type eq 'checkbox' );
103 %       #eventually more options for <SELECT>, etc. fields
104
105         <TD BGCOLOR="#ffffff"><% $value %><TD>
106
107       </TR>
108
109 %   }
110 %
111 % } 
112
113 % foreach (sort { $a cmp $b } $svc_x->virtual_fields) { 
114   <% $svc_x->pvf($_)->widget('HTML', 'view', $svc_x->getfield($_)) %>
115 % } 
116
117
118 </TABLE></TD></TR></TABLE>
119
120 <BR>
121
122 % if ( defined($opt{'html_foot'}) ) {
123
124   <% ref($opt{'html_foot'})
125        ? &{ $opt{'html_foot'} }($svc_x)
126        : $opt{'html_foot'}
127   %>
128   <BR>
129
130 % }
131
132 % if ( $cust_svc ) {
133 <& /elements/table-tickets.html, object => $cust_svc &>
134 % }
135
136 <% joblisting({'svcnum'=>$svcnum}, 1) %>
137
138 <% include('/elements/footer.html') %>
139 <%init>
140
141 die "access denied"
142   unless $FS::CurrentUser::CurrentUser->access_right('View customer services');
143
144 my(%opt) = @_;
145
146 my $conf = new FS::Conf;
147 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
148
149 my $table = $opt{'table'};
150
151 my $fields = $opt{'fields'}
152              #|| [ grep { $_ ne 'svcnum' } dbdef->table($table)->columns ];
153              || [ grep { $_ ne 'svcnum' } fields($table) ];
154
155 my $svcnum;
156 if ( $cgi->param('svcnum') ) {
157   $cgi->param('svcnum') =~ /^(\d+)$/ or die "unparseable svcnum";
158   $svcnum = $1;
159 } else {
160   my($query) = $cgi->keywords;
161   $query =~ /^(\d+)$/ or die "no svcnum";
162   $svcnum = $1;
163 }
164 my $svc_x = qsearchs({
165   'select'    => $opt{'table'}.'.*',
166   'table'     => $opt{'table'},
167   'addl_from' => ' LEFT JOIN cust_svc  USING ( svcnum  ) '.
168                  ' LEFT JOIN cust_pkg  USING ( pkgnum  ) '.
169                  ' LEFT JOIN cust_main USING ( custnum ) ',
170   'hashref'   => { 'svcnum' => $svcnum },
171   'extra_sql' => ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql(
172                             'null_right' => 'View/link unlinked services'
173                           ),
174 }) or die "Unknown svcnum $svcnum in ". $opt{'table'}. " table\n";
175
176 my $cust_svc = $svc_x->cust_svc;
177 my ($label, $value, $svcdb, $part_svc );
178 my $labels = $opt{labels}; #not -> here
179
180 if ( $cust_svc ) {
181   ($label, $value, $svcdb) = $cust_svc->label;
182
183   $part_svc = $cust_svc->part_svc;
184
185   #false laziness w/edit/svc_Common.html
186   #override default labels with service-definition labels if applicable
187   foreach my $field ( keys %$labels ) {
188     my $col = $part_svc->part_svc_column($field);
189     $labels->{$field} = $col->columnlabel if $col->columnlabel !~ /^\s*$/;
190   }
191 } else {
192   $label = "Unlinked $table";
193   $value = $svc_x->label;
194   $svcdb = $table;
195   # just to satisfy callbacks
196   $part_svc = FS::part_svc->new({ svcpart => 0, svcdb => $table });
197 }
198
199 my $pkgnum = $cust_svc->pkgnum if $cust_svc;
200
201 my($cust_pkg, $custnum);
202 if ($pkgnum) {
203   $cust_pkg = $cust_svc->cust_pkg;
204   $custnum = $cust_pkg->custnum;
205 } else {
206   $cust_pkg = '';
207   $custnum = '';
208 }
209
210 &{ $opt{'svc_callback'} }( $cgi, $svc_x, $part_svc, $cust_pkg, $fields, \%opt ) 
211     if $opt{'svc_callback'};
212 </%init>