From 05eb32cbd2239d4e5981292d41eeda75595ea47f Mon Sep 17 00:00:00 2001 From: JiangRu Date: Fri, 13 Feb 2015 14:24:51 +0800 Subject: [PATCH] export son --- app/controllers/admin/bulletins_controller.rb | 102 ++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 103 insertions(+) create mode 100644 app/controllers/admin/bulletins_controller.rb diff --git a/app/controllers/admin/bulletins_controller.rb b/app/controllers/admin/bulletins_controller.rb new file mode 100644 index 0000000..07c2e54 --- /dev/null +++ b/app/controllers/admin/bulletins_controller.rb @@ -0,0 +1,102 @@ +# encoding: utf-8 +class Admin::BulletinsController < ApplicationController + before_filter :set_I18n + + def get_bulletins + # 頁次 + 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 + } + + end + + render :json => { + "bulletins" => bulletins + }.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 diff --git a/config/routes.rb b/config/routes.rb index 319d041..65ff35a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ Rails.application.routes.draw do post 'announcement/preview', to: 'announcements#preview' get 'announcement/destroy_preview/:slug_title-:uid', to: 'announcements#destroy_preview' get 'announcement/approve_bulletin', to: 'announcements#approve_bulletin' + get "announcement/bulletins.json" => "bulletins#get_bulletins" resources :announcements end