]> wagnertech.de Git - mfinanz.git/blob - SL/Presenter/Project.pm
Merge branch 'master' of http://wagnertech.de/git/mfinanz
[mfinanz.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 link_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 => delete $params{style});
23   my $callback    = $params{callback} ?
24                       '&callback=' . $::form->escape(delete $params{callback})
25                     : '';
26
27   my $text = escape($description);
28   if (! delete $params{no_link}) {
29     my $href = 'controller.pl?action=Project/edit'
30                . '&id=' . escape($project->id)
31                . $callback;
32     $text = link_tag($href, $text, %params);
33   }
34
35   is_escaped($text);
36 }
37
38 sub project_picker {
39   my ($name, $value, %params) = @_;
40
41   $value      = SL::DB::Manager::Project->find_by(id => $value) if $value && !ref $value;
42   my $id      = delete($params{id}) || name_to_id($name);
43   my @classes = $params{class} ? ($params{class}) : ();
44   push @classes, 'project_autocomplete';
45
46
47   my %data_params = map { $_ => delete $params{$_}  } grep { defined $params{$_} } qw(customer_id active valid description_style);
48
49   # If there is no 'onClick' parameter, set it to 'this.select()',
50   # so that the user can type directly in the input field
51   # to search another project.
52   if (!grep { m{onclick}i } keys %params) {
53     $params{onClick} = 'this.select()';
54   }
55
56   my $ret =
57     input_tag($name, (ref $value && $value->can('id') ? $value->id : ''), class => "@classes", type => 'hidden', id => $id,
58               'data-project-picker-data' => JSON::to_json(\%data_params),
59     ) .
60     input_tag("", ref $value ? $value->displayable_name : '', id => "${id}_name", %params);
61
62   $::request->layout->add_javascripts('autocomplete_project.js');
63   $::request->presenter->need_reinit_widgets($id);
64
65   html_tag('span', $ret, class => 'project_picker');
66 }
67
68 sub picker { goto &project_picker };
69
70 1;
71
72 __END__
73
74 =pod
75
76 =encoding utf8
77
78 =head1 NAME
79
80 SL::Presenter::Project - Presenter module for project Rose::DB objects
81
82 =head1 SYNOPSIS
83
84   my $project = SL::DB::Manager::Project->get_first;
85   my $html    = SL::Presenter::Project->project($project, display => 'inline');
86
87 =head1 FUNCTIONS
88
89 =over 4
90
91 =item C<project $object, %params>
92
93 Returns a rendered version (actually an instance of
94 L<SL::Presenter::EscapedText>) of the project object C<$customer>.
95
96 Remaining C<%params> are passed to the function
97 C<SL::Presenter::Tag::link_tag>. It can include:
98
99 =over 2
100
101 =item * display
102
103 Either C<inline> (the default) or C<table-cell>. Is passed to the function
104 C<SL::Presenter::Tag::link_tag>.
105
106 =item * style
107
108 Determines what exactly will be output. Can be one of the values with
109 C<both> being the default if it is missing:
110
111 =item * no_link
112
113 If falsish (the default) then the description will be linked to the "edit"
114 dialog.
115
116 =over 2
117
118 =item C<projectnumber> (or simply C<number>)
119
120 Outputs only the project's number.
121
122 =item C<projectdescription> (or simply C<description>)
123
124 Outputs only the project's description.
125
126 =item C<both>
127
128 Outputs the project's number followed by its description in
129 parenthesis (e.g. "12345 (Secret Combinations)"). If the project's
130 description is already part of the project's number then it will not
131 be appended.
132
133 =back
134
135 =item * no_link
136
137 If falsish (the default) then the project's description will be linked to
138 the "edit project" dialog from the master data menu.
139
140 =back
141
142 =back
143
144 =head1 BUGS
145
146 Nothing here yet.
147
148 =head1 AUTHOR
149
150 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
151
152 =cut