class PersonalActivitiesController < ApplicationController
include Admin::ActivitiesHelper
def index
params = OrbitHelper.params
activities = Activity.sort_for_frontend
page = OrbitHelper.page rescue Page.where(page_id: params[:page_id]).first
title_is_paper_format = false
fields_to_show = page.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = ["year", "publish_date", "start_date", "end_date", "activity_name", "work_title", "authors_type", "activity_organizer", "activity_co_organizer", "region", "road_show", "tour_details", "award_name", "awarding_unit"]
end
if params[:keywords].present?
activities = filter_keywords(activities,params[:selectbox],params[:keywords])
end
activities = activities.page(params[:page_no]).per(OrbitHelper.page_data_count)
activities_list = activities.collect do |activity|
{'jps' => fields_to_show.map{|field| {"value"=> get_display_field(activity,field, title_is_paper_format)}}}
end
extras = {"th-year" => I18n.t("personal_activity.year"), "th-publish_date" => I18n.t("personal_activity.publish_date"), "th-start_date" => I18n.t("personal_activity.start_date"), "th-end_date" => I18n.t("personal_activity.end_date"), "th-activity_name" => I18n.t("personal_activity.activity_name"), "th-work_title" => I18n.t("personal_activity.work_title"), "th-authors_type" => I18n.t("personal_activity.authors_type"), "th-activity_organizer" => I18n.t("personal_activity.activity_organizer"), "th-activity_co_organizer" => I18n.t("personal_activity.activity_co_organizer"), "th-region" => I18n.t("personal_activity.region"), "th-road_show" => I18n.t("personal_activity.road_show"), "th-tour_details" => I18n.t("personal_activity.tour_details"), "th-award_name" => I18n.t("personal_activity.award_name"), "th-awarding_unit" => I18n.t("personal_activity.awarding_unit")}
choice_show = []
headers = []
fields_to_show.each do |fs|
col = 2
col = 3 if fs == 'activity_name'
headers << {
'head-title' => t("personal_activity.#{fs}"),
'col' => col
}
choice_show << t("personal_activity.#{fs}")
end
choice_value = fields_to_show
choice_value.unshift('default')
choice_select = choice_value.map { |iter| iter == params[:selectbox] ? 'selected' : '' }
choice_select = choice_select.map { |value| { 'choice_select' => value } }
choice_value = choice_value.map { |value| { 'choice_value' => value } }
choice_default = t('personal_activity.extend_translate.select_class')
choice_show.unshift(choice_default)
choice_show = choice_show.map { |value| { 'choice_show' => value } }
choice = choice_value.zip(choice_show, choice_select)
choice = choice.map { |value| value.inject :merge }
select_text = t('personal_activity.extend_translate.search_class')
search_text = t('personal_activity.extend_translate.word_to_search')
@_request = OrbitHelper.request
csrf_value = form_authenticity_token
extras = extras.merge({ 'url' => '/' + I18n.locale.to_s + params[:url],
'select_text' => select_text,
'search_text' => search_text,
'search_value' => params[:keywords].to_s.gsub(/\"/,''),
'csrf_value' => csrf_value
})
extras["widget-title"] = I18n.t("module_name.personal_activity")
{
"activities" => activities_list,
"headers" => headers,
"extras" => extras,
"total_pages" => activities.total_pages,
'choice' => choice
}
end
def show
params = OrbitHelper.params
plugin = Activity.where(:is_hidden=>false).find_by(uid: params[:uid].to_s)
fields_to_show = ["year", "publish_date", "start_date", "end_date", "activity_name", "work_title", "authors_type", "authors", "activity_organizer", "activity_co_organizer", "region", "road_show", "tour_details", "award_name", "awarding_unit"]
{"plugin_datas"=>plugin.get_plugin_data(fields_to_show)}
end
def get_display_field(activity,field, title_is_paper_format=false)
text_only = false
value = activity.send(field) rescue ""
if field.include?(".")
value = activity
field.split(".").each{|f| value = value.send(f) rescue nil }
end
file_fields = []
link_fields = []
member_fields = []
if file_fields.include?(field)
files = activity.send(field.pluralize)
value = files.map do |file|
url = file.file.url
title = (file.title.blank? ? File.basename(file.file.path) : file.title)
"
#{title}"
end
value = value.join("")
elsif link_fields.include?(field)
links = activity.send(field.pluralize)
value = links.map do |link|
url = link.url
title = (link.title.blank? ? url : link.title)
"#{title}"
end
value = value.join("")
elsif member_fields.include?(field)
members = activity.send(field.pluralize)
value = members.map{|m|
path = OrbitHelper.url_to_plugin_show(m.to_param, 'member') rescue '#'
((text_only rescue false) ? m.name : "#{m.name}")
}
join_text = (text_only rescue false) ? "," : "
"
value = value.join(join_text)
elsif field == "member_profile" || field == "authors"
value = get_authors_show(activity)
end
strftime_hash = {"publish_date"=>"%Y/%m/%d", "start_date"=>"%Y/%m/%d", "end_date"=>"%Y/%m/%d"}
if strftime_hash.keys.include?(field)
value = value.strftime(strftime_hash[field]) rescue value
end
if field == "activity_name"
link = OrbitHelper.url_to_plugin_show(activity.to_param,'personal_activity')
tmp_title = value
url_to_plugin_show_blank = OrbitHelper.instance_variable_get(:@url_to_plugin_show_blank)
value = url_to_plugin_show_blank ? tmp_title : "#{tmp_title}"
end
value
return value
end
def get_fields_for_index
@page = Page.find(params[:page_id]) rescue nil
@fields_to_show = ["year", "publish_date", "start_date", "end_date", "activity_name", "work_title", "authors_type", "activity_organizer", "activity_co_organizer", "region", "road_show", "tour_details", "award_name", "awarding_unit"]
@fields_to_show = @fields_to_show.map { |fs| [t("personal_activity.#{fs}"), fs] }
if @page.present? && @page.custom_string_field == 'table'
@default_fields_to_show = ["year", "publish_date", "start_date", "end_date", "activity_name", "work_title", "authors_type", "activity_organizer", "activity_co_organizer", "region", "road_show", "tour_details", "award_name", "awarding_unit"]
else
@default_fields_to_show = ["year", "activity_name"]
end
render layout: false
end
def save_index_fields
page = Page.find(params[:page_id]) rescue nil
page.custom_array_field = params[:keys]
page.save
render json: { 'success' => true }.to_json
end
def filter_keywords(activities,select_field,keywords)
member_fields = []
file_fields = []
link_fields = []
if select_field == "default"
activities = activities.where(:slug_title=>/#{gsub_invalid_character(keywords)}/)
elsif select_field == "member_profile"
ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
activities = activities.where(:member_profile_id.in=>ms.map{|m| m.id})
elsif member_fields.include?(select_field)
ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
m_ids = ms.map{|m| m.id.to_s }
tmp_activities = activities.select{|p| (p.send("#{select_field.singularize}_ids") & m_ids).count != 0}
activities = activities.where(:id.in=>tmp_activities.map{|p| p.id})
elsif select_field.split(".").count > 1
relate_name = select_field.split(".").first
field_name = select_field.split(".").last.gsub(/^\$+/, '')
relate = relate_name.camelize.constantize
relate_ids = relate.where(field_name=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
activities = activities.where("#{relate_name.singularize}_id"=>{'$in'=>relate_ids})
elsif (Activity.fields[select_field].options[:type] == Date rescue false)
keywords = keywords.split(/[\/\-]/)
if keywords.count > 1
Date.parse(keywords.join("/"))
else
start_time = Date.parse(keywords[0] + "/1/1")
end_time = Date.parse(keywords[0] + "/12/31")
activities = activities.where(select_field=>{'$gte'=>start_time,'$lte'=>end_time})
end
elsif (Activity.fields[select_field].options[:type] == DateTime rescue false)
keywords = keywords.split(/[\/\-]/)
if keywords.count > 1
DateTime.parse(keywords.join("/"))
elsif keywords[0].include?(":")
tmp_activities = activities.select{|p| (p.send(select_field).strftime('%Y/%m/%d %H:%M').include?(keywords[0]) rescue false)}
activities = activities.where(:id.in=>tmp_activities.map{|p| p.id})
else
start_time = DateTime.parse(keywords[0] + "/1/1 00:00")
end_time = DateTime.parse(keywords[0] + "/12/31 23:59")
activities = activities.where(select_field=>{'$gte'=>start_time,'$lte'=>end_time})
end
elsif (Activity.fields[select_field].options[:type] == Integer rescue false)
tmp_activities = activities.select{|p| p.send(select_field).to_s.include?(keywords)}
activities = activities.where(:id.in=>tmp_activities.map{|p| p.id})
elsif file_fields.include?(select_field)
file_field = select_field.camelize.constantize
ids1 = file_field.where(:file=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids2 = file_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids = ids1 + ids2
tmp_activities = activities.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
activities = activities.where(:id.in=>tmp_activities.map{|p| p.id})
elsif link_fields.include?(select_field)
link_field = select_field.camelize.constantize
ids1 = link_field.where(:title=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids2 = link_field.where(:url=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
ids = ids1 + ids2
tmp_activities = activities.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
activities = activities.where(:id.in=>tmp_activities.map{|p| p.id})
else
activities = activities.where(select_field=>/#{gsub_invalid_character(keywords)}/)
end
return activities
end
def gsub_invalid_character(text)
::Regexp.escape(text.to_s)
end
end