1be7a635a1b6f10471fd9d5f59b775fdca209382
[kivitendo-erp.git] / SL / Presenter / Project.pm
1 package SL::Presenter::Project;
2
3 use strict;
4
5 use SL::Presenter::EscapedText qw(escape is_escaped);
6 use SL::Presenter::Tag qw(input_tag html_tag name_to_id select_tag);
7
8 use Exporter qw(import);
9 our @EXPORT_OK = qw(project project_picker);
10
11 use Carp;
12
13 sub project {
14   my ($project, %params) = @_;
15
16   return '' unless $project;
17
18   $params{display} ||= 'inline';
19
20   croak "Unknown display type '$params{display}'" unless $params{display} =~ m/^(?:inline|table-cell)$/;
21
22   my $description = $project->full_description(style => $params{style});
23   my $callback    = $params{callback} ? '&callback=' . $::form->escape($params{callback}) : '';
24
25   my $text = join '', (
26     $params{no_link} ? '' : '<a href="controller.pl?action=Project/edit&amp;id=' . escape($project->id) . $callback . '">',
27     escape($description),
28     $params{no_link} ? '' : '</a>',
29   );
30   is_escaped($text);
31 }
32
33 sub project_picker {
34   my ($name, $value, %params) = @_;
35
36   $value      = SL::DB::Manager::Project->find_by(id => $value) if $value && !ref $value;
37   my $id      = delete($params{id}) || name_to_id($name);
38   my @classes = $params{class} ? ($params{class}) : ();
39   push @classes, 'project_autocomplete';
40
41   my $ret =
42     input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id) .
43     join('', map { $params{$_} ? input_tag("", delete $params{$_}, id => "${id}_${_}", type => 'hidden') : '' } qw(customer_id)) .
44     input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
45
46   $::request->layout->add_javascripts('autocomplete_project.js');
47   $::request->presenter->need_reinit_widgets($id);
48
49   html_tag('span', $ret, class => 'project_picker');
50 }
51
52 sub picker { goto &project_picker };
53
54 1;
55
56 __END__
57
58 =pod
59
60 =encoding utf8
61
62 =head1 NAME
63
64 SL::Presenter::Project - Presenter module for project Rose::DB objects
65
66 =head1 SYNOPSIS
67
68   my $project = SL::DB::Manager::Project->get_first;
69   my $html    = SL::Presenter::Project->project($project, display => 'inline');
70
71 =head1 FUNCTIONS
72
73 =over 4
74
75 =item C<project $object, %params>
76
77 Returns a rendered version (actually an instance of
78 L<SL::Presenter::EscapedText>) of the project object C<$customer>.
79
80 C<%params> can include:
81
82 =over 2
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 project's description
88 (controlled by the C<style> parameter) linked to the corresponding
89 'edit' action.
90
91 =item * style
92
93 Determines what exactly will be output. Can be one of the values with
94 C<both> being the default if it is missing:
95
96 =over 2
97
98 =item C<projectnumber> (or simply C<number>)
99
100 Outputs only the project's number.
101
102 =item C<projectdescription> (or simply C<description>)
103
104 Outputs only the project's description.
105
106 =item C<both>
107
108 Outputs the project's number followed by its description in
109 parenthesis (e.g. "12345 (Secret Combinations)"). If the project's
110 description is already part of the project's number then it will not
111 be appended.
112
113 =back
114
115 =item * no_link
116
117 If falsish (the default) then the project's description will be linked to
118 the "edit project" dialog from the master data menu.
119
120 =back
121
122 =back
123
124 =head1 BUGS
125
126 Nothing here yet.
127
128 =head1 AUTHOR
129
130 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
131
132 =cut