diff --git a/app/controllers/admin/archive_files_controller.rb b/app/controllers/admin/archive_files_controller.rb index 1cadfca..e2205e9 100644 --- a/app/controllers/admin/archive_files_controller.rb +++ b/app/controllers/admin/archive_files_controller.rb @@ -27,7 +27,7 @@ class Admin::ArchiveFilesController < OrbitAdminController def index if ArchiveSortOrder.count == 0 - ArchiveSortOrder.new('sort_order' => false).save + ArchiveSortOrder.create('sort_order' => false) end if !(params['order_asc'].nil?) if params['order_asc'] == 'true' @@ -41,7 +41,7 @@ class Admin::ArchiveFilesController < OrbitAdminController @categories = @module_app.categories.enabled @tags = @module_app.tags @filter_fields = filter_fields(@categories, @tags) - @archives = ArchiveFile.order_by(sort) + @archives = ArchiveFile.local_data.order_by(sort) .with_categories(filters("category")) .with_tags(filters("tag")) .with_status(filters("status")) @@ -52,9 +52,10 @@ class Admin::ArchiveFilesController < OrbitAdminController end def new - @archive_file = ArchiveFile.new - @tags = @module_app.tags - @categories = @module_app.categories.enabled + sort_number = ArchiveSortOrder.first.get_default_order + @archive_file = ArchiveFile.new(:sort_number=>sort_number) + @tags = @module_app.tags + @categories = @module_app.categories.enabled end def edit @@ -99,6 +100,9 @@ class Admin::ArchiveFilesController < OrbitAdminController respond_to do |format| if @archive_file.save + Thread.new do + @archive_file.notify_feed("create") + end format.html { redirect_to(admin_archive_files_path) } format.xml { render :xml => @archive_file, :status => :created, :location => @archive_file } else @@ -129,6 +133,9 @@ class Admin::ArchiveFilesController < OrbitAdminController respond_to do |format| if @archive_file.update_attributes(archive_vars) + Thread.new do + @archive_file.notify_feed("update") + end format.html { redirect_to(admin_archive_files_path) } format.xml { head :ok } else @@ -141,11 +148,72 @@ class Admin::ArchiveFilesController < OrbitAdminController def destroy archive_file = ArchiveFile.find(params[:id]) archive_file.destroy + Thread.new do + archive_file.notify_feed("destroy") + end redirect_to admin_archive_files_path end + def delete + if params[:ids] + ArchiveFile.any_in(:uid => params[:ids]).destroy_all + Thread.new do + ArchiveFile.notify_feed_delete(params[:ids]) + end + end + if request.xhr? + render :nothing => true, :status => 204 + else + redirect_to admin_archive_files_path + end + end + def feed + @table_feed_fields = ["archive.feed_name",:tags , :category , "archive.rssfeed", "archive.jsonfeed"] + @feeds = ArchiveFileFeed.all.asc(:created_at) + end + def feedform + if params[:type] == "new" + @archive_feed = ArchiveFileFeed.new(id: nil) + render :partial => "feed_form" + else params[:type] == "edit" + @archive_feed = ArchiveFileFeed.find(params[:id]) + render :partial => "edit_feed_form" + end + end + def createfeed + archive_feed = ArchiveFileFeed.new(feed_params) + archive_feed.save + #ArchiveFileFeed.create_feed_cache(nil,archive_feed) + feeds = ArchiveFileFeed.all.asc(:created_at) + render :partial => "feed", :collection => feeds + end + + def updatefeed + archive_feed = ArchiveFileFeed.find(params[:id]) + archive_feed.update_attributes(feed_params) + archive_feed.save + #ArchiveFileFeed.create_feed_cache(nil,archive_feed) + feeds = ArchiveFileFeed.all.asc(:created_at) + render :partial => "feed", :collection => feeds + end + + def deletefeed + archive_feed = ArchiveFileFeed.find(params[:id]) + archive_feed.destroy + feeds = ArchiveFileFeed.all.asc(:created_at) + render :partial => "feed", :collection => feeds + end private - + def feed_params + feed_params = params.require(:archive_file_feed).permit! + if feed_params[:tag_ids].nil? + feed_params[:tag_ids] = [] + end + if feed_params[:category_ids].nil? + feed_params[:category_ids] = [] + end + feed_params + end def archive_vars params[:archive_file][:tags] ||=[] params.require(:archive_file).permit! diff --git a/app/controllers/archive_feeds_controller.rb b/app/controllers/archive_feeds_controller.rb new file mode 100644 index 0000000..a42c641 --- /dev/null +++ b/app/controllers/archive_feeds_controller.rb @@ -0,0 +1,92 @@ +require "rss" +class ArchiveFeedsController < ApplicationController + include Admin::ArchiveFilesHelper + def feed_add_remote + if params[:url].present? + uid = params[:uid] + archive_file_feed = ArchiveFileFeed.where(uid: uid).first + if !(archive_file_feed.remote_urls.include?(params[:url])) + archive_file_feed.remote_urls << params[:url] + archive_file_feed.save + end + end + render :json => {success: true} + end + def feed_remove_remote + if params[:url].present? + uid = params[:uid] + archive_file_feed = ArchiveFileFeed.where(uid: uid).first + if archive_file_feed.remote_urls.delete(params[:url]) + archive_file_feed.save + end + end + render :json => {success: true} + end + def feed + uid = params[:uid] + feed_cache = ArchiveFileFeedCache.where(uid: uid) + feed_cache_old = feed_cache.last + count = feed_cache.count + if count > 1 + feed_cache.limit(count-1).destroy + end + feed_cache = feed_cache.first + archive_content = '' + if feed_cache.nil? + archive_content = ArchiveFileFeed.where(uid: uid).first.generate_one_cache_timeout(base_url: request.base_url,timeout: 20) + archive_content = (feed_cache_old.content rescue "") if archive_content.nil? + else + archive_content = feed_cache.content + end + render :json => archive_content + end + + def rssfeed + uid = params[:uid] + @aff = ArchiveFileFeed.find_by(:uid => uid) rescue nil + if !@aff.nil? + tags = @aff.tag_ids + if !tags.empty? + @archive_files = ArchiveFile.local_data.can_display_and_sorted.filter_by_tags(tags) + else + @archive_files = ArchiveFile.local_data.can_display_and_sorted + end + categories = @aff.category_ids + if !categories.empty? + @archive_files = @archive_files.filter_by_categories(categories) + end + end + respond_to do |format| + format.html {redirect_to "/xhr/archive_files/rssfeed/#{@aff.uid}.rss"} + format.rss + end + end + + def feeds + feeds = [] + ArchiveFileFeed.all.each do |aff| + feed = {} + feed["title_translations"] = aff.title_translations + feed["uid"] = aff.uid + feed["url"] = "#{request.base_url}/xhr/archive_files/feed/#{aff.uid}" + feed["xml_url"] = "#{request.base_url}/xhr/archive_files/rssfeed/#{aff.uid}.rss" + feed["tags"] = [] + aff.tag_ids.each do |t| + tag = Tag.find(t) + d = {} + d["name_translations"] = tag.name_translations + feed["tags"] << d + end + feeds << feed + end + render :json => {"feeds" => feeds}.to_json + end + + +end + + + + + + diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index 518b293..e88c767 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -4,331 +4,226 @@ class ArchivesController < ApplicationController ac.render_to_string("archives/custom_widget_data",:locals=>{:@custom_data_field=>@custom_data_field,:@field_name=>@field_name}) end #avoid the categories to be not in the ArchiveCategory - def serve_cmap - serve_path=File.expand_path("../../assets/javascripts/archive/pdf/bcmaps/#{params[:file_name]}.#{params[:extension]}",__FILE__) - if Dir.glob(serve_path).length != 0 - send_file(serve_path, type: "application/octet-stream") - else - render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html' and return - end - end - def index - page = OrbitHelper.page rescue nil - unless page - page = Page.where(url:params['url']).first - end - params = OrbitHelper.params + def serve_cmap + serve_path=File.expand_path("../../assets/javascripts/archive/pdf/bcmaps/#{params[:file_name]}.#{params[:extension]}",__FILE__) + if Dir.glob(serve_path).length != 0 + send_file(serve_path, type: "application/octet-stream") + else + render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found, :content_type => 'text/html' and return + end + end + def show + params = OrbitHelper.params + archive_file = ArchiveFile.find_by(:uid=>params[:uid]) + locale = I18n.locale.to_s + data, serial_number, idx = archive_file.get_frontend_data(locale, 0, 0, true, OrbitHelper.url_to_show("")) + { + "data" => data + } + end + def index + ArchiveFile.check_sort_number + page = OrbitHelper.page rescue nil + unless page + page = Page.where(url:params['url']).first + end + params = OrbitHelper.params @@total_pages = 1 in_class = "" custom_data_field = page.custom_data_field rescue nil - if custom_data_field && custom_data_field["expanded"] == "yes" - in_class = "in" - end + if custom_data_field && custom_data_field["expanded"] == "yes" + in_class = "in" + end + locale = I18n.locale.to_s cats_last = [] - sort_order = (ArchiveSortOrder.first['sort_order'] rescue false) ? 1 : -1 #Order with ascending - if !params['title'].nil? - files_by_category = ArchiveFile.where(is_hidden: false).order_by(:created_at => "desc").group_by(&:category) - categories = files_by_category.keys - categories_sort = get_sorted_cat_with_filter(categories,'orm') - categories_sort.each do |category| - url_to_edit = "" - flag = false - archives = [] - serial_number = 0 - files_by_category[category].each_with_index do |archive,idx| - if archive.title == params['title'] - flag = true - statuses = archive.statuses_with_classname.collect do |status| - { - "status" => status["name"] || "", - "status-class" => "status-#{status['classname']}" - } - end - files = [] - archive.archive_file_multiples.order_by(:sort_number=>'desc').each do |file| - if file.choose_lang.include?(I18n.locale.to_s) - title = (file.file_title.blank? ? File.basename(file.file.path) : file.file_title) rescue "" - extension = file.file.file.extension.downcase rescue "" - serial_number += 1 - files << { - "file-name" => title, - "file-type" => extension, - "file-url" => "/xhr/archive/download?file=#{file.id}", - "serial_number" => serial_number - } - end - end - if archive.urls.present? - archive.urls.each_with_index do |url,i| - serial_number += 1 - files << { - "file-name" => archive.title, - "file-type" => archive.get_url_text(i), - "file-url" => url, - "serial_number" => serial_number - } - end - end - archives << { - "archive-title" => archive.title, - "description" => archive.description, - "created_at" => archive.created_at.strftime('%Y%m%d').to_i, - "archive-url" => archive.url, - "url" => archive.url, - "statuses" => statuses, - "sort_number" => archive.sort_number, - "is_top" => (archive.is_top ? 1 : 0), - "files" => files, - "idx" => (idx + 1) - } - end - end - if flag - cats_last << { - "category-title" => category.title, - "archives" => archives, - "url_to_edit" => url_to_edit - } - end - end - else - if OrbitHelper.page_data_count > 0 - OrbitHelper.set_page_data_count 0 - page = Page.where(:page_id => params["page_id"]).first rescue nil - if !page.nil? - page.data_count = 0 - page.save - end - end - categories = OrbitHelper.page_categories - tags = OrbitHelper.page_tags - module_app = ModuleApp.where(:key=>'archive').first - if categories == ["all"] - categories = module_app.categories.pluck(:id).map(&:to_s) - end - if tags == ["all"] - tags = module_app.tags.sort_by{|tag| ((module_app.asc rescue true) ? tag.sort_number.to_i : -tag.sort_number.to_i)}.map{|tag| tag.id.to_s} - tags << nil - end - if params[:data_count].to_i <=0 - page_data_count = 0 - else - page_data_count = params[:data_count].to_i - end - if params[:page_no].nil? - page_no = 1 - else - page_no = params[:page_no].to_i - end - if (tags.count > 1 && categories.count <= 1 rescue false) - archive_files = ArchiveFile.where(is_hidden: false,:title.nin=>["",nil]).filter_by_categories.order_by(:created_at => "desc") - group_archive_files = {} - cats = [] - each_data_count = [] - archive_files_ids = archive_files.pluck(:id) - archive_files_ids_count = archive_files_ids.count - tags.each do |tag_id| - tag_name = "" - if tag_id - tag = Tag.find(tag_id) rescue nil - next if tag.nil? - url_to_edit = OrbitHelper.current_user ? "/admin/archive_files?filters[tags][]=#{tag_id}" : "" - taggings = Tagging.where(:tag_id=>tag_id).pluck(:taggable_id) - archives = archive_files.where(:id.in=>taggings).to_a - archive_files_ids = archive_files_ids - archives.map{|a| a.id} - tag_name = Tag.find(tag_id).name - else - if categories.count == 1 - url_to_edit = OrbitHelper.current_user ? "/admin/archive_files?filters[category][]=#{categories[0]}" : "" - else - url_to_edit = OrbitHelper.current_user ? "/admin/archive_files" : "" - end - archives = [] - if tags.count > 1 && archive_files_ids_count != archive_files_ids.count - tag_name = I18n.t("archive.other") - archives = archive_files.where(:id.in=>archive_files_ids).to_a - else - archives = archive_files.to_a - end - end - serial_number = 0 - if archives.count != 0 - archives = archives.sort_by{|k| [(k["is_top"] ? 0 : 1) ,(k["sort_number"].nil? ? Float::INFINITY : sort_order * k["sort_number"].to_i),-k["created_at"].to_i]}.map.with_index do |archive,idx| - statuses = archive.statuses_with_classname.collect do |status| - { - "status" => status["name"] || "", - "status-class" => "status-#{status['classname']}" - } - end - files = [] - archive.archive_file_multiples.order_by(:sort_number=>'desc').each do |file| - if file.choose_lang.include?(I18n.locale.to_s) - title = (file.file_title.blank? ? File.basename(file.file.path) : file.file_title) rescue "" - extension = file.file.file.extension.downcase rescue "" - serial_number += 1 - files << { - "file-name" => title, - "file-type" => extension, - "file-url" => "/xhr/archive/download?file=#{file.id}", - "serial_number" => serial_number - } - end - end - if archive.urls.present? - archive.urls.each_with_index do |url,i| - serial_number += 1 - files << { - "file-name" => archive.title, - "file-type" => archive.get_url_text(i), - "file-url" => url, - "serial_number" => serial_number - } - end - end - { - "archive-title" => archive.title, - "description" => archive.description, - "created_at" => archive.created_at.strftime('%Y%m%d').to_i, - "archive-url" => archive.url, - "url" => archive.url, - "statuses" => statuses, - "sort_number" => archive.sort_number, - "is_top" => (archive.is_top ? 1 : 0), - "files" => files, - "idx" => (idx + 1) - } - end - each_data_count.push(archives.length) - sorted = archives - cats << { - "category-title" => tag_name, - "archives" => sorted, - "url_to_edit" => url_to_edit - } - end - end - cats_last = cats - else - files_by_category = ArchiveFile.where(is_hidden: false,:title.nin=>["",nil]).filter_by_categories.filter_by_tags.order_by(:created_at => "desc").group_by(&:category) - each_data_count = [] - categories = files_by_category.keys - categories_sort = get_sorted_cat_with_filter(categories,'orm') - cats = categories_sort.collect do |category| - url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : "" - serial_number = 0 - archives = files_by_category[category].sort_by{|k| [(k["is_top"] ? 0 : 1) ,(k["sort_number"].nil? ? Float::INFINITY : sort_order * k["sort_number"].to_i),-k["created_at"].to_i]}.collect.with_index do |archive,idx| - statuses = archive.statuses_with_classname.collect do |status| - { - "status" => status["name"] || "", - "status-class" => "status-#{status['classname']}" - } - end - files = [] - archive.archive_file_multiples.order_by(:sort_number=>'desc').each do |file| - if file.choose_lang.include?(I18n.locale.to_s) - serial_number += 1 - title = (file.file_title.blank? ? File.basename(file.file.path) : file.file_title) rescue "" - extension = file.file.file.extension.downcase rescue "" - files << { - "file-name" => title, - "file-type" => extension, - "file-url" => "/xhr/archive/download?file=#{file.id}", - "serial_number" => serial_number - } - end - end - if archive.urls.present? - archive.urls.each_with_index do |url,i| - serial_number += 1 - files << { - "file-name" => archive.title, - "file-type" => archive.get_url_text(i), - "file-url" => url, - "serial_number" => serial_number - } - end - end - { - "archive-title" => archive.title, - "description" => archive.description, - "created_at" => archive.created_at.strftime('%Y%m%d').to_i, - "archive-url" => archive.url, - "url" => archive.url, - "statuses" => statuses, - "sort_number" => archive.sort_number, - "is_top" => (archive.is_top ? 1 : 0), - "files" => files, - "idx" => (idx + 1) - } - end - each_data_count.push(archives.length) - sorted = archives - { - "category-title" => (categories.count > 1 ? category.title : ""), - "archives" => sorted, - "url_to_edit" => url_to_edit - } - end - if page_data_count != 0 - all_data_count = 0 - data_start = -1 - data_end = -1 - for i in 0...each_data_count.length - all_data_count_before = all_data_count - all_data_count += each_data_count[i] - if ( data_start == -1 && (all_data_count > (page_no - 1) * page_data_count) ) - data_start = i - data_start_first = (page_no - 1) * page_data_count - all_data_count_before - end - if ( data_end == -1 && (all_data_count > (page_no * page_data_count - 1)) ) - data_end = i - data_end_last = (page_no * page_data_count - 1) - all_data_count_before - end - end - if ( data_end == -1 && data_start != -1) - data_end = i - data_end_last = -1 - end - if data_start!=-1 && page_no>=1 - cats_last = [] - for i in data_start..data_end - if data_start != data_end - if i==data_start - cats_last << Hash[cats[i].map{|k,v| [k , (k=='archives' ? v[data_start_first..-1] : v)]}] - elsif i==data_end - cats_last << Hash[cats[i].map{|k,v| [k , (k=='archives' ? v[0..data_end_last] : v)]}] - else - cats_last << cats[i] - end - else - cats_last << Hash[cats[i].map{|k,v| [k,(k=='archives' ? v[data_start_first..data_end_last] : v)]}] - end - end - else - cats_last = [Hash[cats[0].map{|k,v| [k,'']}]] - end - @@total_pages = (all_data_count.to_f / page_data_count).ceil - else - if page_no!=1 - cats_last = [Hash[cats[0].map{|k,v| [k,'']}]] - @@total_pages = 0 - else - cats_last = cats - @@total_pages = 1 - end - end - end + if !params['title'].nil? + files_by_category = ArchiveFile.can_display.sorted.group_by(&:category) + categories = files_by_category.keys + categories_sort = get_sorted_cat_with_filter(categories,'orm') + categories_sort.each_with_index do |category, cat_idx| + url_to_edit = "" + flag = false + archives = [] + serial_number = 0 + files_by_category[category].each_with_index do |archive,idx| + if archive.title == params['title'] + flag = true + data, serial_number, idx = archive.get_frontend_data(locale, serial_number, idx) + data + end + end + if flag + cats_last << { + "category-id" => category.id, + "category-title" => category.title, + "archives" => archives, + "url_to_edit" => url_to_edit + } + end + end + else + if OrbitHelper.page_data_count > 0 + OrbitHelper.set_page_data_count 0 + page = Page.where(:page_id => params["page_id"]).first rescue nil + if !page.nil? + page.data_count = 0 + page.save + end + end + categories = params["categories"].blank? ? OrbitHelper.page_categories : params["categories"] + tags = params["tags"].blank? ? OrbitHelper.page_tags : params["tags"] + module_app = ModuleApp.where(:key=>'archive').first + if categories == ["all"] + categories = module_app.categories.pluck(:id).map(&:to_s) + end + if tags == ["all"] + tags = module_app.tags.sort_by{|tag| ((module_app.asc rescue true) ? tag.sort_number.to_i : -tag.sort_number.to_i)}.map{|tag| tag.id.to_s} + tags << nil + end + if params[:data_count].to_i <=0 + page_data_count = 0 + else + page_data_count = params[:data_count].to_i + end + if params[:page_no].nil? + page_no = 1 + else + page_no = params[:page_no].to_i + end + if (tags.count > 1 && categories.count <= 1 rescue false) + archive_files = ArchiveFile.can_display.filter_by_categories(categories).sorted + group_archive_files = {} + cats = [] + each_data_count = [] + archive_files_ids = archive_files.pluck(:id) + archive_files_ids_count = archive_files_ids.count + tags.each_with_index do |tag_id, tag_idx| + tag_name = "" + if tag_id + tag = Tag.find(tag_id) rescue nil + next if tag.nil? + url_to_edit = OrbitHelper.current_user ? "/admin/archive_files?filters[tags][]=#{tag_id}" : "" + taggings = Tagging.where(:tag_id=>tag_id).pluck(:taggable_id) + archives = archive_files.where(:id.in=>taggings).to_a + archive_files_ids = archive_files_ids - archives.map{|a| a.id} + tag_name = Tag.find(tag_id).name + else + if categories.count == 1 + url_to_edit = OrbitHelper.current_user ? "/admin/archive_files?filters[category][]=#{categories[0]}" : "" + else + url_to_edit = OrbitHelper.current_user ? "/admin/archive_files" : "" + end + archives = [] + if tags.count > 1 && archive_files_ids_count != archive_files_ids.count + tag_name = I18n.t("archive.other") + archives = archive_files.where(:id.in=>archive_files_ids).to_a + else + archives = archive_files.to_a + end + end + serial_number = 0 + if archives.count != 0 + archives = archives.map.with_index do |archive,idx| + + + end + each_data_count.push(archives.length) + sorted = archives + cats << { + "category-id" => tag_id, + "category-title" => tag_name, + "archives" => sorted, + "url_to_edit" => url_to_edit + } + end + end + cats_last = cats + else + files_by_category = ArchiveFile.can_display.filter_by_categories(categories).filter_by_tags(tags).sorted.group_by(&:category) + each_data_count = [] + categories = files_by_category.keys + categories_sort = get_sorted_cat_with_filter(categories,'orm') + cats = categories_sort.collect.with_index do |category, cat_idx| + url_to_edit = OrbitHelper.user_has_cateogry?(category) ? "/admin/archive_files?filters[category][]=#{category.id.to_s}" : "" + serial_number = 0 + archives = files_by_category[category].collect.with_index do |archive,idx| + data, serial_number, idx = archive.get_frontend_data(locale, serial_number, idx) + data + end + each_data_count.push(archives.length) + sorted = archives + { + "category-id" => category.id, + "category-title" => (categories.count > 1 ? category.title : ""), + "archives" => sorted, + "url_to_edit" => url_to_edit + } + end + if page_data_count != 0 + all_data_count = 0 + data_start = -1 + data_end = -1 + for i in 0...each_data_count.length + all_data_count_before = all_data_count + all_data_count += each_data_count[i] + if ( data_start == -1 && (all_data_count > (page_no - 1) * page_data_count) ) + data_start = i + data_start_first = (page_no - 1) * page_data_count - all_data_count_before + end + if ( data_end == -1 && (all_data_count > (page_no * page_data_count - 1)) ) + data_end = i + data_end_last = (page_no * page_data_count - 1) - all_data_count_before + end + end + if ( data_end == -1 && data_start != -1) + data_end = i + data_end_last = -1 + end + if data_start!=-1 && page_no>=1 + cats_last = [] + for i in data_start..data_end + if data_start != data_end + if i==data_start + cats_last << Hash[cats[i].map{|k,v| [k , (k=='archives' ? v[data_start_first..-1] : v)]}] + elsif i==data_end + cats_last << Hash[cats[i].map{|k,v| [k , (k=='archives' ? v[0..data_end_last] : v)]}] + else + cats_last << cats[i] + end + else + cats_last << Hash[cats[i].map{|k,v| [k,(k=='archives' ? v[data_start_first..data_end_last] : v)]}] + end + end + else + cats_last = [Hash[cats[0].map{|k,v| [k,'']}]] + end + @@total_pages = (all_data_count.to_f / page_data_count).ceil + else + if page_no!=1 + cats_last = [Hash[cats[0].map{|k,v| [k,'']}]] + @@total_pages = 0 + else + cats_last = cats + @@total_pages = 1 + end + end + end + end + if cats_last.count != 0 + cats_last.each_with_index do |h, i| + h["active_class"] = (i == 0 ? 'active' : '') + end end { "categories" => cats_last, - "extras" =>{ - "serial_number-head" => I18n.t("archive.serial_number"), - "date-head" => I18n.t("archive.updated_at"), - "title-head" => I18n.t(:name), - "description-head"=>I18n.t("archive.description"), - "file-head" =>I18n.t("archive.download_file"), - "in_class" => in_class - } + "extras" =>{ + "serial_number-head" => I18n.t("archive.serial_number"), + "date-head" => I18n.t("archive.updated_at"), + "title-head" => I18n.t(:name), + "description-head"=>I18n.t("archive.description"), + "file-head" =>I18n.t("archive.download_file"), + "in_class" => in_class + } } end @@ -336,28 +231,28 @@ class ArchivesController < ApplicationController file_id = params[:file] file = ArchiveFileMultiple.find(file_id) rescue nil if !file.nil? - file.download_count = file.download_count + 1 - file.save - @url = file.file.url - begin - @path = file.file.file.file rescue "" - @filename = @path.split("/").last - @ext = @path.split("/").last.to_s.split(".").last - if @ext == "png" || @ext == "jpg" || @ext == "bmp" || @ext == "pdf" - render "download_file",:layout=>false - else - if (current_site.accessibility_mode rescue false) - render "redirect_to_file",:layout=>false - else - send_file(@path) - end - end - rescue - redirect_to @url - end - else - render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found - end + file.download_count = file.download_count + 1 + file.save + @url = file.file.url + begin + @path = file.file.file.file rescue "" + @filename = @path.split("/").last + @ext = @path.split("/").last.to_s.split(".").last + if @ext == "png" || @ext == "jpg" || @ext == "bmp" || @ext == "pdf" + render "download_file",:layout=>false + else + if (current_site.accessibility_mode rescue false) + render "redirect_to_file",:layout=>false + else + send_file(@path) + end + end + rescue + redirect_to @url + end + else + render :file => "#{Rails.root}/app/views/errors/404.html", :layout => false, :status => :not_found + end end def group_by_category @@ -375,137 +270,143 @@ class ArchivesController < ApplicationController files_by_category_tag.keys.each do |t| archives = [] archive_counts = archive_counts + 1 - files_by_category_tag[t].each_with_index do |archive,index| - a = { - "archive-title" => archive.title, - "description" => archive.description, - "archive-url" => archive.url, - "archive_url" => OrbitHelper.widget_more_url - } - - if t.count > 1 - t.each do |inner_tag| - index = ts.index{|tot| tot["tag-name"] == inner_tag.name} - if !index.nil? - break if ts[index]["archives"].count >= OrbitHelper.widget_data_count - ts[index]["archives"] << a - else - break if archives.count >= OrbitHelper.widget_data_count - archives << a + files_by_category_tag[t].each do |archive| + a = { + "archive-title" => archive.title, + "description" => archive.description, + "archive-url" => archive.url, + "archive_url" => OrbitHelper.widget_more_url + } + + if t.count > 1 + t.each do |inner_tag| + idx = ts.index{|tot| tot["tag-name"] == inner_tag.name} + if !idx.nil? + break if ts[idx]["archives"].count >= OrbitHelper.widget_data_count + ts[idx]["archives"] << a + else + break if archives.count >= OrbitHelper.widget_data_count + archives << a + end end + else + break if archives.count >= OrbitHelper.widget_data_count + archives << a end - else - break if archives.count >= OrbitHelper.widget_data_count - archives << a + end - - end - ts << { - "tag-name" => (t[0].name rescue ""), - "archives" => archives - } if !archives.blank? && archive_counts < OrbitHelper.widget_data_count + ts << { + "tag-name" => (t[0].name rescue ""), + "archives" => archives + } if !archives.blank? && archive_counts < OrbitHelper.widget_data_count end { + "category-id" => cat.id, "category-title" => cat.title, "tags" => ts } end { "categories" => cats, - "extras" => {"widget-title" => "Archives","more_url" => OrbitHelper.widget_more_url} + "extras" => {"more_url" => OrbitHelper.widget_more_url} } end - - def widget - page_data_count = OrbitHelper.widget_data_count - categories = OrbitHelper.widget_categories #data are categories' ids or 'all' - @categories = [] - if categories.first == "all" - categories = OrbitHelper.widget_module_app.categories - categories_sort = get_sorted_cat_with_filter(categories,'orm') - @categories = categories_sort.collect do |cat| - { - "title" => cat.title, - "id" => cat.id.to_s - } - end - else - categories_sort = get_sorted_cat_with_filter(categories,'id') - @categories = categories_sort.collect do |cat| - { - "title" => cat.title, - "id" => cat.id.to_s - } - end + def get_archives_count(cats_with_archives) + cats_with_archives.map{|h| (h["archives"].count rescue 0)}.sum + end + def get_anncs_for_pack_data(categories,tags) + page_data_count = OrbitHelper.widget_data_count + sort_order = (ArchiveSortOrder.first['sort_order'] rescue false) ? 1 : -1 #Order with ascending + subpart = OrbitHelper.get_current_widget + filter_cache_parent_id = subpart.id.to_s + categories.to_s + tags.to_s + page_data_count.to_s + archive_cache = ArchiveCache.where(parent_id: filter_cache_parent_id,locale: I18n.locale.to_s) + count = archive_cache.count + if count > 1 + archive_cache.limit(count-1).destroy end - cats = @categories.collect do |cat| - if ArchiveSortOrder.first.sort_order #Order with ascending - archives_all = ArchiveFile.can_display.where(:category_id => cat["id"]).filter_by_tags(OrbitHelper.widget_tags) - temp_with_nil = archives_all.in(sort_number: nil).order_by(created_at: 'desc') - temp_no_nil = archives_all.not_in(sort_number: nil).order_by(sort_number: 'asc',created_at: 'desc') - archives_temp = temp_no_nil.concat(temp_with_nil) - archives = archives_temp.collect do |archive| - if archive.archive_file_multiples.count==0 - url = (archive[:url][OrbitHelper.get_site_locale].to_s.empty? ? 'javascript:void' : archive[:url][OrbitHelper.get_site_locale]) - else - url = archive.archive_file_multiples.count > 1 ? (OrbitHelper.widget_more_url + '?title=' + archive.title.to_s) : "/xhr/archive/download?file=#{archive.archive_file_multiples.first.id}" - end - { - "archive-title" => archive.title, - "description" => archive.description, - "archive-url" => archive.url, - "archive_url" => url - } - end + if archive_cache.count == 0 + @categories = [] + categorie_ids = [] + if categories.first == "all" + categories = OrbitHelper.widget_module_app.categories + categories_sort = get_sorted_cat_with_filter(categories,'orm') + @categories = categories_sort.collect do |cat| + cat_id = cat.id.to_s + categorie_ids << cat_id + { + "title" => cat.title, + "id" => cat_id + } + end else - archives_temp = ArchiveFile.can_display.where(:category_id => cat["id"]).filter_by_tags(OrbitHelper.widget_tags).order_by(sort_number: 'desc',created_at: 'desc') - archives = archives_temp.collect do |archive| - if archive.archive_file_multiples.count==0 - url = (archive[:url][OrbitHelper.get_site_locale].to_s.empty? ? 'javascript:void' : archive[:url][OrbitHelper.get_site_locale]) - else - url = archive.archive_file_multiples.count > 1 ? (OrbitHelper.widget_more_url + '?title=' + archive.title.to_s) : "/xhr/archive/download?file=#{archive.archive_file_multiples.first.id}" - end - { - "archive-title" => archive.title, - "description" => archive.description, - "archive-url" => archive.url, - "archive_url" => url - } - end + categories_sort = get_sorted_cat_with_filter(categories,'id') + @categories = categories_sort.collect do |cat| + cat_id = cat.id.to_s + categorie_ids << cat_id + { + "title" => cat.title, + "id" => cat_id + } + end end - { - "category-title" => cat["title"], - "category-id" => cat["id"], - "archives" => archives - } + cats_with_archives = @categories.collect do |cat| + archives_sorted = ArchiveFile.can_display.where(:category_id => cat["id"]).filter_by_tags(tags).sorted + archives = archives_sorted.collect do |archive| + archive_url = archive.get_archive_url(locale, OrbitHelper.widget_more_url) + { + "archive-title" => archive.title, + "description" => archive.description, + "archive-url" => archive.url, + "archive_url" => archive_url + } + end + { + "category-title" => cat["title"], + "category-id" => cat["id"], + "archives" => archives + } + end + cats_with_archives = cats_with_archives.select{|h| h["archives"].count != 0} + ArchiveCache.create(parent_id: filter_cache_parent_id,locale: I18n.locale.to_s,filter_result: cats_with_archives) + else + c = archive_cache.first + cats_with_archives = c.filter_result end + cats_with_archives + end + def widget + ArchiveFile.check_sort_number + page_data_count = OrbitHelper.widget_data_count + categories = OrbitHelper.widget_categories #data are categories' ids or 'all' + tags = OrbitHelper.widget_tags + cats_with_archives = get_anncs_for_pack_data(categories, tags) { - "categories" => cats, - "extras" => {"widget-title" => "Archives","more_url" => (OrbitHelper.widget_more_url + '?data_count=' + page_data_count.to_s)} + "categories" => cats_with_archives, + "extras" => {"more_url" => (OrbitHelper.widget_more_url + '?data_count=' + page_data_count.to_s)} } end private - def get_sorted_cat_with_filter(categories,cat_type) - all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil).uniq{|c| c.category_id }.to_a rescue [] - sort_method = ArchiveSortOrder.first.sort_order ? 'asc' : 'desc' - all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: sort_method).uniq{|c| c.category_id }.to_a rescue [] - all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat| - cat.category_id.to_s - end - if cat_type=='id' - categories_temp = ModuleApp.where(:key => "archive").first.categories - categories = categories_temp.select{|cat| categories.include? cat.id.to_s} - end - categories_sort = [] - all_categories_id.each do |cat_id| - category_selected = categories.select{|category| cat_id == category.id.to_s} - if category_selected.length!=0 - categories_sort << category_selected[0] - end - end - categories_sort - end - def self.get_total_pages - @@total_pages - end + def get_sorted_cat_with_filter(categories,cat_type) + all_categories_with_nil = ArchiveCategory.all.in(sort_number: nil).uniq{|c| c.category_id }.to_a rescue [] + sort_method = ArchiveSortOrder.first.sort_order ? 'asc' : 'desc' + all_categories_no_nil = ArchiveCategory.all.not_in(sort_number: nil).order_by(sort_number: sort_method).uniq{|c| c.category_id }.to_a rescue [] + all_categories_id = all_categories_no_nil.concat(all_categories_with_nil).collect do |cat| + cat.category_id.to_s + end + if cat_type=='id' + categories_temp = ModuleApp.where(:key => "archive").first.categories + categories = categories_temp.select{|cat| categories.include? cat.id.to_s} + end + categories_sort = [] + all_categories_id.each do |cat_id| + category_selected = categories.select{|category| cat_id == category.id.to_s} + if category_selected.length!=0 + categories_sort << category_selected[0] + end + end + categories_sort + end + def self.get_total_pages + @@total_pages + end end diff --git a/app/helpers/admin/archive_files_helper.rb b/app/helpers/admin/archive_files_helper.rb index 263353c..18dbe12 100644 --- a/app/helpers/admin/archive_files_helper.rb +++ b/app/helpers/admin/archive_files_helper.rb @@ -1,2 +1,25 @@ module Admin::ArchiveFilesHelper -end + def page_for_archive_file(archive_file) + archive_file_page = nil + pages = Page.where(:module=>'archive').select{|page| page.enabled_for.include?(I18n.locale.to_s)} + + pages.each do |page| + if page.categories.count ==1 + if (page.categories.include?(archive_file.category.id.to_s) rescue false) + archive_file_page = page + end + end + break if !archive_file_page.nil? + end + if archive_file_page.nil? + pages.each do |page| + if (page.categories.include?(archive_file.category.id.to_s) rescue false) + archive_file_page = page + end + break if !archive_file_page.nil? + end + end + archive_file_page = pages.first if archive_file_page.nil? + request.protocol+(request.host_with_port+archive_file_page.url+'/'+archive_file.to_param).gsub('//','/') rescue "#" + end +end \ No newline at end of file diff --git a/app/models/archive_cache.rb b/app/models/archive_cache.rb new file mode 100644 index 0000000..09fb654 --- /dev/null +++ b/app/models/archive_cache.rb @@ -0,0 +1,6 @@ +class ArchiveCache + include Mongoid::Document + field :parent_id + field :filter_result + field :locale,type: String,default: 'zh_tw' +end \ No newline at end of file diff --git a/app/models/archive_file.rb b/app/models/archive_file.rb index f3417c3..e18cf40 100644 --- a/app/models/archive_file.rb +++ b/app/models/archive_file.rb @@ -7,8 +7,42 @@ class ArchiveFile include OrbitModel::Status include OrbitTag::Taggable include Slug - - + require 'archive_model/cache' + unless defined?(ArchiveModel) + Object.send(:remove_const, 'ArchiveModel') rescue nil + $LOADED_FEATURES.select!{|p| !p.include? 'archive_model'} + require 'archive_model/cache' + end + include ::ArchiveModel::Cache + SubPart.class_eval { include ::ArchiveModel::Cache } + Page.class_eval { include ::ArchiveModel::Cache } + index({tmp_sort_number: 1}) + scope :sorted, ->{order(tmp_sort_number: :asc)} + attr_accessor :org_tag_ids,:org_category_id + def tags=(ids) + self.org_tag_ids = self.tag_ids + super(ids) + end + def category=(cat) + self.org_category_id = self.category_id + super(cat) + end + def tag_ids=(ids) + self.org_tag_ids = self.tag_ids + super(ids) + end + def category_id=(cat_id) + self.org_category_id = self.category_id + super(cat_id) + end + def []=(index,value) + if index.to_s=='tags' || index.to_s=='tag_ids' + self.org_tag_ids = self.tag_ids + elsif index.to_s=='category' || index.to_s=='category_id' + self.org_category_id = self.category_id + end + super(index,value) + end # include Tire::Model::Search # include Tire::Model::Callbacks @@ -16,12 +50,29 @@ class ArchiveFile # PAYMENT_TYPES = @site_valid_locales before_save do - cat = self.category rescue nil - if cat && ArchiveCategory.where(:category_id => cat.id).count==0 - ArchiveCategory.create(category_id: cat.id.to_s,sort_number: cat.sort_number) + unless @skip_callback + cat = self.category rescue nil + if cat && ArchiveCategory.where(:category_id => cat.id).count==0 + ArchiveCategory.create(category_id: cat.id.to_s,sort_number: cat.sort_number) + end + end + end + after_save do + unless @skip_callback + if self.sort_number_changed? + self.class.recalc_sort_number + elsif self.tmp_sort_number.nil? + if self.is_top + self.tmp_sort_number = (ArchiveSortOrder.first.max_sort_number.to_i + 1 rescue 1) + @skip_callback = true + self.save + @skip_callback = false + else + self.class.recalc_sort_number + end + end end end - field :title, as: :slug_title, localize: true field :description, localize: true field :urls, localize: true, type: Array @@ -33,11 +84,15 @@ class ArchiveFile field :deadline , :type => DateTime field :uid, type: String field :sort_number, type: Integer + field :tmp_sort_number, type: Integer field :rss2_sn - + field :feed_uid + field :site_feed_id + field :feed_data, :type => Hash, :default => {} # scope :can_display,where(is_hidden: false) - scope :can_display, ->{where(is_hidden: false).order_by([:is_top, :desc],[:sort_number, :asc])} - + scope :local_data, ->{where(:site_feed_id=>nil)} + scope :can_display, ->{where(is_hidden: false,:title.nin=>["",nil])} + scope :can_display_and_sorted, ->{can_display.sorted} # belongs_to :archive_file_category has_many :archive_file_multiples, :autosave => true, :dependent => :destroy @@ -46,7 +101,7 @@ class ArchiveFile # validates :title, :at_least_one => true - after_save :save_archive_file_multiples + after_save :save_archive_file_multiples, :update_tmp_sort_number before_save :add_http @@ -58,7 +113,32 @@ class ArchiveFile end end end - + def self.check_sort_number + archive_sort_order = ArchiveSortOrder.first + if archive_sort_order && archive_sort_order.need_update_sort + self.recalc_sort_number(archive_sort_order.sort_order) + end + end + def self.recalc_sort_number(sort_order=nil) + if sort_order.nil? + sort_order = (ArchiveSortOrder.first['sort_order'] rescue false) ? 1 : -1 + end + sorted_archives = self.all.to_a.sort_by{|k| [(k["is_top"] ? 0 : 1) ,(k["sort_number"].nil? ? Float::INFINITY : sort_order * k["sort_number"].to_i),-k["created_at"].to_i]} + sorted_archives.each_with_index do |a, i| + a.instance_variable_set(:@skip_callback, true) + self.where(:id=>a.id).update_all(:tmp_sort_number => i) + end + max_sort_number = (sorted_archives.count != 0) ? (sort_order == 1 ? sorted_archives.last.tmp_sort_number : sorted_archives.first.tmp_sort_number) : 0 + ArchiveSortOrder.update_all(:max_sort_number=>max_sort_number,:need_update_sort=>false) + self.create_indexes + return max_sort_number + end + def update_tmp_sort_number + return if @skip_callback + if self.new_record? || self.sort_number_changed? + self.class.recalc_sort_number + end + end def get_url_text(idx=0,org=false) url_text = self.url_texts[idx] rescue nil if org @@ -67,8 +147,293 @@ class ArchiveFile url_text.present? ? url_text : "link" end end + def get_data(more_url=nil, base_url=nil, cat_ids=nil, tag_ids=nil, locales=nil) + if locales.nil? + locales = Site.first.in_use_locales rescue I18n.available_locales + end + locale = I18n.locale if locale.nil? + base_url = Site.first.root_url if base_url.nil? + user = User.find(self.create_user_id) rescue nil + a = {} + if !user.nil? + author_translations = user.member_name_translations + locales.each do |locale| + if author_translations[locale].blank? + author_translations[locale] = user.user_name + end + end + else + author_translations = {} + end + a["author_translations"] = author_translations + if more_url.nil? + if cat_ids.nil? + cat_ids = [self.category_id] + end + if tag_ids.nil? + tag_ids = self.tag_ids + end + basic_query = {:module => 'archive',:enabled_for=>locale} + if !cat_ids.blank? + query = basic_query.merge({:categories.all => cat_ids}) + else + query = basic_query.clone + end + if !tag_ids.blank? + query = query.merge({:tags.all => tag_ids}) + end + page = Page.where(query).first || Page.where(basic_query).first + more_url = page ? page.get_url : nil + end + if more_url + a['show_url'] = "#{more_url}/#{self.to_param}" + end + a["id"] = self.uid + a["title_translations"] = self.title_translations + a["description_translations"] = {} + description_translations = self.description_translations + locales.each do |locale| + locale = locale.to_s + a["description_translations"][locale] = self.class.smart_convertor(description_translations[locale],base_url) if !description_translations[locale].blank? + end + a["created_at"] = self.created_at + a["url_translations"] = self.url_translations + a["tags"] = [] + a["category"] = {} + a["files"] = [] + a["params"] = self.to_param + self.tags.each do |tag| + if !tag_ids.include?(tag.id.to_s) + tag_ids << tag.id.to_s + end + a["tags"] << {"name_translations" => tag.name_translations} + end + cat = self.category + a["sort_number"] = nil + a["category"] = {"title_translations" => (cat.title_translations rescue {})} + self.archive_file_multiples.order_by(:sort_number=>'desc').each do |file| + if file.choose_lang.include?(I18n.locale.to_s) + title_translations = {} + locales.each do |locale| + title_translations[locale] = (file.file_title_translations[locale].blank? ? File.basename(file.file.path) : file.file_title_translations[locale]) rescue "" + end + extension = file.file.file.extension.downcase rescue "" + a["files"] << { + "file-name_translations" => title_translations, + "file-type" => extension, + "file-url" => "#{base_url}/xhr/archive/download?file=#{file.id}" + } + end + end + return a + end + def get_related_feeds + related_feeds = ArchiveFileFeed.where({:category_ids=>self.category_id.to_s, :tag_ids.in=>self.tag_ids.map(&:to_s)}).to_a + end + def update_feed_statuses(statuses) #ex: statuses = {:is_top=>1} + self.update(statuses) + end + def update_feed_data(data) + data = data.clone + feed_data_keys = ["author_translations", "tags", "category", "files", "params", "show_url"] + data["feed_data"] = {} + data["feed_uid"] = data["id"] + data.delete("id") + feed_data_keys.each do |k| + data["feed_data"][k] = data[k] + data.delete(k) + end + @skip_callback = true + # ArchiveSortOrder.update_all(:need_update_sort=>true) + self.update(data) + @skip_callback = nil + end + def self.feeds_finish_callback(action="update", args={}) + if action != "destroy" + if action == "update_all" + data = args["data"] + if data + site_feed_id = args["feed_id"] + category_id = args["category_id"] + data.each do |a| + a["category_id"] = category_id if category_id + archive_file = self.where(:feed_uid=> a["id"], :site_feed_id=>site_feed_id).first + if archive_file.nil? + archive_file = self.new(:feed_uid=> a["id"], :site_feed_id=>site_feed_id) + archive_file.instance_variable_set(:@skip_callback, true) + archive_file.save + end + archive_file.update_feed_data(a) + end + self.where(:feed_uid.nin=>data.map{|a| a["id"]}, :site_feed_id=>site_feed_id).destroy + end + end + self.recalc_sort_number + end + ArchiveCache.destroy_all + end + def get_files(locale=nil, serial_number=0) + if locale.nil? + locale = I18n.locale.to_s + end + files = [] + if self.feed_uid + files = self.feed_data["files"].map do |h| + serial_number += 1 + h = h.clone + h["file-name"] = h["file-name_translations"][locale] + h.delete("file-name_translations") + h["serial_number"] = serial_number + h + end + else + self.archive_file_multiples.order_by(:sort_number=>'desc').each do |file| + if file.choose_lang.include?(locale) + title = (file.file_title.blank? ? File.basename(file.file.path) : file.file_title) rescue "" + extension = file.file.file.extension.downcase rescue "" + serial_number += 1 + files << { + "file-name" => title, + "file-type" => extension, + "file-url" => "/xhr/archive/download?file=#{file.id}", + "serial_number" => serial_number + } + end + end + if self.urls.present? + self.urls.each_with_index do |url,i| + serial_number += 1 + files << { + "file-name" => self.title, + "file-type" => self.get_url_text(i), + "file-url" => url, + "serial_number" => serial_number + } + end + end + end + [files, serial_number] + end + def get_frontend_data(locale=nil, serial_number=0, idx=0, show_tags=false, more_url=nil) + created_at_int = self.created_at.strftime('%Y%m%d').to_i + statuses = self.statuses_with_classname.collect do |status| + { + "status" => status["name"] || "", + "status-class" => "status-#{status['classname']}" + } + end + files, serial_number = self.get_files(locale, serial_number) + data = { + "archive-file-url" => (files.count != 0 ? files[0]["file-url"] : "javascript:void"), + "archive-title" => self.title, + "description" => self.description, + "created_at" => created_at_int, + "archive-url" => self.url, + "url" => self.url, + "statuses" => statuses, + "sort_number" => self.sort_number, + "is_top" => (self.is_top ? 1 : 0), + "files" => files, + "idx" => (idx + 1) + } + if show_tags + data["tags"] = self.tags.map do |tag| + {"name"=>tag.name, "url"=>more_url.to_s + "?tags[]=#{tag.id}"} + end + end + if more_url + data["index_url"] = more_url + end + return [data, serial_number, idx] + end + def get_archive_url(locale, more_url) + archive_url = "" + if self.feed_uid + files = self.feed_data["files"] + if files.count == 0 + tmp_urls = self.urls + archive_url = ((tmp_urls.nil? || tmp_urls.count == 0) ? 'javascript:void' : (tmp_urls.count > 1 ? (more_url + '?title=' + self.title.to_s) : tmp_urls[0])) + else + archive_url = files.count > 1 ? (more_url + '?title=' + self.title.to_s) : files[0]["file-url"] + end + else + if self.archive_file_multiples.count==0 + tmp_urls = self.urls + archive_url = ((tmp_urls.nil? || tmp_urls.count == 0) ? 'javascript:void' : (tmp_urls.count > 1 ? (more_url + '?title=' + self.title.to_s) : tmp_urls[0])) + else + archive_url = self.archive_file_multiples.count > 1 ? (more_url + '?title=' + self.title.to_s) : "/xhr/archive/download?file=#{self.archive_file_multiples.first.id}" + end + end + archive_url + end + def notify_feed(type="create") + related_feeds = self.get_related_feeds.select{|feed| feed.remote_urls.count != 0} + if related_feeds.count != 0 + archive_data = self.get_data + if type == "destroy" + tmp_data = {'type'=>'destroy', 'data'=>[self.uid]} + else + tmp_data = {'type'=>type, 'data'=>[archive_data.to_json]} + end + request = Net::HTTP::Post.new('/xhr/feeds/notify_change', 'Content-Type' => 'application/json') + related_feeds.each do |feed| + tmp_data['uid'] = feed.uid + request.body = tmp_data.to_json + feed.remote_urls.each do |remote_url| + uri = URI(remote_url) + http_req = Net::HTTP.new(uri.host, uri.port) + if remote_url.include?('https') + http_req.use_ssl = true + end + response = self.class.http_request( http_req , request ) + end + end + end + end + def self.notify_feed_delete(ids) + all_feeds = ArchiveFileFeed.all.select{|feed| feed.remote_urls.count != 0} + if all_feeds.count != 0 + tmp_data = {'type'=>'destroy'} + request = Net::HTTP::Post.new('/xhr/feeds/notify_change', 'Content-Type' => 'application/json') + all_feeds.each do |feed| + feed_uid = feed.uid + feed_cache = ArchiveFileFeedCache.where(:uid=>feed_uid).first + if feed_cache + tmp_data['uid'] = feed_uid + tmp_data['data'] = ids & JSON.parse(feed_cache.content)["archives"].map{|a| a["id"]} + request.body = tmp_data.to_json + if tmp_data['data'].count != 0 + feed.remote_urls.each do |remote_url| + uri = URI(remote_url) + http_req = Net::HTTP.new(uri.host, uri.port) + if remote_url.include?('https') + http_req.use_ssl = true + end + response = self.http_request( http_req , request ) + end + end + end + end + end + end + def self.http_request(http, request) + response = http.request(request) + if response.code.to_i == 301 || response.code.to_i == 302 + location = response["location"] + new_uri = URI(location) + http = Net::HTTP.new(new_uri.host, new_uri.port) + if location.include?('https') + http.use_ssl = true + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + end + request.instance_variable_set(:@path, new_uri.path) + response = self.http_request(http, request) + end + response + end protected def add_http + return if @skip_callback in_use_locales = Site.first.in_use_locales temp_urls = {} in_use_locales.each do |locale| @@ -134,10 +499,34 @@ class ArchiveFile end def save_archive_file_multiples + return if @skip_callback self.archive_file_multiples.each do |t| if t.should_destroy t.destroy end end end + def self.smart_convertor(text,url) + doc = Nokogiri.HTML(text) + doc.search('a[href]').each do |link| + if link['href'].nil? + link.delete 'href' + elsif link['href'].start_with?('/') + link['href'] = url + link['href'] + elsif link['href'].start_with?('../') + link['href'] = url + link['href'][3..-1] + end + end + doc.search('img[src]').each do |link| + if link['src'].nil? + link.delete 'src' + elsif link['src'].start_with?('/') + link['src'] = url + link['src'] + elsif link['src'].start_with?('../') + link['src'] = url + link['src'][3..-1] + end + end + + return doc.css('body').inner_html + end end diff --git a/app/models/archive_file_feed.rb b/app/models/archive_file_feed.rb new file mode 100644 index 0000000..0ed86ff --- /dev/null +++ b/app/models/archive_file_feed.rb @@ -0,0 +1,107 @@ +class ArchiveFileFeed + include Mongoid::Document + include Mongoid::Timestamps + include Slug + + field :title, as: :slug_title, type: String, localize: true + field :tag_ids, type: Array, default: [] + field :category_ids, type: Array, default: [] + field :remote_urls, type: Array, default: [] + before_save do + ArchiveFileFeedCache.where(uid: self.uid).each do |cache| + cache.regenerate + end + end + def self.create_feed_cache(archive_file=nil,archive_file_feed=nil) + Thread.new do + if !archive_file.nil? + ArchiveFileFeed.where(:tag_ids.in => Array(archive_file.tag_ids).collect{|v| v.to_s}).each do |archive_file_feed| + uid = archive_file_feed.uid + ArchiveFileFeedCache.where(:uid => uid).each do |cache| + cache.regenerate + end + end + elsif !archive_file_feed.nil? + uid = archive_file_feed.uid + ArchiveFileFeedCache.where(:uid => uid).each do |cache| + cache.regenerate + end + end + end + end + def generate_one_cache_timeout(base_url: nil, timeout: nil) + begin + if timeout.nil? + feed_cache_content = self.generate_one_cache(base_url: base_url) + else + Timeout::timeout(timeout) do + feed_cache_content = nil + thread = Thread.new do + feed_cache_content = self.generate_one_cache(base_url: base_url) + end + (1..(timeout.to_i+1)).each do + sleep(1) + break if !feed_cache_content.nil? && !thread.alive? + end + feed_cache_content + end + end + rescue=> e + puts [e,e.backtrace] + nil + end + end + def generate_one_cache(base_url: nil) + base_url = Site.first.root_url if base_url.nil? + uid = self.uid + aff = self + if !aff.nil? + tags = aff.tag_ids + categories = aff.category_ids + if !(categories.empty? && tags.empty?) + archives_before_filter = ArchiveFile.can_display.local_data + if !tags.empty? + archives_before_filter = archives_before_filter.filter_by_tags(tags) + end + if !categories.empty? + archives_before_filter = archives_before_filter.filter_by_categories(categories,paginate=false) + end + archives_before_filter.selector = {"$and"=>[archives_before_filter.selector,{"$or"=>(I18n.available_locales.map{|v| {"title.#{v}"=>{"$nin"=>["", nil]}}})}]} + archives = archives_before_filter.sorted.to_a + else + archives = [] + end + end + all_archives = [] + tag_ids = [] + category_ids = [] + cat_ids = archives.collect{|a| a.category_id.blank? ? nil : a.category_id.to_s}.compact.uniq + tag_ids = archives.collect{|a| a.tag_ids.collect{|v| v.blank? ? nil : v.to_s}}.flatten.compact.uniq + tag_names = tag_ids.map{|tag_id| Tag.find(tag_id).name_translations rescue nil}.compact + category_titles = cat_ids.map{|cat_id| Category.find(cat_id).title_translations rescue nil}.compact + basic_query = {:module => 'archive',:enabled_for=>I18n.locale} + if !cat_ids.blank? + query = basic_query.merge({:categories.all => cat_ids}) + else + query = basic_query.clone + end + if !tag_ids.blank? + query = query.merge({:tags.all => tag_ids}) + end + page = Page.where(query).first || Page.where(basic_query).first + more_url = page ? page.get_url : nil + locales = Site.first.in_use_locales rescue I18n.available_locales + archives.each_with_index do |archive,i| + a = archive.get_data(more_url, base_url, cat_ids, tag_ids, locales) + all_archives << a + end + archives = { + "archives" => all_archives, + "tags" => tag_names, + "categories" => category_titles + }.to_json + ArchiveFileFeedCache.where(uid: uid).destroy + feed_cache = ArchiveFileFeedCache.create(uid: uid,content: archives) + archives + end +end \ No newline at end of file diff --git a/app/models/archive_file_feed_cache.rb b/app/models/archive_file_feed_cache.rb new file mode 100644 index 0000000..73a7c9d --- /dev/null +++ b/app/models/archive_file_feed_cache.rb @@ -0,0 +1,27 @@ +class ArchiveFileFeedCache + include Mongoid::Document + include Mongoid::Timestamps + + field :content, type: String, default: '' + field :uid + def self.regenerate_all + caches = self.all.to_a + time_now = Time.now + caches.each do |cache| + if cache.invalid_date && cache.invalid_date <= time_now + cache.destroy + else + cache.regenerate + end + end + uids = ArchiveFileFeed.all.pluck(:uid) - caches.collect(&:uid) + ArchiveFileFeed.where(:uid.in=> uids).each do |feed| + feed.generate_one_cache() + end + end + def regenerate + uid = self.uid + ArchiveFileFeed.where(uid: uid).each{|v| v.generate_one_cache} rescue nil + self.destroy + end +end \ No newline at end of file diff --git a/app/models/archive_sort_order.rb b/app/models/archive_sort_order.rb index 4e27dfe..6c06005 100644 --- a/app/models/archive_sort_order.rb +++ b/app/models/archive_sort_order.rb @@ -1,8 +1,10 @@ class ArchiveSortOrder include Mongoid::Document - field :sort_order, :type => Boolean # true => desc , false => asc + field :sort_order, :type => Boolean # true => asc , false => desc + field :need_update_sort, :type => Boolean, :default => true + field :max_sort_number, :type => Integer, :default => 0 after_initialize do |record| - if(!record.new_record? && record.sort_order.class != FalseClass && record.sort_order.class != TrueClass) + if(!record.new_record? && record.sort_order.nil?) record.sort_order = true record.save elsif(!record.new_record?) @@ -17,15 +19,31 @@ class ArchiveSortOrder end end after_save do |record| - @module_app = ModuleApp.where(:key=>'archive').first - if !@module_app.nil? - @sort_order = record.sort_order rescue nil - if !@asc.nil? - if !@module_app.attributes[:asc].nil? - @module_app.asc = @sort_order - @module_app.save + unless record.instance_variable_get(:@skip_callback) + if record.sort_order_changed? + @module_app = ModuleApp.where(:key=>'archive').first + if !@module_app.nil? + @sort_order = record.sort_order rescue nil + if !@asc.nil? + if !@module_app.attributes[:asc].nil? + @module_app.asc = @sort_order + @module_app.save + end + end + end + record.instance_variable_set(:@skip_callback, true) + record.update(:need_update_sort=>true) + if record.need_update_sort + Thread.new do + sort_order = record.sort_order ? 1 : -1 + ArchiveFile.recalc_sort_number(sort_order) + record.instance_variable_set(:@skip_callback, false) + end + end end end - end + end + def get_default_order + self.sort_order ? 0 : self.max_sort_number end end \ No newline at end of file diff --git a/app/views/admin/archive_files/_edit_feed_form.html.erb b/app/views/admin/archive_files/_edit_feed_form.html.erb new file mode 100644 index 0000000..859bbf1 --- /dev/null +++ b/app/views/admin/archive_files/_edit_feed_form.html.erb @@ -0,0 +1,61 @@ +<%= form_for @archive_feed, url: admin_archive_file_updatefeed_path(:id => @archive_feed.id), html: {class: "form-horizontal main-forms"} do |f| %> +
+ <% @site_in_use_locales.each do |locale| %> + <%= f.fields_for :title_translations do |f| %> +
+ +
+ <%= f.text_field locale, data: {"fv-validation" => "required;","fv-messages" => "Cannot be empty.;"}, value: (@archive_feed.title_translations[locale.to_s] rescue nil) %> +
+
+ <% end %> + <% end %> +
+
+

