class PersonalCompetitionsController < ApplicationController
def index
params = OrbitHelper.params
filter_value = params[:competitions_filter_value]
competitions = nil
if filter_value.nil? || filter_value == t("personal_competition.competition_types_all") || filter_value.empty?
competitions = Competition.where(:competition_name.nin => [nil, ""]).sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
else
competition_type_id = CompetitionType.any_of({ 'title.en' => filter_value }, { 'title.zh_tw' => filter_value }).first.id
competitions = Competition.where(competition_type_id: competition_type_id).sort_for_frontend.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
end
fields_to_show = Page.where(:page_id => params[:page_id]).first.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = [
"year",
"award_name",
"awarding_unit",
"work_name",
"student_name",
"authors"
]
end
competition_list = []
competitions.each do |competition|
t = []
fields_to_show.each do |fs|
case fs
when "competition_name"
t << {"value" => "" + (competition.send(fs) rescue "") + ""}
when "competition_type"
t << {"value" => (competition.send("competition_type").title rescue "")}
when "authors"
member_profile = competition.send(:member_profile)
role_status_id = member_profile.role_status_ids.first.to_s
path = OrbitHelper.url_to_plugin_show(member_profile.to_param, 'member') rescue '#'
t << {"value" => "" + (member_profile.name rescue "") + "" }
when "competition_date"
t << {"value" => (competition.send(fs).strftime("%Y/%m") rescue "")}
else
t << {"value" => (competition.send(fs) rescue "")}
end
end
competition_list << {"personal_competitions" => t}
end
headers = []
fields_to_show.each do |fs|
col = 2
col = 3 if fs == "award_name"
header = fs == "authors" ? t("users.name") : t("personal_competition.#{fs}")
headers << {
"head-title" => header,
"col" => col
}
end
current_locale = I18n.locale
competition_types = CompetitionType.all.pluck(:title).map { |title| { 'competition_type' => title.to_h[current_locale] } }
competition_types.unshift({ 'competition_type' => t("personal_competition.competition_types_all") })
{
"competitions" => competition_list,
"competition_types" => competition_types,
"extras" => { "widget-title" => t("module_name.personal_competition"),
"url" => '/' + current_locale.to_s + params[:url] },
"headers" => headers,
"total_pages" => competitions.total_pages
}
end
def show
params = OrbitHelper.params
plugin = Competition.where(:is_hidden=>false).find_by(uid: params[:uid].to_s)
fields_to_show = [
"year",
"competition_type",
"competition_name",
"work_name",
"student_name",
"instructor_name",
"award_place",
"competition_date",
"country",
"honoree",
"awarding_unit",
"keywords",
"url",
"note"
]
{"plugin_datas"=>plugin.get_plugin_data(fields_to_show)}
end
def get_fields_for_index
@page = Page.find(params[:page_id]) rescue nil
@fields_to_show = [
"year",
"competition_name",
"award_name",
"work_name",
"student_name",
"instructor_name",
"award_place",
"awarding_unit",
"competition_type",
"competition_date",
"country",
"keywords",
"url",
"note",
"authors",
"award_winner"
]
@fields_to_show = @fields_to_show.map{|fs| [(fs == "authors" ? t("users.name") : t("personal_competition.#{fs}")), fs]}
@default_fields_to_show = [
"year",
"award_name",
"awarding_unit",
"work_name",
"student_name",
"authors"
]
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
end