Presenter: Sub-Presenter auf Funktional geändert
[kivitendo-erp.git] / SL / Presenter / Part.pm
1 package SL::Presenter::Part;
2
3 use strict;
4
5 use SL::DB::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);
10
11 use Exporter qw(import);
12 our @EXPORT_OK = qw(
13   part_picker part select_classification classification_abbreviation
14   type_abbreviation separate_abbreviation typeclass_abbreviation
15 );
16 our %EXPORT_TAGS = (ALL => \@EXPORT_OK);
17
18 use Carp;
19
20 sub part {
21   my ($part, %params) = @_;
22
23   $params{display} ||= 'inline';
24
25   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
26
27   my $text = join '', (
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>',
31   );
32
33   is_escaped($text);
34 }
35
36 sub part_picker {
37   my ($name, $value, %params) = @_;
38
39   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
40   my $id = $params{id} || name_to_id($name);
41
42   my @classes = $params{class} ? ($params{class}) : ();
43   push @classes, 'part_autocomplete';
44
45   my $ret =
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),
48     ) .
49     input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
50
51   $::request->layout->add_javascripts('kivi.Part.js');
52   $::request->presenter->need_reinit_widgets($id);
53
54   html_tag('span', $ret, class => 'part_picker');
55 }
56
57 sub picker { goto &part_picker }
58
59 #
60 # shortcut for article type
61 #
62 sub type_abbreviation {
63   my ($part_type) = @_;
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)');
68 }
69
70 #
71 # Translations for Abbreviations:
72 #
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)')
78 #
79 # and for descriptions
80 # $::locale->text('Purchase')
81 # $::locale->text('Sales')
82 # $::locale->text('Merchandise')
83 # $::locale->text('Production')
84
85 #
86 # shortcut for article type
87 #
88 sub classification_abbreviation {
89   my ($id) = @_;
90   SL::DB::Manager::PartClassification->cache_all();
91   my $obj = SL::DB::PartClassification->load_cached($id);
92   $obj && $obj->abbreviation ? t8($obj->abbreviation) : '';
93 }
94
95 sub typeclass_abbreviation {
96   my ($part) = @_;
97   return '' if !$part || !$part->isa('SL::DB::Part');
98   return type_abbreviation($part->part_type) . classification_abbreviation($part->classification_id);
99 }
100
101 #
102 # shortcut for article type
103 #
104 sub separate_abbreviation {
105   my ($id) = @_;
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) : '';
109 }
110
111 #
112 # generate selection tag
113 #
114 sub select_classification {
115   my ($name, %attributes) = @_;
116
117   $attributes{value_key} = 'id';
118   $attributes{title_key} = 'description';
119
120   my $classification_type_filter = delete $attributes{type} // [];
121
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 );
125 }
126
127 1;
128
129 __END__
130
131 =encoding utf-8
132
133 =head1 NAME
134
135 SL::Presenter::Part - Part related presenter stuff
136
137 =head1 SYNOPSIS
138
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');
142
143 see also L<SL::Presenter>
144
145 =head1 DESCRIPTION
146
147 see L<SL::Presenter>
148
149 =head1 FUNCTIONS
150
151 =over 2
152
153 =item C<part, $object, %params>
154
155 Returns a rendered version (actually an instance of
156 L<SL::Presenter::EscapedText>) of the part object C<$object>
157
158 C<%params> can include:
159
160 =over 4
161
162 =item * display
163
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.
167
168 =back
169
170 =back
171
172 =over 2
173
174 =item C<classification_abbreviation $classification_id>
175
176 Returns the shortcut of the classification
177
178 =back
179
180 =over 2
181
182 =item C<separate_abbreviation $classification_id>
183
184 Returns the shortcut of the classification if the classification has the separate flag set.
185
186 =back
187
188 =over 2
189
190 =item C<select_classification $name,%params>
191
192 Returns an HTML select tag with all available classifications.
193
194 C<%params> can include:
195
196 =over 4
197
198 =item * default
199
200 The id of the selected item.
201
202 =back
203
204 =back
205
206 =over 2
207
208 =item C<part_picker $name, $value, %params>
209
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}>).
213
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>.
217
218 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
219
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
222 C<part,assembly>.
223
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
226 C<h,min>.
227
228 If C<%params> contains C<convertible_unit> only parts with a unit
229 that's convertible to unit will be used for autocompletion.
230
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.
234
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.
240
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>:
244
245 =over 4
246
247 =item * C<commit_one>
248
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.
254
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.
257
258 =item * C<commit_many>
259
260 If more than one element matches the input, the internal state will be set to
261 undefined.
262
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.
268
269 =item * C<commit_none>
270
271 If no element matches the input, the internal state will be set to undefined.
272
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.
275
276 =back
277
278 =back
279
280 =head1 PART PICKER SPECIFICATION
281
282 The following list of design goals were applied:
283
284 =over 4
285
286 =item *
287
288 Parts should not be perceived by the user as distinct inputs of partnumber and
289 description but as a single object
290
291 =item *
292
293 Easy to use without documentation for novice users
294
295 =item *
296
297 Fast to use with keyboard for experienced users
298
299 =item *
300
301 Possible to use without any keyboard interaction for mouse (or touchscreen)
302 users
303
304 =item *
305
306 Must not leave the current page in event of ambiguity (cf. current select_item
307 mechanism)
308
309 =item *
310
311 Should be useable with hand scanners or similar alternative keyboard devices
312
313 =item *
314
315 Should not require a feedback/check loop in the common case
316
317 =item *
318
319 Should not be constrained to exact matches
320
321 =item *
322
323 Must be atomic
324
325 =item *
326
327 Action should be overridable
328
329 =back
330
331 The implementation consists of the following parts which will be referenced later:
332
333 =over 4
334
335 =item 1
336
337 A hidden input (id input), used to hold the id of the selected part. The only
338 input that gets submitted
339
340 =item 2
341
342 An input (dummy input) containing a description of the currently selected part,
343 also used by the user to search for parts
344
345 =item 3
346
347 A jquery.autocomplete mechanism attached to the dummy field
348
349 =item 4
350
351 A popup layer for both feedback and input of additional data in case of
352 ambiguity.
353
354 =item 5
355
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.
359
360 =item 6
361
362 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
363
364 =back
365
366 =head1 BUGS
367
368 None atm :)
369
370 =head1 AUTHOR
371
372 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
373
374 Martin Helmling E<lt>martin.helmling@opendynamic.deE<gt>
375
376 =cut