1 package FS::part_virtual_field;
5 use FS::Record qw( qsearchs qsearch );
6 use FS::Schema qw( dbdef );
8 @ISA = qw( FS::Record );
12 FS::part_virtual_field - Object methods for part_virtual_field records
16 use FS::part_virtual_field;
18 $record = new FS::part_virtual_field \%hash;
19 $record = new FS::part_virtual_field { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::part_virtual_field object represents the definition of a virtual field
32 (see the BACKGROUND section). FS::part_virtual_field contains the name and
33 base table of the field, as well as validation rules and UI hints about the
34 display of the field. The actual data is stored in FS::virtual_field; see
35 its manpage for details.
37 FS::part_virtual_field inherits from FS::Record. The following fields are
42 =item vfieldpart - primary key (assigned automatically)
44 =item name - name of the field
46 =item dbtable - table for which this virtual field is defined
48 =item check_block - Perl code to validate/normalize data
50 =item list_source - Perl code to generate a list of values (UI hint)
52 =item length - expected length of the value (UI hint)
54 =item label - descriptive label for the field (UI hint)
56 =item sequence - sort key (UI hint; unimplemented)
62 "Form is none other than emptiness,
63 and emptiness is none other than form."
66 The virtual field mechanism allows site admins to make trivial changes to
67 the Freeside database schema without modifying the code. Specifically, the
68 user can add custom-defined 'fields' to the set of data tracked by Freeside
69 about objects such as customers and services. These fields are not associated
70 with any logic in the core Freeside system, but may be referenced in peripheral
71 code such as exports, price calculations, or alternate interfaces, or may just
72 be stored in the database for future reference.
74 This system was originally devised for svc_broadband, which (by necessity)
75 comprises such a wide range of access technologies that no static set of fields
76 could contain all the information needed by the exports. In an appalling
77 display of False Laziness, a parallel mechanism was implemented for the
78 router table, to store properties such as passwords to configure routers.
80 The original system treated svc_broadband custom fields (sb_fields) as records
81 in a completely separate table. Any code that accessed or manipulated these
82 fields had to be aware that they were I<not> fields in svc_broadband, but
83 records in sb_field. For example, code that inserted a svc_broadband with
84 several custom fields had to create an FS::svc_broadband object, call its
85 insert() method, and then create several FS::sb_field objects and call I<their>
88 This created a problem for exports. The insert method on any FS::svc_Common
89 object (including svc_broadband) automatically triggers exports after the
90 record has been inserted. However, at this point, the sb_fields had not yet
91 been inserted, so the export could not rely on their presence, which was the
92 original purpose of sb_fields.
94 Hence the new system. Virtual fields are appended to the field list of every
95 record at the FS::Record level, whether the object is created ex nihilo with
96 new() or fetched with qsearch(). The fields() method now returns a list of
97 both real and virtual fields. The insert(), replace(), and delete() methods
98 now update both the base table and the virtual fields, in a single transaction.
100 A new method is provided, virtual_fields(), which gives only the virtual
101 fields. UI code that dynamically generates form widgets to edit virtual field
102 data should use this to figure out what fields are defined. (See below.)
104 Subclasses may override virtual_fields() to restrict the set of virtual
105 fields available. Some discipline and sanity on the part of the programmer
106 are required; in particular, this function should probably not depend on any
107 fields in the record other than the primary key, since the others may change
108 after the object is instantiated. (Making it depend on I<virtual> fields is
109 just asking for pain.) One use of this is seen in FS::svc_Common; another
110 possibility is field-level access control based on FS::UID::getotaker().
112 As a trivial case, a subclass may opt out of supporting virtual fields with
115 sub virtual_fields { () }
123 Create a new record. To add the record to the database, see "insert".
127 sub table { 'part_virtual_field'; }
128 sub virtual_fields { () }
132 Adds this record to the database. If there is an error, returns the error,
133 otherwise returns false.
137 Deletes this record from the database. If there is an error, returns the
138 error, otherwise returns false.
140 =item replace OLD_RECORD
142 Replaces OLD_RECORD with this one in the database. If there is an error,
143 returns the error, otherwise returns false.
147 If there is an error, returns the error, otherwise returns false.
148 Called by the insert and replace methods.
157 my $error = $self->ut_text('name') ||
158 $self->ut_text('dbtable') ||
159 $self->ut_number('length')
161 return $error if $error;
163 # Make sure it's a real table with a numeric primary key
165 if($table = dbdef->table($self->dbtable)) {
166 if($pkey = $table->primary_key) {
167 if($table->column($pkey)->type =~ /int/i) {
168 # this is what it should be
170 $error = "$table.$pkey is not an integer";
173 $error = "$table does not have a single-field primary key";
176 $error = "$table does not exist in the schema";
178 return $error if $error;
180 # Possibly some sanity checks for check_block and list_source?
187 Evaluates list_source.
193 return () unless $self->list_source;
195 my @opts = eval($self->list_source);
204 =item widget UI_TYPE MODE [ VALUE ]
206 Generates UI code for a widget suitable for editing/viewing the field, based on
207 list_source and length.
209 The only UI_TYPE currently supported is 'HTML', and the only MODE is 'view'.
210 Others will be added later.
212 In HTML, all widgets are assumed to be table rows. View widgets look like
213 <TR><TD ALIGN="right">Label</TD><TD BGCOLOR="#ffffff">Value</TD></TR>
215 (Most of the display style stuff, such as the colors, should probably go into
216 a separate module specific to the UI. That can wait, though. The API for
217 this function won't change.)
219 VALUE (optional) is the current value of the field.
225 my ($ui_type, $mode, $value) = @_;
227 my $label = $self->label || $self->name;
229 if ($ui_type eq 'HTML') {
230 if ($mode eq 'view') {
231 $text = q!<TR><TD ALIGN="right">! . $label .
232 q!</TD><TD BGCOLOR="#ffffff">! . $value .
233 q!</TD></TR>! . "\n";
234 } elsif ($mode eq 'edit') {
235 $text = q!<TR><TD ALIGN="right">! . $label .
237 if ($self->list_source) {
238 $text .= q!<SELECT NAME="! . $self->name .
240 foreach ($self->list) {
241 $text .= q!<OPTION VALUE="! . $_ . q!"!;
242 $text .= ' SELECTED' if ($_ eq $value);
243 $text .= '>' . $_ . '</OPTION>' . "\n";
246 $text .= q!<INPUT NAME="! . $self->name .
247 q!" VALUE="! . $value . q!"!;
249 $text .= q! SIZE="! . $self->length . q!"!;
253 $text .= q!</TD></TR>! . "\n";
265 =head2 Semantics of check_block:
267 This has been changed from the sb_field implementation to make check_blocks
268 simpler and more natural to Perl programmers who work on things other than
271 The check_block is eval'd with the (proposed) new value of the field in $_,
272 and the object to be updated in $self. Its return value is ignored. The
273 check_block may change the value of $_ to override the proposed value, or
274 call die() (with an appropriate error message) to reject the update entirely;
275 the error string will be returned as the output of the check() method.
277 This makes check_blocks like
283 The check_block is expected NOT to do anything freaky to $self, like modifying
284 other fields or calling $self->check(). You have been warned.
286 (FIXME: Rewrite some of the warnings from part_sb_field and insert here.)
290 None. It's absolutely falwless.
294 L<FS::Record>, L<FS::virtual_field>