completed the plugin

This commit is contained in:
Harry Bomrah 2014-05-08 15:33:39 +08:00
parent 49f039c64b
commit 352fdfa76c
17 changed files with 728 additions and 6 deletions

View File

@ -1,4 +1,92 @@
class Admin::FaqsController < ApplicationController
class Admin::FaqsController < OrbitAdminController
before_filter :setup_vars
def index
@table_fields = ["Status","Category","Title","Tags"]
@categories = @module_app.categories
@tags = @module_app.tags
@filter_fields = {
:status=>[{:title=>"is_top",:id=>"is_top"},{:title=>"is_hot",:id=>"is_hot"},{:title=>"is_hidden",:id=>"is_hidden"}],
:category=>@categories.map{|c| {:title=>c.title, :id=>c.id}},
:tags=>@tags.map{|tag| {:title=>tag.name, :id=>tag.id}}
}
status = params[:filters][:status].blank? ? [] : params[:filters][:status] rescue []
categories = params[:filters][:category].blank? ? [] : params[:filters][:category] rescue []
tags = params[:filters][:tags].blank? ? [] : params[:filters][:tags] rescue []
@qas = Kaminari.paginate_array(Qa.order_by(sort).with_categories(categories).with_tags(tags).with_status(status)).page(params[:page]).per(10)
if request.xhr?
render :partial => "index"
end
end
def sort
unless params[:sort].blank?
case params[:sort]
when "status"
@sort = [[:is_top, params[:order]],
[:is_hot, params[:order]],
[:is_hidden,params[:order]]]
when "category"
@sort = {:category_id=>params[:order]}
when "title"
@sort = {:title=>params[:order]}
when "last_modified"
@sort = {:update_user_id=>params[:order]}
end
else
@sort = {:created_at=>'desc'}
end
@sort
end
def new
@qa = Qa.new
@tags = @module_app.tags
@categories = @module_app.categories
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @qa }
end
end
def create
@qa = Qa.new(create_params)
@qa.create_user_id = current_user.id
@qa.update_user_id = current_user.id
@qa.save
redirect_to admin_faqs_path
end
def edit
@qa = Qa.find(params[:id])
@tags = @module_app.tags
@categories = @module_app.categories
end
def update
@qa = Qa.find(params[:id])
@qa.update_attributes(create_params)
@qa.update_user_id = current_user.id
@qa.save
redirect_to admin_faqs_path
end
def destroy
@qa = Qa.find(params[:id])
@qa.destroy
redirect_to admin_faqs_path
end
private
def setup_vars
@module_app = ModuleApp.where(:key => "faq").first
end
def create_params
params.require(:qa).permit!
end
end

View File

@ -0,0 +1,36 @@
class FaqsController < ApplicationController
def index
faqs = Qa.filter_by_categories.collect do |qa|
{
"link_to_show" => OrbitHelper.url_to_show(qa.to_param),
"question" => qa.title
}
end
{
"data" => faqs,
"extras" => {"widget-title"=>"Faqs"}
}
end
def show
params = OrbitHelper.params
faq = Qa.find_by_param(params[:uid])
faqs_files = faq.qa_files.collect do |f|
{
"file_url" => f.file.url,
"file_title" => f.title
}
end
faqs_links = faq.qa_links.collect do |f|
{
"link_url" => f.url,
"link_title" => f.title
}
end
{
"extras" => {"question" => faq.title,"answer" => faq.answer},
"faqs_links" => faqs_links,
"faqs_files" => faqs_files
}
end
end

View File

