diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb index f8b9f7b..3662d2d 100644 --- a/app/controllers/archives_controller.rb +++ b/app/controllers/archives_controller.rb @@ -283,7 +283,7 @@ class ArchivesController < ApplicationController "archive-title" => archive.title, "description" => archive.description, "archive-url" => archive.url, - "archive_url" => OrbitHelper.widget_more_url + "archive_url" => archive.get_archive_url(locale, OrbitHelper.widget_more_url) } if t.count > 1 @@ -322,12 +322,13 @@ class ArchivesController < ApplicationController 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) + def get_archives_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) + locale = I18n.locale.to_s + archive_cache = ArchiveCache.where(parent_id: filter_cache_parent_id,locale: locale) count = archive_cache.count if count > 1 archive_cache.limit(count-1).destroy @@ -359,14 +360,10 @@ class ArchivesController < ApplicationController end 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 - } + serial_number = 0 + archives = archives_sorted.to_a.map.with_index do |archive,idx| + data, serial_number, idx = archive.get_widget_data(locale, serial_number, idx) + data end { "category-title" => cat["title"], @@ -375,7 +372,7 @@ class ArchivesController < ApplicationController } 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) + ArchiveCache.create(parent_id: filter_cache_parent_id,locale: locale,filter_result: cats_with_archives) else c = archive_cache.first cats_with_archives = c.filter_result @@ -387,7 +384,7 @@ class ArchivesController < ApplicationController 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) + cats_with_archives = get_archives_for_pack_data(categories, tags) { "categories" => cats_with_archives, "extras" => {"more_url" => (OrbitHelper.widget_more_url + '?data_count=' + page_data_count.to_s)} diff --git a/app/models/archive_file.rb b/app/models/archive_file.rb index f1b7bb3..b575da7 100644 --- a/app/models/archive_file.rb +++ b/app/models/archive_file.rb @@ -314,6 +314,37 @@ class ArchiveFile end [files, serial_number] end + def get_widget_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) + if more_url.nil? + more_url = OrbitHelper.widget_more_url + end + archive_url = self.get_archive_url(locale, more_url) + 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, + "archive_url" => archive_url, + "statuses" => statuses, + "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 + return [data, serial_number, idx] + 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| @@ -331,8 +362,6 @@ class ArchiveFile "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) } diff --git a/archive.gemspec b/archive.gemspec index 6803609..ad64d89 100644 --- a/archive.gemspec +++ b/archive.gemspec @@ -23,129 +23,79 @@ if bundle_update_flag app_path = File.expand_path(__dir__) template_path = env_pwd + '/app/templates' all_template = Dir.glob(template_path+'/*/') + module_name = 'archive' + default_module_info = JSON.parse(File.read("#{app_path}/modules/#{module_name}/info.json")).map{|k,v| [k, v.sort_by{|h| h["filename"].to_i}]}.to_h rescue [] all_template.each do |folder| if !folder.include?('mobile') - begin - next unless File.exist?("#{folder}modules/archive/info.json") - info_json_file = "#{folder}modules/archive/info.json" - info_json = JSON.parse(File.read(info_json_file)) - check_files = ["archive_index7.html.erb","archive_index8.html.erb","archive_index9.html.erb","archive_index10.html.erb","archive_index11.html.erb","archive_index12.html.erb","archive_index13.html.erb"] - old_file_infos = [ - { - "filename" => "archive_index7", - "name" => { - "zh_tw" => "7. 表格列表 ( 模組標題, 類別標題, 檔案名稱, 檔案簡介, 下載連結 )", - "en" => "7. Table list (widget-title, category, filename, download link)" - }, - "thumbnail" => "ar5.png" - }, - { - "filename" => "archive_index8", - "name" => { - "zh_tw" => "8. 表格列表 ( 模組標題, 檔案名稱, 檔案簡介, 下載連結 )", - "en" => "8. Table list (widget-title, filename, download link)" - }, - "thumbnail" => "ar5.png" - } - ] - new_file_infos = [ - { - "filename" => "archive_index7", - "name" => { - "zh_tw" => "7. 表格列表 ( 模組標題, 類別標題, 上傳日期, 檔案名稱, 檔案簡介, 下載連結 )", - "en" => "7. Table list (widget-title, category, Updated At, filename, download link)" - }, - "thumbnail" => "ar5.png" - }, - { - "filename" => "archive_index8", - "name" => { - "zh_tw" => "8. 表格列表 ( 模組標題, 上傳日期, 檔案名稱, 檔案簡介, 下載連結 )", - "en" => "8. Table list (widget-title, Updated At, filename, download link)" - }, - "thumbnail" => "ar5.png" - }, - { - "filename" => "archive_index9", - "name" => { - "zh_tw" => "9. 表格列表 - 簡約 ( 模組標題, 檔案名稱, 下載連結 )", - "en" => "9. Table list - Simple (widget-title, download link)" - }, - "thumbnail" => "ar9.png" - }, - { - "filename" => "archive_index10", - "name" => { - "zh_tw" => "10. 表格列表 ( 模組標題, 類別標題, 編號, 檔案名稱, 下載連結 )", - "en" => "10. Table list (widget-title, category, Serial Number, filename, download link)" - }, - "thumbnail" => "ar5.png" - }, - { - "filename" => "archive_index11", - "name" => { - "zh_tw" => "11. 表格列表 - 簡約 - 展開檔案 ( 模組標題, 類別標題, 編號, 檔案名稱, 下載連結 )", - "en" => "11. Table list - Simple - Unfold Files (widget-title, category, Serial Number, filename, download link)" - }, - "thumbnail" => "ar9.png" - }, - { - "filename" => "archive_index12", - "name" => { - "zh_tw" => "12. 手風琴式列表 - 有下拉 ( 模組標題, 類別標題, 檔案名稱, 下載連結 )", - "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" - } - ] - flags = [] - all_frontend_names = info_json["frontend"].map{|h| h["name"]} - check_files.each_with_index do |check_file,i| - flag = (info_json["frontend"].select{|h| h["filename"] == check_file.split(".").first}.count == 0 rescue false) + info_json_file = "#{folder}modules/#{module_name}/info.json" + if File.exist?(info_json_file) + begin + file_text = File.read(info_json_file) rescue "" + encode_file_text = file_text.encode("UTF-8", "UTF-8", invalid: :replace, replace: "???") + next if (encode_file_text.include?("???") rescue true) + info = JSON.parse(encode_file_text) rescue {} + flag = (info.count != 0 rescue false) if flag - info_json["frontend"].push(new_file_infos[i]) - end - if !flag - old_file_info = old_file_infos[i] - if old_file_info - idx = all_frontend_names.index(old_file_info["name"]) - if idx && idx != -1 - info_json["frontend"][idx] = new_file_infos[i] - flag = true + idx_regex = /^(\d+[\. \t]*)|[ \t]+$/ + update_flags = [] + puts "Checking #{module_name} templates" + default_module_info.each do |info_type, default_sub_info| + update_flag = false + file_prefix = (info_type == 'widgets' ? '_' : '') + sub_info = info[info_type].sort_by{|h| h["filename"].to_i} rescue [] + last_index = sub_info.collect{|v| v["filename"].to_s.scan(/\d+/).collect{|v1| v1.to_i}}.flatten.sort[-1] rescue nil + if last_index.nil? + next end + default_sub_info.each do |h| + name_without_index = h["name"]["zh_tw"].gsub(idx_regex,'') + sub_info_index = (sub_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1) + force_cover = (h["force_cover"] == "true") + if sub_info_index == -1 && h["old_name"] #check with old name + name_without_index = h["old_name"]["zh_tw"].gsub(idx_regex,'') + sub_info_index = (sub_info.index{|hh| hh["name"]["zh_tw"].gsub(idx_regex,'') == name_without_index}||-1 rescue -1) + if sub_info_index != -1 + update_flag = true + force_cover = true + sub_info[sub_info_index][sub_info_index]["name"] = h["name"] + end + end + if sub_info_index == -1 + update_flag = true + copy_h = h.dup + h.delete("force_cover") + h.delete("old_name") + last_index = last_index + 1 + copy_h["filename"] = copy_h["filename"].sub(/\d+/){|ff| last_index.to_s} + copy_h["name"].keys.each do |locale| + copy_h["name"][locale] = copy_h["name"][locale].sub(/\d+/){|ff| last_index.to_s} + end + sub_info << copy_h + bundler_with_clean_env{%x[cp -f #{app_path}/modules/#{module_name}/#{file_prefix}#{h["filename"]}.html.erb #{folder}modules/#{module_name}/#{file_prefix}#{copy_h["filename"]}.html.erb]} + elsif force_cover + bundler_with_clean_env{%x[cp -f #{app_path}/modules/#{module_name}/#{file_prefix}#{h["filename"]}.html.erb #{folder}modules/#{module_name}/#{file_prefix}#{sub_info[sub_info_index]["filename"]}.html.erb]} + end + end + if update_flag + info[info_type] = sub_info + end + update_flags << update_flag end - end - flags << flag - end - force_update_files = ["archive_index1.html.erb","archive_index3.html.erb","archive_index12.html.erb","archive_index13.html.erb"] - puts "updating archive index page" - if flags.select{|flag| flag }.count != 0 - flags.each_with_index do |flag,i| - if flag - bundler_with_clean_env{%x[cp -f #{app_path}/modules/archive/#{check_files[i]} #{folder}modules/archive/#{check_files[i]}]} + if update_flags.select{|flag| flag}.count != 0 + puts "Writing json in #{info_json_file}" + begin + info_json = JSON.pretty_generate(info).gsub(":[",":[\n").gsub(":{",":{\n") + rescue + info_json = info.to_s.gsub("=>",": \n") + end + File.open(info_json_file,"w+"){|f| f.write(info_json)} end + bundler_with_clean_env{%x[cp -rn #{app_path}/modules/#{module_name}/thumbs/* #{folder}modules/#{module_name}/thumbs/.]} end - begin - info_json = JSON.pretty_generate(info_json).gsub(":[",":[\n").gsub(":{",":{\n") - rescue - info_json = info_json.to_s.gsub("=>",": \n") - end - File.open(info_json_file,"w+"){|f| f.write(info_json)} + rescue => e + puts e + puts "There has some error when checking #{module_name} templates" end - force_update_files.each do |update_file| - bundler_with_clean_env{%x[cp -f #{app_path}/modules/archive/#{update_file} #{folder}modules/archive/#{update_file}]} - end - bundler_with_clean_env{%x[cp -rn #{app_path}/modules/archive/thumbs/* #{folder}modules/archive/thumbs/.]} - rescue - puts "There has some error when updating archive index page." end end end diff --git a/modules/archive/_archive_widget3.html.erb b/modules/archive/_archive_widget3.html.erb new file mode 100644 index 0000000..1978557 --- /dev/null +++ b/modules/archive/_archive_widget3.html.erb @@ -0,0 +1,29 @@ +
+

{{widget-title}}

+ +

+ <%= (I18n.locale.to_s =="zh_tw") ? "更多資訊" : "More" %> +

+ +
diff --git a/modules/archive/_archive_widget4.html.erb b/modules/archive/_archive_widget4.html.erb new file mode 100644 index 0000000..903a726 --- /dev/null +++ b/modules/archive/_archive_widget4.html.erb @@ -0,0 +1,51 @@ +
+

+ {{widget-title}} +

+
+
+ +
+
+
+
+ {{archive-title}} + + {{status}} + +
+
+
+ {{file-name}} + {{file-type}} +
+
+
+
+
+
+ {{link_to_edit}} +
+

+ <%= (I18n.locale.to_s =="zh_tw") ? "更多資訊" : "More" %> +

+ +
\ No newline at end of file diff --git a/modules/archive/info.json b/modules/archive/info.json index 44cc7e7..9e5b7cd 100644 --- a/modules/archive/info.json +++ b/modules/archive/info.json @@ -5,7 +5,8 @@ "name" : { "zh_tw" : "1. 列表 ( 模組標題, 類別標題, 檔案名稱, 下載連結 )", "en" : "1. List (widget-title, category, filename, download link)" - }, + }, + "force_cover": "true", "thumbnail" : "ar1.png" }, { @@ -13,7 +14,7 @@ "name" : { "zh_tw" : "2. 手風琴式列表 ( 模組標題, 類別標題, 檔案名稱, 下載連結 )", "en" : "2. Accordion list (widget-title, category, filename, download link)" - }, + }, "thumbnail" : "ar2.png" }, { @@ -21,7 +22,8 @@ "name" : { "zh_tw" : "3. 列表( 無檔案名稱 ) ( 模組標題, 類別標題, 下載連結 )", "en" : "3. List(no filename) (widget-title, category, download link)" - }, + }, + "force_cover": "true", "thumbnail" : "ar3.png" }, { @@ -29,7 +31,7 @@ "name" : { "zh_tw" : "4. 手風琴式列表( 無檔案名稱 ( 模組標題, 類別標題, 下載連結 )", "en" : "4. Accordion list(no filename) (widget-title, category, download link)" - }, + }, "thumbnail" : "ar4.png" }, { @@ -37,7 +39,7 @@ "name" : { "zh_tw" : "5. 表格列表 ( 模組標題, 類別標題, 檔案名稱, 下載連結 )", "en" : "5. Table list (widget-title, category, filename, download link)" - }, + }, "thumbnail" : "ar5.png" }, { @@ -45,7 +47,7 @@ "name" : { "zh_tw" : "6. 表格列表 ( 模組標題, 檔案名稱, 下載連結 )", "en" : "6. Table list (widget-title, filename, download link)" - }, + }, "thumbnail" : "ar5.png" }, { @@ -53,7 +55,11 @@ "name" : { "zh_tw" : "7. 表格列表 ( 模組標題, 類別標題, 上傳日期, 檔案名稱, 檔案簡介, 下載連結 )", "en" : "7. Table list (widget-title, category, Updated At, filename, download link)" - }, + }, + "old_name" : { + "zh_tw" : "7. 表格列表 ( 模組標題, 類別標題, 檔案名稱, 檔案簡介, 下載連結 )", + "en" : "7. Table list (widget-title, category, filename, download link)" + }, "thumbnail" : "ar5.png" }, { @@ -61,7 +67,11 @@ "name" : { "zh_tw" : "8. 表格列表 ( 模組標題, 上傳日期, 檔案名稱, 檔案簡介, 下載連結 )", "en" : "8. Table list (widget-title, Updated At, filename, download link)" - }, + }, + "old_name" : { + "zh_tw" : "8. 表格列表 ( 模組標題, 檔案名稱, 檔案簡介, 下載連結 )", + "en" : "8. Table list (widget-title, filename, download link)" + }, "thumbnail" : "ar5.png" }, { @@ -69,7 +79,7 @@ "name" : { "zh_tw" : "9. 表格列表 - 簡約 ( 模組標題, 檔案名稱, 下載連結 )", "en" : "9. Table list - Simple (widget-title, filename, download link)" - }, + }, "thumbnail" : "ar9.png" }, { @@ -77,7 +87,7 @@ "name" : { "zh_tw" : "10. 表格列表 ( 模組標題, 類別標題, 編號, 檔案名稱, 下載連結 )", "en" : "10. Table list (widget-title, category, Serial Number, filename, download link)" - }, + }, "thumbnail" : "ar5.png" }, { @@ -85,7 +95,7 @@ "name" : { "zh_tw" : "11. 表格列表 - 簡約 - 展開檔案 ( 模組標題, 類別標題, 編號, 檔案名稱, 下載連結 )", "en" : "11. Table list - Simple - Unfold Files (widget-title, category, Serial Number, filename, download link)" - }, + }, "thumbnail" : "ar9.png" }, { @@ -93,7 +103,8 @@ "name" : { "zh_tw" : "12. 手風琴式列表 - 有下拉 ( 模組標題, 類別標題, 檔案名稱, 下載連結 )", "en" : "12. Accordion list - with drop down (widget-title, category, filename, download link)" - }, + }, + "force_cover": "true", "thumbnail" : "ar2.png" }, { @@ -101,7 +112,8 @@ "name": { "zh_tw": "13. 頁籤式 ( 模組標題, 類別標題, 標題, 檔案名稱, 下載連結, 描述 )", "en": "13. Tab list (widget-title, category, title, filename, download link, description)" - }, + }, + "force_cover": "true", "thumbnail": "ar-tab1.png" } ], @@ -111,7 +123,7 @@ "name" : { "zh_tw" : "1. 列表 ( 模組標題, 類別標題, 檔案名稱, 下載頁面連結 )", "en" : "1. List (widget-title, category, link of download page)" - }, + }, "thumbnail" : "thumb.png" }, { @@ -121,6 +133,24 @@ "en" : "2. Accordion list (widget-title, category, link of download page)" }, "thumbnail" : "thumb.png" + }, + { + "filename" : "archive_widget3", + "name" : { + "zh_tw" : "3. 列表 ( 模組標題, 類別標題, 檔案名稱, 包含檔案 )", + "en" : "3. List (widget-title, category, and files)" + }, + "force_cover": "true", + "thumbnail" : "thumb.png" + }, + { + "filename" : "archive_widget4", + "name" : { + "zh_tw" : "4. 手風琴式列表 - 有下拉 ( 模組標題, 類別標題, 包含檔案 )", + "en" : "4. Accordion list - with drop down (widget-title, category, and files)" + }, + "force_cover": "true", + "thumbnail" : "thumb.png" } ] } \ No newline at end of file