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