additionals/app/helpers/additionals_menu_helper.rb

163 lines
5.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2018-09-12 17:04:39 +03:00
module AdditionalsMenuHelper
2018-09-12 21:42:36 +03:00
def additionals_top_menu_setup
return if AdditionalsPlugin.active_hrm?
2018-09-12 21:42:36 +03:00
2020-08-06 09:18:30 +03:00
if Additionals.setting? :remove_help
2018-09-12 21:42:36 +03:00
Redmine::MenuManager.map(:top_menu).delete(:help) if Redmine::MenuManager.map(:top_menu).exists?(:help)
elsif User.current.logged?
handle_top_submenu_item :help, url: '#', symbol: 'fas_question', last: true
2018-09-12 21:42:36 +03:00
@additionals_help_items = additionals_help_menu_items
else
handle_top_menu_item :help, url: Redmine::Info.help_url, symbol: 'fas_question', last: true
2018-09-12 21:42:36 +03:00
end
end
def handle_top_submenu_item(menu_name, **item)
handle_top_menu_item menu_name, with_submenu: true, **item
end
2021-08-04 07:47:41 +03:00
def handle_top_menu_item(menu_name, url:, with_submenu: false, onlyif: nil,
name: nil, parent: nil, title: nil, symbol: nil, before: nil, after: nil, last: false)
2018-09-12 17:04:39 +03:00
Redmine::MenuManager.map(:top_menu).delete(menu_name.to_sym) if Redmine::MenuManager.map(:top_menu).exists?(menu_name.to_sym)
html_options = {}
css_classes = []
css_classes << 'top-submenu' if with_submenu
2021-08-04 07:47:41 +03:00
css_classes << 'external' if url.include? '://'
html_options[:class] = css_classes.join ' ' if css_classes.present?
2021-08-04 07:47:41 +03:00
html_options[:title] = title if title.present?
2018-09-12 17:04:39 +03:00
2021-08-04 07:47:41 +03:00
menu_options = { parent: parent.present? ? parent.to_sym : nil,
2018-09-12 17:04:39 +03:00
html: html_options }
2021-08-04 07:47:41 +03:00
menu_options[:if] = onlyif if onlyif.present?
2018-09-12 17:04:39 +03:00
2021-08-04 07:47:41 +03:00
menu_options[:caption] = if symbol.present? && name.present?
font_awesome_icon symbol, post_text: name
elsif symbol.present?
font_awesome_icon symbol
elsif name.present?
name.to_s
2018-09-12 17:04:39 +03:00
end
2021-08-04 07:47:41 +03:00
if last
2018-09-12 17:04:39 +03:00
menu_options[:last] = true
2021-08-04 07:47:41 +03:00
elsif before.present?
menu_options[:before] = before
elsif after.present?
menu_options[:after] = after
2018-09-12 17:04:39 +03:00
else
menu_options[:before] = :help
end
2021-08-04 07:47:41 +03:00
Redmine::MenuManager.map(:top_menu).push menu_name, url, **menu_options
2018-09-12 17:04:39 +03:00
end
def addtionals_help_plugin_items
2022-05-01 16:25:37 +03:00
user_items = [{ title: 'Redmine Guide',
url: Redmine::Info.help_url,
id: :redmine_guide },
{ title: "Redmine #{l :label_macro_plural}",
url: additionals_macros_path,
id: :macros }]
2022-04-28 16:02:32 +03:00
admin_items = [{ title: 'Redmine Changelog',
2022-05-01 16:25:37 +03:00
url: "https://www.redmine.org/projects/redmine/wiki/Changelog_#{Redmine::VERSION::MAJOR}_#{Redmine::VERSION::MINOR}",
id: :changelog },
{ title: 'Redmine Upgrade',
2022-05-01 16:25:37 +03:00
url: 'https://www.redmine.org/projects/redmine/wiki/RedmineUpgrade',
2022-10-24 20:01:07 +03:00
id: :redmine_upgrade },
{ title: 'Redmine Security Advisories',
2022-10-24 20:01:07 +03:00
url: 'https://www.redmine.org/projects/redmine/wiki/Security_Advisories',
id: :security_advisories }]
Redmine::Plugin.all.each do |plugin|
next if plugin.id == :additionals
2018-09-14 18:55:25 +03:00
plugin_item_base = nil
begin
2018-09-14 18:55:25 +03:00
plugin_item_base = plugin.id.to_s.camelize.constantize
rescue LoadError
2021-06-21 15:52:57 +03:00
Rails.logger.debug { "Ignore plugin #{plugin.id} for help integration" }
rescue StandardError => e
2023-04-12 15:15:53 +03:00
raise e unless e.instance_of? ::NameError
end
plugin_item = plugin_item_base.try :additionals_help_items unless plugin_item_base.nil?
plugin_item = additionals_help_items_fallbacks plugin.id if plugin_item.nil?
2018-09-14 18:55:25 +03:00
next if plugin_item.nil?
plugin_item.each do |temp_item|
2022-05-01 16:25:37 +03:00
title = Array temp_item[:title]
title << l("label_help_#{temp_item[:type]}") if temp_item.key? :type
u_items = { title: safe_join(title, ' '),
url: temp_item[:url],
id: temp_item[:id] }
2018-09-14 18:55:25 +03:00
if !temp_item[:admin].nil? && temp_item[:admin]
admin_items << u_items
else
user_items << u_items
end
end
end
{ user: user_items, admin: admin_items }
end
2018-09-12 17:04:39 +03:00
def additionals_help_menu_items
plugin_items = addtionals_help_plugin_items
pages = plugin_items[:user].sort_by { |k| k[:title] }
2018-09-12 17:04:39 +03:00
if User.current.admin?
2022-05-01 16:25:37 +03:00
pages << { title: '-', id: :sep }
pages += plugin_items[:admin].sort_by { |k| k[:title] }
2018-09-12 17:04:39 +03:00
end
s = []
2022-05-01 16:25:37 +03:00
ids = []
pages.each do |item|
next unless item.key? :id
id = item[:id]
next if ids.include? id
ids << id
s << if item[:title] == '-'
2020-08-09 11:11:52 +03:00
tag.li tag.hr
else
2022-05-01 16:25:37 +03:00
html_options = { class: +"help_item_#{id}" }
if item[:url].include? '://'
html_options[:class] << ' external'
html_options[:target] = '_blank'
end
tag.li link_to(item[:title], item[:url], html_options)
end
2018-09-12 17:04:39 +03:00
end
2020-08-09 11:11:52 +03:00
safe_join s
2018-09-12 17:04:39 +03:00
end
# Plugin help items definition for plugins,
# which do not have additionals_help_menu_items integration
def additionals_help_items_fallbacks(plugin_id)
plugins = { redmine_drawio: [{ title: 'draw.io usage',
2022-05-01 16:25:37 +03:00
id: :drawio,
url: 'https://github.com/mikitex70/redmine_drawio#usage' }],
redmine_contacts: [{ title: 'Redmine CRM',
2022-05-01 16:25:37 +03:00
id: :crm,
2018-09-14 18:55:25 +03:00
url: 'https://www.redmineup.com/pages/help/crm',
admin: true }],
redmine_contacts_helpdesk: [{ title: 'Redmine Helpdesk',
2022-05-01 16:25:37 +03:00
id: :helpdesk,
2018-09-14 18:55:25 +03:00
url: 'https://www.redmineup.com/pages/help/helpdesk',
2022-05-01 16:25:37 +03:00
admin: true }] }
plugins[plugin_id]
end
2018-09-12 17:04:39 +03:00
end