2012-04-15 19:59:39 +02:00
|
|
|
# encoding: utf-8
|
|
|
|
#
|
2009-01-06 11:49:19 +01:00
|
|
|
# Methods added to this helper will be available to all templates in the application.
|
|
|
|
module ApplicationHelper
|
|
|
|
|
|
|
|
def format_time(time = Time.now)
|
2009-01-12 18:26:09 +01:00
|
|
|
I18n.l(time, :format => "%d.%m.%Y %H:%M") unless time.nil?
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_date(time = Time.now)
|
2009-01-12 18:26:09 +01:00
|
|
|
I18n.l(time.to_date) unless time.nil?
|
2009-01-06 15:45:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def format_datetime(time = Time.now)
|
2009-01-12 18:26:09 +01:00
|
|
|
I18n.l(time) unless time.nil?
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-08-22 18:14:56 +02:00
|
|
|
|
|
|
|
def format_datetime_timespec(time, format)
|
|
|
|
I18n.l(time, :format => format) unless (time.nil? or format.nil?)
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
# Creates ajax-controlled-links for pagination
|
2009-01-14 12:46:01 +01:00
|
|
|
def pagination_links_remote(collection, options = {})
|
|
|
|
per_page = options[:per_page] || @per_page
|
|
|
|
params = options[:params] || {}
|
2009-01-06 11:49:19 +01:00
|
|
|
params = params.merge({:per_page => per_page})
|
2012-10-08 15:24:32 +02:00
|
|
|
paginate collection, :params => params, :remote => true
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Link-collection for per_page-options when using the pagination-plugin
|
2009-01-14 12:46:01 +01:00
|
|
|
def items_per_page(options = {})
|
|
|
|
per_page_options = options[:per_page_options] || [20, 50, 100]
|
|
|
|
current = options[:current] || @per_page
|
2011-06-10 12:03:32 +02:00
|
|
|
params = params || {}
|
2009-01-14 12:46:01 +01:00
|
|
|
|
2011-06-10 12:03:32 +02:00
|
|
|
links = per_page_options.map do |per_page|
|
|
|
|
params.merge!({:per_page => per_page})
|
2012-10-08 15:24:32 +02:00
|
|
|
link_class = 'btn'
|
|
|
|
link_class << ' disabled' if per_page == current
|
|
|
|
link_to(per_page, params, :remote => true, class: link_class)
|
|
|
|
end
|
|
|
|
|
|
|
|
content_tag :div, class: 'btn-group pull-right' do
|
|
|
|
links.join.html_safe
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2012-10-08 15:24:32 +02:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2011-05-16 11:00:52 +02:00
|
|
|
|
2009-04-03 17:16:39 +02:00
|
|
|
def sort_link_helper(text, key, options = {})
|
2012-11-10 16:44:05 +01:00
|
|
|
# Hmtl options
|
2009-04-03 17:16:39 +02:00
|
|
|
remote = options[:remote].nil? ? true : options[:remote]
|
2012-11-10 16:44:05 +01:00
|
|
|
class_name = case params[:sort]
|
|
|
|
when key then
|
|
|
|
'sortup'
|
|
|
|
when key + '_reverse' then
|
|
|
|
'sortdown'
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
html_options = {
|
2013-04-10 17:29:06 +02:00
|
|
|
:title => I18n.t('helpers.application.sort_by', text: text),
|
2012-11-10 16:44:05 +01:00
|
|
|
:remote => remote,
|
|
|
|
:class => class_name
|
2009-01-06 11:49:19 +01:00
|
|
|
}
|
2009-04-03 17:16:39 +02:00
|
|
|
|
2012-11-10 16:44:05 +01:00
|
|
|
|
|
|
|
# Url options
|
|
|
|
key += "_reverse" if params[:sort] == key
|
|
|
|
per_page = options[:per_page] || @per_page
|
|
|
|
url_options = params.merge(per_page: per_page, sort: key)
|
|
|
|
url_options.merge!({page: params[:page]}) if params[:page]
|
|
|
|
url_options.merge!({query: params[:query]}) if params[:query]
|
|
|
|
|
|
|
|
link_to(text, url_for(url_options), html_options)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generates a link to the top of the website
|
|
|
|
def link_to_top
|
2012-10-15 21:19:17 +02:00
|
|
|
link_to '#' do
|
|
|
|
content_tag :i, nil, class: 'icon-arrow-up icon-large'
|
|
|
|
end
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
# Returns the weekday. 0 is sunday, 1 is monday and so on
|
|
|
|
def weekday(dayNumber)
|
2013-04-10 17:29:06 +02:00
|
|
|
weekdays = I18n.t('date.day_names')
|
2009-01-06 11:49:19 +01:00
|
|
|
return weekdays[dayNumber]
|
|
|
|
end
|
|
|
|
|
|
|
|
# to set a title for both the h1-tag and the title in the header
|
2009-08-12 18:41:25 +02:00
|
|
|
def title(page_title, show_title = true)
|
2011-05-11 13:38:46 +02:00
|
|
|
content_for(:title) { page_title.to_s }
|
2009-08-12 18:41:25 +02:00
|
|
|
@show_title = show_title
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_title?
|
|
|
|
@show_title
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2009-01-08 16:33:27 +01:00
|
|
|
|
|
|
|
def tab_is_active?(tab)
|
2010-03-22 01:58:37 +01:00
|
|
|
tab[:active].detect {|c| controller.controller_path.match(c) }
|
2009-01-08 16:33:27 +01:00
|
|
|
end
|
2009-01-16 16:19:26 +01:00
|
|
|
|
|
|
|
def icon(name, options={})
|
2011-06-10 12:18:55 +02:00
|
|
|
icons = {
|
2013-04-10 17:29:06 +02:00
|
|
|
:delete => { :file => 'b_drop.png', :alt => I18n.t('ui.delete')},
|
|
|
|
:edit => { :file => 'b_edit.png', :alt => I18n.t('ui.edit')},
|
|
|
|
:members => { :file => 'b_users.png', :alt => I18n.t('helpers.application.edit_user')}
|
2011-06-10 12:18:55 +02:00
|
|
|
}
|
2009-10-16 21:06:55 +02:00
|
|
|
options[:alt] ||= icons[name][:alt]
|
|
|
|
options[:title] ||= icons[name][:title]
|
2009-01-16 16:19:26 +01:00
|
|
|
options.merge!({:size => '16x16',:border => "0"})
|
|
|
|
|
|
|
|
image_tag icons[name][:file], options
|
|
|
|
end
|
2009-01-29 21:28:22 +01:00
|
|
|
|
|
|
|
# Remote links with default 'loader'.gif during request
|
|
|
|
def remote_link_to(text, options={})
|
2009-02-10 13:26:10 +01:00
|
|
|
remote_options = {
|
|
|
|
:before => "Element.show('loader')",
|
|
|
|
:success => "Element.hide('loader')",
|
|
|
|
:method => :get
|
|
|
|
}
|
2012-05-12 10:55:20 +02:00
|
|
|
link_to(text, options[:url], remote_options.merge(options))
|
2009-01-29 21:28:22 +01:00
|
|
|
end
|
2009-02-02 16:35:43 +01:00
|
|
|
|
|
|
|
def format_roles(record)
|
|
|
|
roles = []
|
2013-04-10 17:29:06 +02:00
|
|
|
roles << I18n.t('helpers.application.role_admin') if record.role_admin?
|
|
|
|
roles << I18n.t('helpers.application.role_finance') if record.role_finance?
|
|
|
|
roles << I18n.t('helpers.application.role_suppliers') if record.role_suppliers?
|
|
|
|
roles << I18n.t('helpers.application.role_article_meta') if record.role_article_meta?
|
|
|
|
roles << I18n.t('helpers.application.role_orders') if record.role_orders?
|
2009-02-02 16:35:43 +01:00
|
|
|
roles.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def link_to_gmaps(address)
|
2013-04-10 17:29:06 +02:00
|
|
|
link_to h(address), "http://maps.google.com/?q=#{h(address)}", :title => I18n.t('helpers.application.show_google_maps'),
|
2009-02-02 16:35:43 +01:00
|
|
|
:target => "_blank"
|
|
|
|
end
|
2012-11-12 14:24:49 +01:00
|
|
|
|
|
|
|
# offers a link for writing message to user
|
|
|
|
# checks for nil (useful for relations)
|
|
|
|
def link_to_user_message_if_valid(user)
|
2013-01-26 16:24:45 +01:00
|
|
|
user.nil? ? '??' : link_to(user.nick, new_message_path('message[mail_to]' => user.id),
|
2013-04-10 17:29:06 +02:00
|
|
|
:title => I18n.t('write_message'))
|
2012-12-14 18:10:46 +01:00
|
|
|
end
|
2009-02-02 16:35:43 +01:00
|
|
|
|
2012-10-08 11:51:56 +02:00
|
|
|
def bootstrap_flash
|
|
|
|
flash_messages = []
|
|
|
|
flash.each do |type, message|
|
|
|
|
type = :success if type == :notice
|
|
|
|
type = :error if type == :alert
|
|
|
|
text = content_tag(:div,
|
2013-04-10 17:34:24 +02:00
|
|
|
content_tag(:button, I18n.t('ui.marks.close').html_safe, :class => "close", "data-dismiss" => "alert") +
|
2012-10-08 11:51:56 +02:00
|
|
|
message, :class => "alert fade in alert-#{type}")
|
|
|
|
flash_messages << text if message
|
|
|
|
end
|
|
|
|
flash_messages.join("\n").html_safe
|
|
|
|
end
|
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|