1 package SL::DB::CsvImportProfile;
5 use List::Util qw(first);
7 require SL::DB::MetaSetup::CsvImportProfile;
9 __PACKAGE__->meta->add_relationship(
11 type => 'one to many',
12 class => 'SL::DB::CsvImportProfileSetting',
13 column_map => { id => 'csv_import_profile_id' },
17 __PACKAGE__->meta->initialize;
19 __PACKAGE__->meta->make_manager_class;
21 __PACKAGE__->before_save('_before_save_unset_default_on_others');
27 sub new_with_default {
28 my ($class, $type) = @_;
30 return $class->new(type => $type)->set_defaults;
36 $self->_set_defaults(sep_char => ',',
40 numberformat => $::myconfig{numberformat},
41 dateformat => $::myconfig{dateformat},
42 duplicates => 'no_check',
49 my ($self, %params) = @_;
51 while (my ($key, $value) = each %params) {
52 my $setting = $self->_get_setting($key);
55 $setting = SL::DB::CsvImportProfileSetting->new(key => $key);
56 $self->settings(@{ $self->settings || [] }, $setting);
59 $setting->value($value);
66 my ($self, $key, $default) = @_;
68 my $setting = $self->_get_setting($key);
69 return $setting ? $setting->value : $default;
73 my ($self, %params) = @_;
75 while (my ($key, $value) = each %params) {
76 $self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
82 sub clone_and_reset_deep {
85 my $clone = $self->clone_and_reset;
86 $clone->settings(map { $_->clone_and_reset } $self->settings);
87 $clone->is_default(0);
104 sub _before_save_unset_default_on_others {
107 if ($self->is_default) {
108 SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
109 where => [ type => $self->type,
110 '!id' => $self->id ]);
121 my ($self, $key) = @_;
122 return first { $_->key eq $key } @{ $self->settings || [] };