kivitendo/SL/Controller/VariantProperty.pm @ 59661dc5
e30c7b70 | Werner Hahn | package SL::Controller::VariantProperty;
|
||
use strict;
|
||||
use parent qw(SL::Controller::Base);
|
||||
use SL::Helper::Flash;
|
||||
use SL::Locale::String;
|
||||
use SL::DB::Default;
|
||||
use SL::DB::Manager::VariantProperty;
|
||||
use Rose::Object::MakeMethods::Generic (
|
||||
a4420618 | Tamino Steinert | 'scalar --get_set_init' => [ qw(variant_property) ]
|
||
e30c7b70 | Werner Hahn | );
|
||
#__PACKAGE__->run_before('check_auth');
|
||||
a4420618 | Tamino Steinert | __PACKAGE__->run_before('add_javascripts', only => [ qw(edit_variant_property) ]);
|
||
e30c7b70 | Werner Hahn | |||
#
|
||||
# actions
|
||||
#
|
||||
a4420618 | Tamino Steinert | sub action_list_variant_properties {
|
||
e30c7b70 | Werner Hahn | my ($self) = @_;
|
||
$self->_setup_list_action_bar;
|
||||
fe1af881 | Werner Hahn | $self->render('variant_property/variant_property_list',
|
||
e30c7b70 | Werner Hahn | title => t8('Variant Property'),
|
||
VARIANTPROPERTIES => SL::DB::Manager::VariantProperty->get_all_sorted,
|
||||
);
|
||||
}
|
||||
a4420618 | Tamino Steinert | sub action_edit_variant_property {
|
||
e30c7b70 | Werner Hahn | my ($self) = @_;
|
||
my $is_new = !$self->variant_property->id;
|
||||
$self->_setup_form_action_bar;
|
||||
a4420618 | Tamino Steinert | $self->render(
|
||
'variant_property/variant_property_form',
|
||||
title => ($is_new ? t8('Add Variant Property') : t8('Edit Variant Property')),
|
||||
);
|
||||
e30c7b70 | Werner Hahn | }
|
||
a4420618 | Tamino Steinert | sub action_save_variant_property {
|
||
e30c7b70 | Werner Hahn | my ($self) = @_;
|
||
a4420618 | Tamino Steinert | $self->create_or_update_variant_property;
|
||
e30c7b70 | Werner Hahn | }
|
||
a4420618 | Tamino Steinert | sub action_delete_variant_property {
|
||
e30c7b70 | Werner Hahn | my ($self) = @_;
|
||
a4420618 | Tamino Steinert | if ( eval {
|
||
SL::DB->client->with_transaction(sub {
|
||||
$_->delete for $self->variant_property->property_values;
|
||||
$self->variant_property->delete;
|
||||
});
|
||||
1;
|
||||
} ) {
|
||||
flash_later('info', t8('The Variant Property has been deleted.'));
|
||||
e30c7b70 | Werner Hahn | } else {
|
||
a4420618 | Tamino Steinert | flash_later('error', t8('The Variant Property is in use and cannot be deleted.'));
|
||
e30c7b70 | Werner Hahn | };
|
||
a4420618 | Tamino Steinert | $self->redirect_to(action => 'list_variant_properties');
|
||
e30c7b70 | Werner Hahn | }
|
||
a4420618 | Tamino Steinert | sub action_reorder_variant_properties {
|
||
e30c7b70 | Werner Hahn | my ($self) = @_;
|
||
9fb70609 | Werner Hahn | SL::DB::VariantProperty->reorder_list(@{ $::form->{variant_property_id} || [] });
|
||
e30c7b70 | Werner Hahn | $self->render(\'', { type => 'json' }); # ' emacs happy again
|
||
}
|
||||
a4420618 | Tamino Steinert | sub action_reorder_variant_property_values {
|
||
8752239e | Werner Hahn | my ($self) = @_;
|
||
a4420618 | Tamino Steinert | SL::DB::VariantPropertyValue->reorder_list(@{ $::form->{variant_property_value_id} || [] });
|
||
$self->render(\'', { type => 'json' }); # ' emacs happy again
|
||||
8752239e | Werner Hahn | }
|
||
a4420618 | Tamino Steinert | sub action_edit_variant_property_value {
|
||
8752239e | Werner Hahn | my ($self) = @_;
|
||
$self->js
|
||||
->run(
|
||||
a4420618 | Tamino Steinert | 'kivi.VariantProperty.variant_property_value_dialog',
|
||
t8('Variant Property Value'),
|
||||
$self->render(
|
||||
'variant_property/variant_property_value_form',
|
||||
{ output => 0 },
|
||||
variant_property_value => SL::DB::Manager::VariantPropertyValue->find_by(
|
||||
id => $::form->{variant_property_value_id}
|
||||
),
|
||||
)
|
||||
8752239e | Werner Hahn | )
|
||
->reinit_widgets;
|
||||
$self->js->render;
|
||||
}
|
||||
a4420618 | Tamino Steinert | sub action_save_variant_property_value {
|
||
my ($self) = @_;
|
||||
die "'variant_property_value.id' is needed" unless $::form->{variant_property_value}->{id};
|
||||
my $variant_property_value = SL::DB::Manager::VariantPropertyValue->find_by(
|
||||
id => $::form->{variant_property_value}->{id}
|
||||
) or die t8("Could not find Variant Property Value");
|
||||
$variant_property_value->update_attributes(
|
||||
%{$::form->{variant_property_value}}
|
||||
);
|
||||
flash_later('info', t8('The Variant Property Value has been saved.'));
|
||||
$self->redirect_to(
|
||||
action => 'edit_variant_property',
|
||||
id => $variant_property_value->variant_property_id,
|
||||
);
|
||||
}
|
||||
sub action_add_variant_property_value {
|
||||
my ($self) = @_;
|
||||
my $new_variant_property_value = SL::DB::VariantPropertyValue->new(
|
||||
%{ $::form->{new_variant_property_value} },
|
||||
variant_property => $self->variant_property,
|
||||
)->save;
|
||||
$self->redirect_to(
|
||||
action => 'edit_variant_property',
|
||||
id => $self->variant_property->id,
|
||||
);
|
||||
}
|
||||
e30c7b70 | Werner Hahn | #
|
||
#inits
|
||||
#
|
||||
sub init_variant_property {
|
||||
fe1af881 | Werner Hahn | SL::DB::Manager::VariantProperty->find_by_or_create(id => $::form->{id} || 0)->assign_attributes(%{ $::form->{variant_property} });
|
||
e30c7b70 | Werner Hahn | }
|
||
8752239e | Werner Hahn | sub add_javascripts {
|
||
$::request->{layout}->add_javascripts(qw(kivi.VariantProperty.js));
|
||||
}
|
||||
e30c7b70 | Werner Hahn | #
|
||
# helpers
|
||||
#
|
||||
a4420618 | Tamino Steinert | sub create_or_update_variant_property {
|
||
e30c7b70 | Werner Hahn | my ($self) = @_;
|
||
my $is_new = !$self->variant_property->id;
|
||||
my @errors = $self->variant_property->validate;
|
||||
if (@errors) {
|
||||
flash('error', @errors);
|
||||
a4420618 | Tamino Steinert | $self->action_edit_variant_property();
|
||
e30c7b70 | Werner Hahn | return;
|
||
}
|
||||
$self->variant_property->save;
|
||||
flash_later('info', $is_new ? t8('The Variant Property has been created.') : t8('The Variant Property has been saved.'));
|
||||
a4420618 | Tamino Steinert | $self->redirect_to(
|
||
action => 'edit_variant_property',
|
||||
id => $self->variant_property->id,
|
||||
);
|
||||
8752239e | Werner Hahn | }
|
||
e30c7b70 | Werner Hahn | sub _setup_form_action_bar {
|
||
my ($self) = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
combobox => [
|
||||
action => [
|
||||
t8('Save'),
|
||||
a4420618 | Tamino Steinert | submit => [ '#form', { action => "VariantProperty/save_variant_property" } ],
|
||
e30c7b70 | Werner Hahn | accesskey => 'enter',
|
||
],
|
||||
action => [
|
||||
t8('Delete'),
|
||||
a4420618 | Tamino Steinert | submit => [ '#form', { action => "VariantProperty/delete_variant_property" } ],
|
||
e30c7b70 | Werner Hahn | ],
|
||
],
|
||||
action => [
|
||||
t8('Cancel'),
|
||||
a4420618 | Tamino Steinert | submit => [ '#form', { action => "VariantProperty/list_variant_properties" } ],
|
||
e30c7b70 | Werner Hahn | ],
|
||
);
|
||||
}
|
||||
}
|
||||
sub _setup_list_action_bar {
|
||||
my ($self) = @_;
|
||||
for my $bar ($::request->layout->get('actionbar')) {
|
||||
$bar->add(
|
||||
link => [
|
||||
t8('Add'),
|
||||
a4420618 | Tamino Steinert | link => $self->url_for(action => 'edit_variant_property'),
|
||
e30c7b70 | Werner Hahn | ],
|
||
)
|
||||
};
|
||||
}
|
||||
1;
|