@@ -1,443 +1,448 | |||||
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 | def link_to_signin |
|
35 | def link_to_signin | |
36 | link_to l(:label_login), { :controller => 'account', :action => 'login' }, :class => 'signin' |
|
36 | link_to l(:label_login), { :controller => 'account', :action => 'login' }, :class => 'signin' | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | def link_to_signout |
|
39 | def link_to_signout | |
40 | link_to l(:label_logout), { :controller => 'account', :action => 'logout' }, :class => 'logout' |
|
40 | link_to l(:label_logout), { :controller => 'account', :action => 'logout' }, :class => 'logout' | |
41 | end |
|
41 | end | |
42 |
|
42 | |||
43 | # Display a link to user's account page |
|
43 | # Display a link to user's account page | |
44 | def link_to_user(user) |
|
44 | def link_to_user(user) | |
45 | user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous' |
|
45 | user ? link_to(user, :controller => 'account', :action => 'show', :id => user) : 'Anonymous' | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def link_to_issue(issue) |
|
48 | def link_to_issue(issue) | |
49 | link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue |
|
49 | link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def toggle_link(name, id, options={}) |
|
52 | def toggle_link(name, id, options={}) | |
53 | onclick = "Element.toggle('#{id}'); " |
|
53 | onclick = "Element.toggle('#{id}'); " | |
54 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") |
|
54 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") | |
55 | onclick << "return false;" |
|
55 | onclick << "return false;" | |
56 | link_to(name, "#", :onclick => onclick) |
|
56 | link_to(name, "#", :onclick => onclick) | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def show_and_goto_link(name, id, options={}) |
|
59 | def show_and_goto_link(name, id, options={}) | |
60 | onclick = "Element.show('#{id}'); " |
|
60 | onclick = "Element.show('#{id}'); " | |
61 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") |
|
61 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") | |
62 | onclick << "location.href='##{id}-anchor'; " |
|
62 | onclick << "location.href='##{id}-anchor'; " | |
63 | onclick << "return false;" |
|
63 | onclick << "return false;" | |
64 | link_to(name, "#", options.merge(:onclick => onclick)) |
|
64 | link_to(name, "#", options.merge(:onclick => onclick)) | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | def image_to_function(name, function, html_options = {}) |
|
67 | def image_to_function(name, function, html_options = {}) | |
68 | html_options.symbolize_keys! |
|
68 | html_options.symbolize_keys! | |
69 | tag(:input, html_options.merge({ |
|
69 | tag(:input, html_options.merge({ | |
70 | :type => "image", :src => image_path(name), |
|
70 | :type => "image", :src => image_path(name), | |
71 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" |
|
71 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" | |
72 | })) |
|
72 | })) | |
73 | end |
|
73 | end | |
74 |
|
74 | |||
75 | def prompt_to_remote(name, text, param, url, html_options = {}) |
|
75 | def prompt_to_remote(name, text, param, url, html_options = {}) | |
76 | html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;" |
|
76 | html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;" | |
77 | link_to name, {}, html_options |
|
77 | link_to name, {}, html_options | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | def format_date(date) |
|
80 | def format_date(date) | |
81 | return nil unless date |
|
81 | return nil unless date | |
82 | # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed) |
|
82 | # "Setting.date_format.size < 2" is a temporary fix (content of date_format setting changed) | |
83 | @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format) |
|
83 | @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format) | |
84 | date.strftime(@date_format) |
|
84 | date.strftime(@date_format) | |
85 | end |
|
85 | end | |
86 |
|
86 | |||
87 | def format_time(time, include_date = true) |
|
87 | def format_time(time, include_date = true) | |
88 | return nil unless time |
|
88 | return nil unless time | |
89 | time = time.to_time if time.is_a?(String) |
|
89 | time = time.to_time if time.is_a?(String) | |
90 | zone = User.current.time_zone |
|
90 | zone = User.current.time_zone | |
91 | if time.utc? |
|
91 | if time.utc? | |
92 | local = zone ? zone.adjust(time) : time.getlocal |
|
92 | local = zone ? zone.adjust(time) : time.getlocal | |
93 | else |
|
93 | else | |
94 | local = zone ? zone.adjust(time.getutc) : time |
|
94 | local = zone ? zone.adjust(time.getutc) : time | |
95 | end |
|
95 | end | |
96 | @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format) |
|
96 | @date_format ||= (Setting.date_format.blank? || Setting.date_format.size < 2 ? l(:general_fmt_date) : Setting.date_format) | |
97 | @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format) |
|
97 | @time_format ||= (Setting.time_format.blank? ? l(:general_fmt_time) : Setting.time_format) | |
98 | include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format) |
|
98 | include_date ? local.strftime("#{@date_format} #{@time_format}") : local.strftime(@time_format) | |
99 | end |
|
99 | end | |
100 |
|
100 | |||
101 | def authoring(created, author) |
|
101 | def authoring(created, author) | |
102 | time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created)) |
|
102 | time_tag = content_tag('acronym', distance_of_time_in_words(Time.now, created), :title => format_time(created)) | |
103 | l(:label_added_time_by, author || 'Anonymous', time_tag) |
|
103 | l(:label_added_time_by, author || 'Anonymous', time_tag) | |
104 | end |
|
104 | end | |
105 |
|
105 | |||
106 | def day_name(day) |
|
106 | def day_name(day) | |
107 | l(:general_day_names).split(',')[day-1] |
|
107 | l(:general_day_names).split(',')[day-1] | |
108 | end |
|
108 | end | |
109 |
|
109 | |||
110 | def month_name(month) |
|
110 | def month_name(month) | |
111 | l(:actionview_datehelper_select_month_names).split(',')[month-1] |
|
111 | l(:actionview_datehelper_select_month_names).split(',')[month-1] | |
112 | end |
|
112 | end | |
113 |
|
113 | |||
114 | def pagination_links_full(paginator, count=nil, options={}) |
|
114 | def pagination_links_full(paginator, count=nil, options={}) | |
115 | page_param = options.delete(:page_param) || :page |
|
115 | page_param = options.delete(:page_param) || :page | |
116 | url_param = params.dup |
|
116 | url_param = params.dup | |
|
117 | # don't reuse params if filters are present | |||
|
118 | url_param.clear if url_param.has_key?(:set_filter) | |||
117 |
|
119 | |||
118 | html = '' |
|
120 | html = '' | |
119 | html << link_to_remote(('« ' + l(:label_previous)), |
|
121 | html << link_to_remote(('« ' + l(:label_previous)), | |
120 | {:update => "content", :url => url_param.merge(page_param => paginator.current.previous)}, |
|
122 | {:update => "content", :url => url_param.merge(page_param => paginator.current.previous)}, | |
121 | {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous |
|
123 | {:href => url_for(:params => url_param.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous | |
122 |
|
124 | |||
123 | html << (pagination_links_each(paginator, options) do |n| |
|
125 | html << (pagination_links_each(paginator, options) do |n| | |
124 | link_to_remote(n.to_s, |
|
126 | link_to_remote(n.to_s, | |
125 | {:url => {:params => url_param.merge(page_param => n)}, :update => 'content'}, |
|
127 | {:url => {:params => url_param.merge(page_param => n)}, :update => 'content'}, | |
126 | {:href => url_for(:params => url_param.merge(page_param => n))}) |
|
128 | {:href => url_for(:params => url_param.merge(page_param => n))}) | |
127 | end || '') |
|
129 | end || '') | |
128 |
|
130 | |||
129 | html << ' ' + link_to_remote((l(:label_next) + ' »'), |
|
131 | html << ' ' + link_to_remote((l(:label_next) + ' »'), | |
130 | {:update => "content", :url => url_param.merge(page_param => paginator.current.next)}, |
|
132 | {:update => "content", :url => url_param.merge(page_param => paginator.current.next)}, | |
131 | {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next |
|
133 | {:href => url_for(:params => url_param.merge(page_param => paginator.current.next))}) if paginator.current.next | |
132 |
|
134 | |||
133 | unless count.nil? |
|
135 | unless count.nil? | |
134 | html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ') |
|
136 | html << [" (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})", per_page_links(paginator.items_per_page)].compact.join(' | ') | |
135 | end |
|
137 | end | |
136 |
|
138 | |||
137 | html |
|
139 | html | |
138 | end |
|
140 | end | |
139 |
|
141 | |||
140 | def per_page_links(selected=nil) |
|
142 | def per_page_links(selected=nil) | |
|
143 | url_param = params.dup | |||
|
144 | url_param.clear if url_param.has_key?(:set_filter) | |||
|
145 | ||||
141 | links = Setting.per_page_options_array.collect do |n| |
|
146 | links = Setting.per_page_options_array.collect do |n| | |
142 | n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)}, |
|
147 | n == selected ? n : link_to_remote(n, {:update => "content", :url => params.dup.merge(:per_page => n)}, | |
143 |
{:href => url_for(param |
|
148 | {:href => url_for(url_param.merge(:per_page => n))}) | |
144 | end |
|
149 | end | |
145 | links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil |
|
150 | links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil | |
146 | end |
|
151 | end | |
147 |
|
152 | |||
148 | def html_title(*args) |
|
153 | def html_title(*args) | |
149 | if args.empty? |
|
154 | if args.empty? | |
150 | title = [] |
|
155 | title = [] | |
151 | title << @project.name if @project |
|
156 | title << @project.name if @project | |
152 | title += @html_title if @html_title |
|
157 | title += @html_title if @html_title | |
153 | title << Setting.app_title |
|
158 | title << Setting.app_title | |
154 | title.compact.join(' - ') |
|
159 | title.compact.join(' - ') | |
155 | else |
|
160 | else | |
156 | @html_title ||= [] |
|
161 | @html_title ||= [] | |
157 | @html_title += args |
|
162 | @html_title += args | |
158 | end |
|
163 | end | |
159 | end |
|
164 | end | |
160 |
|
165 | |||
161 | ACCESSKEYS = {:edit => 'e', |
|
166 | ACCESSKEYS = {:edit => 'e', | |
162 | :preview => 'r', |
|
167 | :preview => 'r', | |
163 | :quick_search => 'f', |
|
168 | :quick_search => 'f', | |
164 | :search => '4', |
|
169 | :search => '4', | |
165 | }.freeze unless const_defined?(:ACCESSKEYS) |
|
170 | }.freeze unless const_defined?(:ACCESSKEYS) | |
166 |
|
171 | |||
167 | def accesskey(s) |
|
172 | def accesskey(s) | |
168 | ACCESSKEYS[s] |
|
173 | ACCESSKEYS[s] | |
169 | end |
|
174 | end | |
170 |
|
175 | |||
171 | # Formats text according to system settings. |
|
176 | # Formats text according to system settings. | |
172 | # 2 ways to call this method: |
|
177 | # 2 ways to call this method: | |
173 | # * with a String: textilizable(text, options) |
|
178 | # * with a String: textilizable(text, options) | |
174 | # * with an object and one of its attribute: textilizable(issue, :description, options) |
|
179 | # * with an object and one of its attribute: textilizable(issue, :description, options) | |
175 | def textilizable(*args) |
|
180 | def textilizable(*args) | |
176 | options = args.last.is_a?(Hash) ? args.pop : {} |
|
181 | options = args.last.is_a?(Hash) ? args.pop : {} | |
177 | case args.size |
|
182 | case args.size | |
178 | when 1 |
|
183 | when 1 | |
179 | obj = nil |
|
184 | obj = nil | |
180 | text = args.shift || '' |
|
185 | text = args.shift || '' | |
181 | when 2 |
|
186 | when 2 | |
182 | obj = args.shift |
|
187 | obj = args.shift | |
183 | text = obj.send(args.shift) |
|
188 | text = obj.send(args.shift) | |
184 | else |
|
189 | else | |
185 | raise ArgumentError, 'invalid arguments to textilizable' |
|
190 | raise ArgumentError, 'invalid arguments to textilizable' | |
186 | end |
|
191 | end | |
187 |
|
192 | |||
188 | # when using an image link, try to use an attachment, if possible |
|
193 | # when using an image link, try to use an attachment, if possible | |
189 | attachments = options[:attachments] |
|
194 | attachments = options[:attachments] | |
190 | if attachments |
|
195 | if attachments | |
191 | text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m| |
|
196 | text = text.gsub(/!((\<|\=|\>)?(\([^\)]+\))?(\[[^\]]+\])?(\{[^\}]+\})?)(\S+\.(gif|jpg|jpeg|png))!/) do |m| | |
192 | style = $1 |
|
197 | style = $1 | |
193 | filename = $6 |
|
198 | filename = $6 | |
194 | rf = Regexp.new(filename, Regexp::IGNORECASE) |
|
199 | rf = Regexp.new(filename, Regexp::IGNORECASE) | |
195 | # search for the picture in attachments |
|
200 | # search for the picture in attachments | |
196 | if found = attachments.detect { |att| att.filename =~ rf } |
|
201 | if found = attachments.detect { |att| att.filename =~ rf } | |
197 | image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id |
|
202 | image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id | |
198 | "!#{style}#{image_url}!" |
|
203 | "!#{style}#{image_url}!" | |
199 | else |
|
204 | else | |
200 | "!#{style}#{filename}!" |
|
205 | "!#{style}#{filename}!" | |
201 | end |
|
206 | end | |
202 | end |
|
207 | end | |
203 | end |
|
208 | end | |
204 |
|
209 | |||
205 | text = (Setting.text_formatting == 'textile') ? |
|
210 | text = (Setting.text_formatting == 'textile') ? | |
206 | Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } : |
|
211 | Redmine::WikiFormatting.to_html(text) { |macro, args| exec_macro(macro, obj, args) } : | |
207 | simple_format(auto_link(h(text))) |
|
212 | simple_format(auto_link(h(text))) | |
208 |
|
213 | |||
209 | # different methods for formatting wiki links |
|
214 | # different methods for formatting wiki links | |
210 | case options[:wiki_links] |
|
215 | case options[:wiki_links] | |
211 | when :local |
|
216 | when :local | |
212 | # used for local links to html files |
|
217 | # used for local links to html files | |
213 | format_wiki_link = Proc.new {|project, title| "#{title}.html" } |
|
218 | format_wiki_link = Proc.new {|project, title| "#{title}.html" } | |
214 | when :anchor |
|
219 | when :anchor | |
215 | # used for single-file wiki export |
|
220 | # used for single-file wiki export | |
216 | format_wiki_link = Proc.new {|project, title| "##{title}" } |
|
221 | format_wiki_link = Proc.new {|project, title| "##{title}" } | |
217 | else |
|
222 | else | |
218 | format_wiki_link = Proc.new {|project, title| url_for :controller => 'wiki', :action => 'index', :id => project, :page => title } |
|
223 | format_wiki_link = Proc.new {|project, title| url_for :controller => 'wiki', :action => 'index', :id => project, :page => title } | |
219 | end |
|
224 | end | |
220 |
|
225 | |||
221 | project = options[:project] || @project |
|
226 | project = options[:project] || @project | |
222 |
|
227 | |||
223 | # Wiki links |
|
228 | # Wiki links | |
224 | # |
|
229 | # | |
225 | # Examples: |
|
230 | # Examples: | |
226 | # [[mypage]] |
|
231 | # [[mypage]] | |
227 | # [[mypage|mytext]] |
|
232 | # [[mypage|mytext]] | |
228 | # wiki links can refer other project wikis, using project name or identifier: |
|
233 | # wiki links can refer other project wikis, using project name or identifier: | |
229 | # [[project:]] -> wiki starting page |
|
234 | # [[project:]] -> wiki starting page | |
230 | # [[project:|mytext]] |
|
235 | # [[project:|mytext]] | |
231 | # [[project:mypage]] |
|
236 | # [[project:mypage]] | |
232 | # [[project:mypage|mytext]] |
|
237 | # [[project:mypage|mytext]] | |
233 | text = text.gsub(/(!)?(\[\[([^\]\|]+)(\|([^\]\|]+))?\]\])/) do |m| |
|
238 | text = text.gsub(/(!)?(\[\[([^\]\|]+)(\|([^\]\|]+))?\]\])/) do |m| | |
234 | link_project = project |
|
239 | link_project = project | |
235 | esc, all, page, title = $1, $2, $3, $5 |
|
240 | esc, all, page, title = $1, $2, $3, $5 | |
236 | if esc.nil? |
|
241 | if esc.nil? | |
237 | if page =~ /^([^\:]+)\:(.*)$/ |
|
242 | if page =~ /^([^\:]+)\:(.*)$/ | |
238 | link_project = Project.find_by_name($1) || Project.find_by_identifier($1) |
|
243 | link_project = Project.find_by_name($1) || Project.find_by_identifier($1) | |
239 | page = $2 |
|
244 | page = $2 | |
240 | title ||= $1 if page.blank? |
|
245 | title ||= $1 if page.blank? | |
241 | end |
|
246 | end | |
242 |
|
247 | |||
243 | if link_project && link_project.wiki |
|
248 | if link_project && link_project.wiki | |
244 | # check if page exists |
|
249 | # check if page exists | |
245 | wiki_page = link_project.wiki.find_page(page) |
|
250 | wiki_page = link_project.wiki.find_page(page) | |
246 | link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)), |
|
251 | link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)), | |
247 | :class => ('wiki-page' + (wiki_page ? '' : ' new'))) |
|
252 | :class => ('wiki-page' + (wiki_page ? '' : ' new'))) | |
248 | else |
|
253 | else | |
249 | # project or wiki doesn't exist |
|
254 | # project or wiki doesn't exist | |
250 | title || page |
|
255 | title || page | |
251 | end |
|
256 | end | |
252 | else |
|
257 | else | |
253 | all |
|
258 | all | |
254 | end |
|
259 | end | |
255 | end |
|
260 | end | |
256 |
|
261 | |||
257 | # Redmine links |
|
262 | # Redmine links | |
258 | # |
|
263 | # | |
259 | # Examples: |
|
264 | # Examples: | |
260 | # Issues: |
|
265 | # Issues: | |
261 | # #52 -> Link to issue #52 |
|
266 | # #52 -> Link to issue #52 | |
262 | # Changesets: |
|
267 | # Changesets: | |
263 | # r52 -> Link to revision 52 |
|
268 | # r52 -> Link to revision 52 | |
264 | # Documents: |
|
269 | # Documents: | |
265 | # document#17 -> Link to document with id 17 |
|
270 | # document#17 -> Link to document with id 17 | |
266 | # document:Greetings -> Link to the document with title "Greetings" |
|
271 | # document:Greetings -> Link to the document with title "Greetings" | |
267 | # document:"Some document" -> Link to the document with title "Some document" |
|
272 | # document:"Some document" -> Link to the document with title "Some document" | |
268 | # Versions: |
|
273 | # Versions: | |
269 | # version#3 -> Link to version with id 3 |
|
274 | # version#3 -> Link to version with id 3 | |
270 | # version:1.0.0 -> Link to version named "1.0.0" |
|
275 | # version:1.0.0 -> Link to version named "1.0.0" | |
271 | # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" |
|
276 | # version:"1.0 beta 2" -> Link to version named "1.0 beta 2" | |
272 | # Attachments: |
|
277 | # Attachments: | |
273 | # attachment:file.zip -> Link to the attachment of the current object named file.zip |
|
278 | # attachment:file.zip -> Link to the attachment of the current object named file.zip | |
274 | text = text.gsub(%r{([\s\(,-^])(!)?(attachment|document|version)?((#|r)(\d+)|(:)([^"][^\s<>]+|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m| |
|
279 | text = text.gsub(%r{([\s\(,-^])(!)?(attachment|document|version)?((#|r)(\d+)|(:)([^"][^\s<>]+|"[^"]+"))(?=[[:punct:]]|\s|<|$)}) do |m| | |
275 | leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8 |
|
280 | leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8 | |
276 | link = nil |
|
281 | link = nil | |
277 | if esc.nil? |
|
282 | if esc.nil? | |
278 | if prefix.nil? && sep == 'r' |
|
283 | if prefix.nil? && sep == 'r' | |
279 | if project && (changeset = project.changesets.find_by_revision(oid)) |
|
284 | if project && (changeset = project.changesets.find_by_revision(oid)) | |
280 | link = link_to("r#{oid}", {:controller => 'repositories', :action => 'revision', :id => project.id, :rev => oid}, :class => 'changeset', |
|
285 | link = link_to("r#{oid}", {:controller => 'repositories', :action => 'revision', :id => project.id, :rev => oid}, :class => 'changeset', | |
281 | :title => truncate(changeset.comments, 100)) |
|
286 | :title => truncate(changeset.comments, 100)) | |
282 | end |
|
287 | end | |
283 | elsif sep == '#' |
|
288 | elsif sep == '#' | |
284 | oid = oid.to_i |
|
289 | oid = oid.to_i | |
285 | case prefix |
|
290 | case prefix | |
286 | when nil |
|
291 | when nil | |
287 | if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current)) |
|
292 | if issue = Issue.find_by_id(oid, :include => [:project, :status], :conditions => Project.visible_by(User.current)) | |
288 | link = link_to("##{oid}", {:controller => 'issues', :action => 'show', :id => oid}, :class => 'issue', |
|
293 | link = link_to("##{oid}", {:controller => 'issues', :action => 'show', :id => oid}, :class => 'issue', | |
289 | :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})") |
|
294 | :title => "#{truncate(issue.subject, 100)} (#{issue.status.name})") | |
290 | link = content_tag('del', link) if issue.closed? |
|
295 | link = content_tag('del', link) if issue.closed? | |
291 | end |
|
296 | end | |
292 | when 'document' |
|
297 | when 'document' | |
293 | if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) |
|
298 | if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) | |
294 | link = link_to h(document.title), {:controller => 'documents', :action => 'show', :id => document}, :class => 'document' |
|
299 | link = link_to h(document.title), {:controller => 'documents', :action => 'show', :id => document}, :class => 'document' | |
295 | end |
|
300 | end | |
296 | when 'version' |
|
301 | when 'version' | |
297 | if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) |
|
302 | if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current)) | |
298 | link = link_to h(version.name), {:controller => 'versions', :action => 'show', :id => version}, :class => 'version' |
|
303 | link = link_to h(version.name), {:controller => 'versions', :action => 'show', :id => version}, :class => 'version' | |
299 | end |
|
304 | end | |
300 | end |
|
305 | end | |
301 | elsif sep == ':' |
|
306 | elsif sep == ':' | |
302 | # removes the double quotes if any |
|
307 | # removes the double quotes if any | |
303 | name = oid.gsub(%r{^"(.*)"$}, "\\1") |
|
308 | name = oid.gsub(%r{^"(.*)"$}, "\\1") | |
304 | case prefix |
|
309 | case prefix | |
305 | when 'document' |
|
310 | when 'document' | |
306 | if project && document = project.documents.find_by_title(name) |
|
311 | if project && document = project.documents.find_by_title(name) | |
307 | link = link_to h(document.title), {:controller => 'documents', :action => 'show', :id => document}, :class => 'document' |
|
312 | link = link_to h(document.title), {:controller => 'documents', :action => 'show', :id => document}, :class => 'document' | |
308 | end |
|
313 | end | |
309 | when 'version' |
|
314 | when 'version' | |
310 | if project && version = project.versions.find_by_name(name) |
|
315 | if project && version = project.versions.find_by_name(name) | |
311 | link = link_to h(version.name), {:controller => 'versions', :action => 'show', :id => version}, :class => 'version' |
|
316 | link = link_to h(version.name), {:controller => 'versions', :action => 'show', :id => version}, :class => 'version' | |
312 | end |
|
317 | end | |
313 | when 'attachment' |
|
318 | when 'attachment' | |
314 | if attachments && attachment = attachments.detect {|a| a.filename == name } |
|
319 | if attachments && attachment = attachments.detect {|a| a.filename == name } | |
315 | link = link_to h(attachment.filename), {:controller => 'attachments', :action => 'download', :id => attachment}, :class => 'attachment' |
|
320 | link = link_to h(attachment.filename), {:controller => 'attachments', :action => 'download', :id => attachment}, :class => 'attachment' | |
316 | end |
|
321 | end | |
317 | end |
|
322 | end | |
318 | end |
|
323 | end | |
319 | end |
|
324 | end | |
320 | leading + (link || "#{prefix}#{sep}#{oid}") |
|
325 | leading + (link || "#{prefix}#{sep}#{oid}") | |
321 | end |
|
326 | end | |
322 |
|
327 | |||
323 | text |
|
328 | text | |
324 | end |
|
329 | end | |
325 |
|
330 | |||
326 | # Same as Rails' simple_format helper without using paragraphs |
|
331 | # Same as Rails' simple_format helper without using paragraphs | |
327 | def simple_format_without_paragraph(text) |
|
332 | def simple_format_without_paragraph(text) | |
328 | text.to_s. |
|
333 | text.to_s. | |
329 | gsub(/\r\n?/, "\n"). # \r\n and \r -> \n |
|
334 | gsub(/\r\n?/, "\n"). # \r\n and \r -> \n | |
330 | gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br |
|
335 | gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br | |
331 | gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br |
|
336 | gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br | |
332 | end |
|
337 | end | |
333 |
|
338 | |||
334 | def error_messages_for(object_name, options = {}) |
|
339 | def error_messages_for(object_name, options = {}) | |
335 | options = options.symbolize_keys |
|
340 | options = options.symbolize_keys | |
336 | object = instance_variable_get("@#{object_name}") |
|
341 | object = instance_variable_get("@#{object_name}") | |
337 | if object && !object.errors.empty? |
|
342 | if object && !object.errors.empty? | |
338 | # build full_messages here with controller current language |
|
343 | # build full_messages here with controller current language | |
339 | full_messages = [] |
|
344 | full_messages = [] | |
340 | object.errors.each do |attr, msg| |
|
345 | object.errors.each do |attr, msg| | |
341 | next if msg.nil? |
|
346 | next if msg.nil? | |
342 | msg = msg.first if msg.is_a? Array |
|
347 | msg = msg.first if msg.is_a? Array | |
343 | if attr == "base" |
|
348 | if attr == "base" | |
344 | full_messages << l(msg) |
|
349 | full_messages << l(msg) | |
345 | else |
|
350 | else | |
346 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" |
|
351 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" | |
347 | end |
|
352 | end | |
348 | end |
|
353 | end | |
349 | # retrieve custom values error messages |
|
354 | # retrieve custom values error messages | |
350 | if object.errors[:custom_values] |
|
355 | if object.errors[:custom_values] | |
351 | object.custom_values.each do |v| |
|
356 | object.custom_values.each do |v| | |
352 | v.errors.each do |attr, msg| |
|
357 | v.errors.each do |attr, msg| | |
353 | next if msg.nil? |
|
358 | next if msg.nil? | |
354 | msg = msg.first if msg.is_a? Array |
|
359 | msg = msg.first if msg.is_a? Array | |
355 | full_messages << "« " + v.custom_field.name + " » " + l(msg) |
|
360 | full_messages << "« " + v.custom_field.name + " » " + l(msg) | |
356 | end |
|
361 | end | |
357 | end |
|
362 | end | |
358 | end |
|
363 | end | |
359 | content_tag("div", |
|
364 | content_tag("div", | |
360 | content_tag( |
|
365 | content_tag( | |
361 | options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":" |
|
366 | options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":" | |
362 | ) + |
|
367 | ) + | |
363 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), |
|
368 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), | |
364 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" |
|
369 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" | |
365 | ) |
|
370 | ) | |
366 | else |
|
371 | else | |
367 | "" |
|
372 | "" | |
368 | end |
|
373 | end | |
369 | end |
|
374 | end | |
370 |
|
375 | |||
371 | def lang_options_for_select(blank=true) |
|
376 | def lang_options_for_select(blank=true) | |
372 | (blank ? [["(auto)", ""]] : []) + |
|
377 | (blank ? [["(auto)", ""]] : []) + | |
373 | GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last } |
|
378 | GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last } | |
374 | end |
|
379 | end | |
375 |
|
380 | |||
376 | def label_tag_for(name, option_tags = nil, options = {}) |
|
381 | def label_tag_for(name, option_tags = nil, options = {}) | |
377 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
382 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") | |
378 | content_tag("label", label_text) |
|
383 | content_tag("label", label_text) | |
379 | end |
|
384 | end | |
380 |
|
385 | |||
381 | def labelled_tabular_form_for(name, object, options, &proc) |
|
386 | def labelled_tabular_form_for(name, object, options, &proc) | |
382 | options[:html] ||= {} |
|
387 | options[:html] ||= {} | |
383 | options[:html].store :class, "tabular" |
|
388 | options[:html].store :class, "tabular" | |
384 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) |
|
389 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) | |
385 | end |
|
390 | end | |
386 |
|
391 | |||
387 | def check_all_links(form_name) |
|
392 | def check_all_links(form_name) | |
388 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + |
|
393 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + | |
389 | " | " + |
|
394 | " | " + | |
390 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") |
|
395 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") | |
391 | end |
|
396 | end | |
392 |
|
397 | |||
393 | def progress_bar(pcts, options={}) |
|
398 | def progress_bar(pcts, options={}) | |
394 | pcts = [pcts, pcts] unless pcts.is_a?(Array) |
|
399 | pcts = [pcts, pcts] unless pcts.is_a?(Array) | |
395 | pcts[1] = pcts[1] - pcts[0] |
|
400 | pcts[1] = pcts[1] - pcts[0] | |
396 | pcts << (100 - pcts[1] - pcts[0]) |
|
401 | pcts << (100 - pcts[1] - pcts[0]) | |
397 | width = options[:width] || '100px;' |
|
402 | width = options[:width] || '100px;' | |
398 | legend = options[:legend] || '' |
|
403 | legend = options[:legend] || '' | |
399 | content_tag('table', |
|
404 | content_tag('table', | |
400 | content_tag('tr', |
|
405 | content_tag('tr', | |
401 | (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') + |
|
406 | (pcts[0] > 0 ? content_tag('td', '', :width => "#{pcts[0].floor}%;", :class => 'closed') : '') + | |
402 | (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') + |
|
407 | (pcts[1] > 0 ? content_tag('td', '', :width => "#{pcts[1].floor}%;", :class => 'done') : '') + | |
403 | (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '') |
|
408 | (pcts[2] > 0 ? content_tag('td', '', :width => "#{pcts[2].floor}%;", :class => 'todo') : '') | |
404 | ), :class => 'progress', :style => "width: #{width};") + |
|
409 | ), :class => 'progress', :style => "width: #{width};") + | |
405 | content_tag('p', legend, :class => 'pourcent') |
|
410 | content_tag('p', legend, :class => 'pourcent') | |
406 | end |
|
411 | end | |
407 |
|
412 | |||
408 | def context_menu_link(name, url, options={}) |
|
413 | def context_menu_link(name, url, options={}) | |
409 | options[:class] ||= '' |
|
414 | options[:class] ||= '' | |
410 | if options.delete(:selected) |
|
415 | if options.delete(:selected) | |
411 | options[:class] << ' icon-checked disabled' |
|
416 | options[:class] << ' icon-checked disabled' | |
412 | options[:disabled] = true |
|
417 | options[:disabled] = true | |
413 | end |
|
418 | end | |
414 | if options.delete(:disabled) |
|
419 | if options.delete(:disabled) | |
415 | options.delete(:method) |
|
420 | options.delete(:method) | |
416 | options.delete(:confirm) |
|
421 | options.delete(:confirm) | |
417 | options.delete(:onclick) |
|
422 | options.delete(:onclick) | |
418 | options[:class] << ' disabled' |
|
423 | options[:class] << ' disabled' | |
419 | url = '#' |
|
424 | url = '#' | |
420 | end |
|
425 | end | |
421 | link_to name, url, options |
|
426 | link_to name, url, options | |
422 | end |
|
427 | end | |
423 |
|
428 | |||
424 | def calendar_for(field_id) |
|
429 | def calendar_for(field_id) | |
425 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + |
|
430 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + | |
426 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") |
|
431 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") | |
427 | end |
|
432 | end | |
428 |
|
433 | |||
429 | def wikitoolbar_for(field_id) |
|
434 | def wikitoolbar_for(field_id) | |
430 | return '' unless Setting.text_formatting == 'textile' |
|
435 | return '' unless Setting.text_formatting == 'textile' | |
431 | javascript_include_tag('jstoolbar') + javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.draw();") |
|
436 | javascript_include_tag('jstoolbar') + javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.draw();") | |
432 | end |
|
437 | end | |
433 |
|
438 | |||
434 | def content_for(name, content = nil, &block) |
|
439 | def content_for(name, content = nil, &block) | |
435 | @has_content ||= {} |
|
440 | @has_content ||= {} | |
436 | @has_content[name] = true |
|
441 | @has_content[name] = true | |
437 | super(name, content, &block) |
|
442 | super(name, content, &block) | |
438 | end |
|
443 | end | |
439 |
|
444 | |||
440 | def has_content?(name) |
|
445 | def has_content?(name) | |
441 | (@has_content && @has_content[name]) || false |
|
446 | (@has_content && @has_content[name]) || false | |
442 | end |
|
447 | end | |
443 | end |
|
448 | end |
General Comments 0
You need to be logged in to leave comments.
Login now