backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / part_virtual_field.pm
1 package FS::part_virtual_field;
2 use base qw(FS::Record);
3
4 use strict;
5 use HTML::Entities;
6 use FS::Schema qw( dbdef );
7
8 =head1 NAME
9
10 FS::part_virtual_field - Object methods for part_virtual_field records
11
12 =head1 SYNOPSIS
13
14   use FS::part_virtual_field;
15
16   $record = new FS::part_virtual_field \%hash;
17   $record = new FS::part_virtual_field { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::part_virtual_field object represents the definition of a custom field 
30 (see the BACKGROUND section).  FS::part_virtual_field contains the name and 
31 base table of the field. 
32
33 FS::part_virtual_field inherits from FS::Record.  The following fields are 
34 currently supported:
35
36 =over 2
37
38 =item vfieldpart - primary key (assigned automatically)
39
40 =item name - name of the field
41
42 =item dbtable - table for which this virtual field is defined
43
44 =item length - expected length of the value (UI hint)
45
46 =item label - descriptive label for the field (UI hint)
47
48 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item new HASHREF
55
56 Create a new record.  To add the record to the database, see "insert".
57
58 =cut
59
60 sub table { 'part_virtual_field'; }
61 sub virtual_fields { () }
62 sub virtual_fields_hash { () }
63
64 =item widget UI_TYPE MODE [ VALUE ]
65
66 Generates UI code for a widget suitable for editing/viewing the field, based on 
67 list_source and length.  
68
69 The only UI_TYPE currently supported is 'HTML', and possible MODEs are 'view'
70 and 'edit'.
71
72 In HTML, all widgets are assumed to be table rows.  View widgets look like
73 <TR><TD ALIGN="right">Label</TD><TD BGCOLOR="#ffffff">Value</TD></TR>
74
75 (Most of the display style stuff, such as the colors, should probably go into 
76 a separate module specific to the UI.  That can wait, though.  The API for 
77 this function won't change.)
78
79 VALUE (optional) is the current value of the field.
80
81 =cut
82
83 sub widget {
84   my $self = shift;
85   my ($ui_type, $mode, $value, $header_col_type) = @_;
86   $header_col_type = 'TD' unless $header_col_type;
87   my $text;
88   my $label = $self->label || $self->name;
89
90   if ($ui_type eq 'HTML') {
91     if ($mode eq 'view') {
92       $text = q!<TR><!.$header_col_type.q! ALIGN="right">! . encode_entities($label) .
93               q!</!.$header_col_type.q!><TD BGCOLOR="#ffffff">! . encode_entities($value) .
94               q!</TD></TR>! . "\n";
95     } elsif ($mode eq 'edit') {
96       $text = q!<TR><!.$header_col_type.q! ALIGN="right">! . encode_entities($label) .
97               q!</!.$header_col_type.q!><TD>!;
98         $text .= q!<INPUT TYPE=text NAME="! . $self->name .
99                 q!" VALUE="! . encode_entities($value) . q!"!;
100         if ($self->length) {
101           $text .= q! SIZE="! . $self->length . q!"!;
102         }
103         $text .= '>';
104       $text .= q!</TD></TR>! . "\n";
105     } else {
106       return '';
107     }
108   } else {
109     return '';
110   }
111   return $text;
112 }
113
114
115 =item insert
116
117 Adds this record to the database.  If there is an error, returns the error,
118 otherwise returns false.
119
120 =item delete
121
122 Deletes this record from the database.  If there is an error, returns the
123 error, otherwise returns false.
124
125 =item replace OLD_RECORD
126
127 Replaces OLD_RECORD with this one in the database.  If there is an error,
128 returns the error, otherwise returns false.
129
130 =item check
131
132 If there is an error, returns the error, otherwise returns false.
133 Called by the insert and replace methods.
134
135 =back
136
137 =cut
138
139 sub check {
140   my $self = shift;
141
142   my $error = $self->ut_text('name') ||
143               $self->ut_text('dbtable') ||
144               $self->ut_number('length')
145               ;
146   return $error if $error;
147
148   # Make sure it's a real table with a numeric primary key
149   my ($table, $pkey);
150   if($table = dbdef->table($self->dbtable)) {
151     if($pkey = $table->primary_key) {
152       if($table->column($pkey)->type =~ /int/i) {
153         # this is what it should be
154       } else {
155         $error = "$table.$pkey is not an integer";
156       }
157     } else {
158       $error = "$table does not have a single-field primary key";
159     }
160   } else {
161     $error = "$table does not exist in the schema";
162   }
163   return $error if $error;
164
165   $self->SUPER::check;  
166 }
167
168 =head1 NOTES
169
170 =head1 BUGS
171
172 =head1 SEE ALSO
173
174 L<FS::Record>
175
176 =cut
177
178 1;
179
180