8aecdfe492d9692b1fd405c7e9c273088ba4faec
[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
42   my %data_params = map { $_ => delete $params{$_}  } grep { defined $params{$_} } qw(customer_id active valid description_style);
43
44   my $ret =
45     input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
46               'data-project-picker-data' => JSON::to_json(\%data_params),
47     ) .
48     input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
49
50   $::request->layout->add_javascripts('autocomplete_project.js');
51   $::request->presenter->need_reinit_widgets($id);
52
53   html_tag('span', $ret, class => 'project_picker');
54 }
55
56 sub picker { goto &project_picker };
57
58 1;
59
60 __END__
61
62 =pod
63
64 =encoding utf8
65
66 =head1 NAME
67
68 SL::Presenter::Project - Presenter module for project Rose::DB objects
69
70 =head1 SYNOPSIS
71
72   my $project = SL::DB::Manager::Project->get_first;
73   my $html    = SL::Presenter::Project->project($project, display => 'inline');
74
75 =head1 FUNCTIONS
76
77 =over 4
78
79 =item C<project $object, %params>
80
81 Returns a rendered version (actually an instance of
82 L<SL::Presenter::EscapedText>) of the project object C<$customer>.
83
84 C<%params> can include:
85
86 =over 2
87
88 =item * display
89
90 Either C<inline> (the default) or C<table-cell>. At the moment both
91 representations are identical and produce the project's description
92 (controlled by the C<style> parameter) linked to the corresponding
93 'edit' action.
94
95 =item * style
96
97 Determines what exactly will be output. Can be one of the values with
98 C<both> being the default if it is missing:
99
100 =over 2
101
102 =item C<projectnumber> (or simply C<number>)
103
104 Outputs only the project's number.
105
106 =item C<projectdescription> (or simply C<description>)
107
108 Outputs only the project's description.
109
110 =item C<both>
111
112 Outputs the project's number followed by its description in
113 parenthesis (e.g. "12345 (Secret Combinations)"). If the project's
114 description is already part of the project's number then it will not
115 be appended.
116
117 =back
118
119 =item * no_link
120
121 If falsish (the default) then the project's description will be linked to
122 the "edit project" dialog from the master data menu.
123
124 =back
125
126 =back
127
128 =head1 BUGS
129
130 Nothing here yet.
131
132 =head1 AUTHOR
133
134 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
135
136 =cut