#-- encoding: UTF-8
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2017 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'forwardable'
require 'cgi'
module ApplicationHelper
include OpenProject::TextFormatting
include OpenProject::ObjectLinking
include I18n
include Redmine::I18n
include HookHelper
extend Forwardable
def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
# Return true if user is authorized for controller/action, otherwise false
def authorize_for(controller, action, project: @project)
User.current.allowed_to?({ controller: controller, action: action }, project)
end
# Display a link if user is authorized
#
# @param [String] name Anchor text (passed to link_to)
# @param [Hash] options Hash params. This will checked by authorize_for to see if the user is authorized
# @param [optional, Hash] html_options Options passed to link_to
# @param [optional, Hash] parameters_for_method_reference Extra parameters for link_to
#
# When a block is given, skip the name parameter
def link_to_if_authorized(*args, &block)
name = args.shift unless block_given?
options = args.shift || {}
html_options = args.shift
parameters_for_method_reference = args
return unless authorize_for(options[:controller] || params[:controller], options[:action])
if block_given?
link_to(options, html_options, *parameters_for_method_reference, &block)
else
link_to(name, options, html_options, *parameters_for_method_reference)
end
end
def required_field_name(name = '')
safe_join [name, ' ', content_tag('span', '*', class: 'required')]
end
def li_unless_nil(link, options = {})
content_tag(:li, link, options) if link
end
# Show a sorted linkified (if active) comma-joined list of users
def list_users(users, options = {})
users.sort.map { |u| link_to_user(u, options) }.join(', ')
end
# returns a class name based on the user's status
def user_status_class(user)
'status_' + user.status_name
end
def user_status_i18n(user)
t "status_#{user.status_name}"
end
def toggle_link(name, id, options = {}, html_options = {})
onclick = "jQuery('##{id}').toggle(); "
onclick << (options[:focus] ? "jQuery('##{options[:focus]}').focus(); " : 'this.blur(); ')
onclick << 'return false;'
link_to(name, '#', { onclick: onclick }.merge(html_options))
end
def delete_link(url, options = {})
options = {
method: :delete,
data: { confirm: l(:text_are_you_sure) },
class: 'icon icon-delete'
}.merge(options)
link_to l(:button_delete), url, options
end
def image_to_function(name, function, html_options = {})
html_options.symbolize_keys!
tag(:input, html_options.merge(
type: 'image', src: image_path(name),
onclick: (html_options[:onclick] ? "#{html_options[:onclick]}; " : '') +
"#{function};"
))
end
def format_activity_title(text)
h(truncate_single_line(text, length: 100))
end
def format_activity_day(date)
date == User.current.today ? l(:label_today).titleize : format_date(date)
end
def format_activity_description(text)
html_escape_once(truncate(text.to_s, length: 120).gsub(%r{[\r\n]*<(pre|code)>.*$}m, '...'))
.gsub(/[\r\n]+/, '
')
.html_safe
end
def format_version_name(version)
h(version.to_s_for_project(@project))
end
def due_date_distance_in_words(date)
if date
label = date < Date.today ? :label_roadmap_overdue : :label_roadmap_due_in
l(label, distance_of_date_in_words(Date.today, date))
end
end
def render_page_hierarchy(pages, node = nil, options = {})
return '' unless pages[node]
content_tag :ul, class: 'pages-hierarchy' do
pages[node].map { |page|
content_tag :li do
title = if options[:timestamp] && page.updated_on
l(:label_updated_time, distance_of_time_in_words(Time.now, page.updated_on))
end
concat link_to(page.title, project_wiki_path(page.project, page),
title: title)
concat render_page_hierarchy(pages, page.id, options) if pages[page.id]
end
}.join.html_safe
end
end
def error_messages_for(*params)
objects, options = extract_objects_from_params(params)
error_messages = objects.map { |o| o.errors.full_messages }.flatten
unless error_messages.empty?
render partial: 'common/validation_error',
locals: { error_messages: error_messages,
object_name: options[:object_name].to_s.gsub('_', '') }
end
end
# Taken from Dynamic Form
#
# lib/action_view/helpers/dynamic_form.rb:187-198
def extract_objects_from_params(params)
options = params.extract_options!.symbolize_keys
objects = Array.wrap(options.delete(:object) || params).map { |object|
object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
object = convert_to_model(object)
if object.class.respond_to?(:model_name)
options[:object_name] ||= object.class.model_name.human.downcase
end
object
}
[objects.compact, options]
end
# Renders flash messages
def render_flash_messages
flash.map { |k, v| render_flash_message(k, v) }.join.html_safe
end
def join_flash_messages(messages)
if messages.respond_to?(:join)
messages.join('
').html_safe
else
messages
end
end
def render_flash_message(type, message, html_options = {})
css_classes = ["flash #{type} icon icon-#{type}", html_options.delete(:class)]
# Add autohide class to notice flashes if configured
if type.to_s == 'notice' && User.current.pref.auto_hide_popups?
css_classes << 'autohide-notification'
end
html_options = { class: css_classes.join(' '), role: 'alert' }.merge(html_options)
content_tag :div, html_options do
if User.current.impaired?
concat(content_tag('a', join_flash_messages(message),
href: 'javascript:;',
class: 'impaired--empty-link'))
concat(content_tag(:i, '', class: 'icon-close close-handler',
tabindex: '0',
role: 'button',
aria: { label: ::I18n.t('js.close_popup_title') }))
else
concat(join_flash_messages(message))
concat(content_tag(:i, '', class: 'icon-close close-handler',
tabindex: '0',
role: 'button',
aria: { label: ::I18n.t('js.close_popup_title') }))
end
end
end
# Renders tabs and their content
def render_tabs(tabs)
if tabs.any?
render partial: 'common/tabs', locals: { tabs: tabs }
else
content_tag 'p', l(:label_no_data), class: 'nodata'
end
end
def project_tree_options_for_select(projects, selected: nil, disabled: {}, &_block)
options = ''.html_safe
Project.project_level_list(projects).each do |element|
identifier = element[:project].id
tag_options = {
value: h(identifier),
title: h(element[:project].name),
}
if !selected.nil? && selected.id == identifier
tag_options[:selected] = true
end
tag_options[:disabled] = true if disabled.include? identifier
content = ''.html_safe
content << (' ' * 3 * element[:level] + '» ').html_safe if element[:level] > 0
content << element[:project].name
options << content_tag('option', content, tag_options)
end
options
end
# Yields the given block for each project with its level in the tree
#
# Wrapper for Project#project_tree
def project_tree(projects, &block)
Project.project_tree(projects, &block)
end
# Returns a lft-sorted project hierarchy only when
# the sort helper has deemed a non-default sort option to be selected.
def project_tree_when_sorted(projects, &block)
if default_sort_order?
project_tree(projects, &block)
else
projects.each do |p|
yield p, 0
end
end
end
def project_nested_ul(projects, &_block)
s = ''
if projects.any?
ancestors = []
Project.project_tree(projects) do |project, _level|
if ancestors.empty? || project.is_descendant_of?(ancestors.last)
s << "