ActionBar: Verwendung im »SimpleSystemSetting«-Controller
[kivitendo-erp.git] / SL / Controller / Part.pm
index eb1d52b..de2cb1d 100644 (file)
@@ -5,6 +5,7 @@ use parent qw(SL::Controller::Base);
 
 use Clone qw(clone);
 use SL::DB::Part;
+use SL::DB::PartsGroup;
 use SL::Controller::Helper::GetModels;
 use SL::Locale::String qw(t8);
 use SL::JSON;
@@ -193,6 +194,7 @@ sub render_form {
   my ($self, %params) = @_;
 
   $self->_set_javascript;
+  $self->_setup_form_action_bar;
 
   my (%assortment_vars, %assembly_vars);
   %assortment_vars = %{ $self->prepare_assortment_render_vars } if $self->part->is_assortment;
@@ -215,7 +217,6 @@ sub render_form {
   $self->render(
     'part/form',
     title             => $title_hash{$self->part->part_type},
-    show_edit_buttons => $::auth->assert('part_service_assembly_edit'),
     %assortment_vars,
     %assembly_vars,
     translations_map  => { map { ($_->language_id   => $_) } @{$self->part->translations} },
@@ -260,7 +261,7 @@ sub action_add_multi_assortment_items {
   my $item_objects = $self->parse_add_items_to_objects(part_type => 'assortment');
   my $html         = $self->render_assortment_items_to_html($item_objects);
 
-  $self->js->run('kivi.Part.close_multi_items_dialog')
+  $self->js->run('kivi.Part.close_picker_dialogs')
            ->append('#assortment_rows', $html)
            ->run('kivi.Part.renumber_positions')
            ->run('kivi.Part.assortment_recalc')
@@ -280,7 +281,7 @@ sub action_add_multi_assembly_items {
 
   my $html = $self->render_assembly_items_to_html(\@checked_objects);
 
-  $self->js->run('kivi.Part.close_multi_items_dialog')
+  $self->js->run('kivi.Part.close_picker_dialogs')
            ->append('#assembly_rows', $html)
            ->run('kivi.Part.renumber_positions')
            ->run('kivi.Part.assembly_recalc')
@@ -370,11 +371,9 @@ sub action_add_assembly_item {
 }
 
 sub action_show_multi_items_dialog {
-  require SL::DB::PartsGroup;
   $_[0]->render('part/_multi_items_dialog', { layout => 0 },
-                part_type => 'assortment',
-                partfilter => '', # can I get at the current input of the partpicker here?
-                all_partsgroups => SL::DB::Manager::PartsGroup->get_all);
+    all_partsgroups => SL::DB::Manager::PartsGroup->get_all
+  );
 }
 
 sub action_multi_items_update_result {
@@ -501,22 +500,19 @@ sub action_ajax_autocomplete {
 
   # if someone types something, and hits enter, assume he entered the full name.
   # if something matches, treat that as sole match
-  # unfortunately get_models can't do more than one per package atm, so we d it
-  # the oldfashioned way.
+  # since we need a second get models instance with different filters for that,
+  # we only modify the original filter temporarily in place
   if ($::form->{prefer_exact}) {
+    local $::form->{filter}{'all::ilike'} = delete local $::form->{filter}{'all:substr:multi::ilike'};
+
+    my $exact_models = SL::Controller::Helper::GetModels->new(
+      controller   => $self,
+      sorted       => 0,
+      paginated    => { per_page => 2 },
+      with_objects => [ qw(unit_obj classification) ],
+    );
     my $exact_matches;
-    if (1 == scalar @{ $exact_matches = SL::DB::Manager::Part->get_all(
-      query => [
-        obsolete => 0,
-        SL::DB::Manager::Part->type_filter($::form->{filter}{part_type}),
-        SL::DB::Manager::PartClassification->classification_filter($::form->{filter}{classification_id}),
-        or => [
-          description => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
-          partnumber  => { ilike => $::form->{filter}{'all:substr:multi::ilike'} },
-        ]
-      ],
-      limit => 2,
-    ) }) {
+    if (1 == scalar @{ $exact_matches = $exact_models->get }) {
       $self->parts($exact_matches);
     }
   }
@@ -542,11 +538,11 @@ sub action_test_page {
 }
 
 sub action_part_picker_search {
-  $_[0]->render('part/part_picker_search', { layout => 0 }, parts => $_[0]->parts);
+  $_[0]->render('part/part_picker_search', { layout => 0 });
 }
 
 sub action_part_picker_result {
-  $_[0]->render('part/_part_picker_result', { layout => 0 });
+  $_[0]->render('part/_part_picker_result', { layout => 0 }, parts => $_[0]->parts);
 }
 
 sub action_show {
@@ -600,6 +596,7 @@ sub add {
   check_has_valid_part_type($self->part->part_type);
 
   $self->_set_javascript;
+  $self->_setup_form_action_bar;
 
   my %title_hash = ( part       => t8('Add Part'),
                      assembly   => t8('Add Assembly'),
@@ -609,15 +606,14 @@ sub add {
 
   $self->render(
     'part/form',
-    title             => $title_hash{$self->part->part_type},
-    show_edit_buttons => $::auth->assert('part_service_assembly_edit'),
+    title => $title_hash{$self->part->part_type},
   );
 }
 
 
 sub _set_javascript {
   my ($self) = @_;
-  $::request->layout->use_javascript("${_}.js")  for qw(kivi.Part kivi.PriceRule ckeditor/ckeditor ckeditor/adapters/jquery);
+  $::request->layout->use_javascript("${_}.js")  for qw(kivi.Part kivi.File kivi.PriceRule ckeditor/ckeditor ckeditor/adapters/jquery);
   $::request->layout->add_javascripts_inline("\$(function(){kivi.PriceRule.load_price_rules_for_part(@{[ $self->part->id ]})});") if $self->part->id;
 }
 
@@ -1131,6 +1127,51 @@ sub parse_add_items_to_objects {
   return \@item_objects;
 }
 
+sub _setup_form_action_bar {
+  my ($self) = @_;
+
+  my $may_edit = $::auth->assert('part_service_assembly_edit', 'may fail');
+
+  for my $bar ($::request->layout->get('actionbar')) {
+    $bar->add(
+      combobox => [
+        action => [
+          t8('Save'),
+          call     => [ 'kivi.Part.save' ],
+          disabled => !$may_edit ? t8('You do not have the permissions to access this function.') : undef,
+        ],
+        action => [
+          t8('Use as new'),
+          call     => [ 'kivi.Part.use_as_new' ],
+          disabled => !$self->part->id ? t8('The object has not been saved yet.')
+                    : !$may_edit       ? t8('You do not have the permissions to access this function.')
+                    :                    undef,
+        ],
+      ], # end of combobox "Save"
+
+      action => [
+        t8('Delete'),
+        call     => [ 'kivi.Part.delete' ],
+        confirm  => t8('Do you really want to delete this object?'),
+        disabled => !$self->part->id       ? t8('This object has not been saved yet.')
+                  : !$may_edit             ? t8('You do not have the permissions to access this function.')
+                  : !$self->part->orphaned ? t8('This object has already been used.')
+                  :                          undef,
+      ],
+
+      'separator',
+
+      action => [
+        t8('History'),
+        call     => [ 'kivi.Part.open_history_popup' ],
+        disabled => !$self->part->id ? t8('This object has not been saved yet.')
+                  : !$may_edit       ? t8('You do not have the permissions to access this function.')
+                  :                    undef,
+      ],
+    );
+  }
+}
+
 1;
 
 __END__