@@ -1,294 +1,294 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class RedCloth |
|
19 | 19 | # Patch for RedCloth. Fixed in RedCloth r128 but _why hasn't released it yet. |
|
20 | 20 | # <a href="http://code.whytheluckystiff.net/redcloth/changeset/128">http://code.whytheluckystiff.net/redcloth/changeset/128</a> |
|
21 | 21 | def hard_break( text ) |
|
22 | 22 | text.gsub!( /(.)\n(?!\n|\Z| *([#*=]+(\s|$)|[{|]))/, "\\1<br />" ) if hard_breaks |
|
23 | 23 | end |
|
24 | 24 | end |
|
25 | 25 | |
|
26 | 26 | module ApplicationHelper |
|
27 | 27 | |
|
28 | 28 | def current_role |
|
29 | 29 | @current_role ||= User.current.role_for_project(@project) |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | # Return true if user is authorized for controller/action, otherwise false |
|
33 | 33 | def authorize_for(controller, action) |
|
34 | 34 | User.current.allowed_to?({:controller => controller, :action => action}, @project) |
|
35 | 35 | end |
|
36 | 36 | |
|
37 | 37 | # Display a link if user is authorized |
|
38 | 38 | def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference) |
|
39 | 39 | link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action]) |
|
40 | 40 | end |
|
41 | 41 | |
|
42 | 42 | # Display a link to user's account page |
|
43 | 43 | def link_to_user(user) |
|
44 | 44 | link_to user.name, :controller => 'account', :action => 'show', :id => user |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def link_to_issue(issue) |
|
48 | 48 | link_to "#{issue.tracker.name} ##{issue.id}", :controller => "issues", :action => "show", :id => issue |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | def toggle_link(name, id, options={}) |
|
52 | 52 | onclick = "Element.toggle('#{id}'); " |
|
53 | 53 | onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ") |
|
54 | 54 | onclick << "return false;" |
|
55 | 55 | link_to(name, "#", :onclick => onclick) |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def image_to_function(name, function, html_options = {}) |
|
59 | 59 | html_options.symbolize_keys! |
|
60 | 60 | tag(:input, html_options.merge({ |
|
61 | 61 | :type => "image", :src => image_path(name), |
|
62 | 62 | :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};" |
|
63 | 63 | })) |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def prompt_to_remote(name, text, param, url, html_options = {}) |
|
67 | 67 | html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;" |
|
68 | 68 | link_to name, {}, html_options |
|
69 | 69 | end |
|
70 | 70 | |
|
71 | 71 | def format_date(date) |
|
72 | 72 | return nil unless date |
|
73 | 73 | @date_format_setting ||= Setting.date_format.to_i |
|
74 | 74 | @date_format_setting == 0 ? l_date(date) : date.strftime("%Y-%m-%d") |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def format_time(time) |
|
78 | 78 | return nil unless time |
|
79 | 79 | @date_format_setting ||= Setting.date_format.to_i |
|
80 | 80 | time = time.to_time if time.is_a?(String) |
|
81 | 81 | @date_format_setting == 0 ? l_datetime(time) : (time.strftime("%Y-%m-%d") + ' ' + l_time(time)) |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def day_name(day) |
|
85 | 85 | l(:general_day_names).split(',')[day-1] |
|
86 | 86 | end |
|
87 | 87 | |
|
88 | 88 | def month_name(month) |
|
89 | 89 | l(:actionview_datehelper_select_month_names).split(',')[month-1] |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def pagination_links_full(paginator, options={}, html_options={}) |
|
93 | 93 | page_param = options.delete(:page_param) || :page |
|
94 | 94 | |
|
95 | 95 | html = '' |
|
96 | 96 | html << link_to_remote(('« ' + l(:label_previous)), |
|
97 | 97 | {:update => "content", :url => options.merge(page_param => paginator.current.previous)}, |
|
98 | 98 | {:href => url_for(:params => options.merge(page_param => paginator.current.previous))}) + ' ' if paginator.current.previous |
|
99 | 99 | |
|
100 | 100 | html << (pagination_links_each(paginator, options) do |n| |
|
101 | 101 | link_to_remote(n.to_s, |
|
102 | 102 | {:url => {:params => options.merge(page_param => n)}, :update => 'content'}, |
|
103 | 103 | {:href => url_for(:params => options.merge(page_param => n))}) |
|
104 | 104 | end || '') |
|
105 | 105 | |
|
106 | 106 | html << ' ' + link_to_remote((l(:label_next) + ' »'), |
|
107 | 107 | {:update => "content", :url => options.merge(page_param => paginator.current.next)}, |
|
108 | 108 | {:href => url_for(:params => options.merge(page_param => paginator.current.next))}) if paginator.current.next |
|
109 | 109 | html |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | # textilize text according to system settings and RedCloth availability |
|
113 | 113 | def textilizable(text, options = {}) |
|
114 | 114 | return "" if text.blank? |
|
115 | 115 | |
|
116 | 116 | # different methods for formatting wiki links |
|
117 | 117 | case options[:wiki_links] |
|
118 | 118 | when :local |
|
119 | 119 | # used for local links to html files |
|
120 | 120 | format_wiki_link = Proc.new {|project, title| "#{title}.html" } |
|
121 | 121 | when :anchor |
|
122 | 122 | # used for single-file wiki export |
|
123 | 123 | format_wiki_link = Proc.new {|project, title| "##{title}" } |
|
124 | 124 | else |
|
125 | 125 | format_wiki_link = Proc.new {|project, title| url_for :controller => 'wiki', :action => 'index', :id => project, :page => title } |
|
126 | 126 | end |
|
127 | 127 | |
|
128 | 128 | project = options[:project] || @project |
|
129 | 129 | |
|
130 | 130 | # turn wiki links into html links |
|
131 | 131 | # example: |
|
132 | 132 | # [[mypage]] |
|
133 | 133 | # [[mypage|mytext]] |
|
134 | 134 | # wiki links can refer other project wikis, using project name or identifier: |
|
135 | 135 | # [[project:]] -> wiki starting page |
|
136 | 136 | # [[project:|mytext]] |
|
137 | 137 | # [[project:mypage]] |
|
138 | 138 | # [[project:mypage|mytext]] |
|
139 | 139 | text = text.gsub(/\[\[([^\]\|]+)(\|([^\]\|]+))?\]\]/) do |m| |
|
140 | 140 | link_project = project |
|
141 | 141 | page = $1 |
|
142 | 142 | title = $3 |
|
143 | 143 | if page =~ /^([^\:]+)\:(.*)$/ |
|
144 | 144 | link_project = Project.find_by_name($1) || Project.find_by_identifier($1) |
|
145 | 145 | page = title || $2 |
|
146 | 146 | title = $1 if page.blank? |
|
147 | 147 | end |
|
148 | 148 | link_to((title || page), format_wiki_link.call(link_project, Wiki.titleize(page)), :class => 'wiki-page') |
|
149 | 149 | end |
|
150 | 150 | |
|
151 | 151 | # turn issue ids into links |
|
152 | 152 | # example: |
|
153 | 153 | # #52 -> <a href="/issues/show/52">#52</a> |
|
154 | 154 | text = text.gsub(/#(\d+)(?=\b)/) {|m| link_to "##{$1}", {:controller => 'issues', :action => 'show', :id => $1}, :class => 'issue' } |
|
155 | 155 | |
|
156 | 156 | # turn revision ids into links (@project needed) |
|
157 | 157 | # example: |
|
158 | 158 | # r52 -> <a href="/repositories/revision/6?rev=52">r52</a> (@project.id is 6) |
|
159 | 159 | text = text.gsub(/(?=\b)r(\d+)(?=\b)/) {|m| link_to "r#{$1}", {:controller => 'repositories', :action => 'revision', :id => project.id, :rev => $1}, :class => 'changeset' } if project |
|
160 | 160 | |
|
161 | 161 | # when using an image link, try to use an attachment, if possible |
|
162 | 162 | attachments = options[:attachments] |
|
163 | 163 | if attachments |
|
164 | 164 | text = text.gsub(/!([<>=]*)(\S+\.(gif|jpg|jpeg|png))!/) do |m| |
|
165 | 165 | align = $1 |
|
166 | 166 | filename = $2 |
|
167 | 167 | rf = Regexp.new(filename, Regexp::IGNORECASE) |
|
168 | 168 | # search for the picture in attachments |
|
169 | 169 | if found = attachments.detect { |att| att.filename =~ rf } |
|
170 | 170 | image_url = url_for :controller => 'attachments', :action => 'download', :id => found.id |
|
171 | 171 | "!#{align}#{image_url}!" |
|
172 | 172 | else |
|
173 | 173 | "!#{align}#{filename}!" |
|
174 | 174 | end |
|
175 | 175 | end |
|
176 | 176 | end |
|
177 | 177 | |
|
178 | 178 | # finally textilize text |
|
179 | 179 | @do_textilize ||= (Setting.text_formatting == 'textile') && (ActionView::Helpers::TextHelper.method_defined? "textilize") |
|
180 |
text = @do_textilize ? |
|
|
180 | text = @do_textilize ? RedCloth.new(auto_link(text), [:hard_breaks]).to_html : simple_format(auto_link(h(text))) | |
|
181 | 181 | end |
|
182 | 182 | |
|
183 | 183 | # Same as Rails' simple_format helper without using paragraphs |
|
184 | 184 | def simple_format_without_paragraph(text) |
|
185 | 185 | text.to_s. |
|
186 | 186 | gsub(/\r\n?/, "\n"). # \r\n and \r -> \n |
|
187 | 187 | gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br |
|
188 | 188 | gsub(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | def error_messages_for(object_name, options = {}) |
|
192 | 192 | options = options.symbolize_keys |
|
193 | 193 | object = instance_variable_get("@#{object_name}") |
|
194 | 194 | if object && !object.errors.empty? |
|
195 | 195 | # build full_messages here with controller current language |
|
196 | 196 | full_messages = [] |
|
197 | 197 | object.errors.each do |attr, msg| |
|
198 | 198 | next if msg.nil? |
|
199 | 199 | msg = msg.first if msg.is_a? Array |
|
200 | 200 | if attr == "base" |
|
201 | 201 | full_messages << l(msg) |
|
202 | 202 | else |
|
203 | 203 | full_messages << "« " + (l_has_string?("field_" + attr) ? l("field_" + attr) : object.class.human_attribute_name(attr)) + " » " + l(msg) unless attr == "custom_values" |
|
204 | 204 | end |
|
205 | 205 | end |
|
206 | 206 | # retrieve custom values error messages |
|
207 | 207 | if object.errors[:custom_values] |
|
208 | 208 | object.custom_values.each do |v| |
|
209 | 209 | v.errors.each do |attr, msg| |
|
210 | 210 | next if msg.nil? |
|
211 | 211 | msg = msg.first if msg.is_a? Array |
|
212 | 212 | full_messages << "« " + v.custom_field.name + " » " + l(msg) |
|
213 | 213 | end |
|
214 | 214 | end |
|
215 | 215 | end |
|
216 | 216 | content_tag("div", |
|
217 | 217 | content_tag( |
|
218 | 218 | options[:header_tag] || "span", lwr(:gui_validation_error, full_messages.length) + ":" |
|
219 | 219 | ) + |
|
220 | 220 | content_tag("ul", full_messages.collect { |msg| content_tag("li", msg) }), |
|
221 | 221 | "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation" |
|
222 | 222 | ) |
|
223 | 223 | else |
|
224 | 224 | "" |
|
225 | 225 | end |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | def lang_options_for_select(blank=true) |
|
229 | 229 | (blank ? [["(auto)", ""]] : []) + |
|
230 | 230 | GLoc.valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.first <=> y.first } |
|
231 | 231 | end |
|
232 | 232 | |
|
233 | 233 | def label_tag_for(name, option_tags = nil, options = {}) |
|
234 | 234 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
235 | 235 | content_tag("label", label_text) |
|
236 | 236 | end |
|
237 | 237 | |
|
238 | 238 | def labelled_tabular_form_for(name, object, options, &proc) |
|
239 | 239 | options[:html] ||= {} |
|
240 | 240 | options[:html].store :class, "tabular" |
|
241 | 241 | form_for(name, object, options.merge({ :builder => TabularFormBuilder, :lang => current_language}), &proc) |
|
242 | 242 | end |
|
243 | 243 | |
|
244 | 244 | def check_all_links(form_name) |
|
245 | 245 | link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") + |
|
246 | 246 | " | " + |
|
247 | 247 | link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)") |
|
248 | 248 | end |
|
249 | 249 | |
|
250 | 250 | def calendar_for(field_id) |
|
251 | 251 | image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) + |
|
252 | 252 | javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });") |
|
253 | 253 | end |
|
254 | 254 | |
|
255 | 255 | def wikitoolbar_for(field_id) |
|
256 | 256 | return '' unless Setting.text_formatting == 'textile' |
|
257 | 257 | javascript_include_tag('jstoolbar') + javascript_tag("var toolbar = new jsToolBar($('#{field_id}')); toolbar.draw();") |
|
258 | 258 | end |
|
259 | 259 | end |
|
260 | 260 | |
|
261 | 261 | class TabularFormBuilder < ActionView::Helpers::FormBuilder |
|
262 | 262 | include GLoc |
|
263 | 263 | |
|
264 | 264 | def initialize(object_name, object, template, options, proc) |
|
265 | 265 | set_language_if_valid options.delete(:lang) |
|
266 | 266 | @object_name, @object, @template, @options, @proc = object_name, object, template, options, proc |
|
267 | 267 | end |
|
268 | 268 | |
|
269 | 269 | (field_helpers - %w(radio_button hidden_field) + %w(date_select)).each do |selector| |
|
270 | 270 | src = <<-END_SRC |
|
271 | 271 | def #{selector}(field, options = {}) |
|
272 | 272 | return super if options.delete :no_label |
|
273 | 273 | label_text = l(options[:label]) if options[:label] |
|
274 | 274 | label_text ||= l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) |
|
275 | 275 | label_text << @template.content_tag("span", " *", :class => "required") if options.delete(:required) |
|
276 | 276 | label = @template.content_tag("label", label_text, |
|
277 | 277 | :class => (@object && @object.errors[field] ? "error" : nil), |
|
278 | 278 | :for => (@object_name.to_s + "_" + field.to_s)) |
|
279 | 279 | label + super |
|
280 | 280 | end |
|
281 | 281 | END_SRC |
|
282 | 282 | class_eval src, __FILE__, __LINE__ |
|
283 | 283 | end |
|
284 | 284 | |
|
285 | 285 | def select(field, choices, options = {}, html_options = {}) |
|
286 | 286 | label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "") |
|
287 | 287 | label = @template.content_tag("label", label_text, |
|
288 | 288 | :class => (@object && @object.errors[field] ? "error" : nil), |
|
289 | 289 | :for => (@object_name.to_s + "_" + field.to_s)) |
|
290 | 290 | label + super |
|
291 | 291 | end |
|
292 | 292 | |
|
293 | 293 | end |
|
294 | 294 |
General Comments 0
You need to be logged in to leave comments.
Login now