<%=t(:tags)%>

+
+
+ <% @module_app.tags.each do |tag| %> + + <% end %> +
+
+
+
+

<%=t(:category)%>

+
+
+ <% @module_app.categories.each do |category| %> + + <% end %> +
+
+
+
+<% end %> + + \ No newline at end of file diff --git a/app/views/admin/archive_files/_feed.html.erb b/app/views/admin/archive_files/_feed.html.erb new file mode 100644 index 0000000..530aaef --- /dev/null +++ b/app/views/admin/archive_files/_feed.html.erb @@ -0,0 +1,83 @@ + + + <%= feed.title %> +
+ +
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + + RSS Feed + + + JSON Feed + + \ No newline at end of file diff --git a/app/views/admin/archive_files/_feed_form.html.erb b/app/views/admin/archive_files/_feed_form.html.erb new file mode 100644 index 0000000..1e2ce69 --- /dev/null +++ b/app/views/admin/archive_files/_feed_form.html.erb @@ -0,0 +1,61 @@ +<%= form_for @archive_feed, url: admin_archive_file_createfeed_path, html: {class: "form-horizontal main-forms"} do |f| %> +
+ <% @site_in_use_locales.each do |locale| %> + <%= f.fields_for :title_translations do |f| %> +
+ +
+ <%= f.text_field locale, data: {"fv-validation" => "required;","fv-messages" => "Cannot be empty.;"}, value: (@archive_feed.title_translations[locale.to_s] rescue nil) %> +
+
+ <% end %> + <% end %> +
+
+

