personal_plugin_generator/template_generator/app/controllers/personal_plugin_templates_c...

171 lines
7.5 KiB
Ruby

class PersonalPluginTemplatesController < ApplicationController
include Admin::PluginTemplatesHelper
def index
params = OrbitHelper.params
plugin_templates = PluginTemplate.sort_for_frontend
page = OrbitHelper.page rescue Page.where(page_id: params[:page_id]).first
parse_again_start * 1 if is_one_line_title.count > 0
title_is_paper_format = true
if page.custom_string_field == 'table'
title_is_paper_format = false
fields_to_show = page.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = col_name_to_show_in_index_page
end
else
fields_to_show = col_name_to_show_short
end
parse_again_end
parse_again_start * 1 if is_one_line_title.count == 0
title_is_paper_format = false
fields_to_show = page.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = col_name_to_show_in_index_page
end
parse_again_end
if params[:keywords].present?
plugin_templates = filter_keywords(plugin_templates,params[:selectbox],params[:keywords])
end
plugin_templates = plugin_templates.page(params[:page_no]).per(OrbitHelper.page_data_count)
plugin_templates_list = plugin_templates.collect do |plugin_template|
{'jps' => fields_to_show.map{|field| {"value"=> get_display_field(plugin_template,field, title_is_paper_format)}}}
end
extras = extra_translate_title
choice_show = []
headers = []
fields_to_show.each do |fs|
col = 2
col = 3 if fs == 'slug_title_text'
headers << {
'head-title' => t("personal_plugin_template.#{fs}"),
'col' => col
}
choice_show << t("personal_plugin_template.#{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_plugin_template.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_plugin_template.extend_translate.search_class')
search_text = t('personal_plugin_template.extend_translate.word_to_search')
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(/\"/,'')
})
extras["widget-title"] = I18n.t("module_name.personal_plugin_template")
{
"plugin_templates" => plugin_templates_list,
"headers" => headers,
"extras" => extras,
"total_pages" => plugin_templates.total_pages,
'choice' => choice
}
end
def show
params = OrbitHelper.params
plugin = PluginTemplate.where(:is_hidden=>false).find_by(uid: params[:uid])
fields_to_show = col_name_to_show_in_show_page
{"plugin_datas"=>plugin.get_plugin_data(fields_to_show)}
end
def get_display_field(plugin_template,field, title_is_paper_format=false)
text_only = false
display_field_code
return value
end
def get_fields_for_index
@page = Page.find(params[:page_id]) rescue nil
@fields_to_show = all_fields_to_show_in_index
@fields_to_show = @fields_to_show.map { |fs| [t("personal_plugin_template.#{fs}"), fs] }
if @page.present? && @page.custom_string_field == 'table'
@default_fields_to_show = col_name_to_show_in_index_page
else
@default_fields_to_show = col_name_to_show_short
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(plugin_templates,select_field,keywords)
member_fields = plugin_template_related_members
file_fields = plugin_template_related_files_text
link_fields = plugin_template_related_links_text
if select_field == "default"
plugin_templates = plugin_templates.where(:slug_title=>/#{gsub_invalid_character(keywords)}/)
elsif select_field == "member_profile"
ms = MemberProfile.all.select{|m| m.name.include?(keywords)}
plugin_templates = plugin_templates.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_plugin_templates = plugin_templates.select{|p| (p.send("#{select_field.singularize}_ids") & m_ids).count != 0}
plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id})
elsif select_field.split(".").count > 1
relate_name = select_field.split(".").first
field_name = select_field.split(".").last
relate = relate_name.classify.constantize
relate_ids = relate.where(field_name.to_sym=>/#{gsub_invalid_character(keywords)}/).pluck(:id)
plugin_templates = plugin_templates.where("#{relate_name.singularize}_id".to_sym.in=>relate_ids)
elsif (PluginTemplate.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")
plugin_templates = plugin_templates.where(select_field.to_sym.gte=>start_time,select_field.to_sym.lte=>end_time)
end
elsif (PluginTemplate.fields[select_field].options[:type] == DateTime rescue false)
keywords = keywords.split(/[\/\-]/)
if keywords.count > 1
DateTime.parse(keywords.join("/"))
elsif keywords[0].include?(":")
tmp_plugin_templates = plugin_templates.select{|p| (p.send(select_field).strftime('%Y/%m/%d %H:%M').include?(keywords[0]) rescue false)}
plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.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")
plugin_templates = plugin_templates.where(select_field.to_sym.gte=>start_time,select_field.to_sym.lte=>end_time)
end
elsif (PluginTemplate.fields[select_field].options[:type] == Integer rescue false)
tmp_plugin_templates = plugin_templates.select{|p| p.send(select_field).to_s.include?(keywords)}
plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id})
elsif file_fields.include?(select_field)
file_field = select_field.classify.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_plugin_templates = plugin_templates.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id})
elsif link_fields.include?(select_field)
link_field = select_field.classify.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_plugin_templates = plugin_templates.select{|p| (p.send("#{select_field}_ids") & ids).count != 0}
plugin_templates = plugin_templates.where(:id.in=>tmp_plugin_templates.map{|p| p.id})
else
plugin_templates = plugin_templates.where(select_field.to_sym=>/#{gsub_invalid_character(keywords)}/)
end
return plugin_templates
end
def gsub_invalid_character(text)
text.to_s.gsub(/(\/|\*|\\|\]|\[|\(|\)|\.|\+|\?|\!)/){|ff| "\\"+ff}
end
end