##// END OF EJS Templates
TabularFormBuilder moved out of application_helper.rb...
Jean-Philippe Lang -
r1015:66420fe4b7ec
parent child
Show More
@@ -0,0 +1,51
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 require 'action_view/helpers/form_helper'
19
20 class TabularFormBuilder < ActionView::Helpers::FormBuilder
21 include GLoc
22
23 def initialize(object_name, object, template, options, proc)
24 set_language_if_valid options.delete(:lang)
25 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
26 end
27
28 (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
29 src = <<-END_SRC
30 def #{selector}(field, options = {})
31 return super if options.delete :no_label
32 label_text = l(options[:label]) if options[:label]
33 label_text ||= l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym)
34 label_text << @template.content_tag("span", " *", :class => "required") if options.delete(:required)
35 label = @template.content_tag("label", label_text,
36 :class => (@object && @object.errors[field] ? "error" : nil),
37 :for => (@object_name.to_s + "_" + field.to_s))
38 label + super
39 end
40 END_SRC
41 class_eval src, __FILE__, __LINE__
42 end
43
44 def select(field, choices, options = {}, html_options = {})
45 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
46 label = @template.content_tag("label", label_text,
47 :class => (@object && @object.errors[field] ? "error" : nil),
48 :for => (@object_name.to_s + "_" + field.to_s))
49 label + super
50 end
51 end
@@ -1,421 +1,386
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 include Redmine::WikiFormatting::Macros::Definitions
19 include Redmine::WikiFormatting::Macros::Definitions
20
20
21 def current_role
21 def current_role
22 @current_role ||= User.current.role_for_project(@project)
22 @current_role ||= User.current.role_for_project(@project)
23 end
23 end
24
24
25 # Return true if user is authorized for controller/action, otherwise false
25 # Return true if user is authorized for controller/action, otherwise false
26 def authorize_for(controller, action)
26 def authorize_for(controller, action)
27 User.current.allowed_to?({:controller => controller, :action => action}, @project)
27 User.current.allowed_to?({:controller => controller, :action => action}, @project)
28 end
28 end
29
29
30 # Display a link if user is authorized
30 # Display a link if user is authorized
31 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
31 def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
32 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
32 link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
33 end
33 end
34
34
35 # Display a link to user's account page
35 # Display a link to user's account page
36 def link_to_user(user)
36 def link_to_user(user)
37 user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
37 user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous'
38 end
38 end
39
39
40 def link_to_issue(issue)
40 def link_to_issue(issue)
41 link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue
41 link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue
42 end
42 end
43
43
44 def toggle_link(name, id, options={})
44 def toggle_link(name, id, options={})
45 onclick = "Element.toggle('#{id}'); "
45 onclick = "Element.toggle('#{id}'); "
46 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
46 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
47 onclick << "return false;"
47 onclick << "return false;"
48 link_to(name, "#", :onclick => onclick)
48 link_to(name, "#", :onclick => onclick)
49 end
49 end
50
50
51 def show_and_goto_link(name, id, options={})
51 def show_and_goto_link(name, id, options={})
52 onclick = "Element.show('#{id}'); "
52 onclick = "Element.show('#{id}'); "
53 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
53 onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
54 onclick << "location.href='##{id}-anchor'; "
54 onclick << "location.href='##{id}-anchor'; "
55 onclick << "return false;"
55 onclick << "return false;"
56 link_to(name, "#", options.merge(:onclick => onclick))
56 link_to(name, "#", options.merge(:onclick => onclick))
57 end
57 end
58
58
59 def image_to_function(name, function, html_options = {})
59 def image_to_function(name, function, html_options = {})
60 html_options.symbolize_keys!
60 html_options.symbolize_keys!
61 tag(:input, html_options.merge({
61 tag(:input, html_options.merge({
62 :type => "image", :src => image_path(name),
62 :type => "image", :src => image_path(name),
63 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
63 :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
64 }))
64 }))
65 end
65 end
66
66
67 def prompt_to_remote(name, text, param, url, html_options = {})
67 def prompt_to_remote(name, text, param, url, html_options = {})
68 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
68 html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
69 link_to name, {}, html_options
69 link_to name, {}, html_options
70 end
70 end
71
71
72 def format_date(date)
72 def format_date(date)
73 return nil unless date
73 return nil unless date
74 # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
74 # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed)
75 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
75 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
76 date.strftime(@date_format)
76 date.strftime(@date_format)
77 end
77 end
78
78
79 def format_time(time, include_date = true)
79 def format_time(time, include_date = true)
80 return nil unless time
80 return nil unless time
81 time = time.to_time if time.is_a?(String)
81 time = time.to_time if time.is_a?(String)
82 zone = User.current.time_zone
82 zone = User.current.time_zone
83 if time.utc?
83 if time.utc?
84 local = zone ? zone.adjust(time) : time.getlocal
84 local = zone ? zone.adjust(time) : time.getlocal
85 else
85 else
86 local = zone ? zone.adjust(time.getutc) : time
86 local = zone ? zone.adjust(time.getutc) : time
87 end
87 end
88 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
88 @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format)
89 @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
89 @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format)
90 include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
90 include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format)
91 end
91 end
92
92
93 def authoring(created, author)
93 def authoring(created, author)
94 time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created))
94 time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created))
95 l(:label_added_time_by, author || 'Anonymous', time_tag)
95 l(:label_added_time_by, author || 'Anonymous', time_tag)
96 end
96 end
97
97
98 def day_name(day)
98 def day_name(day)
99 l(:general_day_names).split(',')[day-1]
99 l(:general_day_names).split(',')[day-1]
100 end
100 end
101
101
102 def month_name(month)
102 def month_name(month)
103 l(:actionview_datehelper_select_month_names).split(',')[month-1]
103 l(:actionview_datehelper_select_month_names).split(',')[month-1]
104 end
104 end
105
105
106 def pagination_links_full(paginator, count=nil, options={})
106 def pagination_links_full(paginator, count=nil, options={})
107 page_param = options.delete(:page_param) || :page
107 page_param = options.delete(:page_param) || :page
108 url_param = params.dup
108 url_param = params.dup
109
109
110 html = ''
110 html = ''
111 html << link_to_remote(('&#171; ' + l(:label_previous)),
111 html << link_to_remote(('&#171; ' + l(:label_previous)),
112 {:update => "content", :url => url_param.merge(page_param => paginator.current.previous)},
112 {:update => "content", :url => url_param.merge(page_param => paginator.current.previous)},
113 {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
113 {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous
114
114
115 html << (pagination_links_each(paginator, options) do |n|
115 html << (pagination_links_each(paginator, options) do |n|
116 link_to_remote(n.to_s,
116 link_to_remote(n.to_s,
117 {:url => {:params => url_param.merge(page_param => n)}, :update => 'content'},
117 {:url => {:params => url_param.merge(page_param => n)}, :update => 'content'},
118 {:href => url_for(:params => url_param.merge(page_param => n))})
118 {:href => url_for(:params => url_param.merge(page_param => n))})
119 end || '')
119 end || '')
120
120
121 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
121 html << ' ' + link_to_remote((l(:label_next) + ' &#187;'),
122 {:update => "content", :url => url_param.merge(page_param => paginator.current.next)},
122 {:update => "content", :url => url_param.merge(page_param => paginator.current.next)},
123 {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
123 {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next
124
124
125 unless count.nil?
125 unless count.nil?
126 html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
126 html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ')
127 end
127 end
128
128
129 html
129 html
130 end
130 end
131
131
132 def per_page_links(selected=nil)
132 def per_page_links(selected=nil)
133 links = Setting.per_page_options_array.collect do |n|
133 links = Setting.per_page_options_array.collect do |n|
134 n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
134 n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)},
135 {:href => url_for(params.dup.merge(:per_page => n))})
135 {:href => url_for(params.dup.merge(:per_page => n))})
136 end
136 end
137 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
137 links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
138 end
138 end
139
139
140 def set_html_title(text)
140 def set_html_title(text)
141 @html_header_title = text
141 @html_header_title = text
142 end
142 end
143
143
144 def html_title
144 def html_title
145 title = []
145 title = []
146 title << @project.name if @project
146 title << @project.name if @project
147 title << @html_header_title
147 title << @html_header_title
148 title << Setting.app_title
148 title << Setting.app_title
149 title.compact.join(' - ')
149 title.compact.join(' - ')
150 end
150 end
151
151
152 ACCESSKEYS = {:edit => 'e',
152 ACCESSKEYS = {:edit => 'e',
153 :preview => 'r',
153 :preview => 'r',
154 :quick_search => 'f',
154 :quick_search => 'f',
155 :search => '4',
155 :search => '4',
156 }.freeze unless const_defined?(:ACCESSKEYS)
156 }.freeze unless const_defined?(:ACCESSKEYS)
157
157
158 def accesskey(s)
158 def accesskey(s)
159 ACCESSKEYS[s]
159 ACCESSKEYS[s]
160 end
160 end
161
161
162 # Formats text according to system settings.
162 # Formats text according to system settings.
163 # 2 ways to call this method:
163 # 2 ways to call this method:
164 # * with a String: textilizable(text, options)
164 # * with a String: textilizable(text, options)
165 # * with an object and one of its attribute: textilizable(issue, :description, options)
165 # * with an object and one of its attribute: textilizable(issue, :description, options)
166 def textilizable(*args)
166 def textilizable(*args)
167 options = args.last.is_a?(Hash) ? args.pop : {}
167 options = args.last.is_a?(Hash) ? args.pop : {}
168 case args.size
168 case args.size
169 when 1
169 when 1
170 obj = nil
170 obj = nil
171 text = args.shift || ''
171 text = args.shift || ''
172 when 2
172 when 2
173 obj = args.shift
173 obj = args.shift
174 text = obj.send(args.shift)
174 text = obj.send(args.shift)
175 else
175 else
176 raise ArgumentError, 'invalid arguments to textilizable'
176 raise ArgumentError, 'invalid arguments to textilizable'
177 end
177 end
178
178
179 # when using an image link, try to use an attachment, if possible
179 # when using an image link, try to use an attachment, if possible
180 attachments = options[:attachments]
180 attachments = options[:attachments]
181 if attachments
181 if attachments
182 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m|
182 text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m|
183 style = $1
183 style = $1
184 filename = $6
184 filename = $6
185 rf = Regexp.new(filename, Regexp::IGNORECASE)
185 rf = Regexp.new(filename, Regexp::IGNORECASE)
186 # search for the picture in attachments
186 # search for the picture in attachments
187 if found = attachments.detect { |att| att.filename =~ rf }
187 if found = attachments.detect { |att| att.filename =~ rf }
188 image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id
188 image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id
189 "!#{style}#{image_url}!"
189 "!#{style}#{image_url}!"
190 else
190 else
191 "!#{style}#{filename}!"
191 "!#{style}#{filename}!"
192 end
192 end
193 end
193 end
194 end
194 end
195
195
196 text = (Setting.text_formatting == 'textile') ?
196 text = (Setting.text_formatting == 'textile') ?
197 Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } :
197 Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } :
198 simple_format(auto_link(h(text)))
198 simple_format(auto_link(h(text)))
199
199
200 # different methods for formatting wiki links
200 # different methods for formatting wiki links
201 case options[:wiki_links]
201 case options[:wiki_links]
202 when :local
202 when :local
203 # used for local links to html files
203 # used for local links to html files
204 format_wiki_link = Proc.new {|project, title| "#{title}.html" }
204 format_wiki_link = Proc.new {|project, title| "#{title}.html" }
205 when :anchor
205 when :anchor
206 # used for single-file wiki export
206 # used for single-file wiki export
207 format_wiki_link = Proc.new {|project, title| "##{title}" }
207 format_wiki_link = Proc.new {|project, title| "##{title}" }
208 else
208 else
209 format_wiki_link = Proc.new {|project, title| url_for :controller => 'wiki', :action => 'index', :id => project, :page => title }
209 format_wiki_link = Proc.new {|project, title| url_for :controller => 'wiki', :action => 'index', :id => project, :page => title }
210 end
210 end
211
211
212 project = options[:project] || @project
212 project = options[:project] || @project
213
213
214 # turn wiki links into html links
214 # turn wiki links into html links
215 # example:
215 # example:
216 # [[mypage]]
216 # [[mypage]]
217 # [[mypage|mytext]]
217 # [[mypage|mytext]]
218 # wiki links can refer other project wikis, using project name or identifier:
218 # wiki links can refer other project wikis, using project name or identifier:
219 # [[project:]] -> wiki starting page
219 # [[project:]] -> wiki starting page
220 # [[project:|mytext]]
220 # [[project:|mytext]]
221 # [[project:mypage]]
221 # [[project:mypage]]
222 # [[project:mypage|mytext]]
222 # [[project:mypage|mytext]]
223 text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) do |m|
223 text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) do |m|
224 link_project = project
224 link_project = project
225 page = $1
225 page = $1
226 title = $3
226 title = $3
227 if page =~ /^([^\:]+)\:(.*)$/
227 if page =~ /^([^\:]+)\:(.*)$/
228 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
228 link_project = Project.find_by_name($1) || Project.find_by_identifier($1)
229 page = title || $2
229 page = title || $2
230 title = $1 if page.blank?
230 title = $1 if page.blank?
231 end
231 end
232
232
233 if link_project && link_project.wiki
233 if link_project && link_project.wiki
234 # check if page exists
234 # check if page exists
235 wiki_page = link_project.wiki.find_page(page)
235 wiki_page = link_project.wiki.find_page(page)
236 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)),
236 link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)),
237 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
237 :class => ('wiki-page' + (wiki_page ? '' : ' new')))
238 else
238 else
239 # project or wiki doesn't exist
239 # project or wiki doesn't exist
240 title || page
240 title || page
241 end
241 end
242 end
242 end
243
243
244 # turn issue and revision ids into links
244 # turn issue and revision ids into links
245 # example:
245 # example:
246 # #52 -> <a href="/issues/show/52">#52</a>
246 # #52 -> <a href="/issues/show/52">#52</a>
247 # r52 -> <a href="/repositories/revision/6?rev=52">r52</a> (project.id is 6)
247 # r52 -> <a href="/repositories/revision/6?rev=52">r52</a> (project.id is 6)
248 text = text.gsub(%r{([\s\(,-^])(#|r)(\d+)(?=[[:punct:]]|\s|<|$)}) do |m|
248 text = text.gsub(%r{([\s\(,-^])(#|r)(\d+)(?=[[:punct:]]|\s|<|$)}) do |m|
249 leading, otype, oid = $1, $2, $3
249 leading, otype, oid = $1, $2, $3
250 link = nil
250 link = nil
251 if otype == 'r'
251 if otype == 'r'
252 if project && (changeset = project.changesets.find_by_revision(oid))
252 if project && (changeset = project.changesets.find_by_revision(oid))
253 link = link_to("r#{oid}", {:controller => 'repositories', :action => 'revision', :id => project.id, :rev => oid}, :class => 'changeset',
253 link = link_to("r#{oid}", {:controller => 'repositories', :action => 'revision', :id => project.id, :rev => oid}, :class => 'changeset',
254 :title => truncate(changeset.comments, 100))
254 :title => truncate(changeset.comments, 100))
255 end
255 end
256 else
256 else
257 if issue = Issue.find_by_id(oid.to_i, :include => [:project, :status], :conditions => Project.visible_by(User.current))
257 if issue = Issue.find_by_id(oid.to_i, :include => [:project, :status], :conditions => Project.visible_by(User.current))
258 link = link_to("##{oid}", {:controller => 'issues', :action => 'show', :id => oid}, :class => 'issue',
258 link = link_to("##{oid}", {:controller => 'issues', :action => 'show', :id => oid}, :class => 'issue',
259 :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
259 :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})")
260 link = content_tag('del', link) if issue.closed?
260 link = content_tag('del', link) if issue.closed?
261 end
261 end
262 end
262 end
263 leading + (link || "#{otype}#{oid}")
263 leading + (link || "#{otype}#{oid}")
264 end
264 end
265
265
266 text
266 text
267 end
267 end
268
268
269 # Same as Rails' simple_format helper without using paragraphs
269 # Same as Rails' simple_format helper without using paragraphs
270 def simple_format_without_paragraph(text)
270 def simple_format_without_paragraph(text)
271 text.to_s.
271 text.to_s.
272 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
272 gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
273 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
273 gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
274 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
274 gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br
275 end
275 end
276
276
277 def error_messages_for(object_name, options = {})
277 def error_messages_for(object_name, options = {})
278 options = options.symbolize_keys
278 options = options.symbolize_keys
279 object = instance_variable_get("@#{object_name}")
279 object = instance_variable_get("@#{object_name}")
280 if object && !object.errors.empty?
280 if object && !object.errors.empty?
281 # build full_messages here with controller current language
281 # build full_messages here with controller current language
282 full_messages = []
282 full_messages = []
283 object.errors.each do |attr, msg|
283 object.errors.each do |attr, msg|
284 next if msg.nil?
284 next if msg.nil?
285 msg = msg.first if msg.is_a? Array
285 msg = msg.first if msg.is_a? Array
286 if attr == "base"
286 if attr == "base"
287 full_messages << l(msg)
287 full_messages << l(msg)
288 else
288 else
289 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
289 full_messages << "&#171; " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " &#187; " + l(msg) unless attr == "custom_values"
290 end
290 end
291 end
291 end
292 # retrieve custom values error messages
292 # retrieve custom values error messages
293 if object.errors[:custom_values]
293 if object.errors[:custom_values]
294 object.custom_values.each do |v|
294 object.custom_values.each do |v|
295 v.errors.each do |attr, msg|
295 v.errors.each do |attr, msg|
296 next if msg.nil?
296 next if msg.nil?
297 msg = msg.first if msg.is_a? Array
297 msg = msg.first if msg.is_a? Array
298 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
298 full_messages << "&#171; " + v.custom_field.name + " &#187; " + l(msg)
299 end
299 end
300 end
300 end
301 end
301 end
302 content_tag("div",
302 content_tag("div",
303 content_tag(
303 content_tag(
304 options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
304 options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":"
305 ) +
305 ) +
306 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
306 content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }),
307 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
307 "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
308 )
308 )
309 else
309 else
310 ""
310 ""
311 end
311 end
312 end
312 end
313
313
314 def lang_options_for_select(blank=true)
314 def lang_options_for_select(blank=true)
315 (blank ? [["(auto)", ""]] : []) +
315 (blank ? [["(auto)", ""]] : []) +
316 GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
316 GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
317 end
317 end
318
318
319 def label_tag_for(name, option_tags = nil, options = {})
319 def label_tag_for(name, option_tags = nil, options = {})
320 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
320 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
321 content_tag("label", label_text)
321 content_tag("label", label_text)
322 end
322 end
323
323
324 def labelled_tabular_form_for(name, object, options, &proc)
324 def labelled_tabular_form_for(name, object, options, &proc)
325 options[:html] ||= {}
325 options[:html] ||= {}
326 options[:html].store :class, "tabular"
326 options[:html].store :class, "tabular"
327 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
327 form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc)
328 end
328 end
329
329
330 def check_all_links(form_name)
330 def check_all_links(form_name)
331 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
331 link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
332 " | " +
332 " | " +
333 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
333 link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
334 end
334 end
335
335
336 def progress_bar(pcts, options={})
336 def progress_bar(pcts, options={})
337 pcts = [pcts, pcts] unless pcts.is_a?(Array)
337 pcts = [pcts, pcts] unless pcts.is_a?(Array)
338 pcts[1] = pcts[1] - pcts[0]
338 pcts[1] = pcts[1] - pcts[0]
339 pcts << (100 - pcts[1] - pcts[0])
339 pcts << (100 - pcts[1] - pcts[0])
340 width = options[:width] || '100px;'
340 width = options[:width] || '100px;'
341 legend = options[:legend] || ''
341 legend = options[:legend] || ''
342 content_tag('table',
342 content_tag('table',
343 content_tag('tr',
343 content_tag('tr',
344 (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
344 (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') +
345 (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
345 (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') +
346 (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
346 (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '')
347 ), :class => 'progress', :style => "width: #{width};") +
347 ), :class => 'progress', :style => "width: #{width};") +
348 content_tag('p', legend, :class => 'pourcent')
348 content_tag('p', legend, :class => 'pourcent')
349 end
349 end
350
350
351 def context_menu_link(name, url, options={})
351 def context_menu_link(name, url, options={})
352 options[:class] ||= ''
352 options[:class] ||= ''
353 if options.delete(:selected)
353 if options.delete(:selected)
354 options[:class] << ' icon-checked disabled'
354 options[:class] << ' icon-checked disabled'
355 options[:disabled] = true
355 options[:disabled] = true
356 end
356 end
357 if options.delete(:disabled)
357 if options.delete(:disabled)
358 options.delete(:method)
358 options.delete(:method)
359 options.delete(:confirm)
359 options.delete(:confirm)
360 options.delete(:onclick)
360 options.delete(:onclick)
361 options[:class] << ' disabled'
361 options[:class] << ' disabled'
362 url = '#'
362 url = '#'
363 end
363 end
364 link_to name, url, options
364 link_to name, url, options
365 end
365 end
366
366
367 def calendar_for(field_id)
367 def calendar_for(field_id)
368 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
368 image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
369 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
369 javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
370 end
370 end
371
371
372 def wikitoolbar_for(field_id)
372 def wikitoolbar_for(field_id)
373 return '' unless Setting.text_formatting == 'textile'
373 return '' unless Setting.text_formatting == 'textile'
374 javascript_include_tag('jstoolbar') + javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.draw();")
374 javascript_include_tag('jstoolbar') + javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.draw();")
375 end
375 end
376
376
377 def content_for(name, content = nil, &block)
377 def content_for(name, content = nil, &block)
378 @has_content ||= {}
378 @has_content ||= {}
379 @has_content[name] = true
379 @has_content[name] = true
380 super(name, content, &block)
380 super(name, content, &block)
381 end
381 end
382
382
383 def has_content?(name)
383 def has_content?(name)
384 (@has_content && @has_content[name]) || false
384 (@has_content && @has_content[name]) || false
385 end
385 end
386 end
386 end
387
388 class TabularFormBuilder < ActionView::Helpers::FormBuilder
389 include GLoc
390
391 def initialize(object_name, object, template, options, proc)
392 set_language_if_valid options.delete(:lang)
393 @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc
394 end
395
396 (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector|
397 src = <<-END_SRC
398 def #{selector}(field, options = {})
399 return super if options.delete :no_label
400 label_text = l(options[:label]) if options[:label]
401 label_text ||= l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym)
402 label_text << @template.content_tag("span", " *", :class => "required") if options.delete(:required)
403 label = @template.content_tag("label", label_text,
404 :class => (@object && @object.errors[field] ? "error" : nil),
405 :for => (@object_name.to_s + "_" + field.to_s))
406 label + super
407 end
408 END_SRC
409 class_eval src, __FILE__, __LINE__
410 end
411
412 def select(field, choices, options = {}, html_options = {})
413 label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
414 label = @template.content_tag("label", label_text,
415 :class => (@object && @object.errors[field] ? "error" : nil),
416 :for => (@object_name.to_s + "_" + field.to_s))
417 label + super
418 end
419
420 end
421
General Comments 0
You need to be logged in to leave comments. Login now