This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
orbit-4-1/app/helpers/orbit_backend_helper.rb

448 lines
16 KiB
Ruby
Raw Permalink Normal View History

2012-04-29 15:39:28 +00:00
module OrbitBackendHelper
#========================================================================================================================
# from bulletin_helper.rb
#========================================================================================================================
def show_reject_reason(object)
by_object = object.is_rejected
by_user = (((object.create_user_id == current_user.id) rescue nil) or is_manager? or is_admin?)
by_object && by_user
2012-04-29 15:39:28 +00:00
end
def show_form_status_field(object)
#by_object = (!object.is_expired? and object.is_pending?)
by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin? or is_sub_manager?)
by_user
2012-04-29 15:39:28 +00:00
end
def show_approval_link(object)
by_object = (!object.is_expired? and object.is_pending?)
by_user = ((object.category.authed_users("approval_#{@module_app.key}").include?(current_user) rescue nil) or is_manager? or is_admin? or is_sub_manager?)
by_object and by_user
2012-04-29 15:39:28 +00:00
end
def show_delete_link(object)
if !current_user.nil?
2013-10-18 07:25:19 +00:00
if object.class.fields.include?('create_user_id')
by_object = (object.create_user_id == current_user.id )
by_user = (is_manager? or is_admin?)
by_object or by_user
else
true
end
else
false
end
end
# def show_bulletin_title_at_index (bulletin)
# if bulletin.is_checked?
# link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue ''
# else
# bulletin.title
# end
# end
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
#========================================================================================================================
2012-04-29 15:39:28 +00:00
def is_filter_active?(type, id)
' active' if (@filter[type].include?(id.to_s) rescue nil)
end
def is_sort_active?(field)
res = t(field[:translation])
2013-10-18 07:25:19 +00:00
if params[:sort] && params[:sort].eql?(field[:sort])
res << " " + content_tag(:b, nil, class: (params[:direction].eql?('asc') ? "icons-arrow-up-3" : "icons-arrow-down-4 "))
end
res.html_safe
end
def show_toggle_archive_btn(object)
object.disable ? t(:disable) : t(:enable)
end
def sortable_options(column)
direction = (column == params[:sort] && params[:direction] == "asc") ? "desc" : "asc"
{:sort => column, :direction => direction}
end
# ===============================================================
# associated:
#
# TODO: link for other types
# ===============================================================
def get_value(object, field, authorization, approvable)
res = ''
case field[:type]
when 'associated'
sorted_objects = get_sorted_objects(object.send(field[:db_field]), field[:model_field]) rescue ''
when 'id'
sorted_objects = get_sorted_objects(field[:model].find(object.send(field[:db_field])), field[:model_field]) rescue ''
when 'status'
status = []
if object.is_top?
status << content_tag(:span, t(:top), class: "label label-success")
end
if object.is_hot?
status << content_tag(:span, t(:hot), class: "label label-important")
end
if object.is_hidden?
status << content_tag(:span, t(:hidden), class: "label")
end
2013-07-10 08:42:25 +00:00
if @approvable
if object.is_pending?
status << content_tag(:span, t(:pending), class: "label")
2013-07-10 08:42:25 +00:00
end
if object.is_checked?
status << content_tag(:span, t(:passed), class: "label")
2013-07-10 08:42:25 +00:00
end
if object.is_rejected?
status << content_tag(:span, t(:rejected), class: "label")
2013-07-10 08:42:25 +00:00
end
end
res << status.join(' ')
when 'tags'
sorted_objects = get_sorted_objects(object.tags, 'name') rescue ''
2013-10-18 07:25:19 +00:00
when 'standalone'
res << (field[:need_value] ? eval(field[:display_option]) : field[:display_option])
when 'image'
image = (field[:version] ? object.send(field[:db_field]).send(field[:version]) : object.send(field[:db_field]))
res << image_tag(image)
else
sorted_values = object.send(field[:db_field])
sorted_values = sorted_values.sort if sorted_values.is_a?(Array)
end
2013-10-18 07:25:19 +00:00
res << display_value_from_object(object, sorted_values, field[:display_option]).to_s if sorted_values
res << display_value_from_objects(sorted_objects, field[:model_field], field[:display_option]).to_s if sorted_objects
if field[:quick_edit]
res << (content_tag :div, class: "quick-edit" do
content_tag :ul, class: "nav nav-pills" do
@quick_edit.each do |quick|
concat get_quick_link(quick, object, authorization, approvable)
end
end
end)
end
res.html_safe
end
def index_table
content_tag :table, class: "table main-list" do
concat (content_tag :thead do
(content_tag :tr, class: "sort-header" do
@fields.each do |field|
2013-10-18 07:25:19 +00:00
concat (content_tag :th, "data-hide" => field[:hide], class: "#{field[:header_class]} #{'active' if (params[:sort] && params[:sort].eql?(field[:sort]))}" do
if field[:sort]
link_to is_sort_active?(field), url_for({:filter => @filter}.merge(sortable_options(field[:sort]).merge(:sort_options => field[:db_field])))
else
t(field[:translation])
end
end)
2012-04-29 15:39:28 +00:00
end
end)
end)
concat (content_tag :tbody do
(@objects.each do |object|
authorization = !@authorization || (@authorization && is_authorized(object))
approvable = !@approvable || (@approvable && is_approvable(object))
concat (content_tag :tr do
(@fields.each do |field|
concat(content_tag :td, get_value(object, field, authorization, approvable))
end)
end)
end)
end)
end
end
def set_default_index(&block)
2013-07-10 08:42:25 +00:00
@approvable = @module_app.is_approvable
@authorization = @module_app.is_authorizable
@fields = []
@filter_fields =[]
@filterable = false
@index_footer = nil
@quick_edit = []
block.call
end
protected
def display_value(value)
case value.class.name
when 'DateTime'
display_date_time(value)
when 'Date'
display_date(value)
when 'Time'
display_time(value)
else
value
end
end
def display_value_from_object(object, value, display_option)
if display_option
eval(display_option)
else
display_value(value)
end
end
def display_value_from_objects(objects, field, display_option)
if display_option
objects = Array(objects).map do |object|
eval(display_option)
end
objects.join(' ')
else
objects = Array(objects).map do |object|
display_value(object.send(field)) rescue ''
end
objects.join(', ')
end
end
# ===============================================================
# type:
# check "get_value"
# associated_value:
# check "get_value"
# db_field:
# name of the field(s) in the database
# translation:
# key in the yml translation files
# hide:
# - no value: always shown
# - all: alway hidden
# - phone: hidden for phone
# sort:
# unique value used for keeping the sorted column
# link:
# if provided, the values will be shown as link
# (only available for type 'field' for now)
# ===============================================================
def field(args={})
if !args.blank? && args[:translation] && args[:db_field]
case args[:type]
when 'associated'
@fields << args if args[:model_field] || args[:display_option]
when 'id'
@fields << args if args[:model] && (args[:model_field] || args[:display_option])
when 'image'
args[:header_class] ||= 'span2'
@fields << args
else
@fields << args
2012-04-29 15:39:28 +00:00
end
elsif !args.blank? && args[:type].eql?('tags')
args[:translation] ||= 'tags'
args[:display_option] ||= "content_tag(:span, object.name, class: 'label label-warning')"
@fields << args
2013-10-18 07:25:19 +00:00
elsif !args.blank? && args[:type].eql?('standalone')
@fields << args
2012-04-29 15:39:28 +00:00
end
end
def filter_field(args={})
if !args.blank? && args[:type] && args[:values] && args[:translation]
case args[:type]
when 'array'
@filter_fields << args
when 'objects'
@filter_fields << args if args[:object_field]
2012-09-13 18:38:56 +00:00
end
@filterable ||= true
2012-04-29 15:39:28 +00:00
end
end
# ===============================================================
# args:
# - paginate: default true, there'es no need to specify it
# - link: path to the new action
# TODO: links to other actions
# ===============================================================
def footer(args={})
if args[:link_name]
link_name = args[:link_name]
else
link_name = content_tag(:i, nil, :class => 'icon-plus') + ' ' + t(:add)
end
paginate = args.has_key?(:paginate) ? args[:paginate] : true
link = (is_manager? || is_sub_manager? rescue nil) && args.has_key?(:link) ? true : false
if paginate || link
@index_footer = content_tag :div, class: "bottomnav clearfix" do
concat content_tag :div, link_to(link_name, args[:link].blank? ? '#' : send(args[:link]), :class => (args[:link_class] || 'btn btn-primary'), data: args[:link_data]), class: "action pull-right" if link
concat content_tag :div, paginate(@objects, :params => {:direction => params[:direction], :sort => params[:sort], :filter => @filter, :new_filter => nil, :sort_options => params[:sort_options]}), class: "pagination pagination-centered" if paginate
end
end
end
def get_quick_link(quick, object, authorization, approvable)
if !quick[:link_option].blank?
link_option = ",#{quick[:link_option]}"
else
link_option = ""
end
case quick[:type]
when 'approval'
if show_approval_link(object)
content_tag :li, link_to(t(quick[:translation] || :approval_), eval("#{quick[:link]}('#{object.id}')"), class: "preview_trigger #{quick[:class]}")
end
when 'authorization'
if is_admin? || is_manager?
content_tag :li, link_to(t(quick[:translation] || :authorization_), eval("#{quick[:link]}"), class: "preview_trigger #{quick[:class]}")
end
when 'edit'
if authorization && approvable
content_tag :li, link_to(t(quick[:translation] || :edit), quick[:link].nil? ? '#' : eval("#{quick[:link]}('#{object.id}'#{link_option})"), class: quick[:class], data: eval("#{quick[:data]}"))
end
when 'delete'
if show_delete_link(object)
@delete_options = {title: quick[:title], warning: quick[:warning], cancel: quick[:cancel], submit: quick[:submit]}
content_tag :li, link_to(t(quick[:translation] || :delete_), '#', rel: eval("#{quick[:link]}('#{object.id}'#{link_option})"), class: "delete #{quick[:class] || 'text-error'}")
end
when 'detail'
content_tag :li, link_to(t(quick[:translation] || :detail), '#', class: (quick[:class] || "detail-row"))
when 'reject_reason'
if show_reject_reason(object)
content_tag :li, link_to(t(quick[:translation] || :rejected_reason) + ' ' + truncate(object.not_checked_reason, :length => 10), '#', rel: "tooltip", 'data-original-title' => (wrap_string_with(object.not_checked_reason, :line_width => 24)), class: "reject_info #{quick[:class]}")
end
else
content_tag :li, link_to(t(quick[:translation]), eval("#{quick[:link]}('#{object.id}')"), class: quick[:class])
end
end
def get_sorted_objects(objects, field)
if field
objects_array = Array(objects).inject([]){ |result, value|
result << [value.send(field), value]
}
objects_array.sort.map{|d| d[1]}
else
objects
end
end
def is_approvable(object)
current_or_guest_user.admin? || (!object.is_rejected? && !object.is_checked?)
end
def is_authorized(object)
autorized = @module_app.authorizable_models.inject(false) do |autorized, klass|
if object.is_a?(klass.constantize)
2013-08-19 10:54:35 +00:00
autorized ||= object.user_can_sub_manage?(current_user)
else
2014-01-23 04:28:31 +00:00
autorized ||= object.send(klass.underscore).user_can_sub_manage?(current_user) rescue nil
end
autorized
end
at_least_module_manager || autorized
end
def quick_edit_link(args)
@quick_edit << args
end
def objects(objects)
@objects = objects
end
def with_link?(field, object, value)
if field[:link] && @approvable
if object.class.instance_methods.include?(:is_checked?) && object.is_checked?
link_to(value, eval("#{field[:link]}('#{object.id}')"))
# link_to bulletin.title, panel_announcement_front_end_bulletin_path(bulletin, :category_id => bulletin.bulletin_category.id) rescue ''
else
value
end
elsif field[:link]
link_to(value, eval("#{field[:link]}('#{object.id}')"))
else
value
end
2012-07-05 08:00:45 +00:00
end
# ===============================================================
# Breadcrumbs
# ===============================================================
def back_end_breadcrumb
res = ''
divider = "<span class='divider'>/</span>"
res << "<li><a href='#{admin_dashboards_path}'>#{t(:dashboard_)}</a>#{divider}</li>"
case controller.controller_name
when 'authorizations'
res << "<li><a href='/#{params[:controller]}/#{@module_app.key}'>#{@module_app.title}</a>#{divider}</li>"
case params[:type]
when 'approval'
res << "<li class='active'>#{t(:approval_)}</li>"
when 'category'
res << "<li class='active'>#{t(:category_auth)}</li>"
when nil
res << "<li class='active'>#{t(:module_authorization)}</li>"
else
res << "<li class='active'>#{params[:type].underscore.humanize.capitalize} #{t(:authorization_)}</li>"
end
when 'categories'
res << "<li>#{@module_app.title}#{divider}</li>"
res << "<li class='active'>#{t(:categories)}</li>"
when 'module_tags'
res << "<li>#{@module_app.title}#{divider}</li>"
res << "<li class='active'>#{t(:tags)}</li>"
else
if params[:action].eql?('index')
res << "<li>#{@module_app.title}</li>"
else
res << "<li><a href='/#{params[:controller]}'>#{@module_app.title}</a>#{divider}</li>"
res << "<li class='active'>#{t(params[:action], scope: 'restful_actions')}</li>"
end
end
res.html_safe
end
2013-12-23 10:33:40 +00:00
def site_breadcrumb
res = ''
divider = "<span class='divider'>/</span>"
2013-12-25 04:09:02 +00:00
res << "<li>#{t(:settings)}#{divider}</li>"
2013-12-23 10:33:40 +00:00
site = Site.first
case controller.controller_name
when 'sites'
case controller.action_name
when 'sitemap'
res << "<li class='active'>#{t(:sitemap)}</li>"
when 'responsive_setting'
res << "<li class='active'>#{t(:mobile_settings)}</li>"
2013-12-23 10:33:40 +00:00
when 'mail_setting'
res << "<li class='active'>#{t(:mail_setting)}</li>"
when 'preference'
res << "<li class='active'>#{t(:preference)}</li>"
when 'search_engine'
res << "<li class='active'>#{t(:search_engine)}</li>"
when 'site_info'
res << "<li class='active'>#{t(:site_info)}</li>"
end
when 'items'
2013-12-25 04:09:02 +00:00
res << "<li class='active'>#{t(:structure)}</li>"
2013-12-24 07:58:13 +00:00
when 'module_store'
2013-12-23 10:33:40 +00:00
res << "<li class='active'>#{t(:modules)}</li>"
when 'designs'
res << "<li class='active'>#{t(:templates)}</li>"
end
res.html_safe
end
2014-01-23 04:28:31 +00:00
end