publication/app/controllers/publications_controller.rb

236 lines
9.1 KiB
Ruby

class PublicationsController < ApplicationController
FrontendMethods = ["chapter", "online_read_publication", "online_read_chapter"]
def index
@show_option_items = OrbitHelper.this_module_app.show_option_items rescue nil
is_search = false
if !(@show_option_items.nil?)
OrbitHelper.page.select_option_items.each do |select_option_item|
if select_option_item.field_name == @show_option_items.keys.first.to_s
value = YAML.load(select_option_item.value)
tmp = value[:en]
I18n.with_locale(:en) do
if tmp == t('publication.search_style')
is_search = true
end
end
end
end
end
if is_search
return search_index
end
params = OrbitHelper.params
categories = []
data = []
publications = Publication.order("created_at" => :desc).filter_by_categories
.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
ModuleApp.find_by("key" => 'publication').categories.each do |e|
categories << [e.id.to_s, e.title]
end
category_maps = categories.to_h
publications.each do |node|
status = node.statuses_with_classname.collect do |stat|
{
"status" => stat[:name],
"status-class" =>"status-#{stat[:classname]}"
}
end
tags = node.tags.collect do |tag|
{
"tag" => tag.name
}
end
data << {
"id" => node.id.to_s,
"statuses" => status,
"tags" => tags,
"title" => node.title,
"author" => node.author,
"pub_date" => (node.pub_date.strftime('%Y-%m-%d') rescue ""),
"category" => category_maps[node.category_id],
"cover" => (node.cover ? (OrbitHelper.is_mobile_view ? node.cover.mobile.url : node.cover.url) : ""),
"cover_description" => node.cover_description,
"chapters" => node.chapters.count,
"link_to_show" => (node.is_external_link ? node.external_link : OrbitHelper.url_to_show(node.to_param)),
"pub_information" => node.pub_information
}
end
{
"publications" => data,
"extras" => {
"widget-title" => t("module_name.publication"),
"th_cover" => t('publication.cover'),
"th_pub_date" => t('publication.pub_date'),
"th_title" => t('publication.title'),
"th_author" => t('publication.author'),
"th_chapter" => t('publication.chapter'),
"th_chapters" => t('publication.chapters'),
"categories" => categories
},
"total_pages" => publications.total_pages
}
end
def search_index
publication_ids = Publication.order("created_at" => :desc).filter_by_categories(nil, false).pluck(:id)
chapters = Chapter.where(:publication_id.in => publication_ids).filter_by_tags.order("sort_number" => :asc, "created_at" => :asc)
.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count).includes(:publication)
params = OrbitHelper.params
if !params[:keywords].blank?
regexes = params[:keywords].split(/\s+(?=(?:[^"]*"[^"]*")*[^"]*$)/)
regex = Regexp.union(regexes.map{|word| Regexp.new(word, "i")})
extra_query = []
['title', 'keywords'].each do |key|
I18n.available_locales.each do |locale|
extra_query << {"#{key}.#{locale}" => regex}
end
end
chapters = chapters.where({"$or" => extra_query})
end
data = get_show_render_data(chapters)
@_request = OrbitHelper.request
csrf_value = form_authenticity_token
data['extras'].merge!({
'search_placeholder' => t('chapter.search_placeholder'),
'search_text' => t('chapter.keywords'),
'search_value' => params[:keywords].to_s.gsub(/\"/,''),
'search' => t('chapter.search'),
'csrf_value' => csrf_value,
'is_search' => true,
'url' => OrbitHelper.page.url
})
data['extras']
data
end
def show
@show_option_items = OrbitHelper.this_module_app.show_option_items rescue nil
layout_type = 0
if !(@show_option_items.nil?)
OrbitHelper.page.select_option_items.each do |select_option_item|
if select_option_item.field_name == @show_option_items.keys.first.to_s
value = YAML.load(select_option_item.value)
tmp = value[:en]
I18n.with_locale(:en) do
if tmp == t('publication.table_style')
layout_type = 0
elsif tmp == t('publication.accordion_style') || tmp == t('publication.search_style')
layout_type = 1
end
end
end
end
end
params = OrbitHelper.params
publication = Publication.find_by("uid" => params[:uid])
chapters = Chapter.where("publication_id" => publication.id).order("sort_number" => :asc, "created_at" => :asc)
.page(OrbitHelper.page_number.to_i).per(OrbitHelper.page_data_count)
data = get_show_render_data(chapters)
data['publication'] = [{
"author_display" => (!publication.author.blank? ? '' : "hide"),
"author" => publication.author,
"cover" => publication.cover.url,
"cover_description" => publication.cover_description,
"title" => publication.title,
"pub_date_display" => (publication.pub_date ? '' : "hide"),
"pub_date" => (publication.pub_date.strftime('%Y-%m-%d') rescue ""),
"pub_information" => publication.pub_information,
"file_display" => (publication.full_file ? '' : 'hide'),
"file" => (publication.full_file ? publication.full_file.file.url : ''),
"file_title" => (publication.full_file ? publication.full_file.title : ''),
"file_description" => (publication.full_file ? publication.full_file.description : ''),
"online_reader_display" => (publication.full_file && publication.is_online_flipping_reader ? '' : 'hide'),
"online_reader" => OrbitHelper.url_to_show("#{publication.to_param}?method=online_read_publication")
}]
data['extras']['layout_type'] = layout_type
data
end
def online_read_publication
params = OrbitHelper.params
publication = Publication.find_by("uid" => params[:uid]) rescue nil
data = {
url: (publication.full_file.file.url rescue nil),
file_title: (c.full_file.title rescue nil),
file_description: (c.full_file.description rescue nil),
title: (publication.title rescue nil)
}
end
def online_read_chapter
params = OrbitHelper.params
c = Chapter.find_by("uid" => params[:uid]) rescue nil
data = {
url: (c.chapter_file.file.url rescue nil),
file_title: (c.chapter_file.title rescue nil),
file_description: (c.chapter_file.description rescue nil),
title: (c.title rescue nil)
}
end
def chapter
params = OrbitHelper.params
chapters = [Chapter.find_by("uid" => params[:uid])] rescue []
get_show_render_data(chapters)
end
def widget
#code
end
def get_chapter_data(chapters)
chapters.collect do |node|
publication = node.publication
{
"id" => node.id.to_s,
"title" => node.title,
"author_display" => (!node.author.blank? ? '' : "hide"),
"author" => (!node.author_url.blank? ? "<a title=\"#{node.author}\" href=\"#{node.author_url}\">#{node.author}</a>" : node.author),
"author_description" => node.author_description,
"page_display" => (!node.page.blank? ? '' : 'hide'),
"page" => node.page,
"keywords_display" => (!node.keywords.blank? ? '' : 'hide'),
"keywords" => node.keywords,
"link_to_show" => OrbitHelper.url_to_show("#{node.to_param}?method=chapter"),
"file" => (node.chapter_file ? node.chapter_file.file.url : ''),
"file_title" => (node.chapter_file ? node.chapter_file.title : ''),
"file_description" => (node.chapter_file ? node.chapter_file.description : ''),
"file_display" => (node.chapter_file ? '' : 'hide'),
"online_reader_display" => (node.chapter_file && node.is_online_flipping_reader ? '' : 'hide'),
"online_reader" => OrbitHelper.url_to_show("#{node.to_param}?method=online_read_chapter"),
"tags" => node.tags.map{|tag| {
"tag" => tag.name
}},
"in_class" => 'in',
"text" => node.text,
"link_to_publication" => (publication.is_external_link ? publication.external_link : OrbitHelper.url_to_show(publication.to_param)),
"publication_title" => publication.title
}
end
end
def get_show_render_data(chapters)
data = get_chapter_data(chapters)
{
"chapters" => data,
"extras" => {
"widget-title" => t("module_name.publication"),
"th_keywords" => t('chapter.keywords'),
"th_tags" => t('tags'),
"th_title" => t('publications_.chapter'),
"th_pub_date" => t('publication.pub_date'),
"th_chapter_title" => t('chapter.title'),
"th_author" => t('chapter.author'),
"th_page" => t('chapter.page'),
"th_action" => t('chapter.action'),
"th_cover" => t('publication.cover'),
"th_download" => t('publication.download'),
"th_online_reader" => t('publication.is_online_flipping_reader')
},
"total_pages" => (chapters.total_pages rescue 0)
}
end
end