archive module for orbit 4.5

This commit is contained in:
Harry Bomrah 2014-05-14 19:52:06 +08:00
parent e869de2e2a
commit 8e5f1af790
15 changed files with 616 additions and 5 deletions

View File

@ -1,4 +1,99 @@
class Admin::ArchiveFilesController < ApplicationController
class Admin::ArchiveFilesController < OrbitAdminController
def index
@table_fields = ["Status","Category","Title"]
@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 []
@archives = Kaminari.paginate_array(ArchiveFile.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 new
@archive_file = ArchiveFile.new
@tags = @module_app.tags
@categories = @module_app.categories
end
def edit
@archive_file = ArchiveFile.find(params[:id])
@tags = @module_app.tags
@categories = @module_app.categories
end
def create
@archive_file = ArchiveFile.new(archive_vars)
@archive_file.create_user_id = current_user.id
@archive_file.update_user_id = current_user.id
respond_to do |format|
if @archive_file.save
format.html { redirect_to(admin_archive_files_path) }
format.xml { render :xml => @archive_file, :status => :created, :location => @archive_file }
else
@tags = @module_app.tags
format.html { render :action => "new" }
format.xml { render :xml => @archive_file.errors, :status => :unprocessable_entity }
end
end
end
# PUT /archive_files/1
# PUT /archive_files/1.xml
def update
@archive_file = ArchiveFile.find(params[:id])
@archive_file.update_user_id = current_user.id
respond_to do |format|
if @archive_file.update_attributes(archive_vars)
format.html { redirect_to(admin_archive_files_path(:page => params[:page])) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @archive_file.errors, :status => :unprocessable_entity }
end
end
end
private
def archive_vars
params[:archive_file][:tags] ||=[]
params.require(:archive_file).permit!
end
def setup_vars
@module_app = ModuleApp.where(:key => "archive").first
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
end

View File

@ -0,0 +1,50 @@
class ArchivesController < ApplicationController
def index
categories = OrbitHelper.page_categories
@categories = []
if categories.first == "all"
module_app = OrbitHelper.this_module_app
# debugger
@categories = module_app.categories.collect do |cat|
{
"title" => cat.title,
"id" => cat.id.to_s
}
end
else
categories.each do |cat|
c = Category.find(cat)
@categories << {"title" => c.title, "id" => c.id.to_s}
end
end
cats = @categories.collect do |cat|
archives = ArchiveFile.where(:category_id => cat["id"]).collect do |archive|
statuses = archive.statuses.collect do |status|
{
"archive-status" => status
}
end
files = archive.archive_file_multiples.collect do |file|
{
"file-name" => file.file_title,
"file-type" => file.file.file.extension.downcase,
"file-url" => file.file.url
}
end
{
"archive-title" => archive.title,
"status" => statuses,
"files" => files
}
end
{
"category-title" => cat["title"],
"archives" => archives
}
end
{
"categories" => cats,
"extras" => {"widget-title" => "Archives"}
}
end
end

View File

@ -1,4 +1,94 @@
# encoding: utf-8
class ArchiveFile
include Mongoid::Document
field :title, type: String
include Mongoid::Timestamps
include OrbitCategory::Categorizable
include OrbitModel::Status
include OrbitTag::Taggable
include Slug
# include Tire::Model::Search
# include Tire::Model::Callbacks
# BelongsToCategory = :archive_file_category
# PAYMENT_TYPES = @site_valid_locales
field :title, as: :slug_title, localize: true
field :create_user_id
field :update_user_id
field :postdate , :type => DateTime, :default => Time.now
field :deadline , :type => DateTime
field :uid, type: String
field :sort_number, type: Integer
# scope :can_display,where(is_hidden: false)
# belongs_to :archive_file_category
has_many :archive_file_multiples, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :archive_file_multiples, :allow_destroy => true
# validates :title, :at_least_one => true
after_save :save_archive_file_multiples
# def to_indexed_json
# self.to_json
# end
# search_in :title
# searchable do
# text :titles do
# title_translations.to_a.collect{|t| t[1]}
# end
# boolean :frontend_search do
# !is_hidden
# end
# end
# def self.search( category_id = nil )
# if category_id.to_s.size > 0
# find(:all, :conditions => {archive_file_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, :title)
end
def get_file_icon( file_data )
file_icon = "<span class=\"o-archives-file-type\">#{file_data.split('.')[-1]}</span>".html_safe
end
def save_archive_file_multiples
self.archive_file_multiples.each do |t|
if t.should_destroy
t.destroy
end
end
end
end

View File

@ -0,0 +1,27 @@
class ArchiveFileMultiple
include Mongoid::Document
include Mongoid::Timestamps
mount_uploader :file, AssetUploader
field :file_title, localize: true
# field :description
field :choose_lang, :type => Array, :default => nil
field :should_destroy, :type => Boolean
field :sort_number, :type => Integer
# default_scope asc(:sort_number)
def choose_lang_display(lang)
self.choose_lang.include?(lang)
end
belongs_to :archive_file
# has_many :archive_file_multiple_langs, :autosave => true, :dependent => :destroy
# accepts_nested_attributes_for :archive_file_multiple_langs, :allow_destroy => true
end

View File

@ -0,0 +1,194 @@
<% # encoding: utf-8 %>
<% 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/file-type" %>
<%= javascript_include_tag "lib/module-area" %>
<% end %>
<%#= f.error_messages %>
<!-- 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></li>
<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">
<label class="control-label muted"><%= t(:category) %></label>
<div class="controls">
<%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %>
</div>
</div>
<!-- Sort -->
<div class="control-group">
<label class="control-label muted"><%= t(:sort_number) %></label>
<div class="controls">
<%= f.text_field :sort_number %>
</div>
</div>
</div>
<!-- Status Module -->
<div class="tab-pane fade" id="status">
<!-- Status -->
<div class="control-group">
<label class="control-label muted"><%= t(:status) %></label>
<div class="controls" data-toggle="buttons-checkbox">
<label class="checkbox inline btn <%= 'active' if @archive_file.is_top? %>">
<%= f.check_box :is_top %> <%= t(:top) %>
</label>
<label class="checkbox inline btn <%= 'active' if @archive_file.is_hot? %>">
<%= f.check_box :is_hot %> <%= t(:hot) %>
</label>
<label class="checkbox inline btn <%= 'active' if @archive_file.is_hidden? %>">
<%= f.check_box :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"><%= t(:tags) %></label>
<div class="controls" data-toggle="buttons-checkbox">
<% @tags.each do |tag| %>
<label class="checkbox inline btn <%= 'active' if @archive_file.tags.include?(tag) %>">
<%= check_box_tag 'archive_file[tags][]', tag.id, @archive_file.tags.include?(tag) %> <%= tag.name %>
<%= hidden_field_tag 'archive_file[tags][]', '' %>
</label>
<% end %>
</div>
</div>
</div>
</div>
<!-- Language Tabs -->
<div class="nav-name"><strong><%= t(:language) %></strong></div>
<ul class="nav nav-pills language-nav">
<% I18n.available_locales.each_with_index do |locale, i| %>
<li class="<%= 'active' if i == 0 %>">
<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 %> tab-pane fade <%= ( i == 0 ) ? "in active" : '' %>">
<!-- Title-->
<div class="control-group input-title">
<label class="control-label muted"><%= t(:title) %></label>
<div class="controls">
<%= f.fields_for :title_translations do |f| %>
<%= f.text_field locale, class: "input-block-level", placeholder: t(:title), value: (@archive_file.title_translations[locale] rescue nil) %>
<% end %>
</div>
</div>
</div>
<% end %>
<!-- File -->
<div class="control-group">
<label class="control-label muted"><%= t(:file_) %></label>
<div class="controls">
<!-- Exist -->
<% if @archive_file && !@archive_file.archive_file_multiples.blank? %>
<div class="exist">
<% @archive_file.archive_file_multiples.each_with_index do |archive_file_multiple, i| %>
<%= f.fields_for :archive_file_multiples, archive_file_multiple do |f| %>
<%= render :partial => 'form_file', :object => archive_file_multiple, :locals => {:f => f, :i => i} %>
<% end %>
<% end %>
<hr>
</div>
<% end %>
<!-- Add -->
<div class="add-target">
</div>
<p class="add-btn">
<%= hidden_field_tag 'archive_file_multiple_field_count', @archive_file.archive_file_multiples.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">
<%= hidden_field_tag 'page', params[:page] if !params[:page].blank? %>
<%= f.submit t('submit'), class: 'btn btn-primary' %>
<a href="/<%= I18n.locale.to_s %>/admin/archive_files/" class="btn" >Cancel</a>
</div>
<% content_for :page_specific_javascript do %>
<script>
$(document).ready(function() {
$('.main-forms .add-on').tooltip();
$(document).on('click', '#add_file', function(){
var new_id = $(this).prev().attr('value');
var old_id = new RegExp("new_archive_file_multiples", "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_file', f, :archive_file_multiples) %>").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_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();
}
});
});
</script>
<% end %>

