kivitendo/SL/DB/CsvImportProfile.pm @ 4b1666b7
c2cf302a | Moritz Bunkus | package SL::DB::CsvImportProfile;
|
||
use strict;
|
||||
use List::Util qw(first);
|
||||
f64be46d | Sven Schöling | require SL::DB::MetaSetup::CsvImportProfile;
|
||
c2cf302a | Moritz Bunkus | |||
__PACKAGE__->meta->add_relationship(
|
||||
settings => {
|
||||
type => 'one to many',
|
||||
class => 'SL::DB::CsvImportProfileSetting',
|
||||
column_map => { id => 'csv_import_profile_id' },
|
||||
},
|
||||
);
|
||||
__PACKAGE__->meta->initialize;
|
||||
__PACKAGE__->meta->make_manager_class;
|
||||
__PACKAGE__->before_save('_before_save_unset_default_on_others');
|
||||
#
|
||||
# public functions
|
||||
#
|
||||
7f1f5efe | Moritz Bunkus | sub new_with_default {
|
||
my ($class, $type) = @_;
|
||||
return $class->new(type => $type)->set_defaults;
|
||||
}
|
||||
sub set_defaults {
|
||||
my ($self) = @_;
|
||||
15f58ff3 | Geoffrey Richardson | $self->_set_defaults(sep_char => ',',
|
||
quote_char => '"',
|
||||
escape_char => '"',
|
||||
charset => 'CP850',
|
||||
numberformat => $::myconfig{numberformat},
|
||||
duplicates => 'no_check',
|
||||
);
|
||||
7f1f5efe | Moritz Bunkus | return $self;
|
||
}
|
||||
c2cf302a | Moritz Bunkus | sub set {
|
||
my ($self, %params) = @_;
|
||||
while (my ($key, $value) = each %params) {
|
||||
my $setting = $self->_get_setting($key);
|
||||
if (!$setting) {
|
||||
$setting = SL::DB::CsvImportProfileSetting->new(key => $key);
|
||||
7f1f5efe | Moritz Bunkus | $self->settings(@{ $self->settings || [] }, $setting);
|
||
c2cf302a | Moritz Bunkus | }
|
||
$setting->value($value);
|
||||
}
|
||||
return $self;
|
||||
}
|
||||
sub get {
|
||||
my ($self, $key, $default) = @_;
|
||||
my $setting = $self->_get_setting($key);
|
||||
return $setting ? $setting->value : $default;
|
||||
}
|
||||
7f1f5efe | Moritz Bunkus | sub _set_defaults {
|
||
my ($self, %params) = @_;
|
||||
while (my ($key, $value) = each %params) {
|
||||
$self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
|
||||
}
|
||||
return $self;
|
||||
}
|
||||
67643d03 | Sven Schöling | sub clone_and_reset_deep {
|
||
my ($self) = @_;
|
||||
my $clone = $self->clone_and_reset;
|
||||
$clone->settings(map { $_->clone_and_reset } $self->settings);
|
||||
938dfd8b | Martin Helmling | $clone->is_default(0);
|
||
67643d03 | Sven Schöling | $clone->name('');
|
||
return $clone;
|
||||
}
|
||||
82c3b7eb | Sven Schöling | sub flatten {
|
||
my ($self) = @_;
|
||||
return map {
|
||||
$_->key => $_->value
|
||||
} $self->settings;
|
||||
}
|
||||
c2cf302a | Moritz Bunkus | #
|
||
# hooks
|
||||
#
|
||||
sub _before_save_unset_default_on_others {
|
||||
my ($self) = @_;
|
||||
if ($self->is_default) {
|
||||
SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
|
||||
where => [ type => $self->type,
|
||||
'!id' => $self->id ]);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
#
|
||||
# helper functions
|
||||
#
|
||||
sub _get_setting {
|
||||
my ($self, $key) = @_;
|
||||
7f1f5efe | Moritz Bunkus | return first { $_->key eq $key } @{ $self->settings || [] };
|
||
c2cf302a | Moritz Bunkus | }
|
||
1;
|