fix json & search word

This commit is contained in:
nccu 2015-03-06 14:44:13 +08:00
parent 365019eabe
commit 15d1d867bf
2 changed files with 122 additions and 3 deletions

View File

@ -0,0 +1,119 @@
# encoding: utf-8
class Admin::BulletinsController < ApplicationController
before_filter :set_I18n
def get_bulletins
page = Page.where(:module => "announcement").first rescue nil
# 頁次
page_num = params[:page_num].blank? ? 0 : params[:page_num].to_i
# 每頁顯示的則數
per_page = params[:per_page].blank? ? 10 : params[:per_page].to_i
per_page = per_page > 0 ? per_page : 10
if !params[:keyword].blank?
keyword = Regexp.new(".*"+params[:keyword]+".*")
bulletins = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
else
bulletins = Bulletin.all
end
bulletins = bulletins.where(:is_hot => params[:is_hot]) if !params[:is_hot].blank?
bulletins = bulletins.where(:category_id.in => params[:categories]) if !params[:categories].blank?
bulletins = bulletins.where(:tagged_ids.in => params[:tags]) if !params[:tags].blank?
bulletins = bulletins.where(:is_preview.in=>[false,nil])
bulletins = bulletins.desc( :is_top, :postdate).page(page_num).per(per_page)
bulletins = bulletins.collect do |b|
image = request.protocol + request.host_with_port + b.image.url rescue nil
links = b.bulletin_links.collect do |bl|
{
"title" => bl.title_translations,
"url" => bl.url
}
end rescue nil
files = b.bulletin_files.collect do |bf|
file = request.protocol + request.host_with_port + bf.file.url rescue nil
{
"title" => bf.title_translations,
"description" => bf.description_translations,
"file" => file
}
end rescue nil
ts = b.tags.collect do |t|
{
"name" => t.name_translations
}
end rescue nil
text = {"en" => "", "zh_tw" => ""}
text["en"] = (b.text_translations["en"].nil? ? "" :smart_convertor(b.text_translations["en"]))
text["zh_tw"] = (b.text_translations["zh_tw"].nil? ? "" : smart_convertor(b.text_translations["zh_tw"]))
author = User.find(b.create_user_id).member_profile.name rescue ""
{
"id" => b.id.to_s,
"title" => b.title_translations,
"subtitle" => b.subtitle_translations,
"text" => text,
"postdate" => b.postdate,
"deadline" => b.deadline,
"category" => b.category.title_translations,
"tags" => ts,
"image" => image,
"links" => links,
"files" => files,
"author" => author,
"url" => "/#{I18n.locale.to_s + page.url}/#{b.to_param}"
}
end
# 計算總筆數 Start
if !params[:keyword].blank?
keyword = Regexp.new(".*"+params[:keyword]+".*")
bulletin_count = Bulletin.any_of({:title=>keyword},{:subtitle=>keyword},{:text=>keyword})
else
bulletin_count = Bulletin.all
end
bulletin_count = bulletin_count.where(:is_preview.in=>[false,nil])
total_pages = bulletin_count.count
# End
render :json => {
"bulletins" => bulletins,
"bulletins_count" => bulletins.count,
"page_num" => page_num,
"total_pages" => total_pages,
}.to_json
end
def smart_convertor(text)
html_string = text
links = html_string.scan(/img.*?src="(.*?)"/i)
links.each do |link|
l = link.first
new_link = nil
if l.starts_with?("/")
new_link = request.protocol + request.host_with_port + l
elsif l.starts_with?("..")
l1 = l.gsub("../","")
new_link = request.protocol + request.host_with_port + "/" + l1
end
html_string = html_string.sub(l,new_link) if !new_link.nil?
end
return html_string
end
protected
def set_I18n
I18n.locale = params[:lang] if params[:lang].present?
end
end

View File

@ -8,13 +8,13 @@ class AnnouncementsController < ApplicationController
if params[:keywords] if params[:keywords]
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"} !params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
announcements = Bulletin.where(:title.ne => "").any_of(:title => /#{params[:keywords].to_s}/i).is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:keywords].nil? announcements = Bulletin.where(:title.ne => "").any_of(:title => /#{params[:keywords].to_s}/i).is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:keywords].nil?
announcements_count = ",共" + announcements.count.to_s + "筆資料" announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keyword = "搜尋標題有關 ' " + params[:keywords] + " '" keyword = "搜尋標題有關 ' " + params[:keywords] + " '"
elsif params[:unit] elsif params[:unit]
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"} !params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
announcements = Bulletin.where(:title.ne => "" ,:cache_dept => params[:unit].to_s).is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:unit].nil? announcements = Bulletin.where(:title.ne => "" ,:cache_dept => params[:unit].to_s).is_approved.order_by(sort).filter_by_categories(["all"]).per(15) if !params[:unit].nil?
announcements_count = ",共" + announcements.count.to_s + "筆資料" announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keyword = "列出由:" + params[:unit] + "發佈的資料" keyword = "列出由:" + params[:unit] + "發佈的資料"
elsif params[:tag] elsif params[:tag]
@ -26,7 +26,7 @@ class AnnouncementsController < ApplicationController
!params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"} !params[:sort].blank? ? sort = {params[:sort].to_sym=>params[:order]} : sort = {:is_top=>"desc",:postdate=>"desc"}
category = Category.find_by(:uid => params["category"].split("-").last) rescue nil category = Category.find_by(:uid => params["category"].split("-").last) rescue nil
announcements = Bulletin.all.where(:title.ne => "").can_display.is_approved.order_by(sort).filter_by_categories([category.id.to_s]) if !category.nil? announcements = Bulletin.all.where(:title.ne => "").can_display.is_approved.order_by(sort).filter_by_categories([category.id.to_s]) if !category.nil?
announcements_count = ",共" + announcements.count.to_s + "筆資料" announcements_count = ",共搜尋到" + announcements.count.to_s + "筆資料"
keywords = params[:category].split("-") keywords = params[:category].split("-")
keywords.delete(keywords.last) keywords.delete(keywords.last)