@ -1,4 +1,65 @@
# encoding: utf-8
class Qa
include Mongoid::Document
field :title, type: String
end
include Mongoid::Timestamps
# include OrbitModel::LanguageRestrict
include OrbitModel::Status
include OrbitTag::Taggable
include OrbitCategory::Categorizable
include Slug
field :title,as: :slug_title, localize: true
field :answer, localize: true
field :create_user_id
field :update_user_id
field :uid, type: String
has_many :qa_links, :autosave => true, :dependent => :destroy
has_many :qa_files, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :qa_files, :allow_destroy => true
accepts_nested_attributes_for :qa_links, :allow_destroy => true
# belongs_to :qa_category
before_save :clean_values
# validates :title, :at_least_one => true
def self.search( category_id = nil )
if category_id.to_s.size > 0
find(:all, :conditions => {qa_category_id: category_id}).desc( :is_top, :title )
else
find(:all).desc( :is_top, :title)
end
end
def self.find_by_param(input)
self.find_by(uid: input)
end
def self.widget_datas
where( :is_hidden => false ).desc(:is_top, :created_at)
end
protected
def qa_category_with_title
self.category.title
end
def clean_values
self.qa_links.each do |link|
link.delete if link.url.blank? && link.title.blank?
end
self.tags.delete('')
end
paginates_per 10
end

14
app/models/qa_file.rb Normal file
View File

@ -0,0 +1,14 @@
class QaFile
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :file, AssetUploader
field :description, localize: true
field :should_destroy, :type => Boolean
field :title, localize: true
belongs_to :qa
end

24
app/models/qa_link.rb Normal file
View File

