epaper/app/controllers/e_papers_controller.rb

351 lines
11 KiB
Ruby
Executable File

class EPapersController < ApplicationController
FrontendMethods = ["papers", "topics"]
EmptyImg = "data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA="
EmptyImgUrl = "#{EmptyImg}\" style=\"display: none;"
def self.custom_widget_data
ac = ActionController::Base.new
ac.instance_variable_set(:@custom_data_field, @custom_data_field)
ac.instance_variable_set(:@field_name, @field_name)
ac.render_to_string("e_papers/custom_widget_data")
end
def index
page_no = OrbitHelper.page_number
data_count = OrbitHelper.page_data_count
page = (OrbitHelper.page rescue Page.where(:url=>params[:url]).first)
page_layout = page.layout
custom_data_field = page.custom_data_field
if custom_data_field
criteria_id = (custom_data_field["criteria_id"] || "all" rescue "all")
epaper_topics = nil
if criteria_id == "all"
epaper_topics = EPaperTopic.all
else
criteria = PaperCriteria.where(:id => criteria_id).first
epaper_topics = criteria.epaper_topics
end
else
epaper_topics = EPaperTopic.all
criteria = nil
end
topics = epaper_topics.filter_by_categories.filter_by_tags.desc(:period).page(page_no).per(data_count)
total_pages = topics.total_pages
page_url = page.get_url rescue page.url
if page_layout.include?("_latest")
topics_group = topics.group_by(&:category)
data = topics_group.collect do |category, topics|
category_title = (category ? category.title : "")
topics_data = topics.collect do |topic|
{
"title" => topic.title,
"criteria_title" => topic.criteria_title,
"link_to_show" => OrbitHelper.url_to_show(topic.to_param),
"description" => topic.description,
"img_url" => topic.image.url || EmptyImgUrl,
"img_url_thumb" => topic.image.thumb.url || EmptyImgUrl,
"category" => category_title,
"category_title" => category_title,
"publish_date" => topic.period
}
end
{
"category_title" => category_title,
"category_link" => "#{page_url}?method=topics&category=#{category.id.to_s}",
"topics" => topics_data
}
end
{
"categories" => data,
"extras" => {
},
"total_pages" => total_pages
}
else
data = topics.to_a.collect do |topic|
{
"title" => topic.title,
"criteria_title" => topic.criteria_title,
"link_to_show" => OrbitHelper.url_to_show(topic.to_param),
"description" => topic.description,
"img_url" => topic.image.url || EmptyImgUrl,
"img_url_thumb" => topic.image.thumb.url || EmptyImgUrl,
"category" => (topic.category.title rescue ""),
"category_title" => (topic.category.title rescue ""),
"publish_date" => topic.period
}
end
{
"topics" => data,
"extras" => {
},
"total_pages" => total_pages
}
end
end
def papers
params = OrbitHelper.params
criterias = PaperCriteria.all.desc(:start_date).page(OrbitHelper.page_number).per(OrbitHelper.page_data_count)
data = criterias.collect do |criteria|
{
"title" => criteria.title,
"description" => criteria.description,
"link_to_show" => OrbitHelper.url_to_show(criteria.to_param) + "?method=topics"
}
end
{
"criterias" => data,
"extras" => {
"th_title" => t('e_paper.title'),
"th_description" => t('e_paper.description')
},
"total_pages" => criterias.total_pages
}
end
def show
params = OrbitHelper.params
epaper = EPaperTopic.where(:uid => params[:uid]).first
member = MemberProfile.find(epaper.article_authors.first) rescue nil
if !member.nil?
member_pic = member.get_avatar
member_autobiography = member.autobiography
member_name = member.name
end
{
"content" => epaper.content,
"publish_date" => epaper.period,
"description" => epaper.description,
"image_url" => epaper.image.url || EmptyImgUrl,
"image_thumb_url" => epaper.image.thumb.url || EmptyImgUrl,
"title" => epaper.title,
"category_title" => epaper.category.title,
"criteria_title" => epaper.criteria_title,
"member_pic" => member_pic,
"member_autobiography" => member_autobiography,
"member_name" => member_name
}
end
def topics
params = OrbitHelper.params
if params[:uid].present?
criteria = PaperCriteria.where(:uid => params[:uid]).first
return if criteria.nil?
epaper_topics = criteria.epaper_topics
else
epaper_topics = EPaperTopic.all
end
if params["category"].present?
papers = epaper_topics.where(:category_id => params["category"]).group_by(&:category)
else
papers = epaper_topics.group_by(&:category)
end
data = []
papers_sorted = get_all_categories.map do |v|
tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)}
if tmp.count==0
tmp = nil
end
tmp
end.compact
papers_sorted.each do |paper|
paper.each do |category, topics|
topics_data = Array(topics).compact.collect do |topic|
{
"title" => topic.title,
"link_to_show" => OrbitHelper.url_to_show(topic.to_param),
"description" => topic.description,
"img_url" => topic.image.url || EmptyImgUrl,
"img_url_thumb" => topic.image.thumb.url || EmptyImgUrl,
"category" => category.title
}
end
data << {
"category_title" => category.title,
"category_link" => params["url"] + "/" + params["page"] + "?method=topics&category=#{category.id.to_s}",
"topics" => topics_data
}
end
end
{
"categories" => data,
"extras" => {}
}
end
def widget
subpart = OrbitHelper.get_current_widget
case subpart.widget_type
when /criteria_list.*/
criteria_list
when /category_wise_articles.*/
category_wise_articles
when /latest_criteria.*/
latest_criteria
when /latest_slider.*/
latest_slider
else
subscribe
end
end
def criteria_list
params = OrbitHelper.params
criterias = PaperCriteria.all.desc(:start_date).limit(OrbitHelper.widget_data_count)
data = Array(criterias).compact.collect do |criteria|
{
"title" => criteria.title,
"description" => criteria.description,
"link_to_show" => OrbitHelper.widget_item_url(criteria.to_param) + "?method=topics"
}
end
{
"criterias" => data,
"extras" => {
"th_title" => t('e_paper.title'),
"th_description" => t('e_paper.description'),
"read_more" => OrbitHelper.widget_more_url + "?method=papers"
}
}
end
def category_wise_articles
topics = EPaperTopic.filter_by_widget_categories.filter_by_tags(OrbitHelper.widget_tags).desc(:period)
data = Array(topics).compact.collect do |topic|
{
"title" => topic.title,
"link_to_show" => OrbitHelper.widget_item_url(topic.to_param),
"description" => topic.description,
"img_url" => topic.image.url || EmptyImgUrl,
"img_url_thumb" => topic.image.thumb.url || EmptyImgUrl,
"category" => (topic.category.title rescue nil),
"link_to_show_wise" => OrbitHelper.widget_more_url + "/" + topic.to_param
}
end
{
"topics" => data,
"extras" => {
"more_url" => OrbitHelper.widget_more_url
},
}
end
def latest_criteria
criteria = PaperCriteria.last
papers = criteria.epaper_topics.group_by(&:category)
data = []
papers_sorted = get_all_categories.map do |v|
tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)}
if tmp.count==0
tmp = nil
end
tmp
end.compact
papers_sorted.each do |paper|
paper.each do |category, topics|
topics_data = Array(topics).compact.collect do |topic|
{
"title" => topic.title,
"link_to_show" => OrbitHelper.widget_item_url(topic.to_param),
"description" => topic.description,
"img_url" => topic.image.url || EmptyImgUrl,
"img_url_thumb" => topic.image.thumb.url || EmptyImgUrl,
"category" => (category.title rescue nil)
}
end
data << {
"category_title" => (category.title rescue nil),
"topics" => topics_data
}
end
end
{
"categories" => data,
"extras" => {
"read_more" => OrbitHelper.widget_more_url + "?method=topics"
}
}
end
def latest_slider
criteria = PaperCriteria.last
papers = criteria.epaper_topics.group_by(&:category)
data = []
papers_sorted = get_all_categories.map do |v|
tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)}
if tmp.count==0
tmp = nil
end
tmp
end.compact
papers_sorted.each do |paper|
paper.each do |category, topics|
topics_data = Array(topics).compact.collect do |topic|
{
"title" => topic.title,
"link_to_show" => OrbitHelper.widget_item_url(topic.to_param),
"description" => topic.description,
"img_url" => topic.image.url || EmptyImgUrl,
"img_url_thumb" => topic.image.thumb.url || EmptyImgUrl,
"category" => (category.title rescue nil)
}
end
data << {
"category_title" => (category.title rescue nil),
"topics" => topics_data
}
end
end
{
"categories" => data,
"extras" => {
"read_more" => OrbitHelper.widget_more_url + "?method=topics"
}
}
end
def subscribe
{}
end
def subscribeuser
subscriber = EPaperSubscriber.where(:email => params[:email])
if subscriber.count == 0
subscriber = EPaperSubscriber.new
subscriber.email = params[:email]
subscriber.subscribed = true
subscriber.language = params[:language]
subscriber.save
data = {"success" => true, "msg" => "Successfully Subscribed!!!"}
elsif subscriber.first.subscribed
data = {"success" => false, "msg" => "Already Subscribed!!!"}
elsif !subscriber.first.subscribed
su = subscriber.first
su.subscribed = true
su.save
data = {"success" => true, "msg" => "Successfully Subscribed!!!"}
end
render :json => data.to_json
end
def unsubscribeuser
subscriber = EPaperSubscriber.where(:email => params[:email]).first rescue nil
if !subscriber.nil?
subscriber.subscribed = false
subscriber.save
data = {"success" => true, "msg" => "Successfully Unsubscribed!!!"}
else
data = {"success" => false, "msg" => "You are not a subscriber!!!"}
end
render :json => data.to_json
end
def get_all_categories
app = ModuleApp.where(key: 'e_paper').first
asc_flag = app.asc rescue true
app.categories.enabled.sort_by do |category|
tmp = category.sort_number.to_i rescue 0
asc_flag ? tmp : -tmp
end
end
end