You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

application_helper.rb 44KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2012 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require 'forwardable'
  20. require 'cgi'
  21. module ApplicationHelper
  22. include Redmine::WikiFormatting::Macros::Definitions
  23. include Redmine::I18n
  24. include GravatarHelper::PublicMethods
  25. extend Forwardable
  26. def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
  27. # Return true if user is authorized for controller/action, otherwise false
  28. def authorize_for(controller, action)
  29. User.current.allowed_to?({:controller => controller, :action => action}, @project)
  30. end
  31. # Display a link if user is authorized
  32. #
  33. # @param [String] name Anchor text (passed to link_to)
  34. # @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
  35. # @param [optional, Hash] html_options Options passed to link_to
  36. # @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
  37. def link_to_if_authorized(name, options = {}, html_options = nil, *parameters_for_method_reference)
  38. link_to(name, options, html_options, *parameters_for_method_reference) if authorize_for(options[:controller] || params[:controller], options[:action])
  39. end
  40. # Display a link to remote if user is authorized
  41. def link_to_remote_if_authorized(name, options = {}, html_options = nil)
  42. url = options[:url] || {}
  43. link_to_remote(name, options, html_options) if authorize_for(url[:controller] || params[:controller], url[:action])
  44. end
  45. # Displays a link to user's account page if active
  46. def link_to_user(user, options={})
  47. if user.is_a?(User)
  48. name = h(user.name(options[:format]))
  49. if user.active?
  50. link_to name, :controller => 'users', :action => 'show', :id => user
  51. else
  52. name
  53. end
  54. else
  55. h(user.to_s)
  56. end
  57. end
  58. # Displays a link to +issue+ with its subject.
  59. # Examples:
  60. #
  61. # link_to_issue(issue) # => Defect #6: This is the subject
  62. # link_to_issue(issue, :truncate => 6) # => Defect #6: This i...
  63. # link_to_issue(issue, :subject => false) # => Defect #6
  64. # link_to_issue(issue, :project => true) # => Foo - Defect #6
  65. #
  66. def link_to_issue(issue, options={})
  67. title = nil
  68. subject = nil
  69. if options[:subject] == false
  70. title = truncate(issue.subject, :length => 60)
  71. else
  72. subject = issue.subject
  73. if options[:truncate]
  74. subject = truncate(subject, :length => options[:truncate])
  75. end
  76. end
  77. s = link_to "#{h(issue.tracker)} ##{issue.id}", {:controller => "issues", :action => "show", :id => issue},
  78. :class => issue.css_classes,
  79. :title => title
  80. s << h(": #{subject}") if subject
  81. s = h("#{issue.project} - ") + s if options[:project]
  82. s
  83. end
  84. # Generates a link to an attachment.
  85. # Options:
  86. # * :text - Link text (default to attachment filename)
  87. # * :download - Force download (default: false)
  88. def link_to_attachment(attachment, options={})
  89. text = options.delete(:text) || attachment.filename
  90. action = options.delete(:download) ? 'download' : 'show'
  91. opt_only_path = {}
  92. opt_only_path[:only_path] = (options[:only_path] == false ? false : true)
  93. options.delete(:only_path)
  94. link_to(h(text),
  95. {:controller => 'attachments', :action => action,
  96. :id => attachment, :filename => attachment.filename}.merge(opt_only_path),
  97. options)
  98. end
  99. # Generates a link to a SCM revision
  100. # Options:
  101. # * :text - Link text (default to the formatted revision)
  102. def link_to_revision(revision, repository, options={})
  103. if repository.is_a?(Project)
  104. repository = repository.repository
  105. end
  106. text = options.delete(:text) || format_revision(revision)
  107. rev = revision.respond_to?(:identifier) ? revision.identifier : revision
  108. link_to(
  109. h(text),
  110. {:controller => 'repositories', :action => 'revision', :id => repository.project, :repository_id => repository.identifier_param, :rev => rev},
  111. :title => l(:label_revision_id, format_revision(revision))
  112. )
  113. end
  114. # Generates a link to a message
  115. def link_to_message(message, options={}, html_options = nil)
  116. link_to(
  117. h(truncate(message.subject, :length => 60)),
  118. { :controller => 'messages', :action => 'show',
  119. :board_id => message.board_id,
  120. :id => message.root,
  121. :r => (message.parent_id && message.id),
  122. :anchor => (message.parent_id ? "message-#{message.id}" : nil)
  123. }.merge(options),
  124. html_options
  125. )
  126. end
  127. # Generates a link to a project if active
  128. # Examples:
  129. #
  130. # link_to_project(project) # => link to the specified project overview
  131. # link_to_project(project, :action=>'settings') # => link to project settings
  132. # link_to_project(project, {:only_path => false}, :class => "project") # => 3rd arg adds html options
  133. # link_to_project(project, {}, :class => "project") # => html options with default url (project overview)
  134. #
  135. def link_to_project(project, options={}, html_options = nil)
  136. if project.active?
  137. url = {:controller => 'projects', :action => 'show', :id => project}.merge(options)
  138. link_to(h(project), url, html_options)
  139. else
  140. h(project)
  141. end
  142. end
  143. def toggle_link(name, id, options={})
  144. onclick = "Element.toggle('#{id}'); "
  145. onclick << (options[:focus] ? "Form.Element.focus('#{options[:focus]}'); " : "this.blur(); ")
  146. onclick << "return false;"
  147. link_to(name, "#", :onclick => onclick)
  148. end
  149. def image_to_function(name, function, html_options = {})
  150. html_options.symbolize_keys!
  151. tag(:input, html_options.merge({
  152. :type => "image", :src => image_path(name),
  153. :onclick => (html_options[:onclick] ? "#{html_options[:onclick]}; " : "") + "#{function};"
  154. }))
  155. end
  156. def prompt_to_remote(name, text, param, url, html_options = {})
  157. html_options[:onclick] = "promptToRemote('#{text}', '#{param}', '#{url_for(url)}'); return false;"
  158. link_to name, {}, html_options
  159. end
  160. def format_activity_title(text)
  161. h(truncate_single_line(text, :length => 100))
  162. end
  163. def format_activity_day(date)
  164. date == Date.today ? l(:label_today).titleize : format_date(date)
  165. end
  166. def format_activity_description(text)
  167. h(truncate(text.to_s, :length => 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...')
  168. ).gsub(/[\r\n]+/, "<br />").html_safe
  169. end
  170. def format_version_name(version)
  171. if version.project == @project
  172. h(version)
  173. else
  174. h("#{version.project} - #{version}")
  175. end
  176. end
  177. def due_date_distance_in_words(date)
  178. if date
  179. l((date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in), distance_of_date_in_words(Date.today, date))
  180. end
  181. end
  182. def render_page_hierarchy(pages, node=nil, options={})
  183. content = ''
  184. if pages[node]
  185. content << "<ul class=\"pages-hierarchy\">\n"
  186. pages[node].each do |page|
  187. content << "<li>"
  188. content << link_to(h(page.pretty_title), {:controller => 'wiki', :action => 'show', :project_id => page.project, :id => page.title},
  189. :title => (options[:timestamp] && page.updated_on ? l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on)) : nil))
  190. content << "\n" + render_page_hierarchy(pages, page.id, options) if pages[page.id]
  191. content << "</li>\n"
  192. end
  193. content << "</ul>\n"
  194. end
  195. content.html_safe
  196. end
  197. # Renders flash messages
  198. def render_flash_messages
  199. s = ''
  200. flash.each do |k,v|
  201. s << content_tag('div', v.html_safe, :class => "flash #{k}", :id => "flash_#{k}")
  202. end
  203. s.html_safe
  204. end
  205. # Renders tabs and their content
  206. def render_tabs(tabs)
  207. if tabs.any?
  208. render :partial => 'common/tabs', :locals => {:tabs => tabs}
  209. else
  210. content_tag 'p', l(:label_no_data), :class => "nodata"
  211. end
  212. end
  213. # Renders the project quick-jump box
  214. def render_project_jump_box
  215. return unless User.current.logged?
  216. projects = User.current.memberships.collect(&:project).compact.uniq
  217. if projects.any?
  218. s = '<select onchange="if (this.value != \'\') { window.location = this.value; }">' +
  219. "<option value=''>#{ l(:label_jump_to_a_project) }</option>" +
  220. '<option value="" disabled="disabled">---</option>'
  221. s << project_tree_options_for_select(projects, :selected => @project) do |p|
  222. { :value => url_for(:controller => 'projects', :action => 'show', :id => p, :jump => current_menu_item) }
  223. end
  224. s << '</select>'
  225. s.html_safe
  226. end
  227. end
  228. def project_tree_options_for_select(projects, options = {})
  229. s = ''
  230. project_tree(projects) do |project, level|
  231. name_prefix = (level > 0 ? ('&nbsp;' * 2 * level + '&#187; ').html_safe : '')
  232. tag_options = {:value => project.id}
  233. if project == options[:selected] || (options[:selected].respond_to?(:include?) && options[:selected].include?(project))
  234. tag_options[:selected] = 'selected'
  235. else
  236. tag_options[:selected] = nil
  237. end
  238. tag_options.merge!(yield(project)) if block_given?
  239. s << content_tag('option', name_prefix + h(project), tag_options)
  240. end
  241. s.html_safe
  242. end
  243. # Yields the given block for each project with its level in the tree
  244. #
  245. # Wrapper for Project#project_tree
  246. def project_tree(projects, &block)
  247. Project.project_tree(projects, &block)
  248. end
  249. def project_nested_ul(projects, &block)
  250. s = ''
  251. if projects.any?
  252. ancestors = []
  253. projects.sort_by(&:lft).each do |project|
  254. if (ancestors.empty? || project.is_descendant_of?(ancestors.last))
  255. s << "<ul>\n"
  256. else
  257. ancestors.pop
  258. s << "</li>"
  259. while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
  260. ancestors.pop
  261. s << "</ul></li>\n"
  262. end
  263. end
  264. s << "<li>"
  265. s << yield(project).to_s
  266. ancestors << project
  267. end
  268. s << ("</li></ul>\n" * ancestors.size)
  269. end
  270. s.html_safe
  271. end
  272. def principals_check_box_tags(name, principals)
  273. s = ''
  274. principals.sort.each do |principal|
  275. s << "<label>#{ check_box_tag name, principal.id, false } #{h principal}</label>\n"
  276. end
  277. s.html_safe
  278. end
  279. # Returns a string for users/groups option tags
  280. def principals_options_for_select(collection, selected=nil)
  281. s = ''
  282. if collection.include?(User.current)
  283. s << content_tag('option', "<< #{l(:label_me)} >>".html_safe, :value => User.current.id)
  284. end
  285. groups = ''
  286. collection.sort.each do |element|
  287. selected_attribute = ' selected="selected"' if option_value_selected?(element, selected)
  288. (element.is_a?(Group) ? groups : s) << %(<option value="#{element.id}"#{selected_attribute}>#{h element.name}</option>)
  289. end
  290. unless groups.empty?
  291. s << %(<optgroup label="#{h(l(:label_group_plural))}">#{groups}</optgroup>)
  292. end
  293. s.html_safe
  294. end
  295. # Truncates and returns the string as a single line
  296. def truncate_single_line(string, *args)
  297. truncate(string.to_s, *args).gsub(%r{[\r\n]+}m, ' ')
  298. end
  299. # Truncates at line break after 250 characters or options[:length]
  300. def truncate_lines(string, options={})
  301. length = options[:length] || 250
  302. if string.to_s =~ /\A(.{#{length}}.*?)$/m
  303. "#{$1}..."
  304. else
  305. string
  306. end
  307. end
  308. def anchor(text)
  309. text.to_s.gsub(' ', '_')
  310. end
  311. def html_hours(text)
  312. text.gsub(%r{(\d+)\.(\d+)}, '<span class="hours hours-int">\1</span><span class="hours hours-dec">.\2</span>').html_safe
  313. end
  314. def authoring(created, author, options={})
  315. l(options[:label] || :label_added_time_by, :author => link_to_user(author), :age => time_tag(created)).html_safe
  316. end
  317. def time_tag(time)
  318. text = distance_of_time_in_words(Time.now, time)
  319. if @project
  320. link_to(text, {:controller => 'activities', :action => 'index', :id => @project, :from => time.to_date}, :title => format_time(time))
  321. else
  322. content_tag('acronym', text, :title => format_time(time))
  323. end
  324. end
  325. def syntax_highlight_lines(name, content)
  326. lines = []
  327. syntax_highlight(name, content).each_line { |line| lines << line }
  328. lines
  329. end
  330. def syntax_highlight(name, content)
  331. Redmine::SyntaxHighlighting.highlight_by_filename(content, name)
  332. end
  333. def to_path_param(path)
  334. str = path.to_s.split(%r{[/\\]}).select{|p| !p.blank?}.join("/")
  335. str.blank? ? nil : str
  336. end
  337. def pagination_links_full(paginator, count=nil, options={})
  338. page_param = options.delete(:page_param) || :page
  339. per_page_links = options.delete(:per_page_links)
  340. url_param = params.dup
  341. html = ''
  342. if paginator.current.previous
  343. # \xc2\xab(utf-8) = &#171;
  344. html << link_to_content_update(
  345. "\xc2\xab " + l(:label_previous),
  346. url_param.merge(page_param => paginator.current.previous)) + ' '
  347. end
  348. html << (pagination_links_each(paginator, options) do |n|
  349. link_to_content_update(n.to_s, url_param.merge(page_param => n))
  350. end || '')
  351. if paginator.current.next
  352. # \xc2\xbb(utf-8) = &#187;
  353. html << ' ' + link_to_content_update(
  354. (l(:label_next) + " \xc2\xbb"),
  355. url_param.merge(page_param => paginator.current.next))
  356. end
  357. unless count.nil?
  358. html << " (#{paginator.current.first_item}-#{paginator.current.last_item}/#{count})"
  359. if per_page_links != false && links = per_page_links(paginator.items_per_page)
  360. html << " | #{links}"
  361. end
  362. end
  363. html.html_safe
  364. end
  365. def per_page_links(selected=nil)
  366. links = Setting.per_page_options_array.collect do |n|
  367. n == selected ? n : link_to_content_update(n, params.merge(:per_page => n))
  368. end
  369. links.size > 1 ? l(:label_display_per_page, links.join(', ')) : nil
  370. end
  371. def reorder_links(name, url, method = :post)
  372. link_to(image_tag('2uparrow.png', :alt => l(:label_sort_highest)),
  373. url.merge({"#{name}[move_to]" => 'highest'}),
  374. :method => method, :title => l(:label_sort_highest)) +
  375. link_to(image_tag('1uparrow.png', :alt => l(:label_sort_higher)),
  376. url.merge({"#{name}[move_to]" => 'higher'}),
  377. :method => method, :title => l(:label_sort_higher)) +
  378. link_to(image_tag('1downarrow.png', :alt => l(:label_sort_lower)),
  379. url.merge({"#{name}[move_to]" => 'lower'}),
  380. :method => method, :title => l(:label_sort_lower)) +
  381. link_to(image_tag('2downarrow.png', :alt => l(:label_sort_lowest)),
  382. url.merge({"#{name}[move_to]" => 'lowest'}),
  383. :method => method, :title => l(:label_sort_lowest))
  384. end
  385. def breadcrumb(*args)
  386. elements = args.flatten
  387. elements.any? ? content_tag('p', (args.join(" \xc2\xbb ") + " \xc2\xbb ").html_safe, :class => 'breadcrumb') : nil
  388. end
  389. def other_formats_links(&block)
  390. concat('<p class="other-formats">'.html_safe + l(:label_export_to))
  391. yield Redmine::Views::OtherFormatsBuilder.new(self)
  392. concat('</p>'.html_safe)
  393. end
  394. def page_header_title
  395. if @project.nil? || @project.new_record?
  396. h(Setting.app_title)
  397. else
  398. b = []
  399. ancestors = (@project.root? ? [] : @project.ancestors.visible.all)
  400. if ancestors.any?
  401. root = ancestors.shift
  402. b << link_to_project(root, {:jump => current_menu_item}, :class => 'root')
  403. if ancestors.size > 2
  404. b << "\xe2\x80\xa6"
  405. ancestors = ancestors[-2, 2]
  406. end
  407. b += ancestors.collect {|p| link_to_project(p, {:jump => current_menu_item}, :class => 'ancestor') }
  408. end
  409. b << h(@project)
  410. b.join(" \xc2\xbb ").html_safe
  411. end
  412. end
  413. def html_title(*args)
  414. if args.empty?
  415. title = @html_title || []
  416. title << @project.name if @project
  417. title << Setting.app_title unless Setting.app_title == title.last
  418. title.select {|t| !t.blank? }.join(' - ')
  419. else
  420. @html_title ||= []
  421. @html_title += args
  422. end
  423. end
  424. # Returns the theme, controller name, and action as css classes for the
  425. # HTML body.
  426. def body_css_classes
  427. css = []
  428. if theme = Redmine::Themes.theme(Setting.ui_theme)
  429. css << 'theme-' + theme.name
  430. end
  431. css << 'controller-' + controller_name
  432. css << 'action-' + action_name
  433. css.join(' ')
  434. end
  435. def accesskey(s)
  436. Redmine::AccessKeys.key_for s
  437. end
  438. # Formats text according to system settings.
  439. # 2 ways to call this method:
  440. # * with a String: textilizable(text, options)
  441. # * with an object and one of its attribute: textilizable(issue, :description, options)
  442. def textilizable(*args)
  443. options = args.last.is_a?(Hash) ? args.pop : {}
  444. case args.size
  445. when 1
  446. obj = options[:object]
  447. text = args.shift
  448. when 2
  449. obj = args.shift
  450. attr = args.shift
  451. text = obj.send(attr).to_s
  452. else
  453. raise ArgumentError, 'invalid arguments to textilizable'
  454. end
  455. return '' if text.blank?
  456. project = options[:project] || @project || (obj && obj.respond_to?(:project) ? obj.project : nil)
  457. only_path = options.delete(:only_path) == false ? false : true
  458. text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr)
  459. @parsed_headings = []
  460. @heading_anchors = {}
  461. @current_section = 0 if options[:edit_section_links]
  462. parse_sections(text, project, obj, attr, only_path, options)
  463. text = parse_non_pre_blocks(text) do |text|
  464. [:parse_inline_attachments, :parse_wiki_links, :parse_redmine_links, :parse_macros].each do |method_name|
  465. send method_name, text, project, obj, attr, only_path, options
  466. end
  467. end
  468. parse_headings(text, project, obj, attr, only_path, options)
  469. if @parsed_headings.any?
  470. replace_toc(text, @parsed_headings)
  471. end
  472. text.html_safe
  473. end
  474. def parse_non_pre_blocks(text)
  475. s = StringScanner.new(text)
  476. tags = []
  477. parsed = ''
  478. while !s.eos?
  479. s.scan(/(.*?)(<(\/)?(pre|code)(.*?)>|\z)/im)
  480. text, full_tag, closing, tag = s[1], s[2], s[3], s[4]
  481. if tags.empty?
  482. yield text
  483. end
  484. parsed << text
  485. if tag
  486. if closing
  487. if tags.last == tag.downcase
  488. tags.pop
  489. end
  490. else
  491. tags << tag.downcase
  492. end
  493. parsed << full_tag
  494. end
  495. end
  496. # Close any non closing tags
  497. while tag = tags.pop
  498. parsed << "</#{tag}>"
  499. end
  500. parsed
  501. end
  502. def parse_inline_attachments(text, project, obj, attr, only_path, options)
  503. # when using an image link, try to use an attachment, if possible
  504. if options[:attachments] || (obj && obj.respond_to?(:attachments))
  505. attachments = options[:attachments] || obj.attachments
  506. text.gsub!(/src="([^\/"]+\.(bmp|gif|jpg|jpe|jpeg|png))"(\s+alt="([^"]*)")?/i) do |m|
  507. filename, ext, alt, alttext = $1.downcase, $2, $3, $4
  508. # search for the picture in attachments
  509. if found = Attachment.latest_attach(attachments, filename)
  510. image_url = url_for :only_path => only_path, :controller => 'attachments',
  511. :action => 'download', :id => found
  512. desc = found.description.to_s.gsub('"', '')
  513. if !desc.blank? && alttext.blank?
  514. alt = " title=\"#{desc}\" alt=\"#{desc}\""
  515. end
  516. "src=\"#{image_url}\"#{alt}"
  517. else
  518. m
  519. end
  520. end
  521. end
  522. end
  523. # Wiki links
  524. #
  525. # Examples:
  526. # [[mypage]]
  527. # [[mypage|mytext]]
  528. # wiki links can refer other project wikis, using project name or identifier:
  529. # [[project:]] -> wiki starting page
  530. # [[project:|mytext]]
  531. # [[project:mypage]]
  532. # [[project:mypage|mytext]]
  533. def parse_wiki_links(text, project, obj, attr, only_path, options)
  534. text.gsub!(/(!)?(\[\[([^\]\n\|]+)(\|([^\]\n\|]+))?\]\])/) do |m|
  535. link_project = project
  536. esc, all, page, title = $1, $2, $3, $5
  537. if esc.nil?
  538. if page =~ /^([^\:]+)\:(.*)$/
  539. link_project = Project.find_by_identifier($1) || Project.find_by_name($1)
  540. page = $2
  541. title ||= $1 if page.blank?
  542. end
  543. if link_project && link_project.wiki
  544. # extract anchor
  545. anchor = nil
  546. if page =~ /^(.+?)\#(.+)$/
  547. page, anchor = $1, $2
  548. end
  549. anchor = sanitize_anchor_name(anchor) if anchor.present?
  550. # check if page exists
  551. wiki_page = link_project.wiki.find_page(page)
  552. url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page
  553. "##{anchor}"
  554. else
  555. case options[:wiki_links]
  556. when :local; "#{page.present? ? Wiki.titleize(page) : ''}.html" + (anchor.present? ? "##{anchor}" : '')
  557. when :anchor; "##{page.present? ? Wiki.titleize(page) : title}" + (anchor.present? ? "_#{anchor}" : '') # used for single-file wiki export
  558. else
  559. wiki_page_id = page.present? ? Wiki.titleize(page) : nil
  560. parent = wiki_page.nil? && obj.is_a?(WikiContent) && obj.page && project == link_project ? obj.page.title : nil
  561. url_for(:only_path => only_path, :controller => 'wiki', :action => 'show', :project_id => link_project,
  562. :id => wiki_page_id, :anchor => anchor, :parent => parent)
  563. end
  564. end
  565. link_to(title.present? ? title.html_safe : h(page), url, :class => ('wiki-page' + (wiki_page ? '' : ' new')))
  566. else
  567. # project or wiki doesn't exist
  568. all
  569. end
  570. else
  571. all
  572. end
  573. end
  574. end
  575. # Redmine links
  576. #
  577. # Examples:
  578. # Issues:
  579. # #52 -> Link to issue #52
  580. # Changesets:
  581. # r52 -> Link to revision 52
  582. # commit:a85130f -> Link to scmid starting with a85130f
  583. # Documents:
  584. # document#17 -> Link to document with id 17
  585. # document:Greetings -> Link to the document with title "Greetings"
  586. # document:"Some document" -> Link to the document with title "Some document"
  587. # Versions:
  588. # version#3 -> Link to version with id 3
  589. # version:1.0.0 -> Link to version named "1.0.0"
  590. # version:"1.0 beta 2" -> Link to version named "1.0 beta 2"
  591. # Attachments:
  592. # attachment:file.zip -> Link to the attachment of the current object named file.zip
  593. # Source files:
  594. # source:some/file -> Link to the file located at /some/file in the project's repository
  595. # source:some/file@52 -> Link to the file's revision 52
  596. # source:some/file#L120 -> Link to line 120 of the file
  597. # source:some/file@52#L120 -> Link to line 120 of the file's revision 52
  598. # export:some/file -> Force the download of the file
  599. # Forum messages:
  600. # message#1218 -> Link to message with id 1218
  601. #
  602. # Links can refer other objects from other projects, using project identifier:
  603. # identifier:r52
  604. # identifier:document:"Some document"
  605. # identifier:version:1.0.0
  606. # identifier:source:some/file
  607. def parse_redmine_links(text, project, obj, attr, only_path, options)
  608. text.gsub!(%r{([\s\(,\-\[\>]|^)(!)?(([a-z0-9\-_]+):)?(attachment|document|version|forum|news|message|project|commit|source|export)?(((#)|((([a-z0-9\-]+)\|)?(r)))((\d+)((#note)?-(\d+))?)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]][^A-Za-z0-9_/])|,|\s|\]|<|$)}) do |m|
  609. leading, esc, project_prefix, project_identifier, prefix, repo_prefix, repo_identifier, sep, identifier, comment_suffix, comment_id = $1, $2, $3, $4, $5, $10, $11, $8 || $12 || $18, $14 || $19, $15, $17
  610. link = nil
  611. if project_identifier
  612. project = Project.visible.find_by_identifier(project_identifier)
  613. end
  614. if esc.nil?
  615. if prefix.nil? && sep == 'r'
  616. if project
  617. repository = nil
  618. if repo_identifier
  619. repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
  620. else
  621. repository = project.repository
  622. end
  623. # project.changesets.visible raises an SQL error because of a double join on repositories
  624. if repository && (changeset = Changeset.visible.find_by_repository_id_and_revision(repository.id, identifier))
  625. link = link_to(h("#{project_prefix}#{repo_prefix}r#{identifier}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.revision},
  626. :class => 'changeset',
  627. :title => truncate_single_line(changeset.comments, :length => 100))
  628. end
  629. end
  630. elsif sep == '#'
  631. oid = identifier.to_i
  632. case prefix
  633. when nil
  634. if issue = Issue.visible.find_by_id(oid, :include => :status)
  635. anchor = comment_id ? "note-#{comment_id}" : nil
  636. link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid, :anchor => anchor},
  637. :class => issue.css_classes,
  638. :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
  639. end
  640. when 'document'
  641. if document = Document.visible.find_by_id(oid)
  642. link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
  643. :class => 'document'
  644. end
  645. when 'version'
  646. if version = Version.visible.find_by_id(oid)
  647. link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
  648. :class => 'version'
  649. end
  650. when 'message'
  651. if message = Message.visible.find_by_id(oid, :include => :parent)
  652. link = link_to_message(message, {:only_path => only_path}, :class => 'message')
  653. end
  654. when 'forum'
  655. if board = Board.visible.find_by_id(oid)
  656. link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
  657. :class => 'board'
  658. end
  659. when 'news'
  660. if news = News.visible.find_by_id(oid)
  661. link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
  662. :class => 'news'
  663. end
  664. when 'project'
  665. if p = Project.visible.find_by_id(oid)
  666. link = link_to_project(p, {:only_path => only_path}, :class => 'project')
  667. end
  668. end
  669. elsif sep == ':'
  670. # removes the double quotes if any
  671. name = identifier.gsub(%r{^"(.*)"$}, "\\1")
  672. case prefix
  673. when 'document'
  674. if project && document = project.documents.visible.find_by_title(name)
  675. link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
  676. :class => 'document'
  677. end
  678. when 'version'
  679. if project && version = project.versions.visible.find_by_name(name)
  680. link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
  681. :class => 'version'
  682. end
  683. when 'forum'
  684. if project && board = project.boards.visible.find_by_name(name)
  685. link = link_to h(board.name), {:only_path => only_path, :controller => 'boards', :action => 'show', :id => board, :project_id => board.project},
  686. :class => 'board'
  687. end
  688. when 'news'
  689. if project && news = project.news.visible.find_by_title(name)
  690. link = link_to h(news.title), {:only_path => only_path, :controller => 'news', :action => 'show', :id => news},
  691. :class => 'news'
  692. end
  693. when 'commit', 'source', 'export'
  694. if project
  695. repository = nil
  696. if name =~ %r{^(([a-z0-9\-]+)\|)(.+)$}
  697. repo_prefix, repo_identifier, name = $1, $2, $3
  698. repository = project.repositories.detect {|repo| repo.identifier == repo_identifier}
  699. else
  700. repository = project.repository
  701. end
  702. if prefix == 'commit'
  703. if repository && (changeset = Changeset.visible.find(:first, :conditions => ["repository_id = ? AND scmid LIKE ?", repository.id, "#{name}%"]))
  704. link = link_to h("#{project_prefix}#{repo_prefix}#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :repository_id => repository.identifier_param, :rev => changeset.identifier},
  705. :class => 'changeset',
  706. :title => truncate_single_line(h(changeset.comments), :length => 100)
  707. end
  708. else
  709. if repository && User.current.allowed_to?(:browse_repository, project)
  710. name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
  711. path, rev, anchor = $1, $3, $5
  712. link = link_to h("#{project_prefix}#{prefix}:#{repo_prefix}#{name}"), {:controller => 'repositories', :action => 'entry', :id => project, :repository_id => repository.identifier_param,
  713. :path => to_path_param(path),
  714. :rev => rev,
  715. :anchor => anchor,
  716. :format => (prefix == 'export' ? 'raw' : nil)},
  717. :class => (prefix == 'export' ? 'source download' : 'source')
  718. end
  719. end
  720. repo_prefix = nil
  721. end
  722. when 'attachment'
  723. attachments = options[:attachments] || (obj && obj.respond_to?(:attachments) ? obj.attachments : nil)
  724. if attachments && attachment = attachments.detect {|a| a.filename == name }
  725. link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
  726. :class => 'attachment'
  727. end
  728. when 'project'
  729. if p = Project.visible.find(:first, :conditions => ["identifier = :s OR LOWER(name) = :s", {:s => name.downcase}])
  730. link = link_to_project(p, {:only_path => only_path}, :class => 'project')
  731. end
  732. end
  733. end
  734. end
  735. (leading + (link || "#{project_prefix}#{prefix}#{repo_prefix}#{sep}#{identifier}#{comment_suffix}"))
  736. end
  737. end
  738. HEADING_RE = /(<h(1|2|3|4)( [^>]+)?>(.+?)<\/h(1|2|3|4)>)/i unless const_defined?(:HEADING_RE)
  739. def parse_sections(text, project, obj, attr, only_path, options)
  740. return unless options[:edit_section_links]
  741. text.gsub!(HEADING_RE) do
  742. heading = $1
  743. @current_section += 1
  744. if @current_section > 1
  745. content_tag('div',
  746. link_to(image_tag('edit.png'), options[:edit_section_links].merge(:section => @current_section)),
  747. :class => 'contextual',
  748. :title => l(:button_edit_section)) + heading.html_safe
  749. else
  750. heading
  751. end
  752. end
  753. end
  754. # Headings and TOC
  755. # Adds ids and links to headings unless options[:headings] is set to false
  756. def parse_headings(text, project, obj, attr, only_path, options)
  757. return if options[:headings] == false
  758. text.gsub!(HEADING_RE) do
  759. level, attrs, content = $2.to_i, $3, $4
  760. item = strip_tags(content).strip
  761. anchor = sanitize_anchor_name(item)
  762. # used for single-file wiki export
  763. anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
  764. @heading_anchors[anchor] ||= 0
  765. idx = (@heading_anchors[anchor] += 1)
  766. if idx > 1
  767. anchor = "#{anchor}-#{idx}"
  768. end
  769. @parsed_headings << [level, anchor, item]
  770. "<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
  771. end
  772. end
  773. MACROS_RE = /
  774. (!)? # escaping
  775. (
  776. \{\{ # opening tag
  777. ([\w]+) # macro name
  778. (\(([^\}]*)\))? # optional arguments
  779. \}\} # closing tag
  780. )
  781. /x unless const_defined?(:MACROS_RE)
  782. # Macros substitution
  783. def parse_macros(text, project, obj, attr, only_path, options)
  784. text.gsub!(MACROS_RE) do
  785. esc, all, macro = $1, $2, $3.downcase
  786. args = ($5 || '').split(',').each(&:strip)
  787. if esc.nil?
  788. begin
  789. exec_macro(macro, obj, args)
  790. rescue => e
  791. "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
  792. end || all
  793. else
  794. all
  795. end
  796. end
  797. end
  798. TOC_RE = /<p>\{\{([<>]?)toc\}\}<\/p>/i unless const_defined?(:TOC_RE)
  799. # Renders the TOC with given headings
  800. def replace_toc(text, headings)
  801. text.gsub!(TOC_RE) do
  802. if headings.empty?
  803. ''
  804. else
  805. div_class = 'toc'
  806. div_class << ' right' if $1 == '>'
  807. div_class << ' left' if $1 == '<'
  808. out = "<ul class=\"#{div_class}\"><li>"
  809. root = headings.map(&:first).min
  810. current = root
  811. started = false
  812. headings.each do |level, anchor, item|
  813. if level > current
  814. out << '<ul><li>' * (level - current)
  815. elsif level < current
  816. out << "</li></ul>\n" * (current - level) + "</li><li>"
  817. elsif started
  818. out << '</li><li>'
  819. end
  820. out << "<a href=\"##{anchor}\">#{item}</a>"
  821. current = level
  822. started = true
  823. end
  824. out << '</li></ul>' * (current - root)
  825. out << '</li></ul>'
  826. end
  827. end
  828. end
  829. # Same as Rails' simple_format helper without using paragraphs
  830. def simple_format_without_paragraph(text)
  831. text.to_s.
  832. gsub(/\r\n?/, "\n"). # \r\n and \r -> \n
  833. gsub(/\n\n+/, "<br /><br />"). # 2+ newline -> 2 br
  834. gsub(/([^\n]\n)(?=[^\n])/, '\1<br />'). # 1 newline -> br
  835. html_safe
  836. end
  837. def lang_options_for_select(blank=true)
  838. (blank ? [["(auto)", ""]] : []) +
  839. valid_languages.collect{|lang| [ ll(lang.to_s, :general_lang_name), lang.to_s]}.sort{|x,y| x.last <=> y.last }
  840. end
  841. def label_tag_for(name, option_tags = nil, options = {})
  842. label_text = l(("field_"+field.to_s.gsub(/\_id$/, "")).to_sym) + (options.delete(:required) ? @template.content_tag("span", " *", :class => "required"): "")
  843. content_tag("label", label_text)
  844. end
  845. def labelled_tabular_form_for(*args, &proc)
  846. ActiveSupport::Deprecation.warn "ApplicationHelper#labelled_tabular_form_for is deprecated and will be removed in Redmine 1.5. Use #labelled_form_for instead."
  847. args << {} unless args.last.is_a?(Hash)
  848. options = args.last
  849. options[:html] ||= {}
  850. options[:html][:class] = 'tabular' unless options[:html].has_key?(:class)
  851. options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
  852. form_for(*args, &proc)
  853. end
  854. def labelled_form_for(*args, &proc)
  855. args << {} unless args.last.is_a?(Hash)
  856. options = args.last
  857. if args.first.is_a?(Symbol)
  858. options.merge!(:as => args.shift)
  859. end
  860. options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
  861. form_for(*args, &proc)
  862. end
  863. def labelled_fields_for(*args, &proc)
  864. args << {} unless args.last.is_a?(Hash)
  865. options = args.last
  866. options.merge!({:builder => Redmine::Views::LabelledFormBuilder})
  867. fields_for(*args, &proc)
  868. end
  869. def labelled_remote_form_for(*args, &proc)
  870. args << {} unless args.last.is_a?(Hash)
  871. options = args.last
  872. options.merge!({:builder => Redmine::Views::LabelledFormBuilder, :remote => true})
  873. form_for(*args, &proc)
  874. end
  875. def error_messages_for(*objects)
  876. html = ""
  877. objects = objects.map {|o| o.is_a?(String) ? instance_variable_get("@#{o}") : o}.compact
  878. errors = objects.map {|o| o.errors.full_messages}.flatten
  879. if errors.any?
  880. html << "<div id='errorExplanation'><ul>\n"
  881. errors.each do |error|
  882. html << "<li>#{h error}</li>\n"
  883. end
  884. html << "</ul></div>\n"
  885. end
  886. html.html_safe
  887. end
  888. def back_url_hidden_field_tag
  889. back_url = params[:back_url] || request.env['HTTP_REFERER']
  890. back_url = CGI.unescape(back_url.to_s)
  891. hidden_field_tag('back_url', CGI.escape(back_url), :id => nil) unless back_url.blank?
  892. end
  893. def check_all_links(form_name)
  894. link_to_function(l(:button_check_all), "checkAll('#{form_name}', true)") +
  895. " | ".html_safe +
  896. link_to_function(l(:button_uncheck_all), "checkAll('#{form_name}', false)")
  897. end
  898. def progress_bar(pcts, options={})
  899. pcts = [pcts, pcts] unless pcts.is_a?(Array)
  900. pcts = pcts.collect(&:round)
  901. pcts[1] = pcts[1] - pcts[0]
  902. pcts << (100 - pcts[1] - pcts[0])
  903. width = options[:width] || '100px;'
  904. legend = options[:legend] || ''
  905. content_tag('table',
  906. content_tag('tr',
  907. (pcts[0] > 0 ? content_tag('td', '', :style => "width: #{pcts[0]}%;", :class => 'closed') : ''.html_safe) +
  908. (pcts[1] > 0 ? content_tag('td', '', :style => "width: #{pcts[1]}%;", :class => 'done') : ''.html_safe) +
  909. (pcts[2] > 0 ? content_tag('td', '', :style => "width: #{pcts[2]}%;", :class => 'todo') : ''.html_safe)
  910. ), :class => 'progress', :style => "width: #{width};").html_safe +
  911. content_tag('p', legend, :class => 'pourcent').html_safe
  912. end
  913. def checked_image(checked=true)
  914. if checked
  915. image_tag 'toggle_check.png'
  916. end
  917. end
  918. def context_menu(url)
  919. unless @context_menu_included
  920. content_for :header_tags do
  921. javascript_include_tag('context_menu') +
  922. stylesheet_link_tag('context_menu')
  923. end
  924. if l(:direction) == 'rtl'
  925. content_for :header_tags do
  926. stylesheet_link_tag('context_menu_rtl')
  927. end
  928. end
  929. @context_menu_included = true
  930. end
  931. javascript_tag "new ContextMenu('#{ url_for(url) }')"
  932. end
  933. def calendar_for(field_id)
  934. include_calendar_headers_tags
  935. image_tag("calendar.png", {:id => "#{field_id}_trigger",:class => "calendar-trigger"}) +
  936. javascript_tag("Calendar.setup({inputField : '#{field_id}', ifFormat : '%Y-%m-%d', button : '#{field_id}_trigger' });")
  937. end
  938. def include_calendar_headers_tags
  939. unless @calendar_headers_tags_included
  940. @calendar_headers_tags_included = true
  941. content_for :header_tags do
  942. start_of_week = case Setting.start_of_week.to_i
  943. when 1
  944. 'Calendar._FD = 1;' # Monday
  945. when 7
  946. 'Calendar._FD = 0;' # Sunday
  947. when 6
  948. 'Calendar._FD = 6;' # Saturday
  949. else
  950. '' # use language
  951. end
  952. javascript_include_tag('calendar/calendar') +
  953. javascript_include_tag("calendar/lang/calendar-#{current_language.to_s.downcase}.js") +
  954. javascript_tag(start_of_week) +
  955. javascript_include_tag('calendar/calendar-setup') +
  956. stylesheet_link_tag('calendar')
  957. end
  958. end
  959. end
  960. # Overrides Rails' stylesheet_link_tag with themes and plugins support.
  961. # Examples:
  962. # stylesheet_link_tag('styles') # => picks styles.css from the current theme or defaults
  963. # stylesheet_link_tag('styles', :plugin => 'foo) # => picks styles.css from plugin's assets
  964. #
  965. def stylesheet_link_tag(*sources)
  966. options = sources.last.is_a?(Hash) ? sources.pop : {}
  967. plugin = options.delete(:plugin)
  968. sources = sources.map do |source|
  969. if plugin
  970. "/plugin_assets/#{plugin}/stylesheets/#{source}"
  971. elsif current_theme && current_theme.stylesheets.include?(source)
  972. current_theme.stylesheet_path(source)
  973. else
  974. source
  975. end
  976. end
  977. super sources, options
  978. end
  979. # Overrides Rails' image_tag with themes and plugins support.
  980. # Examples:
  981. # image_tag('image.png') # => picks image.png from the current theme or defaults
  982. # image_tag('image.png', :plugin => 'foo) # => picks image.png from plugin's assets
  983. #
  984. def image_tag(source, options={})
  985. if plugin = options.delete(:plugin)
  986. source = "/plugin_assets/#{plugin}/images/#{source}"
  987. elsif current_theme && current_theme.images.include?(source)
  988. source = current_theme.image_path(source)
  989. end
  990. super source, options
  991. end
  992. # Overrides Rails' javascript_include_tag with plugins support
  993. # Examples:
  994. # javascript_include_tag('scripts') # => picks scripts.js from defaults
  995. # javascript_include_tag('scripts', :plugin => 'foo) # => picks scripts.js from plugin's assets
  996. #
  997. def javascript_include_tag(*sources)
  998. options = sources.last.is_a?(Hash) ? sources.pop : {}
  999. if plugin = options.delete(:plugin)
  1000. sources = sources.map do |source|
  1001. if plugin
  1002. "/plugin_assets/#{plugin}/javascripts/#{source}"
  1003. else
  1004. source
  1005. end
  1006. end
  1007. end
  1008. super sources, options
  1009. end
  1010. def content_for(name, content = nil, &block)
  1011. @has_content ||= {}
  1012. @has_content[name] = true
  1013. super(name, content, &block)
  1014. end
  1015. def has_content?(name)
  1016. (@has_content && @has_content[name]) || false
  1017. end
  1018. def sidebar_content?
  1019. has_content?(:sidebar) || view_layouts_base_sidebar_hook_response.present?
  1020. end
  1021. def view_layouts_base_sidebar_hook_response
  1022. @view_layouts_base_sidebar_hook_response ||= call_hook(:view_layouts_base_sidebar)
  1023. end
  1024. def email_delivery_enabled?
  1025. !!ActionMailer::Base.perform_deliveries
  1026. end
  1027. # Returns the avatar image tag for the given +user+ if avatars are enabled
  1028. # +user+ can be a User or a string that will be scanned for an email address (eg. 'joe <joe@foo.bar>')
  1029. def avatar(user, options = { })
  1030. if Setting.gravatar_enabled?
  1031. options.merge!({:ssl => (request && request.ssl?), :default => Setting.gravatar_default})
  1032. email = nil
  1033. if user.respond_to?(:mail)
  1034. email = user.mail
  1035. elsif user.to_s =~ %r{<(.+?)>}
  1036. email = $1
  1037. end
  1038. return gravatar(email.to_s.downcase, options) unless email.blank? rescue nil
  1039. else
  1040. ''
  1041. end
  1042. end
  1043. def sanitize_anchor_name(anchor)
  1044. anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
  1045. end
  1046. # Returns the javascript tags that are included in the html layout head
  1047. def javascript_heads
  1048. tags = javascript_include_tag('prototype', 'effects', 'dragdrop', 'controls', 'rails', 'application')
  1049. unless User.current.pref.warn_on_leaving_unsaved == '0'
  1050. tags << "\n".html_safe + javascript_tag("Event.observe(window, 'load', function(){ new WarnLeavingUnsaved('#{escape_javascript( l(:text_warn_on_leaving_unsaved) )}'); });")
  1051. end
  1052. tags
  1053. end
  1054. def favicon
  1055. "<link rel='shortcut icon' href='#{image_path('/favicon.ico')}' />".html_safe
  1056. end
  1057. def robot_exclusion_tag
  1058. '<meta name="robots" content="noindex,follow,noarchive" />'.html_safe
  1059. end
  1060. # Returns true if arg is expected in the API response
  1061. def include_in_api_response?(arg)
  1062. unless @included_in_api_response
  1063. param = params[:include]
  1064. @included_in_api_response = param.is_a?(Array) ? param.collect(&:to_s) : param.to_s.split(',')
  1065. @included_in_api_response.collect!(&:strip)
  1066. end
  1067. @included_in_api_response.include?(arg.to_s)
  1068. end
  1069. # Returns options or nil if nometa param or X-Redmine-Nometa header
  1070. # was set in the request
  1071. def api_meta(options)
  1072. if params[:nometa].present? || request.headers['X-Redmine-Nometa']
  1073. # compatibility mode for activeresource clients that raise
  1074. # an error when unserializing an array with attributes
  1075. nil
  1076. else
  1077. options
  1078. end
  1079. end
  1080. private
  1081. def wiki_helper
  1082. helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting)
  1083. extend helper
  1084. return self
  1085. end
  1086. def link_to_content_update(text, url_params = {}, html_options = {})
  1087. link_to(text, url_params, html_options)
  1088. end
  1089. end