publication/app/controllers/publications_controller.rb

145 lines
4.3 KiB
Ruby

class PublicationsController < ApplicationController
def index
params = OrbitHelper.params
categories = []
data = []
publications = Publication.order(created_at: :desc).filter_by_categories.filter_by_tags
.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
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'),
cover: (OrbitHelper.is_mobile_view ? node.cover.mobile.url : node.cover.url),
chapters: node.chapters.count,
link_to_show: OrbitHelper.url_to_show("#{node.to_param}?mode=publication"),
link_to_list: OrbitHelper.url_to_show("#{node.to_param}?mode=many"),
}
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 show
params = OrbitHelper.params
if params[:mode] == 'many'
show_list
elsif params[:mode] == 'chapter'
show_chapter
else
show_publication
end
end
def widget
#code
end
private
def show_list
params = OrbitHelper.params
publication = Publication.find_by(uid: params[:uid])
chapters = Chapter.where(publication_id: publication.id).order(sort_number: :asc)
.page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
data = []
chapters.each do |node|
data << {
id: node.id.to_s,
title: node.title,
author: node.author,
author_description: node.author_description,
page: node.page,
link_to_show: OrbitHelper.url_to_show("#{node.to_param}?mode=chapter"),
file: (node.file.present? ? node.file.url : ''),
file_class: (node.file.present? ? '' : 'hidden')
}
end
{
"chapters" => data,
"extras" => {
"widget-title" => t("module_name.publication"),
"th_title" => t('publications.chapter'),
"th_author" => t('chapter.author'),
"th_page" => t('chapter.page'),
"th_action" => t('chapter.action'),
"publication_title" => publication.title,
"mode" => "many"
},
"total_pages" => chapters.total_pages
}
end
def show_chapter
params = OrbitHelper.params
chapter = Chapter.find_by(uid: params[:uid])
{
"title" => chapter.title,
"title_title" => t('chapter.title'),
"author" => chapter.author,
"author_title" => t('chapter.author'),
"author_description" => chapter.author_description,
"author_description_title" => t('chapter.author_description'),
"text" => chapter.text,
"text_title" => t('chapter.text'),
"extras" => {
"mode" => "chapter"
}
}
end
def show_publication
params = OrbitHelper.params
publication = Publication.find_by(uid: params[:uid])
{
"title" => publication.title,
"title_title" => t('publication.title'),
"author" => publication.author,
"author_title" => t('publication.author'),
"cover" => publication.cover.url,
"cover_title" => t('publication.cover'),
"pub_date" => publication.pub_date.strftime('%Y-%m-%d'),
"pub_date_title" => t('publication.pub_date'),
"pub_information" => publication.pub_information,
"pub_information_title" => t('publication.pub_information'),
link_to_list: OrbitHelper.url_to_show("#{publication.to_param}?mode=many"),
"extras" => {
"mode" => "publication"
}
}
end
end