<%=t(:tags)%>

+
+
+ <% @module_app.tags.each do |tag| %> + + <% end %> +
+
+
+
+

<%=t(:category)%>

+
+
+ <% @module_app.categories.each do |category| %> + + <% end %> +
+
+
+
+<% end %> + + \ No newline at end of file diff --git a/app/views/admin/archive_files/_form.html.erb b/app/views/admin/archive_files/_form.html.erb index 1daf794..e7ce4e6 100644 --- a/app/views/admin/archive_files/_form.html.erb +++ b/app/views/admin/archive_files/_form.html.erb @@ -86,7 +86,8 @@
- <%= f.text_field :sort_number %> + <% extra_data = f.object.new_record? ? {placeholder: f.object.sort_number, title: f.object.sort_number, value: ''} : {} %> + <%= f.number_field :sort_number, extra_data %>
diff --git a/app/views/admin/archive_files/_index.html.erb b/app/views/admin/archive_files/_index.html.erb index 918fae2..38ea911 100644 --- a/app/views/admin/archive_files/_index.html.erb +++ b/app/views/admin/archive_files/_index.html.erb @@ -1,10 +1,96 @@ + <% @table_fields.each do |f| %> <% if f == "archive.downloaded_times" %> <%= thead(f,true,false) %> @@ -17,6 +103,8 @@ <% @archives.each do |archive| %> + <% is_editable = can_edit_or_delete?(archive) %> + diff --git a/modules/archive/archive_index11.html.erb b/modules/archive/archive_index11.html.erb index 1ba6aa7..b4efc6a 100644 --- a/modules/archive/archive_index11.html.erb +++ b/modules/archive/archive_index11.html.erb @@ -23,7 +23,7 @@ {{file-name}} diff --git a/modules/archive/archive_index12.html.erb b/modules/archive/archive_index12.html.erb index 7f1f618..a078919 100644 --- a/modules/archive/archive_index12.html.erb +++ b/modules/archive/archive_index12.html.erb @@ -25,8 +25,8 @@
- {{file-name}} - {{file-type}} + {{file-name}} + {{file-type}}
diff --git a/modules/archive/archive_index13.html.erb b/modules/archive/archive_index13.html.erb new file mode 100644 index 0000000..9339ea5 --- /dev/null +++ b/modules/archive/archive_index13.html.erb @@ -0,0 +1,41 @@ +
+