@ -0,0 +1,24 @@
class QaLink
include Mongoid::Document
include Mongoid::Timestamps
field :url
field :title, localize: true
field :should_destroy, :type => Boolean
belongs_to :qa
before_validation :add_http
# validates :url, :presence => true, :format => /^(http|https):\/\/(([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5})|((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(:[0-9]{1,5})?(\/.*)?/i
protected
def add_http
unless self.url[/^http:\/\//] || self.url[/^https:\/\//]
self.url = 'http://' + self.url
end
end
end

View File

@ -0,0 +1,240 @@
<% content_for :page_specific_css do %>
<%= stylesheet_link_tag "lib/main-forms" %>
<%= stylesheet_link_tag "lib/fileupload" %>
<%= stylesheet_link_tag "lib/main-list" %>
<% end %>
<% content_for :page_specific_javascript do %>
<%= javascript_include_tag "lib/bootstrap-fileupload" %>
<%= javascript_include_tag "lib/bootstrap-datetimepicker" %>
<%= javascript_include_tag "lib/datetimepicker/datetimepicker.js" %>
<%= javascript_include_tag "lib/modal-preview" %>
<%= javascript_include_tag "lib/file-type" %>
<% end %>
<%#= f.error_messages %>
<fieldset>
<!-- Input Area -->
<div class="input-area">
<!-- Module Tabs -->
<div class="nav-name"><strong><%= t(:module) %></strong></div>
<ul class="nav nav-pills module-nav">
<li class="active">
<a href="#basic" data-toggle="tab"><%= t(:basic) %></a>
</li>
<li>
<a href="#status" data-toggle="tab"><%= t(:status) %></a>
</li>
<li>
<a href="#tag" data-toggle="tab"><%= t(:tags) %></a>
</li>
</ul>
<!-- Module -->
<div class="tab-content module-area">
<!-- Basic Module -->
<div class="tab-pane fade in active" id="basic">
<!-- Category -->
<div class="control-group">
<%= f.label :category ,t(:category), :class=>"control-label muted" %>
<div class="controls">
<%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %>
</div>
</div>
</div>
<!-- Status Module -->
<div class="tab-pane fade" id="status">
<!-- Status -->
<div class="control-group">
<label class="control-label muted">Status</label>
<div class="controls" data-toggle="buttons-checkbox">
<label class="checkbox inline btn <%= 'active' if @qa.is_top? %>">
<%= f.check_box :is_top, :value => @qa.is_top %> <%= t(:top) %>
</label>
<label class="checkbox inline btn <%= 'active' if @qa.is_hot? %>">
<%= f.check_box :is_hot, :value => @qa.is_hot %> <%= t(:hot) %>
</label>
<label class="checkbox inline btn <%= 'active' if @qa.is_hidden? %>">
<%= f.check_box :is_hidden, :value => @qa.is_hidden %> <%= t(:hide) %>
</label>
</div>
</div>
</div>
<!-- Tag Module -->
<div class="tab-pane fade" id="tag">
<!-- Tag -->
<div class="control-group">
<label class="control-label muted">Tag</label>
<div class="controls" data-toggle="buttons-checkbox">
<% @tags.each do |tag| %>
<label class="checkbox inline btn <%= 'active' if @qa.tags.include?(tag) %>">
<%= check_box_tag 'qa[tags][]', tag.id, @qa.tags.include?(tag)%>
<%= tag.name %>
</label>
<% end %>
</div>
</div>
</div>
</div>
<!-- Language Tabs -->
<div class="nav-name"><strong>Language</strong></div>
<ul class="nav nav-pills language-nav">
<% I18n.available_locales.each_with_index do |locale, i| %>
<li <%= ( i == 0 ) ? "class=active" : '' %>><a data-toggle="tab" href=".<%= locale %>"><%= t(locale.to_s) %></a></li>
<% end %>
</ul>
<!-- Language -->
<div class="tab-content language-area">
<% I18n.available_locales.each_with_index do |locale, i| %>
<div class="<%= locale %> fade tab-pane <%= ( i == 0 ) ? "in active" : '' %>">
<div class="control-group input-title">
<%= f.label :title , t('faq.question'), :class=>"control-label muted" %>
<div class="controls">
<%= f.fields_for :title_translations do |f| %>
<%= f.text_field locale, :class=>'post-title', :value => (@qa.title_translations[locale] rescue nil) %>
<% end %>
</div>
</div>
<div class="control-group input-content">
<%= f.label :answer ,t('faq.answer'), :class => "control-label muted" %>
<%= f.fields_for :answer_translations do |f| %>
<div class="controls">
<div class="textarea">
<%= f.text_area locale, :style=>"width:100%", :class => 'ckeditor input-block-level', :value => (@qa.answer_translations[locale] rescue nil) %>
</div>
</div>
<% end %>
</div>
</div>
<% end %>
<!-- Link -->
<div class="control-group">
<label class="control-label muted">Link</label>
<div class="controls add-input">
<!-- Exist -->
<% if @qa && !@qa.qa_links.blank? %>
<div class="exist">
<% @qa.qa_links.each_with_index do |qa_link, i| %>
<%= f.fields_for :qa_links, qa_link do |f| %>
<%= render :partial => 'form_qa_link', :object => qa_link, :locals => {:f => f, :i => i} %>
<% end %>
<% end %>
<hr>
</div>
<% end %>
<!-- Add -->
<div class="add-target"></div>
<p class="add-btn">
<%= hidden_field_tag 'qa_link_field_count', @qa.qa_links.count %>
<a id="add_link" class="trigger btn btn-small btn-primary"><i class="icons-plus"></i> <%= t(:add) %></a>
</p>
</div>
</div>
<!-- File -->
<div class="control-group">
<label class="control-label muted">File</label>
<div class="controls">
<!-- Exist -->
<% if @qa && !@qa.qa_files.blank? %>
<div class="exist">
<% @qa.qa_files.each_with_index do |qa_file, i| %>
<%= f.fields_for :qa_files, qa_file do |f| %>
<%= render :partial => 'form_qa_file', :object => qa_file, :locals => {:f => f, :i => i} %>
<% end %>
<% end %>
<hr>
</div>
<% end %>
<!-- Add -->
<div class="add-target">
</div>
<p class="add-btn">
<%= hidden_field_tag 'qa_file_field_count', @qa.qa_files.count %>
<a id="add_file" class="trigger btn btn-small btn-primary"><i class="icons-plus"></i> <%= t(:add) %></a>
</p>
</div>
</div>
</div>
</div>
<!-- Form Actions -->
<div class="form-actions">
<%= f.submit t('submit'), class: 'btn btn-primary' %>
<%#= button_tag t("preview"), id: "button_for_preview", name: "commit", class: 'btn post_preview', type: :button, url: preview_panel_announcement_back_end_bulletins_path %>
<%#= link_to t('cancel'), get_go_back, :class=>"btn" %>
</div>
</fieldset>
<% content_for :page_specific_javascript do %>
<script>
$(function() {
$(document).on('click', '#add_link', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_qa_links", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_qa_link', f, :qa_links) %>").replace(old_id, new_id));
$(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.tab-content').children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
formTip();
});
$(document).on('click', '#add_file', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_qa_files", "g");
var on = $('.language-nav li.active').index();
var le = $(this).parent('.add-btn').prev('.add-target').children('.start-line').length;
$(this).prev().attr('value', parseInt(new_id) + 1);
$(this).parent().siblings('.add-target').append(("<%= escape_javascript(add_attribute 'form_qa_file', f, :qa_files) %>").replace(old_id, new_id));
$(this).parent('.add-btn').prev('.add-target').children('.start-line').eq(le).children('.input-append').find('.tab-content').each(function() {
$(this).children('.tab-pane').eq(on).addClass('in active').siblings().removeClass('in active');
});
formTip();
});
$(document).on('click', '.delete_link', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.delete_file', function(){
$(this).parents('.input-prepend').remove();
});
$(document).on('click', '.remove_existing_record', function(){
if(confirm("<%= I18n.t(:sure?)%>")){
$(this).children('.should_destroy').attr('value', 1);
$(this).parents('.start-line').hide();
}
});
$("#status").on('click',"*[data-toggle=buttons-checkbox] input",function(){
var d = $(this).parent().find("input[type=hidden]");
if($(this).is(":checked")){
d.val("1");
}else{
d.val("0");
}
})
$("#status *[data-toggle=buttons-checkbox] .checkbox").each(function(){
if($(this).hasClass("active")){
$(this).find("input[type=hidden]").val("1");
}
})
});
</script>
<% end %>

