# API for Announcement Module # # Simple access control # params[:access_token] = Site ID # ex: /panel/announcement/api/bulletins.json?access_token=5301d8be45d5f42f4f000001 # # Set Language # Language can be "zh_tw" or "en" # ex: /panel/announcement/api/bulletins.json?lang=en&access_token=5301d8be45d5f42f4f000001 # # Get Categories and Tags # ex: /panel/announcement/api/categories_tags.json&access_token=5301d8be45d5f42f4f000001 # # Query Bulletins # Set page number use params[:page_num] # Set number of results per page use params[:per_page] # ex: /panel/announcement/api/bulletins.json?page_num=3&per_page=15&access_token=5301d8be45d5f42f4f000001 # # Filter bulletins with categories use params[:categories] # ex: /panel/announcement/api/bulletins.json?categories[]=53046e4045d5f462a2000006&access_token=5301d8be45d5f42f4f000001 # # Filter bulletins with tags use params[:tags] # ex: /panel/announcement/api/bulletins.json?tags[]=5318e58d45d5f41127000007&access_token=5301d8be45d5f42f4f000001 # # Filter bulletins with "is_hot" use params[:is_hot] # ex: /panel/announcement/api/bulletins.json?is_hot=1&access_token=5301d8be45d5f42f4f000001 # # Search bulletin title, subtitle, text with params[:keyword] # ex: /panel/announcement/api/bulletins.json?keyword=大學&access_token=5301d8be45d5f42f4f000001 class Panel::Announcement::Api::BulletinsController < OrbitWidgetController before_filter :set_I18n, :check_site_id def get_categories_tags module_id = ModuleApp.where(:key=>"announcement").first.id categories = Category.where(:module_app_id => module_id).collect{|c| {"id"=>c.id, "title"=>c.title_translations}} module_tags = ModuleTag.where(:module_app_id => module_id).collect{|t| t.id} tags = Tag.any_of({:tag_lease_id.in => module_tags}).collect{|t| {"id"=>t.id, "title"=>t.name_translations}} render :json => { "categories" => categories, "tags" => tags } end 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}).available_for_lang(I18n.locale) else bulletins = Bulletin.available_for_lang(I18n.locale) 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.desc( :is_top, :postdate).page(page_num).per(per_page) total_pages = (bulletins.count / per_page) + 1 render :json => { "bulletins" => bulletins, "bulletins_count" => bulletins.count, "page_num" => page_num, "total_pages" => total_pages, "lang"=>I18n.locale } end protected def set_I18n I18n.locale = params[:lang] if params[:lang].present? end def check_site_id raise ActionController::RoutingError.new('Not Found') if Site.first.id.to_s != params['access_token'] end end