epic-ts
[kivitendo-erp.git] / SL / Presenter / Part.pm
1 package SL::Presenter::Part;
2
3 use strict;
4
5 use SL::DB::Part;
6
7 use Exporter qw(import);
8 our @EXPORT = qw(part_picker part);
9
10 use Carp;
11
12 sub part {
13   my ($self, $part, %params) = @_;
14
15   $params{display} ||= 'inline';
16
17   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
18
19   my $text = join '', (
20     $params{no_link} ? '' : '<a href="ic.pl?action=edit&id=' . $self->escape($part->id) . '">',
21     $self->escape($part->partnumber),
22     $params{no_link} ? '' : '</a>',
23   );
24   return $self->escaped_text($text);
25 }
26
27 sub part_picker {
28   my ($self, $name, $value, %params) = @_;
29
30   $value = SL::DB::Manager::Part->find_by(id => $value) if $value && !ref $value;
31   my $id = delete($params{id}) || $self->name_to_id($name);
32   my $fat_set_item = delete $params{fat_set_item};
33
34   my @classes = $params{class} ? ($params{class}) : ();
35   push @classes, 'part_autocomplete';
36   push @classes, 'partpicker_fat_set_item' if $fat_set_item;
37
38   my $ret =
39     $self->input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id) .
40     join('', map { $params{$_} ? $self->input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(type unit convertible_unit)) .
41     $self->input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
42
43   $::request->layout->add_javascripts('autocomplete_part.js');
44   $::request->presenter->need_reinit_widgets($id);
45
46   $self->html_tag('span', $ret, class => 'part_picker');
47 }
48
49 1;
50
51 __END__
52
53 =encoding utf-8
54
55 =head1 NAME
56
57 SL::Presenter::Part - Part related presenter stuff
58
59 =head1 SYNOPSIS
60
61   # Create an html link for editing/opening a part/service/assembly
62   my $object = my $object = SL::DB::Manager::Part->get_first;
63   my $html   = SL::Presenter->get->part($object, display => 'inline');
64
65 see also L<SL::Presenter>
66
67 =head1 DESCRIPTION
68
69 see L<SL::Presenter>
70
71 =head1 FUNCTIONS
72
73 =over 2
74
75 =item C<part, $object, %params>
76
77 Returns a rendered version (actually an instance of
78 L<SL::Presenter::EscapedText>) of the part object C<$object>
79
80 C<%params> can include:
81
82 =over 4
83
84 =item * display
85
86 Either C<inline> (the default) or C<table-cell>. At the moment both
87 representations are identical and produce the part's name linked
88 to the corresponding 'edit' action.
89
90 =back
91
92 =back
93
94 =over 2
95
96 =item C<part_picker $name, $value, %params>
97
98 All-in-one picker widget for parts. The name will be both id and name
99 of the resulting hidden C<id> input field (but the ID can be
100 overwritten with C<$params{id}>).
101
102 An additional dummy input will be generated which is used to find
103 parts. For a detailed description of its behaviour, see section
104 C<PART PICKER SPECIFICATION>.
105
106 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
107
108 If C<%params> contains C<type> only parts of this type will be used
109 for autocompletion. You may comma separate multiple types as in
110 C<part,assembly>.
111
112 If C<%params> contains C<unit> only parts with this unit will be used
113 for autocompletion. You may comma separate multiple units as in
114 C<h,min>.
115
116 If C<%params> contains C<convertible_unit> only parts with a unit
117 that's convertible to unit will be used for autocompletion.
118
119 Obsolete parts will by default not be displayed for selection. However they are
120 accepted as default values and can persist during updates. As with other
121 selectors though, they are not selectable once overridden.
122
123 C<part_picker> will register it's javascript for inclusion in the next header
124 rendering. If you write a standard controller that only call C<render> once, it
125 will just work.  In case the header is generated in a different render call
126 (multiple blocks, ajax, old C<bin/moilla> style controllers) you need to
127 include C<js/autocomplete_part.js> yourself.
128
129 =back
130
131 =head1 PART PICKER SPECIFICATION
132
133 The following list of design goals were applied:
134
135 =over 4
136
137 =item *
138
139 Parts should not be perceived by the user as distinct inputs of partnumber and
140 description but as a single object
141
142 =item *
143
144 Easy to use without documentation for novice users
145
146 =item *
147
148 Fast to use with keyboard for experienced users
149
150 =item *
151
152 Possible to use without any keyboard interaction for mouse (or touchscreen)
153 users
154
155 =item *
156
157 Must not leave the current page in event of ambiguity (cf. current select_item
158 mechanism)
159
160 =item *
161
162 Should be useable with hand scanners or similar alternative keyboard devices
163
164 =item *
165
166 Should not require a feedback/check loop in the common case
167
168 =item *
169
170 Should not be constrained to exact matches
171
172 =back
173
174 The implementation consists of the following parts which will be referenced later:
175
176 =over 4
177
178 =item 1
179
180 A hidden input (id input), used to hold the id of the selected part. The only
181 input that gets submitted
182
183 =item 2
184
185 An input (dummy input) containing a description of the currently selected part,
186 also used by the user to search for parts
187
188 =item 3
189
190 A jquery.autocomplete mechanism attached to the dummy field
191
192 =item 4
193
194 A popup layer for both feedback and input of additional data in case of
195 ambiguity.
196
197 =item 5
198
199 An internal status of the part picker, indicating whether id input and dummy
200 input are consistent. After leaving the dummy input the part picker must
201 place itself in a consistent status.
202
203 =item 6
204
205 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
206
207 =back
208
209 =head1 BUGS
210
211 None atm :)
212
213 =head1 AUTHOR
214
215 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
216
217 =cut