From 8e5f1af79036de07881a7b579fb84063614d2652 Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Wed, 14 May 2014 19:52:06 +0800 Subject: [PATCH] archive module for orbit 4.5 --- .../admin/archive_files_controller.rb | 97 ++++++++- app/controllers/archives_controller.rb | 50 +++++ app/models/archive_file.rb | 92 ++++++++- app/models/archive_file_multiple.rb | 27 +++ app/views/admin/archive_files/_form.html.erb | 194 ++++++++++++++++++ .../admin/archive_files/_form_file.html.erb | 66 ++++++ app/views/admin/archive_files/_index.html.erb | 30 +++ app/views/admin/archive_files/destroy.js.erb | 1 + app/views/admin/archive_files/edit.html.erb | 5 + app/views/admin/archive_files/index.html.erb | 9 +- app/views/admin/archive_files/new.html.erb | 5 + app/views/archives/index.html.erb | 1 + config/locales/en.yml | 16 ++ config/locales/zh_tw.yml | 17 ++ lib/archive/engine.rb | 11 +- 15 files changed, 616 insertions(+), 5 deletions(-) create mode 100644 app/controllers/archives_controller.rb create mode 100644 app/models/archive_file_multiple.rb create mode 100644 app/views/admin/archive_files/_form.html.erb create mode 100644 app/views/admin/archive_files/_form_file.html.erb create mode 100644 app/views/admin/archive_files/_index.html.erb create mode 100644 app/views/admin/archive_files/destroy.js.erb create mode 100644 app/views/admin/archive_files/edit.html.erb create mode 100644 app/views/admin/archive_files/new.html.erb create mode 100644 app/views/archives/index.html.erb create mode 100644 config/locales/en.yml create mode 100644 config/locales/zh_tw.yml diff --git a/app/controllers/admin/archive_files_controller.rb b/app/controllers/admin/archive_files_controller.rb index bda9d99..1fa8d50 100644 --- a/app/controllers/admin/archive_files_controller.rb +++ b/app/controllers/admin/archive_files_controller.rb @@ -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 diff --git a/app/controllers/archives_controller.rb b/app/controllers/archives_controller.rb new file mode 100644 index 0000000..a2ffd5d --- /dev/null +++ b/app/controllers/archives_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/archive_file.rb b/app/models/archive_file.rb index 510558e..ea2e331 100644 --- a/app/models/archive_file.rb +++ b/app/models/archive_file.rb @@ -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 = "#{file_data.split('.')[-1]}".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 diff --git a/app/models/archive_file_multiple.rb b/app/models/archive_file_multiple.rb new file mode 100644 index 0000000..b5993d9 --- /dev/null +++ b/app/models/archive_file_multiple.rb @@ -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 diff --git a/app/views/admin/archive_files/_form.html.erb b/app/views/admin/archive_files/_form.html.erb new file mode 100644 index 0000000..6a04b95 --- /dev/null +++ b/app/views/admin/archive_files/_form.html.erb @@ -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 %> + + +
+ + + + + + +
+ + +
+ + +
+ +
+ <%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %> +
+
+ + +
+ +
+ <%= f.text_field :sort_number %> +
+
+ +
+ + +
+ + +
+ +
+ + + +
+
+ +
+ + +
+ + +
+ +
+ <% @tags.each do |tag| %> + + <% end %> +
+
+ +
+ +
+ + + + + + +
+ + <% I18n.available_locales.each_with_index do |locale, i| %> + +
"> + + +
+ +
+ <%= 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 %> +
+
+ +
+ + <% end %> + + +
+ +
+ + + <% if @archive_file && !@archive_file.archive_file_multiples.blank? %> +
+ <% @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 %> +
+
+ <% end %> + + +
+
+

+ <%= hidden_field_tag 'archive_file_multiple_field_count', @archive_file.archive_file_multiples.count %> + <%= t(:add) %> +