View File

@ -0,0 +1,55 @@
<% if form_qa_file.new_record? %>
<div class="fileupload fileupload-new start-line" data-provides="fileupload">
<% else %>
<div class="fileupload fileupload-exist start-line" data-provides="fileupload">
<% if form_qa_file.file.blank? %>
<%= t(:no_file) %>
<% else %>
<%= link_to content_tag(:i) + form_qa_file.file_identifier, form_qa_file.file.url, {:class => 'file-link file-type', :target => '_blank', :title => form_qa_file.file_identifier} %>
<% end %>
<% end %>
<div class="input-prepend input-append">
<label>
<span class="add-on btn btn-file" title='<%= t(:file_) %>'>
<i class="icons-paperclip"></i>
<%= f.file_field :file %>
</span>
<div class="uneditable-input input-medium">
<i class="icon-file fileupload-exists"></i>
<span class="fileupload-preview"><%= (form_qa_file.new_record? || form_qa_file.file.blank?) ? t(:select_file) : t(:change_file) %></span>
</div>
</label>
<span class="add-on icons-pencil" title='<%= t(:alternative) %>'></span>
<span class="tab-content">
<% I18n.available_locales.each_with_index do |locale, i| %>
<span class="tab-pane fade <%= ( i == 0 ) ? "in active" : '' %> <%= locale.to_s %>">
<%= f.fields_for :title_translations do |f| %>
<%= f.text_field locale, :class => "input-medium", placeholder: t(:alternative), :value => (form_qa_file.title_translations[locale] rescue nil) %>
<% end %>
</span>
<% end %>
</span>
<span class="add-on icons-pencil" title='<%= t(:description) %>'></span>
<span class="tab-content">
<% I18n.available_locales.each_with_index do |locale, i| %>
<span class="tab-pane fade <%= ( i == 0 ) ? "in active" : '' %> <%= locale.to_s %>">
<%= f.fields_for :description_translations do |f| %>
<%= f.text_field locale, :class => "input-medium", placeholder: t(:description), :value => (form_qa_file.description_translations[locale] rescue nil) %>
<% end %>
</span>
<% end %>
</span>
</span>
<% if form_qa_file.new_record? %>
<span class="delete_file add-on btn" title="<%= t(:delete_) %>">
<a class="icon-trash"></a>
</span>
<% else %>
<span class="remove_existing_record add-on btn" title="<%= t(:remove) %>">
<%= f.hidden_field :id %>
<a class="icon-remove"></a>
<%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %>
</span>
<% end %>
</div>
</div>

