use charnames ':full';
use vars qw( $AUTOLOAD
%virtual_fields_cache %fk_method_cache $fk_table_cache
- $money_char $lat_lower $lon_upper
+ %virtual_fields_hash_cache $money_char $lat_lower $lon_upper
$use_placeholders
);
use Carp qw(carp cluck croak confess);
use FS::SearchCache;
use FS::Msgcat qw(gettext);
#use FS::Conf; #dependency loop bs, in install_callback below instead
+use Email::Valid;
use FS::part_virtual_field;
#regular FS::TABLE methods
#on it.
+C<$FS::Record::qsearch_qualify_columns> package global is enabled by default.
+When enabled, the WHERE clause generated from the 'hashref' parameter has
+the table name prepended to each column name. WHERE column = 'value' becomes
+WHERE table.coumn = 'value'
+
=cut
my %TYPE = (); #for debugging
}
+=item virtual_fields_hash [ TABLE ]
+
+Returns a list of virtual field records as a hash defined for the table. This should not
+be exported, and should only be called as an instance or class method.
+
+=cut
+
+sub virtual_fields_hash {
+ my $self = shift;
+ my $table;
+ $table = $self->table or confess "virtual_fields called on non-table";
+
+ confess "Unknown table $table" unless dbdef->table($table);
+
+ return () unless dbdef->table('part_virtual_field');
+
+ unless ( $virtual_fields_hash_cache{$table} ) {
+ $virtual_fields_hash_cache{$table} = [];
+ my $concat = [ "'cf_'", "name" ];
+ my $select = concat_sql($concat).' as name, label, length';
+ my @vfields = qsearch({
+ select => $select,
+ table => 'part_virtual_field',
+ hashref => { 'dbtable' => $table, },
+ });
+
+ foreach (@vfields) {
+ push @{ $virtual_fields_hash_cache{$table} }, $_->{Hash};
+ }
+ }
+
+ @{$virtual_fields_hash_cache{$table}};
+
+}
+
=item process_batch_import JOB OPTIONS_HASHREF PARAMS
Processes a batch import as a queued JSRPC job
sub ut_ip46 {
my( $self, $field ) = @_;
- my $ip_addr = $self->getfield( $field );
-
- # strip user-entered leading 0's from IPv4 addresses
- # Parsers like NetAddr::IP interpret them as octal instead of decimal
- $ip_addr = join( '.', (
- map{ int($_) }
- split( /\./, $ip_addr )
- )
- ) if $ip_addr =~ /\./ && $ip_addr =~ /[\.^]0/;
-
- my $ip = NetAddr::IP->new( $ip_addr )
- or return "Illegal (IP address) $field: ".$self->getfield($field);
+ my $ip = NetAddr::IP->new(
+ $self->_ut_ip_strip_leading_zeros( $self->getfield( $field ) )
+ ) or return "Illegal (IP address) $field: ".$self->getfield($field);
$self->setfield($field, lc($ip->addr));
return '';
}
$self->ut_ip46($field);
}
+sub _ut_ip_strip_leading_zeros {
+ # strip user-entered leading 0's from IP addresses
+ # so parsers like NetAddr::IP don't mangle the address
+ # e.g. NetAddr::IP converts 10.0.022.220 into 10.0.18.220
+
+ my ( $self, $ip ) = @_;
+
+ return join '.', map int, split /\./, $ip
+ if $ip
+ && $ip =~ /\./
+ && $ip =~ /[\.^]0/;
+ $ip;
+}
+
+
=item ut_coord COLUMN [ LOWER [ UPPER ] ]
Check/untaint coordinates.
}
+
+=item ut_email COLUMN
+
+Check column contains a valid E-Mail address
+
+=cut
+
+sub ut_email {
+ my ( $self, $field ) = @_;
+ Email::Valid->address( $self->getfield( $field ) )
+ ? ''
+ : "Illegal (email) field $field: ". $self->getfield( $field );
+}
+
+=item ut_emailn COLUMN
+
+Check column contains a valid E-Mail address
+
+May be null
+
+=cut
+
+sub ut_emailn {
+ my ( $self, $field ) = @_;
+
+ $self->getfield( $field ) =~ /^$/
+ ? $self->getfield( $field, '' )
+ : $self->ut_email( $field );
+}
+
=item trim_whitespace FIELD[, FIELD ... ]
Strip leading and trailing spaces from the value in the named FIELD(s).
&& driver_name eq 'Pg'
)
{
- dbh->quote($value, { pg_type => PG_BYTEA() });
+ local $@;
+
+ eval { $value = dbh->quote($value, { pg_type => PG_BYTEA() }); };
+
+ if ( $@ && $@ =~ /Wide character/i ) {
+ warn 'Correcting malformed UTF-8 string for binary quote()'
+ if $DEBUG;
+ utf8::decode($value);
+ utf8::encode($value);
+ $value = dbh->quote($value, { pg_type => PG_BYTEA() });
+ }
+
+ $value;
} else {
dbh->quote($value);
}