View File

@ -0,0 +1,66 @@
<% if form_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_file.file.blank? %>
<%= t(:no_file) %>
<% else %>
<%= link_to content_tag(:i) + form_file.file_identifier, form_file.file.url, {:class => 'file-link file-type', :target => '_blank', :title => form_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_file.new_record? || form_file.file.blank?) ? t(:select_file) : t(:change_file) %></span>
</div>
</label>
<span class="add-on icons-pencil" title="<%= t('file.name') %>"></span>
<span class="tab-content">
<% I18n.available_locales.each_with_index do |locale, i| %>
<span class="tab-pane fade <%= ( i == 0 ) ? "in active" : '' %> <%= locale %>">
<%= f.fields_for :file_title_translations do |f| %>
<%= f.text_field locale, :class => "input-medium", placeholder: t('file.name'), :value => (form_file.file_title_translations[locale] rescue nil) %>
<% end %>
</span>
<% end %>
</span>
<span class="add-on icon-list-ol" title="<%= t(:sort_number) %>"></span>
<span class="tab-content">
<%= f.text_field :sort_number , :class => "input-mini" %>
</span>
<span class="add-on btn-group btn" title="<%= t('archive.show_lang') %>">
<i class="icons-earth"></i> <span class="caret"></span>
<ul class="dropdown-menu">
<% I18n.available_locales.each do |locale| %>
<li>
<label class="checkbox">
<%= check_box_tag "archive_file[archive_file_multiples_attributes][#{( form_file.new_record? ? 'new_archive_file_multiples' : "#{i}" )}][choose_lang][]", locale, (form_file.choose_lang.nil? ? true : form_file.choose_lang.include?(locale)) %>
<%= t(locale.to_s) %>
</label>
</li>
<% end %>
</ul>
<%= hidden_field_tag 'archive_file[archive_file_multiples_attributes][0][choose_lang][]', '' %>
</span>
<% if form_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,30 @@
<table class="table main-list">
<thead>
<tr class="sort-header">
<% @table_fields.each do |f| %>
<%= thead(f) %>
<% end %>
</tr>
</thead>
<tbody>
<% @archives.each do |archive| %>
<tr>
<td>
<%= archive.status_for_table %>
</td>
<td>
<%= archive.category.title %>
</td>
<td>
<a href="#" target="_blank"><%= archive.title %></a>
<div class="quick-edit">
<ul class="nav nav-pills">
<li><a href="/<%= I18n.locale.to_s %>/admin/archive_files/<%= archive.id.to_s %>/edit"><%= t(:edit) %></a></li>
<li><a href="/admin/archive_files/<%= archive.id.to_s %>" data-method="delete" data-confirm="Are you sure?"><%= t(:delete_) %></a></li>
</ul>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@ -0,0 +1 @@
$("#<%= dom_id @archive_file %>").remove();