View File

@ -0,0 +1,26 @@
<div class="input-prepend input-append start-line">
<span class="add-on icons-link" title="<%= t(:url) %>"></span>
<%= f.text_field :url, class: "input-large", placeholder: t(:url) %>
<span class="add-on icons-pencil" title="<%= t(:url_alt) %>"></span>
<span class="tab-content">
<% I18n.available_locales.each_with_index do |locale, i| %>
<span class="tab-pane fade <%= ( i == 0 ) ? "in active" : '' %> <%= locale.to_s %>">
<%= f.fields_for :title_translations do |f| %>
<%= f.text_field locale, :class => "input-large", placeholder: t(:url_alt), :value => (form_link.title_translations[locale] rescue nil) %>
<% end %>
</span>
<% end %>
</span>
<% if form_qa_link.new_record? %>
<span class="delete_link add-on btn" title="<%= t(:delete_) %>">
<a class="icon-trash"></a>
</span>
<% else %>
<span class="remove_existing_record add-on btn" title="<%= t(:remove) %>">
<%= f.hidden_field :id %>
<a class="remove_existing_record icon-remove"></a>
<%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %>
</span>
<% end %>
</div>

View File

@ -0,0 +1,35 @@
<table class="table main-list">
<thead>
<tr class="sort-header">
<% @table_fields.each do |f| %>
<%= thead(f) %>
<% end %>
</tr>
</thead>
<tbody>
<% @qas.each do |qa| %>
<tr>
<td>
<%= qa.status_for_table %>
</td>
<td>
<%= qa.category.title %>
</td>
<td>
<a href="#" target="_blank"><%= qa.title %></a>
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="/<%= I18n.locale.to_s %>/admin/faqs/<%= qa.id.to_s %>/edit"><%= t(:edit) %></a></li>
<li><a href="/admin/faqs/<%= qa.id.to_s %>" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
</ul>
</div>
</td>
<td>
<% qa.tags.each do |tag| %>
<span class="label label-warning"><%= tag.name %></span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@ -0,0 +1,3 @@
<%= form_for @qa, :url => {:action => "update"}, :html => {:class => 'form-horizontal main-forms'} do |f| %>
<%= render :partial => 'form', :locals => {:f => f} %>
<% end %>

View File

