diff --git a/app/assets/stylesheets/ask/ask.css b/app/assets/stylesheets/ask/ask.css index f0adc19..a5ce17c 100644 --- a/app/assets/stylesheets/ask/ask.css +++ b/app/assets/stylesheets/ask/ask.css @@ -1,3 +1,7 @@ +.asks-3-column .ask-questions { + display: flex; + flex-wrap: wrap; +} .ask-question .control-group.col-sm-6:nth-child(2n+1) { clear: left; } diff --git a/app/controllers/asks_controller.rb b/app/controllers/asks_controller.rb index 692c8a7..902a199 100644 --- a/app/controllers/asks_controller.rb +++ b/app/controllers/asks_controller.rb @@ -55,12 +55,133 @@ class AsksController < ApplicationController 'email_regex' => email_regex } } + when /.*_pub.*/ + data_count = OrbitHelper.widget_data_count.to_i + categories = OrbitHelper.widget_categories || [] + if categories.blank? + module_app = ModuleApp.where(:key => "ask").first + categories = module_app.categories.enabled.pluck(:id) + end + default_ask_setting = AskSetting.first + ask_settings_info = categories.map do |cat| + ask_setting = AskCategorySetting.enabled.where(category_id: cat.to_s).first + ask_setting = default_ask_setting if ask_setting.nil? + custom_fields = ask_setting.custom_fields.map do |k,field_info| + options = nil + multiple = false + ext_type = 0 + case field_info["type"] + when "select", "radio_button", "checkbox" + multiple = (field_info["type"] == "checkbox") + options = field_info[:options].map do |i, opt_vals| + opt_val = opt_vals[I18n.locale] + if opt_val.blank? + opt_val = opt_vals.values.select{|s| s.present?} + end + [i, opt_val] + end.to_h + when "file", "image" + ext_type = 1 + when "date" + ext_type = 2 + end + [k, { + options: options, + multiple: multiple, + ext_type: ext_type + }] + end + if custom_fields.present? + sort_number = ask_setting.get_cache_sort_number + custom_fields = custom_fields.sort_by do |k, v| + if k.start_with?('default@') + sort_number[k] + else + sort_number["custom@#{ask_setting.id}@#{k}"] + end + end + end + [cat, + { + setting: ask_setting, + custom_fields: custom_fields.to_h + } + ] + end.to_h + ask_questions = AskQuestion.where(:situation => 'is_published', :category_id.in => categories) + if data_count != 0 + ask_questions = ask_questions.page(1).per(data_count) + end + ask_index_page = OrbitHelper.widget_more_url rescue nil + ask_questions = ask_questions.collect do |v| + url = ((ask_index_page+"?item=#{v.id}") rescue "javascript:alert('#{t('ask.no_index_page')}')") + cat = v.category_id.to_s + extra_info = ask_settings_info[cat] + d = { + "link_url" => link_to(v.title,"#{url}",title: v.title), + "identity" => v[:identity], + "title" => v.title, + "name" => v.name, + "sex" => v.sex, + "content" => v.content, + "category" => v.category.title + } + ext_fields = [ + { + "fk" => "ask-sex-#{v.sex}", + "fv" => "" + } + ] + if extra_info.present? && extra_info[:custom_fields].present? + ext_fields += extra_info[:custom_fields].map do |k, field_opt| + fv = v.custom_values[k] + if fv.present? + options = field_opt[:options] + if options + if field_opt[:multiple] + fv = fv.values rescue [] + else + fv = Array(fv) + end + fv = fv.map{|vv| options[vv]}.join("
").html_safe + else + case field_opt[:ext_type] + when 1 # file, image + fv = "#{fv[0]}".html_safe + when 2 # date + fv = fv["datetime"].values[0].values rescue [] + if fv.count == 2 + fv = fv[0].to_s.gsub('-', '/') + " ~ " + fv[1].to_s.gsub('-', '/') + else + fv = fv[0].to_s.gsub('-', '/') + end + end + end + end + { + "fk"=> "ask-q-#{k}", + "fv"=> fv + } + end + end + d["ext_fields"] = ext_fields + d + end + { + 'ask_questions' => ask_questions, + 'extras' => {'more_title'=>I18n.t('ask.more_title'), + 'more_href'=>ask_index_page} + } else data_count = OrbitHelper.widget_data_count.to_i + categories = OrbitHelper.widget_categories || [] + if categories.blank? + module_app = ModuleApp.where(:key => "ask").first + categories = module_app.categories.enabled.pluck(:id) + end + ask_questions = AskQuestion.where(:situation => 'is_published', :category_id.in => categories) if data_count != 0 - ask_questions = AskQuestion.where(situation: 'is_published').page(1).per(data_count) - else - ask_questions = AskQuestion.where(situation: 'is_published') + ask_questions = ask_questions.page(1).per(data_count) end ask_index_page = OrbitHelper.widget_more_url rescue nil ask_questions = ask_questions.collect do |v| @@ -169,14 +290,66 @@ class AsksController < ApplicationController page_number = OrbitHelper.page_number.to_i page_number = 1 if page_number == 0 page_data_count = OrbitHelper.page_data_count.to_i - if page_data_count != 0 - ask_questions = AskQuestion.where(situation: 'is_published').page(page_number).per(page_data_count) - else - ask_questions = AskQuestion.where(situation: 'is_published') + categories = OrbitHelper.page_categories || [] + if categories.blank? + module_app = ModuleApp.where(:key => "ask").first + categories = module_app.categories.enabled.pluck(:id) end + default_ask_setting = AskSetting.first + ask_settings_info = categories.map do |cat| + ask_setting = AskCategorySetting.enabled.where(category_id: cat.to_s).first + ask_setting = default_ask_setting if ask_setting.nil? + custom_fields = ask_setting.custom_fields.map do |k,field_info| + options = nil + multiple = false + ext_type = 0 + case field_info["type"] + when "select", "radio_button", "checkbox" + multiple = (field_info["type"] == "checkbox") + options = field_info[:options].map do |i, opt_vals| + opt_val = opt_vals[I18n.locale] + if opt_val.blank? + opt_val = opt_vals.values.select{|s| s.present?} + end + [i, opt_val] + end.to_h + when "file", "image" + ext_type = 1 + when "date" + ext_type = 2 + end + [k, { + options: options, + multiple: multiple, + ext_type: ext_type + }] + end + if custom_fields.present? + sort_number = ask_setting.get_cache_sort_number + custom_fields = custom_fields.sort_by do |k, v| + if k.start_with?('default@') + sort_number[k] + else + sort_number["custom@#{ask_setting.id}@#{k}"] + end + end + end + [cat, + { + setting: ask_setting, + custom_fields: custom_fields.to_h + } + ] + end.to_h + ask_questions = AskQuestion.where(:situation => 'is_published', :category_id.in => categories) + if page_data_count != 0 + ask_questions = ask_questions.page(1).per(page_data_count) + end + { 'layout_type' => 'published_index', 'ask_questions' => ask_questions, + 'ask_settings_info' => ask_settings_info, 'url' => @params['url'] } else diff --git a/app/views/asks/published_index.erb b/app/views/asks/published_index.erb index c840abb..ebcb57f 100644 --- a/app/views/asks/published_index.erb +++ b/app/views/asks/published_index.erb @@ -1,18 +1,62 @@ - - - - - - - - - <% data['ask_questions'].each do |ask_question| %> - - - - +<% + ask_questions = data['ask_questions'] + ask_settings_info = data['ask_settings_info'] +%> +
+
+ <% ask_questions.each do |ask_question| %> +
+
+
<%= link_to ask_question.title,"#{data['url']}?item=#{ask_question.id}",title: ask_question.title %>
+
<%= ask_question.content %>
+
+
<%= ask_question.name %>
+
<%= ask_question[:identity] %>
+
+ <% + cat = ask_question.category_id.to_s + extra_info = ask_settings_info[cat] + %> +
+
+ <% if extra_info.present? && extra_info[:custom_fields].present? %> + <% + extra_info[:custom_fields].each do |k, field_opt| + fk = "ask-q-#{k}" + fv = ask_question.custom_values[k] + if fv.present? + options = field_opt[:options] + if options + if field_opt[:multiple] + fv = fv.values rescue [] + else + fv = Array(fv) + end + fv = fv.map{|vv| options[vv]}.join("
").html_safe + else + case field_opt[:ext_type] + when 1 # file, image + fv = "#{fv[0]}".html_safe + when 2 # date + fv = fv["datetime"].values[0].values rescue [] + if fv.count == 2 + fv = fv[0].to_s.gsub('-', '/') + " ~ " + fv[1].to_s.gsub('-', '/') + else + fv = fv[0].to_s.gsub('-', '/') + end + end + end + end + %> +
+ <%= fv %> +
+ <% end %> + <% end %> +
+
<% end %> -
-
<%= t('categories') %><%= t('title') %>
<%= ask_question.category.title %><%= link_to ask_question.title,"#{data['url']}?item=#{ask_question.id}",title: ask_question.title %>
+ + <% total_pages = data['ask_questions'].total_pages %> <%= create_pagination(total_pages).html_safe if total_pages > 1 %> \ No newline at end of file diff --git a/modules/ask/_ask_widget_pub_3_column.html.erb b/modules/ask/_ask_widget_pub_3_column.html.erb new file mode 100644 index 0000000..25d7d2e --- /dev/null +++ b/modules/ask/_ask_widget_pub_3_column.html.erb @@ -0,0 +1,21 @@ +
+

+ {{widget-title}} +

+
+
+
+
{{link_url}}
+
{{content}}
+
+
{{name}}
+
{{identity}}
+
+
+
{{fv}}
+
+
+
+
+
{{more_title}}
+
diff --git a/modules/ask/info.json b/modules/ask/info.json index 9dd7c12..9e2da0f 100644 --- a/modules/ask/info.json +++ b/modules/ask/info.json @@ -15,6 +15,14 @@ "en" : "2. widget-title, published list" }, "thumbnail" : "thumb.png" + }, + { + "filename" : "ask_widget_pub_3_column", + "name" : { + "zh_tw" : "3. 模組標題、 發佈回覆列表-3欄式", + "en" : "3. widget-title, published list - 3 Column" + }, + "thumbnail" : "thumb.png" } ] } \ No newline at end of file