|
@@
-1,184
+1,184
|
|
1
|
# redMine - project management software
|
|
1
|
# redMine - project management software
|
|
2
|
# Copyright (C) 2006 Jean-Philippe Lang
|
|
2
|
# Copyright (C) 2006 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 format_date(date)
|
|
53
|
def format_date(date)
|
|
54
|
l_date(date) if date
|
|
54
|
l_date(date) if date
|
|
55
|
end
|
|
55
|
end
|
|
56
|
|
|
56
|
|
|
57
|
def format_time(time)
|
|
57
|
def format_time(time)
|
|
58
|
l_datetime(time) if time
|
|
58
|
l_datetime(time) if time
|
|
59
|
end
|
|
59
|
end
|
|
60
|
|
|
60
|
|
|
61
|
def day_name(day)
|
|
61
|
def day_name(day)
|
|
62
|
l(:general_day_names).split(',')[day-1]
|
|
62
|
l(:general_day_names).split(',')[day-1]
|
|
63
|
end
|
|
63
|
end
|
|
64
|
|
|
64
|
|
|
65
|
def month_name(month)
|
|
65
|
def month_name(month)
|
|
66
|
l(:actionview_datehelper_select_month_names).split(',')[month-1]
|
|
66
|
l(:actionview_datehelper_select_month_names).split(',')[month-1]
|
|
67
|
end
|
|
67
|
end
|
|
68
|
|
|
68
|
|
|
69
|
def pagination_links_full(paginator, options={}, html_options={})
|
|
69
|
def pagination_links_full(paginator, options={}, html_options={})
|
|
70
|
html = ''
|
|
70
|
html = ''
|
|
71
|
html << link_to_remote(('« ' + l(:label_previous)),
|
|
71
|
html << link_to_remote(('« ' + l(:label_previous)),
|
|
72
|
{:update => "content", :url => { :page => paginator.current.previous }},
|
|
72
|
{:update => "content", :url => { :page => paginator.current.previous }},
|
|
73
|
{:href => url_for(:action => 'list', :params => @params.merge({:page => paginator.current.previous}))}) + ' ' if paginator.current.previous
|
|
73
|
{:href => url_for(:action => 'list', :params => @params.merge({:page => paginator.current.previous}))}) + ' ' if paginator.current.previous
|
|
74
|
|
|
74
|
|
|
75
|
html << (pagination_links_each(paginator, options) do |n|
|
|
75
|
html << (pagination_links_each(paginator, options) do |n|
|
|
76
|
link_to_remote(n.to_s,
|
|
76
|
link_to_remote(n.to_s,
|
|
77
|
{:url => {:action => 'list', :params => @params.merge({:page => n})}, :update => 'content'},
|
|
77
|
{:url => {:action => 'list', :params => @params.merge({:page => n})}, :update => 'content'},
|
|
78
|
{:href => url_for(:action => 'list', :params => @params.merge({:page => n}))})
|
|
78
|
{:href => url_for(:action => 'list', :params => @params.merge({:page => n}))})
|
|
79
|
end || '')
|
|
79
|
end || '')
|
|
80
|
|
|
80
|
|
|
81
|
html << ' ' + link_to_remote((l(:label_next) + ' »'),
|
|
81
|
html << ' ' + link_to_remote((l(:label_next) + ' »'),
|
|
82
|
{:update => "content", :url => { :page => paginator.current.next }},
|
|
82
|
{:update => "content", :url => { :page => paginator.current.next }},
|
|
83
|
{:href => url_for(:action => 'list', :params => @params.merge({:page => paginator.current.next}))}) if paginator.current.next
|
|
83
|
{:href => url_for(:action => 'list', :params => @params.merge({:page => paginator.current.next}))}) if paginator.current.next
|
|
84
|
html
|
|
84
|
html
|
|
85
|
end
|
|
85
|
end
|
|
86
|
|
|
86
|
|
|
87
|
def textilizable(text)
|
|
87
|
def textilizable(text)
|
|
88
|
$RDM_TEXTILE_DISABLED ? text : RedCloth.new(text).to_html
|
|
88
|
$RDM_TEXTILE_DISABLED ? simple_format(auto_link(h(text))) : RedCloth.new(h(text)).to_html
|
|
89
|
end
|
|
89
|
end
|
|
90
|
|
|
90
|
|
|
91
|
def error_messages_for(object_name, options = {})
|
|
91
|
def error_messages_for(object_name, options = {})
|
|
92
|
options = options.symbolize_keys
|
|
92
|
options = options.symbolize_keys
|
|
93
|
object = instance_variable_get("@#{object_name}")
|
|
93
|
object = instance_variable_get("@#{object_name}")
|
|
94
|
if object && !object.errors.empty?
|
|
94
|
if object && !object.errors.empty?
|
|
95
|
# build full_messages here with controller current language
|
|
95
|
# build full_messages here with controller current language
|
|
96
|
full_messages = []
|
|
96
|
full_messages = []
|
|
97
|
object.errors.each do |attr, msg|
|
|
97
|
object.errors.each do |attr, msg|
|
|
98
|
next if msg.nil?
|
|
98
|
next if msg.nil?
|
|
99
|
if attr == "base"
|
|
99
|
if attr == "base"
|
|
100
|
full_messages << l(msg)
|
|
100
|
full_messages << l(msg)
|
|
101
|
else
|
|
101
|
else
|
|
102
|
full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values"
|
|
102
|
full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values"
|
|
103
|
end
|
|
103
|
end
|
|
104
|
end
|
|
104
|
end
|
|
105
|
# retrieve custom values error messages
|
|
105
|
# retrieve custom values error messages
|
|
106
|
if object.errors[:custom_values]
|
|
106
|
if object.errors[:custom_values]
|
|
107
|
object.custom_values.each do |v|
|
|
107
|
object.custom_values.each do |v|
|
|
108
|
v.errors.each do |attr, msg|
|
|
108
|
v.errors.each do |attr, msg|
|
|
109
|
next if msg.nil?
|
|
109
|
next if msg.nil?
|
|
110
|
full_messages << "« " + v.custom_field.name + " » " + l(msg)
|
|
110
|
full_messages << "« " + v.custom_field.name + " » " + l(msg)
|
|
111
|
end
|
|
111
|
end
|
|
112
|
end
|
|
112
|
end
|
|
113
|
end
|
|
113
|
end
|
|
114
|
content_tag("div",
|
|
114
|
content_tag("div",
|
|
115
|
content_tag(
|
|
115
|
content_tag(
|
|
116
|
options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :"
|
|
116
|
options[:header_tag] || "h2", lwr(:gui_validation_error, full_messages.length) + " :"
|
|
117
|
) +
|
|
117
|
) +
|
|
118
|
content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
|
|
118
|
content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
|
|
119
|
"id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
|
|
119
|
"id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
|
|
120
|
)
|
|
120
|
)
|
|
121
|
else
|
|
121
|
else
|
|
122
|
""
|
|
122
|
""
|
|
123
|
end
|
|
123
|
end
|
|
124
|
end
|
|
124
|
end
|
|
125
|
|
|
125
|
|
|
126
|
def lang_options_for_select
|
|
126
|
def lang_options_for_select
|
|
127
|
(GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]}
|
|
127
|
(GLoc.valid_languages.sort {|x,y| x.to_s <=> y.to_s }).collect {|lang| [ l_lang_name(lang.to_s, lang), lang.to_s]}
|
|
128
|
end
|
|
128
|
end
|
|
129
|
|
|
129
|
|
|
130
|
def label_tag_for(name, option_tags = nil, options = {})
|
|
130
|
def label_tag_for(name, option_tags = nil, options = {})
|
|
131
|
label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
|
|
131
|
label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
|
|
132
|
content_tag("label", label_text)
|
|
132
|
content_tag("label", label_text)
|
|
133
|
end
|
|
133
|
end
|
|
134
|
|
|
134
|
|
|
135
|
def labelled_tabular_form_for(name, object, options, &proc)
|
|
135
|
def labelled_tabular_form_for(name, object, options, &proc)
|
|
136
|
options[:html] ||= {}
|
|
136
|
options[:html] ||= {}
|
|
137
|
options[:html].store :class, "tabular"
|
|
137
|
options[:html].store :class, "tabular"
|
|
138
|
form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
|
|
138
|
form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
|
|
139
|
end
|
|
139
|
end
|
|
140
|
|
|
140
|
|
|
141
|
def check_all_links(form_name)
|
|
141
|
def check_all_links(form_name)
|
|
142
|
link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
|
|
142
|
link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
|
|
143
|
" | " +
|
|
143
|
" | " +
|
|
144
|
link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
|
|
144
|
link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
|
|
145
|
end
|
|
145
|
end
|
|
146
|
|
|
146
|
|
|
147
|
def calendar_for(field_id)
|
|
147
|
def calendar_for(field_id)
|
|
148
|
image_tag("calendar", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
|
|
148
|
image_tag("calendar", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
|
|
149
|
javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
|
|
149
|
javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
|
|
150
|
end
|
|
150
|
end
|
|
151
|
end
|
|
151
|
end
|
|
152
|
|
|
152
|
|
|
153
|
class TabularFormBuilder < ActionView::Helpers::FormBuilder
|
|
153
|
class TabularFormBuilder < ActionView::Helpers::FormBuilder
|
|
154
|
include GLoc
|
|
154
|
include GLoc
|
|
155
|
|
|
155
|
|
|
156
|
def initialize(object_name, object, template, options, proc)
|
|
156
|
def initialize(object_name, object, template, options, proc)
|
|
157
|
set_language_if_valid options.delete(:lang)
|
|
157
|
set_language_if_valid options.delete(:lang)
|
|
158
|
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
|
|
158
|
@object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
|
|
159
|
end
|
|
159
|
end
|
|
160
|
|
|
160
|
|
|
161
|
(field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
|
|
161
|
(field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
|
|
162
|
src = <<-END_SRC
|
|
162
|
src = <<-END_SRC
|
|
163
|
def #{selector}(field, options = {})
|
|
163
|
def #{selector}(field, options = {})
|
|
164
|
return super if options.delete :no_label
|
|
164
|
return super if options.delete :no_label
|
|
165
|
label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
|
|
165
|
label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
|
|
166
|
label = @template.content_tag("label", label_text,
|
|
166
|
label = @template.content_tag("label", label_text,
|
|
167
|
:class => (@object && @object.errors[field] ? "error" : nil),
|
|
167
|
:class => (@object && @object.errors[field] ? "error" : nil),
|
|
168
|
:for => (@object_name.to_s + "_" + field.to_s))
|
|
168
|
:for => (@object_name.to_s + "_" + field.to_s))
|
|
169
|
label + super
|
|
169
|
label + super
|
|
170
|
end
|
|
170
|
end
|
|
171
|
END_SRC
|
|
171
|
END_SRC
|
|
172
|
class_eval src, __FILE__, __LINE__
|
|
172
|
class_eval src, __FILE__, __LINE__
|
|
173
|
end
|
|
173
|
end
|
|
174
|
|
|
174
|
|
|
175
|
def select(field, choices, options = {}, html_options = {})
|
|
175
|
def select(field, choices, options = {}, html_options = {})
|
|
176
|
label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
|
|
176
|
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,
|
|
177
|
label = @template.content_tag("label", label_text,
|
|
178
|
:class => (@object && @object.errors[field] ? "error" : nil),
|
|
178
|
:class => (@object && @object.errors[field] ? "error" : nil),
|
|
179
|
:for => (@object_name.to_s + "_" + field.to_s))
|
|
179
|
:for => (@object_name.to_s + "_" + field.to_s))
|
|
180
|
label + super
|
|
180
|
label + super
|
|
181
|
end
|
|
181
|
end
|
|
182
|
|
|
182
|
|
|
183
|
end
|
|
183
|
end
|
|
184
|
|
|
184
|
|