1fbce1e068dbceab742ce4f02f96482619c14a6c
[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(column type unit convertible_unit)) .
41     $self->input_tag("", (ref $value && $value->can('description')) ? $value->description : '', id => "${id}_name", %params);
42
43   $::request->presenter->need_reinit_widgets($id);
44
45   $self->html_tag('span', $ret, class => 'part_picker');
46 }
47
48 1;
49
50 __END__
51
52 =encoding utf-8
53
54 =head1 NAME
55
56 SL::Presenter::Part - Part related presenter stuff
57
58 =head1 SYNOPSIS
59
60   # Create an html link for editing/opening a part/service/assembly
61   my $object = my $object = SL::DB::Manager::Part->get_first;
62   my $html   = SL::Presenter->get->part($object, display => 'inline');
63
64 see also L<SL::Presenter>
65
66 =head1 DESCRIPTION
67
68 see L<SL::Presenter>
69
70 =head1 FUNCTIONS
71
72 =over 2
73
74 =item C<part, $object, %params>
75
76 Returns a rendered version (actually an instance of
77 L<SL::Presenter::EscapedText>) of the part object C<$object>
78
79 C<%params> can include:
80
81 =over 4
82
83 =item * display
84
85 Either C<inline> (the default) or C<table-cell>. At the moment both
86 representations are identical and produce the part's name linked
87 to the corresponding 'edit' action.
88
89 =back
90
91 =back
92
93 =over 2
94
95 =item C<part_picker $name, $value, %params>
96
97 All-in-one picker widget for parts. The name will be both id and name
98 of the resulting hidden C<id> input field (but the ID can be
99 overwritten with C<$params{id}>).
100
101 An additional dummy input will be generated which is used to find
102 parts. For a detailed description of it's behaviour, see section
103 C<PART PICKER SPECIFICATION>.
104
105 C<$value> can be a parts id or a C<Rose::DB:Object> instance.
106
107 If C<%params> contains C<type> only parts of this type will be used
108 for autocompletion. You may comma separate multiple types as in
109 C<part,assembly>.
110
111 If C<%params> contains C<unit> only parts with this unit will be used
112 for autocompletion. You may comma separate multiple units as in
113 C<h,min>.
114
115 If C<%params> contains C<convertible_unit> only parts with a unit
116 that's convertible to unit will be used for autocompletion.
117
118 Obsolete parts will by default not displayed for selection. However they are
119 accepted as default values and can persist during updates. As with other
120 selectors though, they are not selectable once overridden.
121
122 Currently you must include C<js/autocomplete_part.js> in your controller, the
123 presenter can not do this from the template.
124
125 =back
126
127 =head1 PART PICKER SPECIFICATION
128
129 The following list of design goals were applied:
130
131 =over 4
132
133 =item *
134
135 Parts should not be perceived by the user as distinct inputs of partnumber and
136 description but as a single object
137
138 =item *
139
140 Easy to use without documentation for novice users
141
142 =item *
143
144 Fast to use with keyboard for experienced users
145
146 =item *
147
148 Possible to use without any keyboard interaction for mouse (or touchscreen)
149 users
150
151 =item *
152
153 Must not leave the current page in event of ambiguity (cf. current select_item
154 mechanism)
155
156 =item *
157
158 Should be useable with hand scanners or similar alternative keyboard devices
159
160 =item *
161
162 Should not require a feedback/check loop in the common case
163
164 =item *
165
166 Should not be constraint to exact matches
167
168 =back
169
170 The implementation consists of the following parts which will be referenced later:
171
172 =over 4
173
174 =item 1
175
176 A hidden input (id input), used to hold the id of the selected part. The only
177 input that gets submitted
178
179 =item 2
180
181 An input (dummy input) containing a description of the currently selected part,
182 also used by the user to search for parts
183
184 =item 3
185
186 A jquery.autocomplete mechanism attached to the dummy field
187
188 =item 4
189
190 A popup layer for both feedback and input of additional data in case of
191 ambiguity.
192
193 =item 5
194
195 An internal status of the part picker, indicating wether id input and dummy
196 input are consistent. After leaving the dummy input the part picker must
197 place itself in a consistent status.
198
199 =item 6
200
201 A clickable icon (popup trigger) attached to the dummy input, which triggers the popup layer.
202
203 =back
204
205 =head1 BUGS
206
207 None atm :)
208
209 =head1 AUTHOR
210
211 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
212
213 =cut