links/app/controllers/web_resources_controller.rb

91 lines
3.0 KiB
Ruby
Raw Normal View History

2014-05-05 11:33:35 +00:00
class WebResourcesController < ApplicationController
2021-04-01 09:20:29 +00:00
require 'nokogiri'
2014-05-05 11:33:35 +00:00
def index
2021-08-11 03:27:57 +00:00
OrbitHelper.render_css_in_head(["web_resources_front"])
2022-12-26 09:24:35 +00:00
links = WebLink.where(:title.ne => "").can_display.filter_by_categories.filter_by_tags
2014-05-05 11:33:35 +00:00
web_link = links.collect do |link|
2014-06-18 10:07:48 +00:00
statuses = link.statuses_with_classname.collect do |status|
{
2014-06-18 10:07:48 +00:00
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
2015-01-07 02:53:01 +00:00
if link.link_open == "new_window"
target = "_blank"
elsif link.link_open == "local"
target = "_self"
end
2021-04-01 09:20:29 +00:00
doc = Nokogiri::HTML(link.title.to_s)
text = doc.css("body")[0].text rescue ""
if text.blank? && doc.css("img").count != 0
text = doc.css("img").map{|img| img.attr("alt") rescue ""}.select{|t| t.present?}.first
end
2021-08-11 03:27:57 +00:00
desc = link.image_description
desc = (desc.blank? ? "web resource image" : desc)
2021-08-12 03:31:30 +00:00
display_image = link.image.thumb.url.blank? ? ' hide' : ''
2014-05-05 11:33:35 +00:00
{
"title" => link.title,
2021-04-01 09:20:29 +00:00
"title_text" => text,
2021-08-11 03:27:57 +00:00
"img_src" => link.image.thumb.url || "/assets/announcement-default.jpg",
"img_description" => desc,
2021-08-12 03:25:48 +00:00
"display_image" => display_image,
2021-08-11 03:27:57 +00:00
"context" => nl2br(link.context.to_s),
"statuses" => statuses,
2017-11-27 06:01:40 +00:00
"category" => link.category.title,
2015-01-07 02:53:01 +00:00
"link_to_show" => link.url,
"target" => target
2014-05-05 11:33:35 +00:00
}
end
{
2014-05-26 03:05:29 +00:00
"web_link" => web_link,
2014-08-08 08:32:09 +00:00
"extras" => {
"widget-title"=>t(:web_resource)
},
"total_pages" => links.total_pages
2014-12-01 06:28:46 +00:00
}
2014-05-05 11:33:35 +00:00
end
def widget
2022-12-26 10:46:26 +00:00
cats = OrbitHelper.widget_categories || []
tags = OrbitHelper.widget_tags || []
tags = ['all'] if tags.blank?
links = WebLink.where(:title.ne => "").can_display.filter_by_categories(cats, false).filter_by_tags(tags)
2014-05-05 11:33:35 +00:00
web_link = links.collect do |link|
2014-06-18 10:07:48 +00:00
statuses = link.statuses_with_classname.collect do |status|
2014-06-06 09:45:39 +00:00
{
2014-06-18 10:07:48 +00:00
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
2014-06-06 09:45:39 +00:00
}
end
2021-08-12 03:25:48 +00:00
if link.link_open == "new_window"
target = "_blank"
elsif link.link_open == "local"
target = "_self"
end
doc = Nokogiri::HTML(link.title.to_s)
text = doc.css("body")[0].text rescue ""
if text.blank? && doc.css("img").count != 0
text = doc.css("img").map{|img| img.attr("alt") rescue ""}.select{|t| t.present?}.first
end
desc = link.image_description
desc = (desc.blank? ? "web resource image" : desc)
2021-08-12 03:31:30 +00:00
display_image = link.image.thumb.url.blank? ? ' hide' : ''
2021-08-12 03:25:48 +00:00
{
"title" => link.title,
"title_text" => text,
"img_src" => link.image.thumb.url || "/assets/announcement-default.jpg",
"img_description" => desc,
"display_image" => display_image,
"context" => nl2br(link.context.to_s),
"statuses" => statuses,
"link_to_show" => link.url,
"target" => target
}
2014-05-05 11:33:35 +00:00
end
{
2014-05-26 03:05:29 +00:00
"web_link" => web_link,
"extras" => {"widget-title"=>t(:web_resource),"more_url" => OrbitHelper.widget_more_url}
2014-12-01 06:28:46 +00:00
}
2014-05-05 11:33:35 +00:00
end
end