@ -1,2 +1,73 @@
<h1>Admin::Faqs#index</h1>
<p>Find me in app/views/admin/faqs/index.html.erb</p>
<% content_for :right_nav do %>
<ul class="nav nav-pills filter-nav pull-right">
<% @filter_fields.keys.each do |field| %>
<li class="accordion-group">
<div class="accordion-heading">
<a href="#collapse-<%= field %>" data-toggle="collapse" data-parent="#filter" class="accordion-toggle"><%= t(field) %></a>
</div>
</li>
<% end %>
</ul>
<div class="filter-group accordion-group">
<% @filter_fields.keys.each do |field| %>
<div class="accordion-body collapse" id="collapse-<%= field %>">
<div class="accordion-inner pagination-right" data-toggle="buttons-checkbox">
<% @filter_fields[field].each do |val| %>
<%= link_to t(val[:title]), "#", :onclick => "filter.addFilter('filters[#{field}][]=#{val[:id]}')", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %>
<% end %>
</div>
<div class="filter-clear">
<a href="" class="btn btn-link btn-small"><i class="icons-cycle"></i> <%= t(:clear) %></a>
</div>
</div>
<% end %>
</div>
<% end %>
<div id="faqslist">
<%= render 'index'%>
</div>
<script type="text/javascript">
var Filter = function(dom){
var makeFilters = function(){
return (window.location.search ? window.location.search.replace('?','').split('&') : []);
}
var filters = makeFilters(),
dom = $(dom),
mainUrl = window.location.pathname;
var updateTable = function(url){
xurl = (url == null ? ( filters.length ? mainUrl + "?" + filters.join('&') : mainUrl ) : null);
$.ajax({
url : xurl,
type : "get",
dataType : "html"
}).done(function(data){
history.pushState(null, null, decodeURIComponent(xurl));
filters = makeFilters();
dom.html(data);
$(".pagination a").click(function(){
updateTable($(this).attr('href'));
return false;
});
})
}
this.addFilter = function(filter){
$.each(filters,function(idx,data){
if(data.indexOf("page=")>-1) filters.splice(idx,1);
});
if( (index = filters.indexOf(filter) ) > -1){
mainUrl = mainUrl.replace(filter,'');
filters.splice(index,1);
}else{
filters.push(filter);
}
updateTable();
return false;
};
}
var filter = new Filter("#faqslist");
</script>

View File

@ -0,0 +1,8 @@
<div id="poststuff">
<%= form_for @qa, :url => {:action=>"create"}, :html => {:class => 'form-horizontal main-forms'} do |f| %>
<%= render :partial => 'form', :locals => {:f => f} %>
<% end %>
</div>
<%#= link_back %>

View File

@ -0,0 +1 @@
<%= render_view %>

View File

@ -0,0 +1 @@
<%= render_view %>

25
config/locales/en.yml Normal file
View File

@ -0,0 +1,25 @@
en:
faq:
all: All
new: New
default_widget:
question: Question
title: Question
answer: Answer
to_more: Read More
qa_category_with_title: Category
faq: FAQ
widget:
index: FAQ Widget
frontend:
faq: FAQ Frontend
read_more: Read More
question: Question
answer: Answer
qa:
create_qa_category_success: FAQ category was successfully created
editing_web_resource: Editing FAQ
list_of_web_resource: FAQ list
new_web_resource: New FAQ
update_qa_category_success: FAQ was successfully updated

24
config/locales/zh_tw.yml Normal file
View File

@ -0,0 +1,24 @@
zh_tw:
faq:
default_widget:
question: 問題
title: 問題
answer: 回答
to_more: 閱讀更多
qa_category_with_title: 類別
faq: 問與答
widget:
index: 問與答Widget
frontend:
faq: 問與答前台
read_more: 閱讀更多
question: 問題
answer: 回答
qa:
create_qa_category_success: 問與答類別已成功建立
editing_web_resource: 編輯問與答
list_of_web_resource: 問與答列表
new_web_resource: 新增問與答
update_qa_category_success: 問與答已成功更新

View File

@ -12,8 +12,18 @@ module Faq
side_bar do
head_label_i18n 'faq.faq', icon_class: "icons-help"
available_for [:admin,:manager,:sub_manager]
active_for_controllers ({:private=>['faq']})
active_for_controllers (['admin/faqs'])
head_link_path "admin_faqs_path"
context_link 'faq.all',
:link_path=>"admin_faqs_path" ,
:priority=>1,
:active_for_action=>{'admin/faqs'=>"index"}
context_link 'faq.new',
:link_path=>"new_admin_faq_path" ,
:priority=>2,
:active_for_action=>{'admin/faqs'=>"new"}
end
end
end