+ {{page-title}} +

+ +
+
+ + {{link_to_edit}} +
+
+
+ \ No newline at end of file diff --git a/modules/archive/archive_index2.html.erb b/modules/archive/archive_index2.html.erb index 70fe62e..7982a22 100644 --- a/modules/archive/archive_index2.html.erb +++ b/modules/archive/archive_index2.html.erb @@ -24,7 +24,7 @@
{{file-name}} - {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index3.html.erb b/modules/archive/archive_index3.html.erb index ad5e1ad..21fb229 100644 --- a/modules/archive/archive_index3.html.erb +++ b/modules/archive/archive_index3.html.erb @@ -13,7 +13,7 @@
{{file-name}} - {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index4.html.erb b/modules/archive/archive_index4.html.erb index 48ebcf4..a73366f 100644 --- a/modules/archive/archive_index4.html.erb +++ b/modules/archive/archive_index4.html.erb @@ -22,7 +22,7 @@
{{file-name}} - {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index5.html.erb b/modules/archive/archive_index5.html.erb index 5263026..76ce40d 100644 --- a/modules/archive/archive_index5.html.erb +++ b/modules/archive/archive_index5.html.erb @@ -21,7 +21,7 @@
- {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index6.html.erb b/modules/archive/archive_index6.html.erb index 2c46edf..b2b343d 100644 --- a/modules/archive/archive_index6.html.erb +++ b/modules/archive/archive_index6.html.erb @@ -26,7 +26,7 @@
- {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index7.html.erb b/modules/archive/archive_index7.html.erb index 097115b..4ccc941 100644 --- a/modules/archive/archive_index7.html.erb +++ b/modules/archive/archive_index7.html.erb @@ -23,7 +23,7 @@
{{description}}
- {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index8.html.erb b/modules/archive/archive_index8.html.erb index 35b2e0b..48a14a3 100644 --- a/modules/archive/archive_index8.html.erb +++ b/modules/archive/archive_index8.html.erb @@ -28,7 +28,7 @@
{{description}}
- {{file-type}} + {{file-type}}
diff --git a/modules/archive/archive_index9.html.erb b/modules/archive/archive_index9.html.erb index 06c96f2..b721385 100644 --- a/modules/archive/archive_index9.html.erb +++ b/modules/archive/archive_index9.html.erb @@ -18,7 +18,7 @@ {{file-name}} diff --git a/modules/archive/info.json b/modules/archive/info.json index 0971445..44cc7e7 100644 --- a/modules/archive/info.json +++ b/modules/archive/info.json @@ -95,7 +95,15 @@ "en" : "12. Accordion list - with drop down (widget-title, category, filename, download link)" }, "thumbnail" : "ar2.png" - } + }, + { + "filename": "archive_index13", + "name": { + "zh_tw": "13. 頁籤式 ( 模組標題, 類別標題, 標題, 檔案名稱, 下載連結, 描述 )", + "en": "13. Tab list (widget-title, category, title, filename, download link, description)" + }, + "thumbnail": "ar-tab1.png" + } ], "widgets" : [ { diff --git a/modules/archive/thumbs/ar-tab1.png b/modules/archive/thumbs/ar-tab1.png new file mode 100644 index 0000000..2f887da Binary files /dev/null and b/modules/archive/thumbs/ar-tab1.png differ
<% if is_editable %><% end %> <%= archive.status_for_table %> <%= archive.category.title rescue "" %> @@ -28,7 +116,7 @@ <%= archive.title %>
diff --git a/modules/archive/archive_index10.html.erb b/modules/archive/archive_index10.html.erb index c585b12..f56f01b 100644 --- a/modules/archive/archive_index10.html.erb +++ b/modules/archive/archive_index10.html.erb @@ -22,7 +22,7 @@
- {{file-type}} + {{file-type}}
- {{file-type}} + {{file-type}}
- {{file-type}} + {{file-type}}