@@ -1,195 +1,197 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | module ApplicationHelper |
|
18 | module ApplicationHelper | |
19 |
|
19 | |||
20 | # Return current logged in user or nil |
|
20 | # Return current logged in user or nil | |
21 | def loggedin? |
|
21 | def loggedin? | |
22 | @logged_in_user |
|
22 | @logged_in_user | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | # Return true if user is logged in and is admin, otherwise false |
|
25 | # Return true if user is logged in and is admin, otherwise false | |
26 | def admin_loggedin? |
|
26 | def admin_loggedin? | |
27 | @logged_in_user and @logged_in_user.admin? |
|
27 | @logged_in_user and @logged_in_user.admin? | |
28 | end |
|
28 | end | |
29 |
|
29 | |||
30 | # Return true if user is authorized for controller/action, otherwise false |
|
30 | # Return true if user is authorized for controller/action, otherwise false | |
31 | def authorize_for(controller, action) |
|
31 | def authorize_for(controller, action) | |
32 | # check if action is allowed on public projects |
|
32 | # check if action is allowed on public projects | |
33 | if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ] |
|
33 | if @project.is_public? and Permission.allowed_to_public "%s/%s" % [ controller, action ] | |
34 | return true |
|
34 | return true | |
35 | end |
|
35 | end | |
36 | # check if user is authorized |
|
36 | # check if user is authorized | |
37 | if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project.id) ) ) |
|
37 | if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project.id) ) ) | |
38 | return true |
|
38 | return true | |
39 | end |
|
39 | end | |
40 | return false |
|
40 | return false | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | # Display a link if user is authorized |
|
43 | # Display a link if user is authorized | |
44 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
44 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) | |
45 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller], options[:action]) |
|
45 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller], options[:action]) | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | # Display a link to user's account page |
|
48 | # Display a link to user's account page | |
49 | def link_to_user(user) |
|
49 | def link_to_user(user) | |
50 | link_to user.display_name, :controller => 'account', :action => 'show', :id => user |
|
50 | link_to user.display_name, :controller => 'account', :action => 'show', :id => user | |
51 | end |
|
51 | end | |
52 |
|
52 | |||
53 | def image_to_function(name, function, html_options = {}) |
|
53 | def image_to_function(name, function, html_options = {}) | |
54 | html_options.symbolize_keys! |
|
54 | html_options.symbolize_keys! | |
55 | tag(:input, html_options.merge({ |
|
55 | tag(:input, html_options.merge({ | |
56 | :type => "image", :src => image_path(name), |
|
56 | :type => "image", :src => image_path(name), | |
57 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" |
|
57 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" | |
58 | })) |
|
58 | })) | |
59 | end |
|
59 | end | |
60 |
|
60 | |||
61 | def format_date(date) |
|
61 | def format_date(date) | |
62 | l_date(date) if date |
|
62 | l_date(date) if date | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def format_time(time) |
|
65 | def format_time(time) | |
66 | l_datetime(time) if time |
|
66 | l_datetime(time) if time | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def day_name(day) |
|
69 | def day_name(day) | |
70 | l(:general_day_names).split(',')[day-1] |
|
70 | l(:general_day_names).split(',')[day-1] | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | def month_name(month) |
|
73 | def month_name(month) | |
74 | l(:actionview_datehelper_select_month_names).split(',')[month-1] |
|
74 | l(:actionview_datehelper_select_month_names).split(',')[month-1] | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def pagination_links_full(paginator, options={}, html_options={}) |
|
77 | def pagination_links_full(paginator, options={}, html_options={}) | |
78 | html = '' |
|
78 | html = '' | |
79 | html << link_to_remote(('« ' + l(:label_previous)), |
|
79 | html << link_to_remote(('« ' + l(:label_previous)), | |
80 | {:update => "content", :url => { :page => paginator.current.previous }}, |
|
80 | {:update => "content", :url => { :page => paginator.current.previous }}, | |
81 | {:href => url_for(:action => 'list', :params => params.merge({:page => paginator.current.previous}))}) + ' ' if paginator.current.previous |
|
81 | {:href => url_for(:action => 'list', :params => params.merge({:page => paginator.current.previous}))}) + ' ' if paginator.current.previous | |
82 |
|
82 | |||
83 | html << (pagination_links_each(paginator, options) do |n| |
|
83 | html << (pagination_links_each(paginator, options) do |n| | |
84 | link_to_remote(n.to_s, |
|
84 | link_to_remote(n.to_s, | |
85 | {:url => {:action => 'list', :params => params.merge({:page => n})}, :update => 'content'}, |
|
85 | {:url => {:action => 'list', :params => params.merge({:page => n})}, :update => 'content'}, | |
86 | {:href => url_for(:action => 'list', :params => params.merge({:page => n}))}) |
|
86 | {:href => url_for(:action => 'list', :params => params.merge({:page => n}))}) | |
87 | end || '') |
|
87 | end || '') | |
88 |
|
88 | |||
89 | html << ' ' + link_to_remote((l(:label_next) + ' »'), |
|
89 | html << ' ' + link_to_remote((l(:label_next) + ' »'), | |
90 | {:update => "content", :url => { :page => paginator.current.next }}, |
|
90 | {:update => "content", :url => { :page => paginator.current.next }}, | |
91 | {:href => url_for(:action => 'list', :params => params.merge({:page => paginator.current.next}))}) if paginator.current.next |
|
91 | {:href => url_for(:action => 'list', :params => params.merge({:page => paginator.current.next}))}) if paginator.current.next | |
92 | html |
|
92 | html | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | def textilizable(text) |
|
95 | def textilizable(text) | |
96 | text = (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize") ? RedCloth.new(h(text)).to_html : simple_format(auto_link(h(text))) |
|
96 | text = (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize") ? RedCloth.new(h(text)).to_html : simple_format(auto_link(h(text))) | |
97 | # turn "#id" patterns into links to issues |
|
97 | # turn "#id" patterns into links to issues | |
98 | text = text.gsub(/#(\d+)([^;\d])/, "<a href='/issues/show/\\1'>#\\1</a>\\2") |
|
98 | text = text.gsub(/#(\d+)([^;\d])/, "<a href='/issues/show/\\1'>#\\1</a>\\2") | |
99 | end |
|
99 | end | |
100 |
|
100 | |||
101 | def error_messages_for(object_name, options = {}) |
|
101 | def error_messages_for(object_name, options = {}) | |
102 | options = options.symbolize_keys |
|
102 | options = options.symbolize_keys | |
103 | object = instance_variable_get("@#{object_name}") |
|
103 | object = instance_variable_get("@#{object_name}") | |
104 | if object && !object.errors.empty? |
|
104 | if object && !object.errors.empty? | |
105 | # build full_messages here with controller current language |
|
105 | # build full_messages here with controller current language | |
106 | full_messages = [] |
|
106 | full_messages = [] | |
107 | object.errors.each do |attr, msg| |
|
107 | object.errors.each do |attr, msg| | |
108 | next if msg.nil? |
|
108 | next if msg.nil? | |
|
109 | msg = msg.first if msg.is_a? Array | |||
109 | if attr == "base" |
|
110 | if attr == "base" | |
110 | full_messages << l(msg) |
|
111 | full_messages << l(msg) | |
111 | else |
|
112 | else | |
112 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" |
|
113 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" | |
113 | end |
|
114 | end | |
114 | end |
|
115 | end | |
115 | # retrieve custom values error messages |
|
116 | # retrieve custom values error messages | |
116 | if object.errors[:custom_values] |
|
117 | if object.errors[:custom_values] | |
117 | object.custom_values.each do |v| |
|
118 | object.custom_values.each do |v| | |
118 | v.errors.each do |attr, msg| |
|
119 | v.errors.each do |attr, msg| | |
119 | next if msg.nil? |
|
120 | next if msg.nil? | |
|
121 | msg = msg.first if msg.is_a? Array | |||
120 | full_messages << "« " + v.custom_field.name + " » " + l(msg) |
|
122 | full_messages << "« " + v.custom_field.name + " » " + l(msg) | |
121 | end |
|
123 | end | |
122 | end |
|
124 | end | |
123 | end |
|
125 | end | |
124 | content_tag("div", |
|
126 | content_tag("div", | |
125 | content_tag( |
|
127 | content_tag( | |
126 | options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :" |
|
128 | options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :" | |
127 | ) + |
|
129 | ) + | |
128 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), |
|
130 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), | |
129 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" |
|
131 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" | |
130 | ) |
|
132 | ) | |
131 | else |
|
133 | else | |
132 | "" |
|
134 | "" | |
133 | end |
|
135 | end | |
134 | end |
|
136 | end | |
135 |
|
137 | |||
136 | def lang_options_for_select(blank=true) |
|
138 | def lang_options_for_select(blank=true) | |
137 | (blank ? [["(auto)", ""]] : []) + |
|
139 | (blank ? [["(auto)", ""]] : []) + | |
138 | (GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]} |
|
140 | (GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]} | |
139 | end |
|
141 | end | |
140 |
|
142 | |||
141 | def label_tag_for(name, option_tags = nil, options = {}) |
|
143 | def label_tag_for(name, option_tags = nil, options = {}) | |
142 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
144 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
143 | content_tag("label", label_text) |
|
145 | content_tag("label", label_text) | |
144 | end |
|
146 | end | |
145 |
|
147 | |||
146 | def labelled_tabular_form_for(name, object, options, &proc) |
|
148 | def labelled_tabular_form_for(name, object, options, &proc) | |
147 | options[:html] ||= {} |
|
149 | options[:html] ||= {} | |
148 | options[:html].store :class, "tabular" |
|
150 | options[:html].store :class, "tabular" | |
149 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) |
|
151 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) | |
150 | end |
|
152 | end | |
151 |
|
153 | |||
152 | def check_all_links(form_name) |
|
154 | def check_all_links(form_name) | |
153 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + |
|
155 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + | |
154 | " | " + |
|
156 | " | " + | |
155 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") |
|
157 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") | |
156 | end |
|
158 | end | |
157 |
|
159 | |||
158 | def calendar_for(field_id) |
|
160 | def calendar_for(field_id) | |
159 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + |
|
161 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + | |
160 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") |
|
162 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") | |
161 | end |
|
163 | end | |
162 | end |
|
164 | end | |
163 |
|
165 | |||
164 | class TabularFormBuilder < ActionView::Helpers::FormBuilder |
|
166 | class TabularFormBuilder < ActionView::Helpers::FormBuilder | |
165 | include GLoc |
|
167 | include GLoc | |
166 |
|
168 | |||
167 | def initialize(object_name, object, template, options, proc) |
|
169 | def initialize(object_name, object, template, options, proc) | |
168 | set_language_if_valid options.delete(:lang) |
|
170 | set_language_if_valid options.delete(:lang) | |
169 | @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc |
|
171 | @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc | |
170 | end |
|
172 | end | |
171 |
|
173 | |||
172 | (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector| |
|
174 | (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector| | |
173 | src = <<-END_SRC |
|
175 | src = <<-END_SRC | |
174 | def #{selector}(field, options = {}) |
|
176 | def #{selector}(field, options = {}) | |
175 | return super if options.delete :no_label |
|
177 | return super if options.delete :no_label | |
176 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
178 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
177 | label = @template.content_tag("label", label_text, |
|
179 | label = @template.content_tag("label", label_text, | |
178 | :class => (@object && @object.errors[field] ? "error" : nil), |
|
180 | :class => (@object && @object.errors[field] ? "error" : nil), | |
179 | :for => (@object_name.to_s + "_" + field.to_s)) |
|
181 | :for => (@object_name.to_s + "_" + field.to_s)) | |
180 | label + super |
|
182 | label + super | |
181 | end |
|
183 | end | |
182 | END_SRC |
|
184 | END_SRC | |
183 | class_eval src, __FILE__, __LINE__ |
|
185 | class_eval src, __FILE__, __LINE__ | |
184 | end |
|
186 | end | |
185 |
|
187 | |||
186 | def select(field, choices, options = {}, html_options = {}) |
|
188 | def select(field, choices, options = {}, html_options = {}) | |
187 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
189 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
188 | label = @template.content_tag("label", label_text, |
|
190 | label = @template.content_tag("label", label_text, | |
189 | :class => (@object && @object.errors[field] ? "error" : nil), |
|
191 | :class => (@object && @object.errors[field] ? "error" : nil), | |
190 | :for => (@object_name.to_s + "_" + field.to_s)) |
|
192 | :for => (@object_name.to_s + "_" + field.to_s)) | |
191 | label + super |
|
193 | label + super | |
192 | end |
|
194 | end | |
193 |
|
195 | |||
194 | end |
|
196 | end | |
195 |
|
197 |
General Comments 0
You need to be logged in to leave comments.
Login now