personal-certificate/app/controllers/personal_certificates_contr...

81 lines
2.3 KiB
Ruby

class PersonalCertificatesController < ApplicationController
def index
params = OrbitHelper.params
certificates = Certificate.where(:is_hidden=>false).order_by(:created_at=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
fields_to_show = Page.where(:page_id => params[:page_id]).first.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = [
"title",
"issued_by",
"authors"
]
end
certificates_list = []
certificates.each do |certificate|
t = []
fields_to_show.each do |fs|
case fs
when "title"
t << {"value" => "<a href='#{OrbitHelper.url_to_show(certificate.to_param)}'>" + (certificate.send(fs) rescue "") + "</a>"}
when "authors"
t << {"value" => (certificate.send(:member_profile).name rescue "")}
else
t << {"value" => (certificate.send(fs) rescue "")}
end
end
certificates_list << {"personal_certificates" => t}
end
headers = []
fields_to_show.each do |fs|
col = 2
col = 3 if fs == "title"
header = fs == "authors" ? t("users.name") : t("personal_certificate.#{fs}")
headers << {
"head-title" => header,
"col" => col
}
end
{
"certificates" => certificates_list,
"headers" => headers,
"extras" => {"widget-title" => t("module_name.personal_certificate")},
"total_pages" => certificates.total_pages
}
end
def show
params = OrbitHelper.params
plugin = Certificate.where(:is_hidden=>false).find_by(uid: params[:uid])
fields_to_show = [
"title",
"issued_by"
]
{"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 = [
"title",
"issued_by",
"authors"
]
@fields_to_show = @fields_to_show.map{|fs| [(fs == "authors" ? t("users.name") : t("personal_certificate.#{fs}")), fs]}
@default_fields_to_show = [
"title",
"issued_by",
"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