+ +
+
+
+ +
+ + +
+ <%= hidden_field_tag 'page', params[:page] if !params[:page].blank? %> + <%= f.submit t('submit'), class: 'btn btn-primary' %> + Cancel +
+ +<% content_for :page_specific_javascript do %> + +<% end %> diff --git a/app/views/admin/archive_files/_form_file.html.erb b/app/views/admin/archive_files/_form_file.html.erb new file mode 100644 index 0000000..0f2e426 --- /dev/null +++ b/app/views/admin/archive_files/_form_file.html.erb @@ -0,0 +1,66 @@ +<% if form_file.new_record? %> +
+<% else %> +
+ <% 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 %> +
+ + + + <% I18n.available_locales.each_with_index do |locale, i| %> + <%= 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 %> + + <% end %> + + + + + <%= f.text_field :sort_number , :class => "input-mini" %> + + + + + + <%= hidden_field_tag 'archive_file[archive_file_multiples_attributes][0][choose_lang][]', '' %> + + + <% if form_file.new_record? %> + + + + <% else %> + + <%= f.hidden_field :id %> + + <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + + <% end %> +
+
+ diff --git a/app/views/admin/archive_files/_index.html.erb b/app/views/admin/archive_files/_index.html.erb new file mode 100644 index 0000000..3ae3f8a --- /dev/null +++ b/app/views/admin/archive_files/_index.html.erb @@ -0,0 +1,30 @@ + + + + <% @table_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <% @archives.each do |archive| %> + + + + + + <% end %> + +
+ <%= archive.status_for_table %> + + <%= archive.category.title %> + + <%= archive.title %> + +
\ No newline at end of file diff --git a/app/views/admin/archive_files/destroy.js.erb b/app/views/admin/archive_files/destroy.js.erb new file mode 100644 index 0000000..7dc6287 --- /dev/null +++ b/app/views/admin/archive_files/destroy.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id @archive_file %>").remove(); \ No newline at end of file diff --git a/app/views/admin/archive_files/edit.html.erb b/app/views/admin/archive_files/edit.html.erb new file mode 100644 index 0000000..d5f4e3b --- /dev/null +++ b/app/views/admin/archive_files/edit.html.erb @@ -0,0 +1,5 @@ +<%= form_for @archive_file, :url => {:action => "update"}, html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render partial: 'form', locals: {f: f} %> +
+<% end %> diff --git a/app/views/admin/archive_files/index.html.erb b/app/views/admin/archive_files/index.html.erb index c2e1c08..875c1f7 100644 --- a/app/views/admin/archive_files/index.html.erb +++ b/app/views/admin/archive_files/index.html.erb @@ -1,2 +1,7 @@ -

Admin::ArchiveFiles#index

-

Find me in app/views/admin/archive_files/index.html.erb

+<%= render_filter @filter_fields %> +
+ <%= render 'index'%> +
+ diff --git a/app/views/admin/archive_files/new.html.erb b/app/views/admin/archive_files/new.html.erb new file mode 100644 index 0000000..abbfe41 --- /dev/null +++ b/app/views/admin/archive_files/new.html.erb @@ -0,0 +1,5 @@ +<%= form_for @archive_file, :url => {:action => "create"}, html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render partial: 'form', locals: {f: f} %> +
+<% end %> diff --git a/app/views/archives/index.html.erb b/app/views/archives/index.html.erb new file mode 100644 index 0000000..648b75c --- /dev/null +++ b/app/views/archives/index.html.erb @@ -0,0 +1 @@ +<%= render_view %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000..22252aa --- /dev/null +++ b/config/locales/en.yml @@ -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 Front-end + widget: + index: Archive Widget \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml new file mode 100644 index 0000000..d81d138 --- /dev/null +++ b/config/locales/zh_tw.yml @@ -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: 排序數 diff --git a/lib/archive/engine.rb b/lib/archive/engine.rb index 1c71cb1..1f37775 100644 --- a/lib/archive/engine.rb +++ b/lib/archive/engine.rb @@ -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