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