can modify fields

This commit is contained in:
Harry Bomrah 2016-03-21 19:02:08 +08:00
parent 2a2cb7637c
commit 1433be1a9b
5 changed files with 137 additions and 23 deletions

View File

@ -1,25 +1,52 @@
class PersonalHonorsController < ApplicationController
def index
params = OrbitHelper.params
honors = Honor.where(:is_hidden=>false).order_by(:year=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
honor_list = honors.collect do |honor|
{
"year" => honor.year,
"award_name" => honor.award_name,
"awarding_unit" => (honor.awarding_unit rescue ""),
"award_winner" => (honor.member_profile.name rescue ""),
"link_to_show" => OrbitHelper.url_to_show(honor.to_param)
fields_to_show = Page.where(:page_id => params[:page_id]).first.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = [
"honor_type",
"year",
"award_name",
"authors",
"awarding_unit"
]
end
honor_list = []
honors.each do |honor|
t = []
fields_to_show.each do |fs|
case fs
when "award_name"
t << {"value" => "<a href='#{OrbitHelper.url_to_show(honor.to_param)}'>" + (honor.send(fs) rescue "") + "</a>"}
when "honor_type"
t << {"value" => (honor.send("honor_type").title rescue "")}
when "authors"
t << {"value" => (honor.send(:member_profile).name rescue "")}
else
t << {"value" => (honor.send(fs) rescue "")}
end
end
honor_list << {"personal_honors" => t}
end
headers = []
fields_to_show.each do |fs|
col = 2
col = 3 if fs == "award_name"
header = fs == "authors" ? t("users.name") : t("personal_honor.#{fs}")
headers << {
"head-title" => header,
"col" => col
}
end
{
"honors" => honor_list,
"extras" => {
"widget-title" => t("module_name.personal_honor"),
"th_year" => t('personal_honor.year'),
"th_award_name" => t('personal_honor.award_name'),
"th_awarding_unit" => t('personal_honor.awarding_unit'),
"th_award_winner" => t('personal_honor.award_winner'),
"th_detail" => t('detail')
},
"extras" => {"widget-title" => t("module_name.personal_honor")},
"headers" => headers,
"total_pages" => honors.total_pages
}
end
@ -40,4 +67,35 @@ class PersonalHonorsController < ApplicationController
{"plugin_datas"=>plugin.get_plugin_data(fields_to_show)}
end
def get_fields_for_index
@page = Page.find(params[:page_id]) rescue nil
@fields_to_show = [
"year",
"award_name",
"awarding_unit",
"honor_type",
"keywords",
"url",
"note",
"authors",
"award_winner"
]
@fields_to_show = @fields_to_show.map{|fs| [(fs == "authors" ? t("users.name") : t("personal_honor.#{fs}")), fs]}
@default_fields_to_show = [
"honor_type",
"year",
"award_name",
"authors",
"awarding_unit"
]
render :layout => false
end
def save_index_fields
page = Page.find(params[:page_id]) rescue nil
page.custom_array_field = params[:keys]
page.save
render :json => {"success" => true}.to_json
end
end

View File

@ -39,13 +39,18 @@ class Honor
end
def self.get_plugin_datas_to_member(datas)
fields_to_show = [
"honor_type",
"year",
"award_name",
"awarding_unit"
]
page = Page.where(:module => "personal_honor").first rescue nil
if !page.nil? && !page.custom_array_field.blank?
fields_to_show = page.custom_array_field
else
fields_to_show = [
"honor_type",
"year",
"award_name",
"awarding_unit"
]
end
fields_to_remove = []

View File

@ -0,0 +1,48 @@
<% if !@page.nil? %>
<form id="form_for_plugin_fields" action="">
<div class="form-inline">
<ul class="current-fields">
<% if @page.custom_array_field.blank? %>
<% @default_fields_to_show.each do |fs| %>
<li data-attrib-key="<%= fs %>" class="clearfix"><span class="field-value"><%= t("personal.honor.#{fs}") %></span><span class="remove-field"><i class="icon-remove-sign"></i></span></li>
<% end %>
<% else %>
<% @page.custom_array_field.each do |fs| %>
<li data-attrib-key="<%= fs %>" class="clearfix"><span class="field-value"><%= t("personal.honor.#{fs}") %></span><span class="remove-field"><i class="icon-remove-sign"></i></span></li>
<% end %>
<% end %>
</ul>
</div>
<div class="form-horizontal controls-row">
<div class="attr-type-wrap control-group">
<label class="attr control-label">Fields: </label>
<div class="attr controls">
<%= select_tag "fields_to_show_for_pp", options_for_select(@fields_to_show), prompt: "---Select something---" %>
</div>
</div>
<a href="#" class="add-pp-field btn btn-info">Add Field</a>
<input type="hidden" name="plugin_key" value="<%= @page.module %>">
<input type="hidden" name="plugin_page_frontend_id" value="<%= @page.id.to_s %>">
</div>
</form>
<script type="text/javascript">
$(".current-fields").sortable();
var select = $("select#fields_to_show_for_pp");
$(".add-pp-field").on("click",function(){
var val = select.val(),
text = select.find("option:selected").text(),
li = null;
if(val != ""){
li = '<li class="clearfix" data-attrib-key="' + val + '"><span class="field-value">' + text + '</span><span class="remove-field"><i class="icon-remove-sign"></i></span></li>';
}
$("#modify_plugin_fields ul.current-fields").append(li);
})
$(document).on("click",".remove-field",function(){
$(this).parent().remove();
})
</script>
<% else %>
<h3>Page not found.</h3>
<% end %>

View File

@ -28,5 +28,7 @@ Rails.application.routes.draw do
resources :honor_types
post "honor_types/update_order" => "honor_types#update_order"
end
get "/xhr/personal_honor/get_fields_for_index" => "personal_honors#get_fields_for_index"
post "/xhr/personal_honor/save_index_fields" => "personal_honors#save_index_fields"
end
end

View File

@ -4,7 +4,8 @@ module PersonalHonor
OrbitApp.registration "PersonalHonor",:type=> 'ModuleApp' do
module_label 'module_name.personal_honor'
base_url File.expand_path File.dirname(__FILE__)
personal_plugin :enable => true, :sort_number => '50', :app_name=>"Honor", :intro_app_name=>"PersonalHonorIntro",:path=>"/plugin/personal_honor/profile",:front_path=>"/profile",:admin_path=>"/admin/honors",:i18n=>'module_name.personal_honor', :module_app_name=>'PersonalHonor'
personal_plugin :enable => true, :sort_number => '50', :app_name=>"Honor", :intro_app_name=>"PersonalHonorIntro",:path=>"/plugin/personal_honor/profile",:front_path=>"/profile",:admin_path=>"/admin/honors",:i18n=>'module_name.personal_honor', :module_app_name=>'PersonalHonor', :field_modifiable => true
version "0.1"
desktop_enabled true