# coding: utf-8
module ForemanTasks
module ForemanTasksHelper
def recurring_logic_state(recurring_logic)
icon, status = case recurring_logic.state
when 'active'
'glyphicon-info-sign'
when 'finished'
['glyphicon-ok-sign', 'status-ok']
when 'cancelled'
['glyphicon-warning-sign', 'status-error']
else
'glyphicon-question-sign'
end
content_tag(:i, ' '.html_safe, :class => "glyphicon #{icon}") + content_tag(:span, recurring_logic.humanized_state, :class => status)
end
def task_result_icon_class(task)
return 'task-status pficon-help' if task.state != 'stopped'
icon_class = case task.result
when 'success'
'pficon-ok'
when 'error'
'pficon-error-circle-o'
when 'warning'
'pficon-ok status-warn'
else
'pficon-help'
end
"task-status #{icon_class}"
end
def time_in_words_span(time)
if time.nil?
_('N/A')
else
content_tag :span, (time > Time.now.utc ? _('in %s') : _('%s ago')) % time_ago_in_words(time),
:'data-original-title' => time.try(:in_time_zone), :rel => 'twipsy'
end
end
def duration_in_words_span(start, finish)
if start.nil?
_('N/A')
else
content_tag :span, distance_of_time_in_words(start, finish),
:'data-original-title' => number_with_delimiter((finish - start).to_i) + _(' seconds'), :rel => 'twipsy'
end
end
def recurring_logic_action_buttons(recurring_logic)
buttons = []
if authorized_for(:permission => :edit_recurring_logics, :auth_object => recurring_logic)
buttons << link_to(N_('Cancel'), cancel_foreman_tasks_recurring_logic_path(recurring_logic), :method => :post, :class => 'btn btn-danger') unless %w[cancelled finished].include? recurring_logic.state
end
button_group buttons
end
def recurring_logic_next_occurrence(recurring_logic)
if %w[finished cancelled].include? recurring_logic.state
'-'
else
recurring_logic.next_occurrence_time
end
end
def time_f(f, attr, field_options = {}, time_options = {}, html_options = {})
f.fields_for attr do |fields|
field(fields, attr, field_options) do
fields.time_select attr, time_options, html_options
end
end
end
def date_f(f, attr, field_options = {}, date_options = {}, html_options = {})
f.fields_for attr do |fields|
field(fields, attr, field_options) do
fields.date_select attr, date_options, html_options
end
end
end
def datetime_f(f, attr, field_options = {}, datetime_options = {}, html_options = {})
f.fields_for attr do |fields|
field(fields, attr, field_options) do
[
content_tag(:span, nil, :class => 'date', :style => 'white-space: nowrap;') do
fields.date_select(attr, datetime_options, html_options)
end,
' — ',
content_tag(:span, nil, :class => 'time', :style => 'white-space: nowrap;') do
fields.time_select(attr, datetime_options.merge(:ignore_date => true), html_options)
end
].join
end
end
end
def inline_checkboxes_f(f, attr, field_options = {}, checkboxes = {}, options = {})
field(f, attr, field_options) do
checkboxes.map do |key, name|
[f.check_box(key, options), " #{name} "]
end.flatten.join('')
end
end
def trigger_selector(f, triggering = Triggering.new, _options = {})
render :partial => 'common/trigger_form', :locals => { :f => f, :triggering => triggering }
end
private
def future_mode_fieldset(f, triggering)
tags = []
tags << text_f(f, :start_at_raw, :label => _('Start at'), :placeholder => 'YYYY-mm-dd HH:MM')
tags << text_f(f, :start_before_raw, :label => _('Start before'), :placeholder => 'YYYY-mm-dd HH:MM',
:label_help => _('Indicates that the action should be cancelled if it cannot be started before this time.'))
content_tag(:fieldset, nil, :id => 'trigger_mode_future', :class => "trigger_mode_form #{'hidden' unless triggering.future?}") do
tags.join.html_safe
end
end
def recurring_mode_fieldset(f, triggering)
tags = []
tags << selectable_f(f, :input_type, %w[cronline monthly weekly daily hourly], {}, :label => _('Repeats'), :id => 'input_type_selector')
tags += [
cronline_fieldset(f, triggering),
monthly_fieldset(f, triggering),
weekly_fieldset(f, triggering),
time_picker_fieldset(f, triggering)
]
content_tag(:fieldset, nil, :id => 'trigger_mode_recurring', :class => "trigger_mode_form #{'hidden' unless triggering.recurring?}") do
tags.join.html_safe
end
end
def cronline_fieldset(f, triggering)
options = [
# TRANSLATORS: this translation is referring to an option which is a time interval
_('is minute (range: 0-59)'),
# TRANSLATORS: this translation is referring to an option which is a time interval
_('is hour (range: 0-23)'),
# TRANSLATORS: this translation is referring to an option which is a time interval
_('is day of month (range: 1-31)'),
# TRANSLATORS: this translation is referring to an option which is a time interval
_('is month (range: 1-12)'),
# TRANSLATORS: this translation is referring to an option which is a time interval
_('is day of week (range: 0-6)')
].map { |opt| content_tag(:li, opt) }.join
help = _("Cron line format 'a b c d e', where: %s") % "