2018-09-12 17:04:39 +03:00
module AdditionalsMenuHelper
2018-09-12 21:42:36 +03:00
def additionals_top_menu_setup
2018-12-14 11:08:57 +03:00
return unless User . current . try ( :hrm_user_type_id ) . nil?
2018-09-12 21:42:36 +03:00
2020-08-06 09:18:30 +03:00
if Additionals . setting? :remove_mypage
2018-09-12 21:42:36 +03:00
Redmine :: MenuManager . map ( :top_menu ) . delete ( :my_page ) if Redmine :: MenuManager . map ( :top_menu ) . exists? ( :my_page )
else
2020-09-02 14:18:05 +03:00
handle_top_menu_item ( :my_page , { url : my_page_path , after : :home , if : proc { User . current . logged? } } )
2018-09-12 21:42:36 +03:00
end
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 )
2018-09-14 13:37:05 +03:00
elsif User . current . logged?
2020-09-02 14:18:05 +03:00
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
2018-09-14 13:37:05 +03:00
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
2020-09-02 14:18:05 +03:00
def handle_top_submenu_item ( menu_name , item )
handle_top_menu_item menu_name , item , with_submenu : true
end
def handle_top_menu_item ( menu_name , item , with_submenu : 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 = { }
2020-09-02 14:18:05 +03:00
css_classes = [ ]
css_classes << 'top-submenu' if with_submenu
css_classes << 'external' if item [ :url ] . include? '://'
html_options [ :class ] = css_classes . join ( ' ' ) if css_classes . present?
2018-09-12 17:04:39 +03:00
html_options [ :title ] = item [ :title ] if item [ :title ] . present?
menu_options = { parent : item [ :parent ] . present? ? item [ :parent ] . to_sym : nil ,
html : html_options }
menu_options [ :if ] = menu_options [ :if ] if menu_options [ :if ] . present?
menu_options [ :caption ] = if item [ :symbol ] . present? && item [ :name ] . present?
font_awesome_icon ( item [ :symbol ] , post_text : item [ :name ] )
elsif item [ :symbol ] . present?
font_awesome_icon ( item [ :symbol ] )
elsif item [ :name ] . present?
item [ :name ] . to_s
end
if item [ :last ] . present? && item [ :last ]
menu_options [ :last ] = true
elsif item [ :before ] . present?
menu_options [ :before ] = item [ :before ]
elsif item [ :after ] . present?
menu_options [ :after ] = item [ :after ]
else
menu_options [ :before ] = :help
end
2020-08-06 09:18:30 +03:00
Redmine :: MenuManager . map ( :top_menu ) . push menu_name , item [ :url ] , menu_options
2018-09-12 17:04:39 +03:00
end
2018-12-14 10:52:18 +03:00
def render_custom_top_menu_item
items = additionals_build_custom_items
return if items . empty?
user_roles = Role . givable
. joins ( :members ) . where ( members : { user_id : User . current . id } )
. joins ( members : :project ) . where ( projects : { status : Project :: STATUS_ACTIVE } )
. distinct
. reorder ( nil )
2020-07-27 11:49:23 +03:00
. ids
2018-12-14 10:52:18 +03:00
items . each do | item |
2020-08-06 09:18:30 +03:00
additionals_custom_top_menu_item item , user_roles
2018-09-12 17:04:39 +03:00
end
2018-12-14 10:52:18 +03:00
end
def additionals_build_custom_items
items = [ ]
Additionals :: MAX_CUSTOM_MENU_ITEMS . times do | num |
menu_name = " custom_menu #{ num } "
item = { menu_name : menu_name . to_sym ,
2020-08-06 09:18:30 +03:00
url : Additionals . setting ( " #{ menu_name } _url " ) ,
name : Additionals . setting ( " #{ menu_name } _name " ) ,
title : Additionals . setting ( " #{ menu_name } _title " ) ,
roles : Additionals . setting ( " #{ menu_name } _roles " ) }
2018-12-14 10:52:18 +03:00
if item [ :name ] . present? && item [ :url ] . present? && item [ :roles ] . present?
items << item
elsif Redmine :: MenuManager . map ( :top_menu ) . exists? ( item [ :menu_name ] )
Redmine :: MenuManager . map ( :top_menu ) . delete ( item [ :menu_name ] )
end
end
items
end
2018-09-12 17:04:39 +03:00
2018-12-14 10:52:18 +03:00
def additionals_custom_top_menu_item ( item , user_roles )
2018-09-12 17:04:39 +03:00
show_entry = false
item [ :roles ] . each do | role |
if user_roles . empty? && role . to_i == Role :: BUILTIN_ANONYMOUS
show_entry = true
break
elsif User . current . logged? && role . to_i == Role :: BUILTIN_NON_MEMBER
# if user is logged in and non_member is active in item,
# always show it
show_entry = true
break
end
user_roles . each do | user_role |
2018-12-14 10:52:18 +03:00
if role . to_i == user_role
2018-09-12 17:04:39 +03:00
show_entry = true
break
end
end
break if show_entry == true
end
if show_entry
2020-09-02 14:18:05 +03:00
handle_top_submenu_item ( item [ :menu_name ] , item )
2018-12-14 10:52:18 +03:00
elsif Redmine :: MenuManager . map ( :top_menu ) . exists? ( item [ :menu_name ] )
Redmine :: MenuManager . map ( :top_menu ) . delete ( item [ :menu_name ] )
2018-09-12 17:04:39 +03:00
end
end
2018-09-14 17:25:07 +03:00
def addtionals_help_plugin_items
user_items = [ { title : 'Redmine Guide' , url : Redmine :: Info . help_url } ,
2020-08-08 19:44:40 +03:00
{ title : " Redmine #{ l :label_macro_plural } " , url : additionals_macros_path } ]
2018-09-14 17:25:07 +03:00
2020-04-08 08:55:04 +03:00
admin_items = [ { title : 'Additionals' ,
url : 'https://additionals.readthedocs.io/en/latest/manual/' , manual : true } ,
{ title : 'Redmine Changelog' ,
url : " https://www.redmine.org/projects/redmine/wiki/Changelog_ #{ Redmine :: VERSION :: MAJOR } _ #{ Redmine :: VERSION :: MINOR } " } ,
{ title : 'Redmine Upgrade' ,
url : 'https://www.redmine.org/projects/redmine/wiki/RedmineUpgrade' } ,
{ title : 'Redmine Security Advisories' ,
url : 'https://www.redmine.org/projects/redmine/wiki/Security_Advisories' } ]
2018-09-14 17:25:07 +03:00
Redmine :: Plugin . all . each do | plugin |
next if plugin . id == :additionals
2018-09-14 18:55:25 +03:00
plugin_item_base = nil
2018-09-14 17:25:07 +03:00
begin
2018-09-14 18:55:25 +03:00
plugin_item_base = plugin . id . to_s . camelize . constantize
2018-10-25 16:02:35 +03:00
rescue LoadError
2018-10-25 17:23:10 +03:00
Rails . logger . debug " Ignore plugin #{ plugin . id } for help integration "
2018-09-14 17:25:07 +03:00
rescue StandardError = > e
raise e unless e . class . to_s == 'NameError'
end
2018-09-14 18:55:25 +03:00
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 17:25:07 +03:00
2018-09-14 18:55:25 +03:00
next if plugin_item . nil?
plugin_item . each do | temp_item |
u_items = if ! temp_item [ :manual ] . nil? && temp_item [ :manual ]
2020-08-08 19:44:40 +03:00
{ title : " #{ temp_item [ :title ] } #{ l :label_help_manual } " , url : temp_item [ :url ] }
2018-09-14 18:55:25 +03:00
else
{ title : temp_item [ :title ] , url : temp_item [ :url ] }
end
if ! temp_item [ :admin ] . nil? && temp_item [ :admin ]
admin_items << u_items
else
user_items << u_items
end
2018-09-14 17:25:07 +03:00
end
end
{ user : user_items , admin : admin_items }
end
2018-09-12 17:04:39 +03:00
def additionals_help_menu_items
2018-09-14 17:25:07 +03:00
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?
2018-09-12 17:38:07 +03:00
pages << { title : '-' }
2018-09-14 17:25:07 +03:00
pages += plugin_items [ :admin ] . sort_by { | k | k [ :title ] }
2018-09-12 17:04:39 +03:00
end
s = [ ]
2018-09-12 17:21:31 +03:00
pages . each_with_index do | item , idx |
2018-09-12 17:38:07 +03:00
s << if item [ :title ] == '-'
2020-08-09 11:11:52 +03:00
tag . li tag . hr
2018-09-12 17:38:07 +03:00
else
2020-08-06 09:18:30 +03:00
html_options = { class : " help_item_ #{ idx } " }
2018-09-12 17:38:07 +03:00
if item [ :url ] . include? '://'
html_options [ :class ] << ' external'
html_options [ :target ] = '_blank'
end
2020-07-27 11:49:23 +03:00
tag . li ( link_to ( item [ :title ] , item [ :url ] , html_options ) )
2018-09-12 17:38:07 +03:00
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
2018-09-14 17:25:07 +03:00
# Plugin help items definition for plugins,
# which do not have additionals_help_menu_items integration
def additionals_help_items_fallbacks ( plugin_id )
2018-12-02 10:35:44 +03:00
plugins = { redmine_wiki_lists : [ { title : 'Wiki Lists Macros' ,
2018-09-14 17:25:07 +03:00
url : 'https://www.r-labs.org/projects/wiki_lists/wiki/Wiki_Lists_en' } ] ,
redmine_wiki_extensions : [ { title : 'Wiki Extensions' ,
2018-09-14 18:55:25 +03:00
url : 'https://www.r-labs.org/projects/r-labs/wiki/Wiki_Extensions_en' } ] ,
redmine_git_hosting : [ { title : 'Redmine Git Hosting' ,
url : 'http://redmine-git-hosting.io/get_started/' ,
admin : true } ] ,
2018-09-14 17:25:07 +03:00
redmine_contacts : [ { title : 'Redmine CRM' ,
2018-09-14 18:55:25 +03:00
url : 'https://www.redmineup.com/pages/help/crm' ,
admin : true } ] ,
2018-09-14 17:25:07 +03:00
redmine_contacts_helpdesk : [ { title : 'Redmine Helpdesk' ,
2018-09-14 18:55:25 +03:00
url : 'https://www.redmineup.com/pages/help/helpdesk' ,
admin : true } ] ,
2018-09-14 17:25:07 +03:00
redmine_ldap_sync : [ { title : 'Redmine LDAP' ,
2018-09-14 18:55:25 +03:00
url : 'https://www.redmine.org/projects/redmine/wiki/RedmineLDAP' ,
admin : true } ,
2018-09-14 17:25:07 +03:00
{ title : 'Redmine LDAP Sync' ,
2018-09-14 18:55:25 +03:00
url : 'https://github.com/thorin/redmine_ldap_sync/blob/master/README.md' ,
admin : true } ] }
2018-09-14 17:25:07 +03:00
plugins [ plugin_id ]
end
2018-09-12 17:04:39 +03:00
end