1 package SL::Presenter::CustomerVendor;
5 use SL::Presenter::EscapedText qw(escape is_escaped);
6 use SL::Presenter::Tag qw(input_tag html_tag name_to_id select_tag);
8 use Exporter qw(import);
9 our @EXPORT_OK = qw(customer_vendor customer vendor customer_vendor_picker customer_picker vendor_picker);
14 my ($customer_vendor, %params) = @_;
15 return _customer_vendor($customer_vendor, ref($customer_vendor) eq 'SL::DB::Customer' ? 'customer' : 'vendor', %params);
19 my ($customer, %params) = @_;
20 return _customer_vendor($customer, 'customer', %params);
24 my ($vendor, %params) = @_;
25 return _customer_vendor($vendor, 'vendor', %params);
28 sub _customer_vendor {
29 my ($cv, $type, %params) = @_;
31 $params{display} ||= 'inline';
33 croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
35 my $callback = $params{callback} ? '&callback=' . $::form->escape($params{callback}) : '';
38 $params{no_link} ? '' : '<a href="controller.pl?action=CustomerVendor/edit&db=' . $type . '&id=' . escape($cv->id) . '">',
40 $params{no_link} ? '' : '</a>',
46 sub customer_vendor_picker {
47 my ($name, $value, %params) = @_;
49 $params{type} //= 'customer' if 'SL::DB::Customer' eq ref $value;
50 $params{type} //= 'vendor' if 'SL::DB::Vendor' eq ref $value;
52 croak 'Unknown "type" parameter' unless $params{type} =~ m{^(?:customer|vendor)$};
53 croak 'Unknown value class' if $value && ref($value) && (ref($value) !~ m{^SL::DB::(?:Customer|Vendor)$});
55 if ($value && !ref $value) {
56 my $class = $params{type} eq 'customer' ? 'SL::DB::Manager::Customer' : 'SL::DB::Manager::Vendor';
57 $value = $class->find_by(id => $value);
60 my $id = delete($params{id}) || name_to_id($name);
62 my @classes = $params{class} ? ($params{class}) : ();
63 push @classes, 'customer_vendor_autocomplete';
65 # do not use reserved html attribute 'type' for cv type
66 $params{cv_type} = delete $params{type};
69 input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
70 'data-customer-vendor-picker-data' => JSON::to_json(\%params),
72 input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
74 $::request->layout->add_javascripts('kivi.CustomerVendor.js');
75 $::request->presenter->need_reinit_widgets($id);
77 html_tag('span', $ret, class => 'customer_vendor_picker');
80 sub customer_picker { my ($name, $value, @slurp) = @_; customer_vendor_picker($name, $value, @slurp, type => 'customer') }
81 sub vendor_picker { my ($name, $value, @slurp) = @_; customer_vendor_picker($name, $value, @slurp, type => 'vendor') }
82 sub picker { goto &customer_vendor_picker }
94 SL::Presenter::CustomerVendor - Presenter module for customer and
95 vendor Rose::DB objects
100 my $customer = SL::DB::Manager::Customer->get_first;
101 my $html = SL::Presenter::CustomerVendor::customer($customer, display => 'inline');
104 my $vendor = SL::DB::Manager::Vendor->get_first;
105 my $html = SL::Presenter::Customer::Vendor::vendor($customer, display => 'inline');
111 =item C<customer $object, %params>
113 Returns a rendered version (actually an instance of
114 L<SL::Presenter::EscapedText>) of the customer object C<$object>.
116 C<%params> can include:
122 Either C<inline> (the default) or C<table-cell>. At the moment both
123 representations are identical and produce the customer's name linked
124 to the corresponding 'edit' action.
128 If falsish (the default) then the customer's name will be linked to
129 the "edit customer" dialog from the master data menu.
133 =item C<vendor $object, %params>
135 Returns a rendered version (actually an instance of
136 L<SL::Presenter::EscapedText>) of the vendor object C<$object>.
138 C<%params> can include:
144 Either C<inline> (the default) or C<table-cell>. At the moment both
145 representations are identical and produce the vendor's name linked
146 to the corresponding 'edit' action.
150 If falsish (the default) then the vendor's name will be linked to
151 the "edit vendor" dialog from the master data menu.
155 =item C<customer_vendor $object, %params>
157 Returns a rendered version (actually an instance of
158 L<SL::Presenter::EscapedText>) of the customer or vendor object
159 C<$object> by calling either L</customer> or L</vendor> depending on
160 C<$object>'s type. See the respective functions for available
171 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>