1 package FS::part_virtual_field;
6 use FS::Schema qw( dbdef );
7 use CGI qw(escapeHTML);
9 @ISA = qw( FS::Record );
13 FS::part_virtual_field - Object methods for part_virtual_field records
17 use FS::part_virtual_field;
19 $record = new FS::part_virtual_field \%hash;
20 $record = new FS::part_virtual_field { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::part_virtual_field object represents the definition of a custom field
33 (see the BACKGROUND section). FS::part_virtual_field contains the name and
34 base table of the field.
36 FS::part_virtual_field inherits from FS::Record. The following fields are
41 =item vfieldpart - primary key (assigned automatically)
43 =item name - name of the field
45 =item dbtable - table for which this virtual field is defined
47 =item length - expected length of the value (UI hint)
49 =item label - descriptive label for the field (UI hint)
59 Create a new record. To add the record to the database, see "insert".
63 sub table { 'part_virtual_field'; }
64 sub virtual_fields { () }
66 =item widget UI_TYPE MODE [ VALUE ]
68 Generates UI code for a widget suitable for editing/viewing the field, based on
69 list_source and length.
71 The only UI_TYPE currently supported is 'HTML', and possible MODEs are 'view'
74 In HTML, all widgets are assumed to be table rows. View widgets look like
75 <TR><TD ALIGN="right">Label</TD><TD BGCOLOR="#ffffff">Value</TD></TR>
77 (Most of the display style stuff, such as the colors, should probably go into
78 a separate module specific to the UI. That can wait, though. The API for
79 this function won't change.)
81 VALUE (optional) is the current value of the field.
87 my ($ui_type, $mode, $value) = @_;
89 my $label = $self->label || $self->name;
91 if ($ui_type eq 'HTML') {
92 if ($mode eq 'view') {
93 $text = q!<TR><TD ALIGN="right">! . $label .
94 q!</TD><TD BGCOLOR="#ffffff">! . $value .
96 } elsif ($mode eq 'edit') {
97 $text = q!<TR><TD ALIGN="right">! . $label .
99 $text .= q!<INPUT NAME="! . $self->name .
100 q!" VALUE="! . escapeHTML($value) . q!"!;
102 $text .= q! SIZE="! . $self->length . q!"!;
105 $text .= q!</TD></TR>! . "\n";
118 Adds this record to the database. If there is an error, returns the error,
119 otherwise returns false.
123 Deletes this record from the database. If there is an error, returns the
124 error, otherwise returns false.
126 =item replace OLD_RECORD
128 Replaces OLD_RECORD with this one in the database. If there is an error,
129 returns the error, otherwise returns false.
133 If there is an error, returns the error, otherwise returns false.
134 Called by the insert and replace methods.
143 my $error = $self->ut_text('name') ||
144 $self->ut_text('dbtable') ||
145 $self->ut_number('length')
147 return $error if $error;
149 # Make sure it's a real table with a numeric primary key
151 if($table = dbdef->table($self->dbtable)) {
152 if($pkey = $table->primary_key) {
153 if($table->column($pkey)->type =~ /int/i) {
154 # this is what it should be
156 $error = "$table.$pkey is not an integer";
159 $error = "$table does not have a single-field primary key";
162 $error = "$table does not exist in the schema";
164 return $error if $error;