1 package SL::Presenter::Part;
 
   6 use SL::DB::PartClassification;
 
   7 use SL::Locale::String qw(t8);
 
   9 use Exporter qw(import);
 
  10 our @EXPORT = qw(part_picker part select_classification classification_abbreviation type_abbreviation separate_abbreviation typeclass_abbreviation);
 
  15   my ($self, $part, %params) = @_;
 
  17   $params{display} ||= 'inline';
 
  19   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
 
  22     $params{no_link} ? '' : '<a href="controller.pl?action=Part/edit&part.id=' . $self->escape($part->id) . '">',
 
  23     $self->escape($part->partnumber),
 
  24     $params{no_link} ? '' : '</a>',
 
  26   return $self->escaped_text($text);
 
  30   my ($self, $name, $value, %params) = @_;
 
  32   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
 
  33   my $id = $params{id} || $self->name_to_id($name);
 
  35   my @classes = $params{class} ? ($params{class}) : ();
 
  36   push @classes, 'part_autocomplete';
 
  39     $self->input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
 
  40       'data-part-picker-data' => JSON::to_json(\%params),
 
  42     $self->input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
 
  44   $::request->layout->add_javascripts('kivi.Part.js');
 
  45   $::request->presenter->need_reinit_widgets($id);
 
  47   $self->html_tag('span', $ret, class => 'part_picker');
 
  51 # shortcut for article type
 
  53 sub type_abbreviation {
 
  54   my ($self, $part_type) = @_;
 
  55   return $::locale->text('Assembly (typeabbreviation)')   if $part_type eq 'assembly';
 
  56   return $::locale->text('Part (typeabbreviation)')       if $part_type eq 'part';
 
  57   return $::locale->text('Assortment (typeabbreviation)') if $part_type eq 'assortment';
 
  58   return $::locale->text('Service (typeabbreviation)');
 
  62 # Translations for Abbreviations:
 
  64 # $::locale->text('None (typeabbreviation)')
 
  65 # $::locale->text('Purchase (typeabbreviation)')
 
  66 # $::locale->text('Sales (typeabbreviation)')
 
  67 # $::locale->text('Merchandise (typeabbreviation)')
 
  68 # $::locale->text('Production (typeabbreviation)')
 
  70 # and for descriptions
 
  71 # $::locale->text('Purchase')
 
  72 # $::locale->text('Sales')
 
  73 # $::locale->text('Merchandise')
 
  74 # $::locale->text('Production')
 
  77 # shortcut for article type
 
  79 sub classification_abbreviation {
 
  81   SL::DB::Manager::PartClassification->cache_all();
 
  82   my $obj = SL::DB::PartClassification->load_cached($id);
 
  83   $obj && $obj->abbreviation ? t8($obj->abbreviation) : '';
 
  86 sub typeclass_abbreviation {
 
  87   my ($self, $part) = @_;
 
  88   return '' if !$part || !$part->isa('SL::DB::Part');
 
  89   return $self->type_abbreviation($part->part_type).$self->classification_abbreviation($part->classification_id);
 
  93 # shortcut for article type
 
  95 sub separate_abbreviation {
 
  97   SL::DB::Manager::PartClassification->cache_all();
 
  98   my $obj = SL::DB::PartClassification->load_cached($id);
 
  99   $obj && $obj->abbreviation && $obj->report_separate ? t8($obj->abbreviation) : '';
 
 103 # generate selection tag
 
 105 sub select_classification {
 
 106   my ($self, $name, %attributes) = @_;
 
 107   $attributes{value_key} = 'id';
 
 108   $attributes{title_key} = 'description';
 
 109   my $collection = SL::DB::Manager::PartClassification->get_all_sorted();
 
 110   $_->description($::locale->text($_->description)) for @{ $collection };
 
 111   return $self->select_tag( $name, $collection, %attributes );
 
 122 SL::Presenter::Part - Part related presenter stuff
 
 126   # Create an html link for editing/opening a part/service/assembly
 
 127   my $object = SL::DB::Manager::Part->get_first;
 
 128   my $html   = SL::Presenter->get->part($object, display => 'inline');
 
 130 see also L<SL::Presenter>
 
 140 =item C<part, $object, %params>
 
 142 Returns a rendered version (actually an instance of
 
 143 L<SL::Presenter::EscapedText>) of the part object C<$object>
 
 145 C<%params> can include:
 
 151 Either C<inline> (the default) or C<table-cell>. At the moment both
 
 152 representations are identical and produce the part's name linked
 
 153 to the corresponding 'edit' action.
 
 161 =item C<classification_abbreviation $classification_id>
 
 163 Returns the shortcut of the classification
 
 169 =item C<separate_abbreviation $classification_id>
 
 171 Returns the shortcut of the classification if the classification has the separate flag set.
 
 177 =item C<select_classification $name,%params>
 
 179 Returns an HTML select tag with all available classifications.
 
 181 C<%params> can include:
 
 187 The id of the selected item.
 
 195 =item C<part_picker $name, $value, %params>
 
 197 All-in-one picker widget for parts. The name will be both id and name
 
 198 of the resulting hidden C<id> input field (but the ID can be
 
 199 overwritten with C<$params{id}>).
 
 201 An additional dummy input will be generated which is used to find
 
 202 parts. For a detailed description of its behaviour, see section
 
 203 C<PART PICKER SPECIFICATION>.
 
 205 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
 
 207 If C<%params> contains C<part_type> only parts of this type will be used
 
 208 for autocompletion. You may comma separate multiple types as in
 
 211 If C<%params> contains C<unit> only parts with this unit will be used
 
 212 for autocompletion. You may comma separate multiple units as in
 
 215 If C<%params> contains C<convertible_unit> only parts with a unit
 
 216 that's convertible to unit will be used for autocompletion.
 
 218 Obsolete parts will by default not be displayed for selection. However they are
 
 219 accepted as default values and can persist during updates. As with other
 
 220 selectors though, they are not selectable once overridden.
 
 222 C<part_picker> will register it's javascript for inclusion in the next header
 
 223 rendering. If you write a standard controller that only calls C<render> once, it
 
 224 will just work. In case the header is generated in a different render call
 
 225 (multiple blocks, ajax, old C<bin/mozilla> style controllers) you need to
 
 226 include C<kivi.Part.js> yourself.
 
 228 On pressing <enter> the picker will try to commit the current selection,
 
 229 resulting in one of the following events, whose corresponding callbacks can be
 
 230 set in C<params.actions>:
 
 234 =item * C<commit_one>
 
 236 If exactly one element matches the input, the internal id will be set to this
 
 237 id, the internal state will be set to C<PICKED> and the C<change> event on the
 
 238 picker will be fired. Additionally, if C<params> contains C<fat_set_item> a
 
 239 special event C<set_item:PartPicker> will be fired which is guaranteed to
 
 240 contain a complete JSON representation of the part.
 
 242 After that the action C<commit_one> will be executed, which defaults to
 
 243 clicking a button with id C<update_button> for backward compatibility reasons.
 
 245 =item * C<commit_many>
 
 247 If more than one element matches the input, the internal state will be set to
 
 250 After that the action C<commit_one> will be executed, which defaults to
 
 251 opening a popup dialog for graphical interaction. If C<params> contains
 
 252 C<multiple> an alternative popup will be opened, allowing multiple items to be
 
 253 selected. Note however that this requires an additional callback
 
 254 C<set_multi_items> to work.
 
 256 =item * C<commit_none>
 
 258 If no element matches the input, the internal state will be set to undefined.
 
 260 If an action for C<commit_none> exists, it will be called with the picker
 
 261 object and current term. The caller can then implement creation of new parts.
 
 267 =head1 PART PICKER SPECIFICATION
 
 269 The following list of design goals were applied:
 
 275 Parts should not be perceived by the user as distinct inputs of partnumber and
 
 276 description but as a single object
 
 280 Easy to use without documentation for novice users
 
 284 Fast to use with keyboard for experienced users
 
 288 Possible to use without any keyboard interaction for mouse (or touchscreen)
 
 293 Must not leave the current page in event of ambiguity (cf. current select_item
 
 298 Should be useable with hand scanners or similar alternative keyboard devices
 
 302 Should not require a feedback/check loop in the common case
 
 306 Should not be constrained to exact matches
 
 314 Action should be overridable
 
 318 The implementation consists of the following parts which will be referenced later:
 
 324 A hidden input (id input), used to hold the id of the selected part. The only
 
 325 input that gets submitted
 
 329 An input (dummy input) containing a description of the currently selected part,
 
 330 also used by the user to search for parts
 
 334 A jquery.autocomplete mechanism attached to the dummy field
 
 338 A popup layer for both feedback and input of additional data in case of
 
 343 An internal status of the part picker, indicating whether id input and dummy
 
 344 input are consistent. After leaving the dummy input the part picker must
 
 345 place itself in a consistent status.
 
 349 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
 
 359 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
 
 361 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>