This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First commit to prepare for Rails5, Redmine4.
- Loading branch information
1 parent
e597c6b
commit 7495836
Showing
18 changed files
with
310 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
module Banner | ||
class BannerHeaderHooks < Redmine::Hook::ViewListener | ||
include ApplicationHelper | ||
include BannerHelper | ||
|
||
def view_layouts_base_html_head(_context = {}) | ||
o = stylesheet_link_tag('banner', plugin: 'redmine_banner') | ||
o << javascript_include_tag('banner', plugin: 'redmine_banner') | ||
o | ||
end | ||
end | ||
|
||
# | ||
# for Project Banner | ||
# | ||
class ProjectBannerMessageHooks < Redmine::Hook::ViewListener | ||
def view_layouts_base_content(context = {}) | ||
context[:controller].send( | ||
:render_to_string, | ||
partial: 'banner/project_body_bottom' | ||
) | ||
end | ||
end | ||
|
||
class BannerMessageHooks < Redmine::Hook::ViewListener | ||
include BannerHelper | ||
|
||
# Override for conditional render_on | ||
# Ref. http://www.redmine.org/boards/3/topics/4316 | ||
# | ||
def self.render_on(hook, options = {}) | ||
define_method hook do |context| | ||
if !options.include?(:if) || evaluate_if_option(options[:if], context) | ||
context[:controller].send(:render_to_string, { locals: context }.merge(options)) | ||
end | ||
end | ||
end | ||
|
||
def pass_timer?(_context) | ||
banner_setting = Setting.plugin_redmine_banner | ||
return true unless banner_setting['use_timer'] == 'true' | ||
|
||
now = Time.now | ||
start_date = get_time( | ||
banner_setting['start_ymd'], | ||
banner_setting['start_hour'], | ||
banner_setting['start_min'] | ||
) | ||
|
||
end_date = get_time( | ||
banner_setting['end_ymd'], | ||
banner_setting['end_hour'], | ||
banner_setting['end_min'] | ||
) | ||
now.between?(start_date, end_date) | ||
end | ||
|
||
def should_display_header?(context) | ||
# When Disabled, false. | ||
return false if Setting.plugin_redmine_banner['display_part'] == 'footer' | ||
pass_timer?(context) | ||
end | ||
|
||
def should_display_footer?(context) | ||
# When Disabled, false. | ||
return false if Setting.plugin_redmine_banner['display_part'] == 'header' | ||
pass_timer?(context) | ||
end | ||
|
||
render_on :view_layouts_base_body_bottom, partial: 'banner/body_bottom', | ||
if: :should_display_footer? | ||
|
||
private | ||
|
||
def evaluate_if_option(if_option, context) | ||
return false unless should_display(context) | ||
case if_option | ||
when Symbol | ||
send(if_option, context) | ||
when Method, Proc | ||
if_option.call(context) | ||
end | ||
end | ||
|
||
def should_display(context) | ||
banner_setting = Setting.plugin_redmine_banner | ||
return false if ((context[:controller].class.name != 'AccountController') && | ||
(context[:controller].action_name != 'login')) && | ||
(banner_setting['display_only_login_page'] == 'true') | ||
|
||
return false if !User.current.logged? && banner_setting['only_authenticated'] == 'true' | ||
return false unless banner_setting['enable'] == 'true' | ||
true | ||
end | ||
end | ||
|
||
# TODO: view_layouts_base_after_top_menu is not supported Redmine itself. | ||
# Now use javascript to insert after top-menu. (Submitted ticket: http://www.redmine.org/issues/9915) | ||
class BannerTopMenuHooks < BannerMessageHooks | ||
render_on :view_layouts_base_body_bottom, partial: 'banner/after_top_menu', | ||
if: :should_display_header? | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module Banner | ||
module BannerHelper | ||
def get_time(ymd, h, m) | ||
d = Date.strptime(ymd, '%Y-%m-%d') | ||
Time.mktime(d.year, d.month, d.day, h.to_i, m.to_i) | ||
end | ||
|
||
def enabled?(project) | ||
return false if project.nil? | ||
project.module_enabled? :banner | ||
end | ||
|
||
def action_to_display?(controller, display_part) | ||
action_name = controller.action_name | ||
controller_name = controller.controller_name | ||
return true if display_part == 'all' | ||
|
||
case display_part | ||
when 'overview' then | ||
return true if controller_name == 'projects' && action_name == 'show' | ||
when 'overview_and_issues' then | ||
if controller_name == 'issues' || (controller_name == 'projects' && action_name == 'show') | ||
return true | ||
end | ||
when 'new_issue' then | ||
return true if controller_name == 'issues' && action_name == 'new' | ||
else | ||
return false | ||
end | ||
end | ||
|
||
module_function :enabled? | ||
module_function :action_to_display? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'projects_helper' | ||
|
||
module Banner | ||
module ProjectsHelperPatch | ||
extend ActiveSupport::Concern | ||
|
||
def project_settings_tabs | ||
tabs = super | ||
action = { name: 'banner', | ||
controller: 'banner', | ||
action: :show, | ||
partial: 'banner/show', label: :banner } | ||
tabs << action if User.current.allowed_to?(action, @project) | ||
tabs | ||
end | ||
end | ||
end | ||
|
||
ProjectsHelper.prepend Banner::ProjectsHelperPatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Patch for banner plugin. This affects in "plugin" action of Redmine Settings | ||
# controller. | ||
# Now banner plugin does not have own model(table). So, datetime informations | ||
# are stored as string and required datetime validation by controller. | ||
# | ||
# TODO Store banner settings to banner's own model (table). | ||
# | ||
module Banner | ||
module SettingsControllerPatch | ||
extend ActiveSupport::Concern | ||
include BannerHelper | ||
# included do | ||
# alias_method_chain(:plugin, :banner_date_validation) | ||
# end | ||
|
||
# | ||
# Before posting start / end date, do validation check.(In case setting "Use timer".) | ||
# | ||
def plugin | ||
param_id = params[:id] | ||
return super if param_id != 'redmine_banner' | ||
plugin = Redmine::Plugin.find(param_id) | ||
settings = Setting["plugin_#{plugin.id}"] | ||
|
||
@banner_updated_on = nil | ||
if Setting.find_by_name('plugin_redmine_banner').present? | ||
@banner_updated_on = Setting.find_by_name('plugin_redmine_banner').updated_on.localtime | ||
end | ||
|
||
# date range check | ||
current_time = Time.now | ||
begin | ||
# date range check | ||
@start_datetime = generate_time(settings, 'start', current_time) | ||
@end_datetime = generate_time(settings, 'end', current_time) | ||
rescue => ex | ||
# Ref. https://github.com/akiko-pusu/redmine_banner/issues/11 | ||
# Logging when Argument Error | ||
if logger | ||
logger.warn "Redmine Banner Warning: #{ex} / Invalid date setting / From #{settings['start_ymd']} to #{settings['end_ymd']}. Reset to current datetime. " | ||
end | ||
@start_datetime = current_time | ||
@end_datetime = current_time | ||
settings['use_timer'] = 'false' | ||
end | ||
|
||
if request.post? | ||
param_settings = params[:settings] | ||
return super if param_settings[:use_timer] != 'true' | ||
begin | ||
unless validate_date_range?(param_settings) | ||
flash[:error] = l(:error_banner_date_range) | ||
redirect_to action: 'plugin', id: plugin.id | ||
return | ||
end | ||
|
||
rescue => ex | ||
# Argument Error | ||
# TODO: Exception will happen about 2038 problem. (Fixed on Ruby1.9) | ||
s_string = "#{param_settings[:start_ymd]} #{param_settings[:start_hour]}:#{param_settings[:start_min]}" | ||
e_string = "#{param_settings[:end_ymd]} #{param_settings[:end_hour]}:#{param_settings[:end_min]}" | ||
|
||
flash[:error] = "#{l(:error_banner_date_range)} / #{ex}: From #{s_string} to #{e_string} " | ||
redirect_to action: 'plugin', id: plugin.id | ||
return | ||
end | ||
end | ||
|
||
# Continue to do default action | ||
super | ||
end | ||
|
||
private | ||
|
||
def generate_time(settings, type, current_time) | ||
return current_time if settings["#{type}_ymd"].blank? | ||
|
||
# generate time | ||
d = Date.strptime(settings["#{type}_ymd"], '%Y-%m-%d') | ||
d_year = d.year.to_i | ||
d_month = d.month.to_i | ||
d_day = d.day.to_i | ||
d_hour = settings["#{type}_hour"].blank? ? current_time.hour.to_i : settings["#{type}_hour"].to_i | ||
d_min = settings["#{type}_min"].blank? ? current_time.min.to_i : settings["#{type}_min"].to_i | ||
Time.mktime(d_year, d_month, d_day, d_hour, d_min) | ||
end | ||
|
||
def validate_date_range?(param_settings) | ||
s_time = get_time(param_settings[:start_ymd], param_settings[:start_hour], param_settings[:start_min]) | ||
e_time = get_time(param_settings[:end_ymd], param_settings[:end_hour], param_settings[:end_min]) | ||
e_time > s_time | ||
end | ||
end | ||
end | ||
|
||
SettingsController.prepend Banner::SettingsControllerPatch |
Oops, something went wrong.