2021-04-18 13:34:55 +02:00
# frozen_string_literal: true
2021-12-07 19:03:09 +01:00
require 'redmine_plugin_kit'
2017-07-26 11:01:51 +02:00
module Additionals
MAX_CUSTOM_MENU_ITEMS = 5
2021-04-18 13:34:55 +02:00
DEFAULT_MODAL_WIDTH = '350px'
GOTO_LIST = " \xc2 \xbb "
2023-11-05 06:21:43 +01:00
LIST_SEPARATOR = " #{ GOTO_LIST } " . freeze
2018-08-23 14:56:32 +02:00
2024-08-26 19:24:34 +02:00
SQL_NO_RESULT_CONDITION = '1=0'
SQL_WITH_TRUE_CONDITION = '1=1'
2021-12-07 19:03:09 +01:00
include RedminePluginKit :: PluginBase
2020-12-11 13:35:37 +01:00
2021-12-07 18:51:09 +01:00
class << self
2020-10-13 19:41:52 +02:00
def class_prefix ( klass )
klass_name = klass . is_a? ( String ) ? klass : klass . name
klass_name . underscore . tr '/' , '_'
end
2018-03-10 14:07:48 +01:00
def now_with_user_time_zone ( user = User . current )
if user . time_zone . nil?
2022-06-29 09:28:17 +02:00
Time . current
2018-03-10 14:07:48 +01:00
else
user . time_zone . now
end
end
2021-08-25 17:55:16 +02:00
def time_zone_correct ( time , user : User . current )
timezone = user . time_zone || Time . zone
timezone . utc_offset - Time . zone . local_to_utc ( time ) . localtime . utc_offset
end
2019-12-30 16:09:04 +01:00
def hash_remove_with_default ( field , options , default = nil )
value = nil
if options . key? field
value = options [ field ]
2020-09-08 09:40:06 +02:00
options . delete field
2019-12-30 16:09:04 +01:00
elsif ! default . nil?
value = default
end
[ value , options ]
end
2023-12-15 11:46:01 +01:00
def single_page_limit
Setting . per_page_options_array . second || Setting . per_page_options_array . first || 25
2023-03-03 07:28:04 +01:00
end
2021-06-13 19:17:30 +02:00
def split_ids ( phrase , limit : nil )
2023-12-15 11:46:01 +01:00
limit || = single_page_limit
2022-06-08 20:01:59 +02:00
raw_ids = phrase . strip_split
2021-06-13 19:17:30 +02:00
ids = [ ]
raw_ids . each do | id |
if id . include? '-'
range = id . split ( '-' ) . map ( & :strip )
if range . size == 2
left_id = range . first . to_i
right_id = range . last . to_i
min = [ left_id , right_id ] . min
max = [ left_id , right_id ] . max
# if range to large, take lowest numbers + last possible number
ids << if max - min > limit
old_max = max
max = limit + min - 2
ids << ( min .. max ) . to_a
old_max
else
( min .. max ) . to_a
end
end
else
ids << id . to_i
end
end
ids . flatten!
ids . uniq!
ids . take limit
end
2022-03-12 17:50:45 +01:00
def max_live_search_results
if setting ( :max_live_search_results ) . present?
setting ( :max_live_search_results ) . to_i
else
50
end
end
2021-12-29 10:18:07 +01:00
def debug ( message = 'running' , console : false )
if console
RedminePluginKit :: Debug . msg message
else
RedminePluginKit :: Debug . log message
end
2021-12-07 18:51:09 +01:00
end
2024-08-25 19:14:50 +02:00
def redmine6?
Redmine :: VERSION . to_s > = '5.1' && Redmine :: VERSION :: BRANCH == 'devel'
end
2019-06-22 11:39:42 +02:00
private
2021-12-07 18:51:09 +01:00
def setup
RenderAsync . configuration . jquery = true
loader . add_patch %w[ ApplicationController
AutoCompletesController
Issue
TimeEntry
2022-05-26 18:41:01 +02:00
Mailer
2021-12-07 18:51:09 +01:00
Project
2022-04-07 20:19:19 +02:00
ProjectQuery
2021-12-07 18:51:09 +01:00
Wiki
2022-12-16 19:33:54 +01:00
WikiPage
2021-12-07 18:51:09 +01:00
ProjectsController
WelcomeController
ReportsController
2023-01-09 17:22:02 +01:00
SettingsController
2021-12-07 18:51:09 +01:00
Principal
Query
QueryFilter
Role
User
UserPreference ]
loader . add_helper %w[ Issues
Wiki
CustomFields ]
2023-01-23 10:23:23 +01:00
loader . add_helper ( { controller : 'Issues' , helper : 'AdditionalsCommonJournals' } )
2023-10-13 16:09:34 +02:00
loader . add_patch [ { target : Redmine :: Views :: LabelledFormBuilder , patch : 'LabelledFormBuilder' } ]
2021-12-07 18:51:09 +01:00
loader . add_global_helper [ Additionals :: Helpers ,
2024-10-05 16:33:56 +02:00
AdditionalsIconsHelper ,
2021-12-07 18:51:09 +01:00
AdditionalsFontawesomeHelper ,
AdditionalsMenuHelper ,
AdditionalsSelect2Helper ]
Redmine :: WikiFormatting . format_names . each do | format |
case format
when 'markdown'
loader . add_patch [ { target : Redmine :: WikiFormatting :: Markdown :: HTML , patch : 'FormatterMarkdown' } ,
{ target : Redmine :: WikiFormatting :: Markdown :: Helper , patch : 'FormattingHelper' } ]
2022-04-04 19:00:53 +02:00
when 'common_mark'
2022-04-10 13:45:24 +02:00
loader . add_patch [ { target : Redmine :: WikiFormatting :: CommonMark :: Formatter , patch : 'FormatterCommonMark' } ]
2022-04-04 19:00:53 +02:00
loader . add_patch [ { target : Redmine :: WikiFormatting :: CommonMark :: Helper , patch : 'FormattingHelper' } ]
2021-12-07 18:51:09 +01:00
when 'textile'
loader . add_patch [ { target : Redmine :: WikiFormatting :: Textile :: Formatter , patch : 'FormatterTextile' } ,
{ target : Redmine :: WikiFormatting :: Textile :: Helper , patch : 'FormattingHelper' } ]
end
end
2022-04-10 13:45:24 +02:00
# Clients
loader . require_files File . join ( 'wiki_formatting' , 'common_mark' , '**/*_filter.rb' )
2021-12-07 18:51:09 +01:00
# Apply patches and helper
loader . apply!
# Macros
loader . load_macros!
# Load view hooks
loader . load_view_hooks!
2017-10-03 14:09:32 +02:00
end
2017-07-26 11:01:51 +02:00
end
2020-11-29 13:17:02 +01:00
# Run the classic redmine plugin initializer after rails boot
class Plugin < :: Rails :: Engine
2022-04-10 13:45:24 +02:00
require 'tanuki_emoji'
2020-11-29 13:17:02 +01:00
require 'render_async'
require 'rss'
2020-11-29 16:58:44 +01:00
require 'slim'
2020-11-29 13:17:02 +01:00
config . after_initialize do
# engine_name could be used (additionals_plugin), but can
# create some side effencts
plugin_id = 'additionals'
# if plugin is already in plugins directory, use this and leave here
next if Redmine :: Plugin . installed? plugin_id
# gem is used as redmine plugin
require File . expand_path '../init' , __dir__
Additionals :: Gemify . install_assets plugin_id
Additionals :: Gemify . create_plugin_hint plugin_id
end
end
2017-07-26 11:01:51 +02:00
end
2022-06-08 20:01:59 +02:00
class String
def strip_split ( sep = ',' )
split ( sep ) . map ( & :strip ) . compact_blank
end
end
class Array
# alias for join with ', ' as seperator
2023-10-18 10:40:49 +02:00
def to_comma_list
2022-06-08 20:01:59 +02:00
join ', '
end
end