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 duplicates => 'no_check',
48 my ($self, %params) = @_;
50 while (my ($key, $value) = each %params) {
51 my $setting = $self->_get_setting($key);
54 $setting = SL::DB::CsvImportProfileSetting->new(key => $key);
55 $self->settings(@{ $self->settings || [] }, $setting);
58 $setting->value($value);
65 my ($self, $key, $default) = @_;
67 my $setting = $self->_get_setting($key);
68 return $setting ? $setting->value : $default;
72 my ($self, %params) = @_;
74 while (my ($key, $value) = each %params) {
75 $self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
81 sub clone_and_reset_deep {
84 my $clone = $self->clone_and_reset;
85 $clone->settings(map { $_->clone_and_reset } $self->settings);
86 $clone->is_default(0);
103 sub _before_save_unset_default_on_others {
106 if ($self->is_default) {
107 SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
108 where => [ type => $self->type,
109 '!id' => $self->id ]);
120 my ($self, $key) = @_;
121 return first { $_->key eq $key } @{ $self->settings || [] };