1 package SL::Presenter::Part;
 
   6 use SL::DB::PartClassification;
 
   7 use SL::Locale::String qw(t8);
 
   8 use SL::Presenter::EscapedText qw(escape is_escaped);
 
   9 use SL::Presenter::Tag qw(input_tag html_tag name_to_id select_tag);
 
  11 use Exporter qw(import);
 
  13   part_picker part select_classification classification_abbreviation
 
  14   type_abbreviation separate_abbreviation typeclass_abbreviation
 
  16 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
 
  21   my ($part, %params) = @_;
 
  23   $params{display} ||= 'inline';
 
  25   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
 
  28     $params{no_link} ? '' : '<a href="controller.pl?action=Part/edit&part.id=' . escape($part->id) . '">',
 
  29     escape($part->partnumber),
 
  30     $params{no_link} ? '' : '</a>',
 
  37   my ($name, $value, %params) = @_;
 
  39   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
 
  40   my $id = $params{id} || name_to_id($name);
 
  42   my @classes = $params{class} ? ($params{class}) : ();
 
  43   push @classes, 'part_autocomplete';
 
  46     input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
 
  47       'data-part-picker-data' => JSON::to_json(\%params),
 
  49     input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
 
  51   $::request->layout->add_javascripts('kivi.Part.js');
 
  52   $::request->presenter->need_reinit_widgets($id);
 
  54   html_tag('span', $ret, class => 'part_picker');
 
  57 sub picker { goto &part_picker }
 
  60 # shortcut for article type
 
  62 sub type_abbreviation {
 
  64   return $::locale->text('Assembly (typeabbreviation)')   if $part_type eq 'assembly';
 
  65   return $::locale->text('Part (typeabbreviation)')       if $part_type eq 'part';
 
  66   return $::locale->text('Assortment (typeabbreviation)') if $part_type eq 'assortment';
 
  67   return $::locale->text('Service (typeabbreviation)');
 
  71 # Translations for Abbreviations:
 
  73 # $::locale->text('None (typeabbreviation)')
 
  74 # $::locale->text('Purchase (typeabbreviation)')
 
  75 # $::locale->text('Sales (typeabbreviation)')
 
  76 # $::locale->text('Merchandise (typeabbreviation)')
 
  77 # $::locale->text('Production (typeabbreviation)')
 
  79 # and for descriptions
 
  80 # $::locale->text('Purchase')
 
  81 # $::locale->text('Sales')
 
  82 # $::locale->text('Merchandise')
 
  83 # $::locale->text('Production')
 
  86 # shortcut for article type
 
  88 sub classification_abbreviation {
 
  90   SL::DB::Manager::PartClassification->cache_all();
 
  91   my $obj = SL::DB::PartClassification->load_cached($id);
 
  92   $obj && $obj->abbreviation ? t8($obj->abbreviation) : '';
 
  95 sub typeclass_abbreviation {
 
  97   return '' if !$part || !$part->isa('SL::DB::Part');
 
  98   return type_abbreviation($part->part_type) . classification_abbreviation($part->classification_id);
 
 102 # shortcut for article type
 
 104 sub separate_abbreviation {
 
 106   SL::DB::Manager::PartClassification->cache_all();
 
 107   my $obj = SL::DB::PartClassification->load_cached($id);
 
 108   $obj && $obj->abbreviation && $obj->report_separate ? t8($obj->abbreviation) : '';
 
 112 # generate selection tag
 
 114 sub select_classification {
 
 115   my ($name, %attributes) = @_;
 
 117   $attributes{value_key} = 'id';
 
 118   $attributes{title_key} = 'description';
 
 120   my $classification_type_filter = delete $attributes{type} // [];
 
 122   my $collection = SL::DB::Manager::PartClassification->get_all_sorted( where => $classification_type_filter );
 
 123   $_->description($::locale->text($_->description)) for @{ $collection };
 
 124   select_tag( $name, $collection, %attributes );
 
 135 SL::Presenter::Part - Part related presenter stuff
 
 139   # Create an html link for editing/opening a part/service/assembly
 
 140   my $object = SL::DB::Manager::Part->get_first;
 
 141   my $html   = SL::Presenter->get->part($object, display => 'inline');
 
 143 see also L<SL::Presenter>
 
 153 =item C<part, $object, %params>
 
 155 Returns a rendered version (actually an instance of
 
 156 L<SL::Presenter::EscapedText>) of the part object C<$object>
 
 158 C<%params> can include:
 
 164 Either C<inline> (the default) or C<table-cell>. At the moment both
 
 165 representations are identical and produce the part's name linked
 
 166 to the corresponding 'edit' action.
 
 174 =item C<classification_abbreviation $classification_id>
 
 176 Returns the shortcut of the classification
 
 182 =item C<separate_abbreviation $classification_id>
 
 184 Returns the shortcut of the classification if the classification has the separate flag set.
 
 190 =item C<select_classification $name,%params>
 
 192 Returns an HTML select tag with all available classifications.
 
 194 C<%params> can include:
 
 200 The id of the selected item.
 
 208 =item C<part_picker $name, $value, %params>
 
 210 All-in-one picker widget for parts. The name will be both id and name
 
 211 of the resulting hidden C<id> input field (but the ID can be
 
 212 overwritten with C<$params{id}>).
 
 214 An additional dummy input will be generated which is used to find
 
 215 parts. For a detailed description of its behaviour, see section
 
 216 C<PART PICKER SPECIFICATION>.
 
 218 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
 
 220 If C<%params> contains C<part_type> only parts of this type will be used
 
 221 for autocompletion. You may comma separate multiple types as in
 
 224 If C<%params> contains C<unit> only parts with this unit will be used
 
 225 for autocompletion. You may comma separate multiple units as in
 
 228 If C<%params> contains C<convertible_unit> only parts with a unit
 
 229 that's convertible to unit will be used for autocompletion.
 
 231 Obsolete parts will by default not be displayed for selection. However they are
 
 232 accepted as default values and can persist during updates. As with other
 
 233 selectors though, they are not selectable once overridden.
 
 235 C<part_picker> will register it's javascript for inclusion in the next header
 
 236 rendering. If you write a standard controller that only calls C<render> once, it
 
 237 will just work. In case the header is generated in a different render call
 
 238 (multiple blocks, ajax, old C<bin/mozilla> style controllers) you need to
 
 239 include C<kivi.Part.js> yourself.
 
 241 On pressing <enter> the picker will try to commit the current selection,
 
 242 resulting in one of the following events, whose corresponding callbacks can be
 
 243 set in C<params.actions>:
 
 247 =item * C<commit_one>
 
 249 If exactly one element matches the input, the internal id will be set to this
 
 250 id, the internal state will be set to C<PICKED> and the C<change> event on the
 
 251 picker will be fired. Additionally, if C<params> contains C<fat_set_item> a
 
 252 special event C<set_item:PartPicker> will be fired which is guaranteed to
 
 253 contain a complete JSON representation of the part.
 
 255 After that the action C<commit_one> will be executed, which defaults to
 
 256 clicking a button with id C<update_button> for backward compatibility reasons.
 
 258 =item * C<commit_many>
 
 260 If more than one element matches the input, the internal state will be set to
 
 263 After that the action C<commit_one> will be executed, which defaults to
 
 264 opening a popup dialog for graphical interaction. If C<params> contains
 
 265 C<multiple> an alternative popup will be opened, allowing multiple items to be
 
 266 selected. Note however that this requires an additional callback
 
 267 C<set_multi_items> to work.
 
 269 =item * C<commit_none>
 
 271 If no element matches the input, the internal state will be set to undefined.
 
 273 If an action for C<commit_none> exists, it will be called with the picker
 
 274 object and current term. The caller can then implement creation of new parts.
 
 280 =head1 PART PICKER SPECIFICATION
 
 282 The following list of design goals were applied:
 
 288 Parts should not be perceived by the user as distinct inputs of partnumber and
 
 289 description but as a single object
 
 293 Easy to use without documentation for novice users
 
 297 Fast to use with keyboard for experienced users
 
 301 Possible to use without any keyboard interaction for mouse (or touchscreen)
 
 306 Must not leave the current page in event of ambiguity (cf. current select_item
 
 311 Should be useable with hand scanners or similar alternative keyboard devices
 
 315 Should not require a feedback/check loop in the common case
 
 319 Should not be constrained to exact matches
 
 327 Action should be overridable
 
 331 The implementation consists of the following parts which will be referenced later:
 
 337 A hidden input (id input), used to hold the id of the selected part. The only
 
 338 input that gets submitted
 
 342 An input (dummy input) containing a description of the currently selected part,
 
 343 also used by the user to search for parts
 
 347 A jquery.autocomplete mechanism attached to the dummy field
 
 351 A popup layer for both feedback and input of additional data in case of
 
 356 An internal status of the part picker, indicating whether id input and dummy
 
 357 input are consistent. After leaving the dummy input the part picker must
 
 358 place itself in a consistent status.
 
 362 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
 
 372 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
 
 374 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>