faq/app/controllers/faqs_controller.rb

96 lines
3.0 KiB
Ruby

class FaqsController < ApplicationController
def index
faqs = Qa.can_display.order_by(:order_position => "asc").filter_by_categories
locale = I18n.locale.to_s
f = faqs.collect do |qa|
statuses = qa.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
qa_files = qa.qa_files.map do |qa_file|
next if !qa_file.enabled_for?(locale)
file = qa_file.file
next if file.path.nil?
{
"file_url" => file.url + "\" title=\"#{qa_file.file_title}",
"file_title" => (qa_file.title.blank? ? File.basename(qa_file["file"]) : qa_file.title)
}
end.compact
{
"link_to_show" => OrbitHelper.url_to_show(qa.to_param),
"question" => qa.title,
"answer" => qa.answer,
"statuses" => statuses,
"qa_files" => qa_files
}
end
{
"faqs" => f,
"extras" => {"widget-title"=>"Faqs"},
"total_pages" => faqs.total_pages
}
end
def show
params = OrbitHelper.params
faq = Qa.can_display.find_by_param(params[:uid])
url_to_edit = OrbitHelper.user_can_edit?(faq) ? "/admin/faqs/#{faq.id.to_s}/edit" : ""
return {} if (faq.category.disable rescue false)
locale = I18n.locale.to_s
faqs_files = faq.qa_files.collect do |qa_file|
next if !qa_file.enabled_for?(locale)
file = qa_file.file
next if file.path.nil?
{
"file_url" => file.url + "\" title=\"#{qa_file.file_title}",
"file_title" => qa_file.title
}
end.compact
faqs_links = faq.qa_links.collect do |f|
{
"link_url" => f.url,
"link_title" => f.title
}
end
tags = faq.tags.map{|tag| { "tag" => tag.name } } rescue []
temp_rich_title = Nokogiri::HTML(faq.rich_title).css('body')
temp_rich_title.children.each_with_index do |child,i|
if !child.attribute('style').nil?
temp_rich_title.children[i].attribute('style').value = child.attribute('style').value.gsub(/\b(?:font-size\s*?:\s*([^;>]*?)(?=[;">}]))/,'')
end
end
{
"extras" => {"question" => temp_rich_title,"answer" => faq.answer},
"faqs_links" => faqs_links,
"faqs_files" => faqs_files,
"tags" => tags,
"url_to_edit" => url_to_edit
}
end
def widget
faqs = Qa.filter_by_widget_categories.collect do |qa|
statuses = qa.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
{
"link_to_show" => OrbitHelper.widget_item_url(qa.to_param),
"title" => qa.title,
"postdate" => qa.created_at.strftime('%Y-%m-%d %H:%M'),
"statuses" => statuses
}
end
{
"faqs" => faqs,
"extras" => {"widget-title"=>"Faqs","more_url"=>OrbitHelper.widget_more_url}
}
end
end