1 package FS::part_virtual_field;
2 use base qw(FS::Record);
5 use CGI qw(escapeHTML);
6 use FS::Schema qw( dbdef );
10 FS::part_virtual_field - Object methods for part_virtual_field records
14 use FS::part_virtual_field;
16 $record = new FS::part_virtual_field \%hash;
17 $record = new FS::part_virtual_field { 'column' => 'value' };
19 $error = $record->insert;
21 $error = $new_record->replace($old_record);
23 $error = $record->delete;
25 $error = $record->check;
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.
33 FS::part_virtual_field inherits from FS::Record. The following fields are
38 =item vfieldpart - primary key (assigned automatically)
40 =item name - name of the field
42 =item dbtable - table for which this virtual field is defined
44 =item length - expected length of the value (UI hint)
46 =item label - descriptive label for the field (UI hint)
56 Create a new record. To add the record to the database, see "insert".
60 sub table { 'part_virtual_field'; }
61 sub virtual_fields { () }
63 =item widget UI_TYPE MODE [ VALUE ]
65 Generates UI code for a widget suitable for editing/viewing the field, based on
66 list_source and length.
68 The only UI_TYPE currently supported is 'HTML', and possible MODEs are 'view'
71 In HTML, all widgets are assumed to be table rows. View widgets look like
72 <TR><TD ALIGN="right">Label</TD><TD BGCOLOR="#ffffff">Value</TD></TR>
74 (Most of the display style stuff, such as the colors, should probably go into
75 a separate module specific to the UI. That can wait, though. The API for
76 this function won't change.)
78 VALUE (optional) is the current value of the field.
84 my ($ui_type, $mode, $value) = @_;
86 my $label = $self->label || $self->name;
88 if ($ui_type eq 'HTML') {
89 if ($mode eq 'view') {
90 $text = q!<TR><TD ALIGN="right">! . $label .
91 q!</TD><TD BGCOLOR="#ffffff">! . $value .
93 } elsif ($mode eq 'edit') {
94 $text = q!<TR><TD ALIGN="right">! . $label .
96 $text .= q!<INPUT NAME="! . $self->name .
97 q!" VALUE="! . escapeHTML($value) . q!"!;
99 $text .= q! SIZE="! . $self->length . q!"!;
102 $text .= q!</TD></TR>! . "\n";
115 Adds this record to the database. If there is an error, returns the error,
116 otherwise returns false.
120 Deletes this record from the database. If there is an error, returns the
121 error, otherwise returns false.
123 =item replace OLD_RECORD
125 Replaces OLD_RECORD with this one in the database. If there is an error,
126 returns the error, otherwise returns false.
130 If there is an error, returns the error, otherwise returns false.
131 Called by the insert and replace methods.
140 my $error = $self->ut_text('name') ||
141 $self->ut_text('dbtable') ||
142 $self->ut_number('length')
144 return $error if $error;
146 # Make sure it's a real table with a numeric primary key
148 if($table = dbdef->table($self->dbtable)) {
149 if($pkey = $table->primary_key) {
150 if($table->column($pkey)->type =~ /int/i) {
151 # this is what it should be
153 $error = "$table.$pkey is not an integer";
156 $error = "$table does not have a single-field primary key";
159 $error = "$table does not exist in the schema";
161 return $error if $error;