links/app/controllers/web_resources_controller.rb

95 lines
3.1 KiB
Ruby

class WebResourcesController < ApplicationController
require 'nokogiri'
def index
OrbitHelper.render_css_in_head(["web_resources_front"])
links = WebLink.where(:title.ne => "").can_display.filter_by_categories.filter_by_tags
web_link = links.collect do |link|
statuses = link.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
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)
display_image = link.image.thumb.url.blank? ? ' hide' : ''
{
"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,
"category" => link.category.title,
"link_to_show" => link.url,
"target" => target
}
end
{
"web_link" => web_link,
"extras" => {
"widget-title"=>t(:web_resource)
},
"total_pages" => links.total_pages
}
end
def widget
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)
web_link = links.collect do |link|
statuses = link.statuses_with_classname.collect do |status|
{
"status" => status["name"],
"status-class" => "status-#{status['classname']}"
}
end
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)
display_image = link.image.thumb.url.blank? ? ' hide' : ''
{
"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
}
end
{
"web_link" => web_link,
"extras" => {"widget-title"=>t(:web_resource),"more_url" => OrbitHelper.widget_more_url}
}
end
def nl2br(val)
val.gsub(/\r\n/, '<br />').html_safe
end
end