View File

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

View File

@ -1,2 +1,7 @@
<h1>Admin::ArchiveFiles#index</h1>
<p>Find me in app/views/admin/archive_files/index.html.erb</p>
<%= render_filter @filter_fields %>
<div id="archivelist">
<%= render 'index'%>
</div>
<script type="text/javascript">
filter.init("#archivelist");
</script>

View File

@ -0,0 +1,5 @@
<%= form_for @archive_file, :url => {:action => "create"}, html: {class: "form-horizontal main-forms previewable"} do |f| %>
<fieldset>
<%= render partial: 'form', locals: {f: f} %>
</fieldset>
<% end %>

View File

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

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

@ -0,0 +1,16 @@
en:
archive:
archive: Archive
all: All
new: New
show_lang: Language
download: Download
archive: Archive
Title: Title
Files: Files
Category: Category
frontend:
archive: Archive Frontend
widget:
index: Archive Widget

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

@ -0,0 +1,17 @@
zh_tw:
archive:
show_lang: 呈現語系
download: 檔案下載
archive: 檔案室
Title: 標題
Files: 檔案
Category: 類別
frontend:
archive: 檔案室前台
widget:
index: 檔案室Widget
mongoid:
attributes:
archive_file:
sort_number: 排序數

View File

@ -12,9 +12,18 @@ module Archive
side_bar do
head_label_i18n 'archive.archive', icon_class: "icons-archive"
available_for [:admin,:manager,:sub_manager]
active_for_controllers ({:private=>['archive_files']})
active_for_controllers (['admin/archive_files'])
head_link_path "admin_archive_files_path"
context_link 'archive.all',
:link_path=>"admin_archive_files_path" ,
:priority=>1,
:active_for_action=>{'admin/archive_files'=>"index"}
context_link 'archive.new',
:link_path=>"new_admin_archive_file_path" ,
:priority=>2,
:active_for_action=>{'admin/archive_files'=>"new"}
end
end
end