2025-01-21 11:06:52 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2014-07-04 14:28:12 +02:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2014-07-04 14:28:12 +02:00
|
|
|
#
|
|
|
|
|
# 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:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2014-07-04 14:28:12 +02:00
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2014-07-04 14:28:12 +02:00
|
|
|
#++
|
|
|
|
|
|
|
|
|
|
module OpenProject
|
|
|
|
|
module ObjectLinking
|
2015-09-15 09:58:33 +02:00
|
|
|
# path helpers shim to support deprecated :only_path option
|
|
|
|
|
%i(project settings_project topic work_package).each do |model|
|
|
|
|
|
define_method :"#{model}_path_or_url" do |*args, options|
|
|
|
|
|
if options.delete(:only_path) == false
|
|
|
|
|
__send__(:"#{model}_url", *args, options)
|
|
|
|
|
else
|
|
|
|
|
__send__(:"#{model}_path", *args, options)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-04 14:28:12 +02:00
|
|
|
# Displays a link to user's account page if active or registered
|
2025-01-28 09:13:03 +01:00
|
|
|
# Will attach a user hover card to the link.
|
2025-01-30 09:12:42 +01:00
|
|
|
def link_to_user(user, options = {}) # rubocop:disable Metrics/AbcSize
|
2026-05-28 13:02:42 +02:00
|
|
|
return content_tag(:span, h(user.to_s), class: options[:class]) unless user.is_a?(User)
|
|
|
|
|
return content_tag(:span, h(user.name), class: options[:class]) if user_not_linkable?(user)
|
2018-08-09 12:53:38 +01:00
|
|
|
|
2025-01-26 22:31:47 +01:00
|
|
|
only_path = options.delete(:only_path) { true }
|
|
|
|
|
name = options.delete(:name) { user.name }
|
|
|
|
|
options[:title] ||= I18n.t(:label_user_named, name:)
|
2025-01-21 09:05:58 +01:00
|
|
|
|
2025-02-07 10:59:09 +01:00
|
|
|
add_hover_card_options(user, options, only_path:)
|
2025-01-26 22:31:47 +01:00
|
|
|
|
|
|
|
|
link_to(name, user_url(user, only_path:), options)
|
2014-07-04 14:28:12 +02:00
|
|
|
end
|
|
|
|
|
|
2025-01-26 22:31:47 +01:00
|
|
|
# Displays a link to group's account page
|
2021-02-22 20:02:21 +01:00
|
|
|
def link_to_group(group, options = {})
|
2026-05-28 13:02:42 +02:00
|
|
|
return content_tag(:span, h(group.to_s), class: options[:class]) unless group.is_a?(Group)
|
2021-02-22 20:02:21 +01:00
|
|
|
|
|
|
|
|
name = group.name
|
|
|
|
|
href = show_group_url(group,
|
|
|
|
|
only_path: options.delete(:only_path) { true })
|
|
|
|
|
options[:title] ||= I18n.t(:label_group_named, name:)
|
|
|
|
|
|
|
|
|
|
link_to(name, href, options)
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-04 14:28:12 +02:00
|
|
|
# Generates a link to an attachment.
|
|
|
|
|
# Options:
|
|
|
|
|
# * :text - Link text (default to attachment filename)
|
|
|
|
|
# * :download - Force download (default: false)
|
2014-11-03 21:53:03 +01:00
|
|
|
def link_to_attachment(attachment, options = {})
|
2014-07-04 14:28:12 +02:00
|
|
|
text = options.delete(:text) || attachment.filename
|
|
|
|
|
|
2019-06-12 16:12:07 +02:00
|
|
|
link_to text,
|
|
|
|
|
url_to_attachment(attachment, only_path: options.delete(:only_path) { true }),
|
2014-07-04 14:28:12 +02:00
|
|
|
options
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Generates a link to a SCM revision
|
|
|
|
|
# Options:
|
|
|
|
|
# * :text - Link text (default to the formatted revision)
|
2014-11-03 21:53:03 +01:00
|
|
|
def link_to_revision(revision, project, options = {})
|
2015-04-01 14:23:18 +02:00
|
|
|
text = options.delete(:text) || format_revision(revision)
|
|
|
|
|
rev = revision.respond_to?(:identifier) ? revision.identifier : revision
|
|
|
|
|
url_opts = { controller: "/repositories", action: "revision", project_id: project, rev: }
|
2020-09-16 11:26:15 +02:00
|
|
|
html_options = { title: I18n.t(:label_revision_id, value: format_revision(revision)) }.merge(options)
|
2015-04-01 14:23:18 +02:00
|
|
|
link_to(h(text), url_opts, html_options)
|
2014-07-04 14:28:12 +02:00
|
|
|
end
|
|
|
|
|
|
2023-03-13 20:33:41 +01:00
|
|
|
# Generates a link to a query
|
|
|
|
|
def link_to_query(query, options = {}, html_options = nil)
|
|
|
|
|
text = h(query.name)
|
|
|
|
|
url = project_work_packages_url([query.project.id], only_path: options.delete(:only_path) { true }, query_id: query.id)
|
|
|
|
|
link_to(text, url, html_options)
|
|
|
|
|
end
|
|
|
|
|
|
2014-07-04 14:28:12 +02:00
|
|
|
# Generates a link to a message
|
2026-02-03 16:56:47 +01:00
|
|
|
def link_to_message(message, options = {}, html_options = nil) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
|
|
|
|
|
only_path = options.delete(:only_path)
|
|
|
|
|
link = if only_path
|
|
|
|
|
project_forum_topic_path(message.forum.project, message.forum, options.delete(:no_root) ? message : message.root,
|
|
|
|
|
{
|
|
|
|
|
r: message.parent_id && message.id,
|
|
|
|
|
anchor: (message.parent_id ? "message-#{message.id}" : nil)
|
|
|
|
|
}.merge(options))
|
|
|
|
|
else
|
|
|
|
|
project_forum_topic_url(message.forum.project, message.forum, options.delete(:no_root) ? message : message.root,
|
|
|
|
|
{
|
|
|
|
|
r: message.parent_id && message.id,
|
|
|
|
|
anchor: (message.parent_id ? "message-#{message.id}" : nil)
|
|
|
|
|
}.merge(options))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
link_to(h(truncate(message.subject, length: 60)), link, html_options)
|
2014-07-04 14:28:12 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Generates a link to a project if active
|
|
|
|
|
# Examples:
|
|
|
|
|
#
|
|
|
|
|
# link_to_project(project) # => link to the specified project overview
|
2015-10-29 19:12:02 +01:00
|
|
|
# link_to_project(project, {only_path: false}, class: "project") # => 3rd arg adds html options
|
|
|
|
|
# link_to_project(project, {}, class: "project") # => html options with default url (project overview)
|
2014-07-04 14:28:12 +02:00
|
|
|
#
|
2014-11-03 21:53:03 +01:00
|
|
|
def link_to_project(project, options = {}, html_options = nil, show_icon = false)
|
2019-06-12 16:12:07 +02:00
|
|
|
project_name = project_link_name(project, show_icon)
|
2014-07-04 14:28:12 +02:00
|
|
|
|
2020-01-13 10:27:44 +01:00
|
|
|
if project.active?
|
2019-06-12 16:12:07 +02:00
|
|
|
link_to(project_name, project_path_or_url(project, options), html_options)
|
|
|
|
|
else
|
|
|
|
|
project_name
|
2026-01-26 06:40:58 +01:00
|
|
|
end
|
2019-06-12 16:12:07 +02:00
|
|
|
end
|
|
|
|
|
|
2025-01-28 09:13:03 +01:00
|
|
|
# Like #link_to_user, but will render a Primer link instead of a regular link.
|
2025-01-21 11:06:52 +01:00
|
|
|
def primer_link_to_user(user, options = {})
|
|
|
|
|
options[:href] ||= user_path(user)
|
|
|
|
|
options[:target] ||= "_blank"
|
|
|
|
|
options[:underline] ||= false
|
|
|
|
|
|
|
|
|
|
options = add_hover_card_options(user, options)
|
|
|
|
|
|
|
|
|
|
render Primer::Beta::Link.new(**options) do
|
|
|
|
|
user.name
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-12 16:12:07 +02:00
|
|
|
private
|
|
|
|
|
|
2025-01-28 09:13:03 +01:00
|
|
|
# Accepts a user and an options hash. Will apply a hover card config for the user to the options hash.
|
|
|
|
|
# Will not do anything if `hover_card` is set to false within the options.
|
|
|
|
|
# You can use this method if you want to render a link and apply a user hover card to it.
|
2025-02-07 10:59:09 +01:00
|
|
|
def add_hover_card_options(user, options, only_path: true)
|
2025-02-05 07:48:25 +01:00
|
|
|
if options.delete(:hover_card) { true } && user.is_a?(User)
|
2025-01-21 11:06:52 +01:00
|
|
|
options[:data] ||= {}
|
2025-01-23 14:53:47 +01:00
|
|
|
|
|
|
|
|
hover_card_url = hover_card_user_url(user, only_path:)
|
|
|
|
|
options[:data][:hover_card_url] = hover_card_url
|
2025-02-06 18:04:25 +01:00
|
|
|
options[:data][:hover_card_trigger_target] = "trigger"
|
2025-01-21 11:06:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
options
|
|
|
|
|
end
|
|
|
|
|
|
2019-06-12 16:12:07 +02:00
|
|
|
def project_link_name(project, show_icon)
|
2014-07-04 14:28:12 +02:00
|
|
|
if show_icon && User.current.member_of?(project)
|
2026-02-18 07:47:11 +01:00
|
|
|
label = ActiveSupport::SafeBuffer.new
|
|
|
|
|
label << I18n.t(:description_my_project)
|
|
|
|
|
label << " ".html_safe
|
|
|
|
|
|
|
|
|
|
icon_wrapper("icon-context icon-star", label) + project.name
|
2019-06-12 16:12:07 +02:00
|
|
|
else
|
|
|
|
|
project.name
|
2014-07-04 14:28:12 +02:00
|
|
|
end
|
2019-06-12 16:12:07 +02:00
|
|
|
end
|
2014-07-04 14:28:12 +02:00
|
|
|
|
2019-06-12 16:12:07 +02:00
|
|
|
def url_to_attachment(attachment, only_path: true)
|
|
|
|
|
if only_path
|
|
|
|
|
v3_paths.attachment_content(attachment.id)
|
2014-07-04 14:28:12 +02:00
|
|
|
else
|
2019-06-12 16:12:07 +02:00
|
|
|
v3_paths.url_for(:attachment_content, attachment.id)
|
2014-07-04 14:28:12 +02:00
|
|
|
end
|
|
|
|
|
end
|
2023-07-18 18:01:34 +02:00
|
|
|
|
|
|
|
|
def url_to_file_link(file_link, only_path: true)
|
|
|
|
|
if only_path
|
|
|
|
|
v3_paths.file_link_open(file_link.id)
|
|
|
|
|
else
|
|
|
|
|
v3_paths.url_for(:file_link_open, file_link.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
2023-07-19 17:44:14 +02:00
|
|
|
|
|
|
|
|
def v3_paths
|
|
|
|
|
# Including the module breaks the application in strange and mysterious ways
|
|
|
|
|
API::V3::Utilities::PathHelper::ApiV3Path
|
|
|
|
|
end
|
2026-05-28 13:02:42 +02:00
|
|
|
|
|
|
|
|
def user_not_linkable?(user)
|
|
|
|
|
(user.locked? || user.deleted?) && !User.current.admin?
|
|
|
|
|
end
|
2014-07-04 14:28:12 +02:00
|
|
|
end
|
|
|
|
|
end
|