can modify fields now

This commit is contained in:
Harry Bomrah 2016-03-21 18:08:25 +08:00
parent ea16bf3c9b
commit 1940ed789c
6 changed files with 141 additions and 37 deletions

View File

@ -0,0 +1,81 @@
class PersonalCertificatesController < ApplicationController
def index
params = OrbitHelper.params
certificates = Certificate.where(:is_hidden=>false).order_by(:created_at=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
fields_to_show = Page.where(:page_id => params[:page_id]).first.custom_array_field rescue []
if fields_to_show.blank?
fields_to_show = [
"title",
"issued_by",
"authors"
]
end
certificates_list = []
certificates.each do |certificate|
t = []
fields_to_show.each do |fs|
case fs
when "title"
t << {"value" => "<a href='#{OrbitHelper.url_to_show(certificate.to_param)}'>" + (certificate.send(fs) rescue "") + "</a>"}
when "authors"
t << {"value" => (certificate.send(:member_profile).name rescue "")}
else
t << {"value" => (certificate.send(fs) rescue "")}
end
end
certificates_list << {"personal_certificates" => t}
end
headers = []
fields_to_show.each do |fs|
col = 2
col = 3 if fs == "title"
header = fs == "authors" ? t("users.name") : t("personal_certificate.#{fs}")
headers << {
"head-title" => header,
"col" => col
}
end
{
"certificates" => certificates_list,
"headers" => headers,
"extras" => {"widget-title" => t("module_name.personal_certificate")},
"total_pages" => certificates.total_pages
}
end
def show
params = OrbitHelper.params
plugin = Certificate.where(:is_hidden=>false).find_by(uid: params[:uid])
fields_to_show = [
"title",
"issued_by"
]
{"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 = [
"title",
"issued_by",
"authors"
]
@fields_to_show = @fields_to_show.map{|fs| [(fs == "authors" ? t("users.name") : t("personal_certificate.#{fs}")), fs]}
@default_fields_to_show = [
"title",
"issued_by",
"authors"
]
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

@ -1,32 +0,0 @@
class PersonalCertificatesController < ApplicationController
def index
certificates = Certificate.where(:is_hidden=>false).order_by(:created_at=>'desc').page(OrbitHelper.params[:page_no]).per(OrbitHelper.page_data_count)
certificates_list = certificates.collect do |certificate|
{
"title" => certificate.title,
"issued_by" => certificate.issued_by,
"link_to_show" => OrbitHelper.url_to_show(certificate.to_param)
}
end
{
"courses" => certificates_list,
"extras" => {
"widget-title" => t("module_name.personal_certificate"),
"th_title" => t('personal_certificate.title'),
"th_issued_by" => t("personal_certificate.issued_by")
},
"total_pages" => certificates.total_pages
}
end
def show
params = OrbitHelper.params
plugin = Certificate.where(:is_hidden=>false).find_by(uid: params[:uid])
fields_to_show = [
"title",
"issued_by"
]
{"plugin_datas"=>plugin.get_plugin_data(fields_to_show)}
end
end

View File

@ -12,11 +12,16 @@ class Certificate
scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:created_at =>'desc') } scope :sort_for_frontend, ->{ where(:is_hidden=>false).order_by(:created_at =>'desc') }
def self.get_plugin_datas_to_member(datas) def self.get_plugin_datas_to_member(datas)
page = Page.where(:module => "personal_certificate").first rescue nil
fields_to_show = [ if !page.nil? && !page.custom_array_field.blank?
"title", fields_to_show = page.custom_array_field
"issued_by", else
] fields_to_show = [
"title",
"issued_by"
]
end
fields_to_remove = [] 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_certificate.#{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_certificate.#{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

@ -21,5 +21,7 @@ Rails.application.routes.draw do
end end
end end
end end
get "/xhr/personal_certificate/get_fields_for_index" => "personal_certificates#get_fields_for_index"
post "/xhr/personal_certificate/save_index_fields" => "personal_certificates#save_index_fields"
end end
end end

View File

@ -4,7 +4,7 @@ module PersonalCertificate
OrbitApp.registration "PersonalCertificate",:type=> 'ModuleApp' do OrbitApp.registration "PersonalCertificate",:type=> 'ModuleApp' do
module_label 'module_name.certificates' module_label 'module_name.certificates'
base_url File.expand_path File.dirname(__FILE__) base_url File.expand_path File.dirname(__FILE__)
personal_plugin :enable => true, :sort_number => '35', :app_name=>"Certificate", :intro_app_name=>"CertificateIntro",:path=>"/plugin/personal_certificate/profile",:front_path=>"/profile",:admin_path=>"/admin/certificates/",:i18n=>'module_name.certificates', :module_app_name=>'Certificate' personal_plugin :enable => true, :sort_number => '35', :app_name=>"Certificate", :intro_app_name=>"CertificateIntro",:path=>"/plugin/personal_certificate/profile",:front_path=>"/profile",:admin_path=>"/admin/certificates/",:i18n=>'module_name.certificates', :module_app_name=>'Certificate', :field_modifiable => true
version "0.1" version "0.1"
desktop_enabled true desktop_enabled true