2017-07-26 12:01:51 +03:00
module Additionals
MAX_CUSTOM_MENU_ITEMS = 5
2017-11-17 09:10:50 +03:00
SELECT2_INIT_ENTRIES = 20
2017-07-26 12:01:51 +03:00
2019-01-27 11:40:17 +03:00
GOTO_LIST = " \xc2 \xbb " . freeze
2020-08-06 09:18:30 +03:00
LIST_SEPARATOR = " #{ GOTO_LIST } " . freeze
2018-08-23 15:56:32 +03:00
2020-07-27 11:49:23 +03:00
RenderAsync . configuration . jquery = true
2017-10-03 15:09:32 +03:00
class << self
2018-02-12 16:53:05 +03:00
def setup
2020-04-18 12:58:38 +03:00
incompatible_plugins ( %w[ redmine_issue_control_panel
2018-02-12 16:53:05 +03:00
redmine_editauthor
redmine_changeauthor
2018-08-30 13:15:49 +03:00
redmine_auto_watch ] )
2020-03-27 17:08:37 +03:00
2020-04-12 10:13:59 +03:00
patch ( %w[ AccountController
ApplicationController
AutoCompletesController
Issue
2018-07-18 00:04:49 +03:00
IssuePriority
2018-02-12 16:53:05 +03:00
TimeEntry
2018-12-02 20:40:47 +03:00
Project
2018-02-12 16:53:05 +03:00
Wiki
WikiController
2020-07-27 11:49:23 +03:00
ProjectsController
WelcomeController
2019-10-01 18:19:31 +03:00
ReportsController
2018-12-03 12:15:50 +03:00
Principal
2018-12-02 20:40:47 +03:00
QueryFilter
Role
2019-03-21 21:51:09 +03:00
User
2018-09-12 21:42:36 +03:00
UserPreference ] )
2018-02-12 16:53:05 +03:00
Redmine :: WikiFormatting . format_names . each do | format |
case format
when 'markdown'
2019-10-01 12:21:49 +03:00
Redmine :: WikiFormatting :: Markdown :: HTML . include Patches :: FormatterMarkdownPatch
Redmine :: WikiFormatting :: Markdown :: Helper . include Patches :: FormattingHelperPatch
2018-02-12 16:53:05 +03:00
when 'textile'
2019-10-01 12:21:49 +03:00
Redmine :: WikiFormatting :: Textile :: Formatter . include Patches :: FormatterTextilePatch
Redmine :: WikiFormatting :: Textile :: Helper . include Patches :: FormattingHelperPatch
2018-02-12 16:53:05 +03:00
end
end
2018-07-31 17:09:40 +03:00
IssuesController . send ( :helper , AdditionalsIssuesHelper )
2019-04-24 13:12:34 +03:00
SettingsController . send ( :helper , AdditionalsSettingsHelper )
2018-07-31 17:09:40 +03:00
WikiController . send ( :helper , AdditionalsWikiPdfHelper )
2020-05-05 22:03:33 +03:00
CustomFieldsController . send ( :helper , AdditionalsCustomFieldsHelper )
2019-04-24 13:12:34 +03:00
# Static class patches
2019-10-01 12:21:49 +03:00
Redmine :: AccessControl . include Additionals :: Patches :: AccessControlPatch
2018-02-12 16:53:05 +03:00
# Global helpers
2019-10-01 12:21:49 +03:00
ActionView :: Base . include Additionals :: Helpers
ActionView :: Base . include AdditionalsFontawesomeHelper
ActionView :: Base . include AdditionalsMenuHelper
ActionView :: Base . include Additionals :: AdditionalsSelect2Helper
2018-02-12 16:53:05 +03:00
# Hooks
require_dependency 'additionals/hooks'
# Macros
2019-12-22 10:40:26 +03:00
load_macros ( %w[ cryptocompare date fa gist gmap google_docs group_users iframe
2018-08-31 18:02:03 +03:00
issue redmine_issue redmine_wiki
2018-06-30 14:17:28 +03:00
last_updated_at last_updated_by meteoblue member new_issue project
2020-01-22 13:00:54 +03:00
recently_updated reddit slideshare tradingview twitter user vimeo youtube asciinema ] )
2018-02-12 16:53:05 +03:00
end
2018-12-29 23:33:57 +03:00
def settings_compatible ( plugin_name )
if Setting [ plugin_name ] . class == Hash
2020-03-18 20:41:56 +03:00
# convert Rails 4 data (this runs only once)
new_settings = ActiveSupport :: HashWithIndifferentAccess . new ( Setting [ plugin_name ] )
Setting . send ( " #{ plugin_name } = " , new_settings )
new_settings
2018-09-26 13:52:48 +03:00
else
2018-12-29 20:12:09 +03:00
# Rails 5 uses ActiveSupport::HashWithIndifferentAccess
2018-12-29 23:33:57 +03:00
Setting [ plugin_name ]
2018-09-26 13:52:48 +03:00
end
2017-10-03 15:09:32 +03:00
end
2017-07-26 12:01:51 +03:00
2019-06-22 12:39:42 +03:00
# support with default setting as fall back
def setting ( value )
if settings . key? value
settings [ value ]
else
load_settings [ value ]
end
end
2017-10-03 15:09:32 +03:00
def setting? ( value )
2019-06-22 12:39:42 +03:00
true ? ( setting ( value ) )
2017-10-28 18:31:15 +03:00
end
def true? ( value )
2019-06-13 20:33:59 +03:00
return false if value . is_a? FalseClass
return true if value . is_a? ( TrueClass ) || value . to_i == 1 || value . to_s . casecmp ( 'true' ) . zero?
2018-09-10 18:24:07 +03:00
2017-10-28 18:31:15 +03:00
false
2017-10-03 15:09:32 +03:00
end
2017-08-29 16:39:12 +03:00
2018-03-10 16:07:48 +03:00
def now_with_user_time_zone ( user = User . current )
if user . time_zone . nil?
Time . zone . now
else
user . time_zone . now
end
end
2017-10-03 15:09:32 +03:00
def incompatible_plugins ( plugins = [ ] , title = 'additionals' )
plugins . each do | plugin |
2017-12-13 09:07:18 +03:00
raise " \n \033 [31m #{ title } plugin cannot be used with #{ plugin } plugin'. \033 [0m " if Redmine :: Plugin . installed? ( plugin )
2017-07-13 13:05:17 +03:00
end
end
2017-07-26 12:01:51 +03:00
2017-10-03 15:09:32 +03:00
def patch ( patches = [ ] , plugin_id = 'additionals' )
patches . each do | name |
2019-12-19 10:28:27 +03:00
patch_dir = Rails . root . join ( " plugins/ #{ plugin_id } /lib/ #{ plugin_id } /patches " )
2017-10-03 15:09:32 +03:00
require " #{ patch_dir } / #{ name . underscore } _patch "
2017-07-26 12:01:51 +03:00
2017-10-03 15:09:32 +03:00
target = name . constantize
patch = " #{ plugin_id . camelize } ::Patches:: #{ name } Patch " . constantize
2017-07-13 13:05:17 +03:00
2020-04-12 10:13:59 +03:00
target . include ( patch ) unless target . included_modules . include? ( patch )
2017-07-26 12:01:51 +03:00
end
2017-06-07 20:23:20 +03:00
end
2017-10-03 15:09:32 +03:00
def load_macros ( macros = [ ] , plugin_id = 'additionals' )
2019-12-19 10:28:27 +03:00
macro_dir = Rails . root . join ( " plugins/ #{ plugin_id } /lib/ #{ plugin_id } /wiki_macros " )
2017-10-03 15:09:32 +03:00
macros . each do | macro |
require_dependency " #{ macro_dir } / #{ macro . underscore } _macro "
end
2017-06-07 20:23:20 +03:00
end
2017-10-03 15:09:32 +03:00
def load_settings ( plugin_id = 'additionals' )
2020-08-06 09:18:30 +03:00
cached_settings_name = " @load_settings_ #{ plugin_id } "
2019-06-22 12:39:42 +03:00
cached_settings = instance_variable_get ( cached_settings_name )
if cached_settings . nil?
2019-12-19 10:28:27 +03:00
data = YAML . safe_load ( ERB . new ( IO . read ( Rails . root . join ( " plugins/ #{ plugin_id } /config/settings.yml " ) ) ) . result ) || { }
2019-06-22 12:39:42 +03:00
instance_variable_set ( cached_settings_name , data . symbolize_keys )
else
cached_settings
end
end
2019-12-30 18:09:04 +03:00
def hash_remove_with_default ( field , options , default = nil )
value = nil
if options . key? field
value = options [ field ]
options . delete ( field )
elsif ! default . nil?
value = default
end
[ value , options ]
end
2019-06-22 12:39:42 +03:00
private
def settings
settings_compatible ( :plugin_additionals )
2017-10-03 15:09:32 +03:00
end
2017-07-26 12:01:51 +03:00
end
end