From 9448c519dfb146fa23d3a66b33123c70479bdc81 Mon Sep 17 00:00:00 2001 From: manson Date: Thu, 1 May 2014 16:41:00 +0800 Subject: [PATCH] Basic functions of announcement module --- .../admin/announcements_controller.rb | 96 ++++- app/controllers/announcements_controller.rb | 12 +- app/models/bulletin.rb | 33 +- app/models/bulletin_file.rb | 13 + app/models/bulletin_link.rb | 24 ++ app/views/admin/announcements/_form.html.erb | 342 ++++++++++++++++++ .../admin/announcements/_form_file.html.erb | 55 +++ .../admin/announcements/_form_link.html.erb | 26 ++ app/views/admin/announcements/edit.html.erb | 5 + app/views/admin/announcements/index.html.erb | 98 ++++- app/views/admin/announcements/new.html.erb | 18 +- config/locales/en.yml | 49 ++- config/locales/zh_tw.yml | 49 ++- lib/announcement/engine.rb | 22 ++ 14 files changed, 803 insertions(+), 39 deletions(-) create mode 100644 app/models/bulletin_file.rb create mode 100644 app/models/bulletin_link.rb create mode 100644 app/views/admin/announcements/_form.html.erb create mode 100644 app/views/admin/announcements/_form_file.html.erb create mode 100644 app/views/admin/announcements/_form_link.html.erb create mode 100644 app/views/admin/announcements/edit.html.erb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 433fdd0..1d59105 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -1,21 +1,97 @@ -class Admin::AnnouncementsController < ApplicationController +# encoding: utf-8 +class Admin::AnnouncementsController < OrbitAdminController + before_filter :setup_vars + + def index + @tags = @module_app.tags + @categories = @module_app.categories + @statuses = [] + @bulletins = Kaminari.paginate_array(Bulletin.order_by(sort).all).page(params[:page]).per(10) + @table_fields = [:status, :category, :title, :start_date, :end_date, :last_modified] + + @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}} + } + 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 "start_date" + @sort = {:postdate=>params[:order]} + when "end_date" + @sort = {:deadline=>params[:order]} + when "last_modified" + @sort = {:update_user_id=>params[:order]} + end + else + @sort = {:created_at=>'desc'} + end + @sort + end + def new - @announcement = Bulletin.new + @tags =@module_app.tags + @categories = @module_app.categories + @statuses = [] + @bulletin = Bulletin.new end def create - @announcement = Bulletin.new(bulletin_params) - @announcement.save + bulletin = Bulletin.new(bulletin_params) + bulletin.create_user_id = current_user.id + bulletin.update_user_id = current_user.id + bulletin.save + redirect_to "/admin/announcements" end - def index - @announcements = Bulletin.all + def edit + @tags =@module_app.tags + @categories = @module_app.categories + @statuses = [] + @bulletin = Bulletin.find(params[:id]) + end + + def update + uid = params[:id].split('-').last + bulletin = Bulletin.find_by(:uid=>uid) + bulletin_params[:tags] = bulletin_params[:tags].blank? ? [] : bulletin_params[:tags] + bulletin.update_attributes(bulletin_params) + bulletin.save + redirect_to "/admin/announcements" + end + + def destroy + bulletin = Bulletin.find(params[:id]) + bulletin.destroy + redirect_to "/admin/announcements" + end + + def delete + if params[:ids] + bulletins = Bulletin.any_in(:uid => params[:ids]).destroy_all + end + redirect_to "/admin/announcements" end private - # Never trust parameters from the scary internet, only allow the white list through. - def bulletin_params - params.require(:bulletin).permit(title_translations: [:en, :zh_tw], body_translations: [:en, :zh_tw]) - end + def bulletin_params + params.require(:bulletin).permit! + end + + def setup_vars + @module_app = ModuleApp.where(:key=>"announcement").first + end + end diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 61487c1..b1f424d 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,11 +1,11 @@ class AnnouncementsController < ApplicationController def index - announcements = Bulletin.filter_by_categories + announcements = Bulletin.order_by(:created_at=>'desc').filter_by_categories anns = announcements.collect do |a| { "title" => a.title, - "body" => a.body, + "body" => a.text, "link_to_show" => OrbitHelper.url_to_show(a.to_param), "more" => "More", } @@ -17,11 +17,11 @@ class AnnouncementsController < ApplicationController end def widget - announcements = Bulletin.all + announcements = Bulletin.order_by(:created_at=>'desc').filter_by_categories anns = announcements.collect do |a| { "title" => a.title, - "subtitle" => a.body, + "subtitle" => a.text, "link_to_show" => OrbitHelper.url_to_show(a.to_param), "more" => "More", } @@ -35,10 +35,10 @@ class AnnouncementsController < ApplicationController def show params = OrbitHelper.params - announcement = Bulletin.find_by_param(params[:uid]) + announcement = Bulletin.find_by(:uid=>params[:uid]) { "title" => announcement.title, - "body" => announcement.body + "body" => announcement.text } end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 15f759f..62fff8b 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -1,16 +1,41 @@ class Bulletin include Mongoid::Document include Mongoid::Timestamps + include Impressionist::Impressionable + + include OrbitModel::Status include OrbitTag::Taggable include OrbitCategory::Categorizable include Slug field :title, as: :slug_title, type: String, localize: true - field :body, type: String, localize: true - field :uid, type: String + field :subtitle, localize: true + field :text, localize: true + field :create_user_id + field :update_user_id + field :public, :type => Boolean, :default => true + field :postdate , :type => DateTime, :default => Time.now + field :deadline , :type => DateTime - def self.find_by_param(input) - self.find_by(uid: input) + field :email_sent, :type => Boolean, :default => false + field :email_sentdate , :type => DateTime + field :email_user_ids + field :other_mailaddress + + mount_uploader :image, ImageUploader + + has_many :bulletin_links, :autosave => true, :dependent => :destroy + has_many :bulletin_files, :autosave => true, :dependent => :destroy + + accepts_nested_attributes_for :bulletin_files, :allow_destroy => true + accepts_nested_attributes_for :bulletin_links, :allow_destroy => true + + def update_user + User.find(update_user_id) rescue nil + end + + def update_user=(user) + self.update_user_id = user.id end end diff --git a/app/models/bulletin_file.rb b/app/models/bulletin_file.rb new file mode 100644 index 0000000..d121bd2 --- /dev/null +++ b/app/models/bulletin_file.rb @@ -0,0 +1,13 @@ +class BulletinFile + + include Mongoid::Document + include Mongoid::Timestamps + + mount_uploader :file, AssetUploader + + field :description, localize: true + field :title, localize: true + + belongs_to :bulletin + +end diff --git a/app/models/bulletin_link.rb b/app/models/bulletin_link.rb new file mode 100644 index 0000000..755dea8 --- /dev/null +++ b/app/models/bulletin_link.rb @@ -0,0 +1,24 @@ +require 'uri' + +class BulletinLink + include Mongoid::Document + include Mongoid::Timestamps + + field :url + field :title, localize: true + + belongs_to :bulletin + + before_validation :add_http + + validates :url, :presence => true, :format => /\A(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})?(\/.*)?\Z/i + + protected + + def add_http + unless self.url[/^http:\/\//] || self.url[/^https:\/\//] + self.url = 'http://' + self.url + end + end + +end \ No newline at end of file diff --git a/app/views/admin/announcements/_form.html.erb b/app/views/admin/announcements/_form.html.erb new file mode 100644 index 0000000..70c27bc --- /dev/null +++ b/app/views/admin/announcements/_form.html.erb @@ -0,0 +1,342 @@ +<% 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" %> + <%= javascript_include_tag "lib/module-area" %> + <%= javascript_include_tag "member-selection" %> +<% end %> + + +
+ + + + + + +
+ + +
+ + +
+ +
+ <%= f.select :category_id, @categories.collect{|t| [ t.title, t.id ]} %> +
+
+ + +
+ +
+ <%= f.datetime_picker :postdate, :no_label => true %> +
+
+ +
+ +
+ <%= f.datetime_picker :deadline, :no_label => true %> +
+
+ +
+ + + <%# if show_form_status_field(@bulletin) %> +
+ + +
+ +
+ + + +
+
+ +
+ <%# end %> + + +
+ + +
+ +
+ <% @tags.each do |tag| %> + + <% end %> +
+
+ +
+ + +
+ + +
+ +
+
+
+ <% if @bulletin.image.file %> + <%= image_tag @bulletin.image %> + <% else %> + + <% end %> +
+
+ + <%= t(:select_image) %> + <%= t(:change) %> + <%= f.file_field :image %> + + <%= t(:cancel) %> +
+ +
+
+
+
+ +
+ + +
+ + +
+ +
+ + + +
+

+ <%#= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_user_ids][]', users: @email_users} %> +

+
+
+
+
+ +
+
+ <%= "#{t("announcement.other_mailaddress")}(#{t("announcement.other_mailaddress_note")})"%> + <%= f.text_area :other_mailaddress, :class=>"span12", :cols=>"25", :rows=>"10" %> +
+
+
+ +
+
+ +
+ <%#= f.datetime_picker :email_sentdate, :no_label => true %> +
+
+
+ +
+ +
+ + + + + + +
+ + <% Site.first.in_use_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: (@bulletin.title_translations[locale] rescue nil) %> + <% end %> +
+
+ + +
+ +
+
+ <%= f.fields_for :subtitle_translations do |f| %> + <%= f.text_area locale, rows: 2, class: "input-block-level", value: (@bulletin.subtitle_translations[locale] rescue nil) %> + <% end %> +
+
+
+ + +
+ +
+
+ <%= f.fields_for :text_translations do |f| %> + <%= f.cktext_area locale, rows: 5, class: "input-block-level", :value => (@bulletin.text_translations[locale] rescue nil) %> + <% end %> +
+
+
+ +
+ + <% end %> + + +
+ +
+ + + <% if @bulletin && !@bulletin.bulletin_links.blank? %> +
+ <% @bulletin.bulletin_links.each_with_index do |bulletin_link, i| %> + <%= f.fields_for :bulletin_links, bulletin_link do |f| %> + <%= render :partial => 'form_link', :object => bulletin_link, :locals => {:f => f, :i => i} %> + <% end %> + <% end %> +
+
+ <% end %> + + +
+
+

+ <%= hidden_field_tag 'bulletin_link_field_count', @bulletin.bulletin_links.count %> + <%= t(:add) %> +

+ +
+
+ + +
+ +
+ + + <% if @bulletin && !@bulletin.bulletin_files.blank? %> +
+ <% @bulletin.bulletin_files.each_with_index do |bulletin_file, i| %> + <%= f.fields_for :bulletin_files, bulletin_file do |f| %> + <%= render :partial => 'form_file', :object => bulletin_file, :locals => {:f => f, :i => i} %> + <% end %> + <% end %> +
+
+ <% end %> + + +
+
+

+ <%= hidden_field_tag 'bulletin_file_field_count', @bulletin.bulletin_files.count %> + <%= t(:add) %> +

+ +
+
+ +
+ +
+ + +
+ <%= 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: admin_announcements_path %> + <%= link_to t('cancel'), admin_announcements_path, :class=>"btn" %> +
+ +<% content_for :page_specific_javascript do %> + +<% end %> \ No newline at end of file diff --git a/app/views/admin/announcements/_form_file.html.erb b/app/views/admin/announcements/_form_file.html.erb new file mode 100644 index 0000000..2bad106 --- /dev/null +++ b/app/views/admin/announcements/_form_file.html.erb @@ -0,0 +1,55 @@ +<% 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 %> +
+ + + + <% Site.first.in_use_locales.each_with_index do |locale, i| %> + <%= locale %>"> + <%= f.fields_for :title_translations do |f| %> + <%= f.text_field locale, :class => "input-medium", placeholder: t(:alternative), :value => (form_file.title_translations[locale] rescue nil) %> + <% end %> + + <% end %> + + + + <% Site.first.in_use_locales.each_with_index do |locale, i| %> + <%= locale %>"> + <%= f.fields_for :description_translations do |f| %> + <%= f.text_field locale, :class => "input-medium", placeholder: t(:description), :value => (form_file.description_translations[locale] rescue nil) %> + <% end %> + + <% end %> + + + <% if form_file.new_record? %> + + + + <% else %> + + <%= f.hidden_field :id %> + + <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + + <% end %> +
+
\ No newline at end of file diff --git a/app/views/admin/announcements/_form_link.html.erb b/app/views/admin/announcements/_form_link.html.erb new file mode 100644 index 0000000..2e04304 --- /dev/null +++ b/app/views/admin/announcements/_form_link.html.erb @@ -0,0 +1,26 @@ +
+ + <%= f.text_field :url, class: "input-large", placeholder: t(:url) %> + + + <% Site.first.in_use_locales.each_with_index do |locale, i| %> + <%= locale %>"> + <%= 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 %> + + <% end %> + + + <% if form_link.new_record? %> + + + + <% else %> + + <%= f.hidden_field :id %> + + <%= f.hidden_field :_destroy, :value => nil, :class => 'should_destroy' %> + + <% end %> +
diff --git a/app/views/admin/announcements/edit.html.erb b/app/views/admin/announcements/edit.html.erb new file mode 100644 index 0000000..bf61125 --- /dev/null +++ b/app/views/admin/announcements/edit.html.erb @@ -0,0 +1,5 @@ +<%= form_for @bulletin, url: admin_announcement_path(@bulletin), html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render :partial => 'form', locals: {f: f} %> +
+<% end %> \ No newline at end of file diff --git a/app/views/admin/announcements/index.html.erb b/app/views/admin/announcements/index.html.erb index 0cb7a7f..46f211a 100644 --- a/app/views/admin/announcements/index.html.erb +++ b/app/views/admin/announcements/index.html.erb @@ -1,3 +1,95 @@ -<% @announcements.each do |a|%> - <%= a.title %> -<% end %> \ No newline at end of file + + +<% content_for :right_nav do %> + +
+ <% @filter_fields.keys.each do |field| %> +
+
+ <% @filter_fields[field].each do |val| %> + <%= link_to t(val[:title]), "?filters[#{field}][]=#{val[:id]}", :class => "btn btn-small #{is_filter_active?(field, val[:id])}" %> + <% end %> +
+ +
+ <% end %> +
+<% end %> + + + + + <% @table_fields.each do |f| %> + <%= thead(f) %> + <% end %> + + + + <% @bulletins.each do |b| %> + + + + + + + + + <% end %> + +
<%= b.status_for_table %><%= b.category.title %> + <%= b.title %> + + <%= format_value b.postdate %><%= format_value b.deadline %><%= b.update_user.user_name %>
+ +<%= + content_tag :div, class: "bottomnav clearfix" do + content_tag :div, paginate(@bulletins), class: "pagination pagination-centered" + end +%> + +<%= render 'layouts/delete_modal', delete_options: @delete_options %> \ No newline at end of file diff --git a/app/views/admin/announcements/new.html.erb b/app/views/admin/announcements/new.html.erb index 8b01d33..9ecaab7 100644 --- a/app/views/admin/announcements/new.html.erb +++ b/app/views/admin/announcements/new.html.erb @@ -1,15 +1,5 @@ -<%= form_for @announcement, url: {action: "create"} do |f| %> - <% I18n.available_locales.each do |locale| %> - Title <%= locale.to_s %>: - <%= f.fields_for :title_translations do |n| %> - <%= n.text_field locale %> - <% end %> - - Body <%= locale.to_s %>: - <%= f.fields_for :body_translations do |n| %> - <%= n.text_area locale %> - <% end %> - <% end %> - - <%= f.submit "Create Announcement" %> +<%= form_for @bulletin, url: admin_announcements_path, html: {class: "form-horizontal main-forms previewable"} do |f| %> +
+ <%= render :partial => 'form', locals: {f: f} %> +
<% end %> \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index d0830fa..3cc6bc5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,3 +1,50 @@ en: + announcement: - announcement: Announcement \ No newline at end of file + add_new: Add New + all_articles: All Articles + announcement: Announcement + approval_setting: Approval Setting + approve_bulletin_fail: Approval Fail + approve_bulletin_success: Approve Successfully + bulletins: Bulletins + categories: Categories + create_bulletin_success: Create Bulletin Successfully + create_bulletin_category_success: Create Category Successfully + date: Announcement Date + default_widget: + bulletin_category_with_title: Bulletin Category with Title + postdate: Post Date + subtitle: Subtitle + title: Title + editing_announcement: Edit Announcement + editing_announcement_category: Edit Category + file: Attachment + file_description: File Description + file_name: File Name + frontend: + bulletins: Announcement front-end + search_result: Search result + link_name: Link Name + new_bulletin_category: New Bulletin Category + picture: Cover Picture + search: Search + selected_file: Select File + update_bulletin_category_success: Update Category Successfully + url: URL + widget: + bulletins_and_web_links: Differential Nav. + index: Index + search: Search + email_reminder: Email Reminder + activate_email_reminder: Activate Email Reminder + email_sentdate: Email Time + email_to: Email To + mail_subject: this is an announcement reminder from【%{site_title}】 + other_mailaddress: Other Email + other_mailaddress_note: Divide different email accounts with "," + mail_hi: Hi + mail_url_view: This email is the reminder of an announcement, please click the link for the details + mail_source: Source + mail_time: Time + image_upload_size_note: The following recommendations %{image_upload_size} upload size diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 2c906cc..a8af0d1 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -3,4 +3,51 @@ zh_tw: announcement: add_new: 新建 all_articles: 文章列表 - announcement: 公告 \ No newline at end of file + announcement: 公告 + approval_setting: 審核設定 + approve_bulletin_fail: 審核失敗 + approve_bulletin_success: 審核成功 + bulletins: 公告 + categories: 類別 + create_bulletin_success: 建立公告成功 + create_bulletin_category_success: 建立類別成功 + date: 起迄日期 + default_widget: + bulletin_category_with_title: 公告類別及標題 + postdate: 張貼日期 + subtitle: 副標題 + title: 標題 + editing_announcement: 編輯類別 + editing_announcement_category: 編輯類別 + error: + no_avilb_cate_for_posting: 沒有可以張貼的類別 + file: 附加檔案 + file_description: 檔案描述 + file_name: 檔案名稱 + frontend: + bulletins: 公告前台 + search_result: 搜尋結果頁 + link_name: 連結名稱 + new_bulletin_category: 新增公告類別 + picture: 刊頭圖片 + search: 搜尋 + selected_file: 選擇檔案 + update_bulletin_category_success: 更新類別成功 + url: 連結位置 + widget: + bulletins_and_web_links: 分眾頁籤 + index: 索引 + search: 搜尋 + more: 更多+ + email_reminder: 寄送提醒 + activate_email_reminder: 開啟寄送提醒 + email_sentdate: 寄送時間 + email_to: 寄送對象 + other_mailaddress: 其他Mail + other_mailaddress_note: 輸入多組mail時,請用","逗號隔開 + mail_subject: 來自【%{site_title}】的公告事件提醒 + mail_hi: 您好 + mail_url_view: 此封信件為公告事件提醒,請點選以下連結詳細觀看 + mail_source: 來源 + mail_time: 時間 + image_upload_size_note: 建議檔案小於%{image_upload_size} diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index 95586fc..4e6ebd1 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -12,7 +12,29 @@ module Announcement head_label_i18n 'announcement.announcement', icon_class: "icons-megaphone" active_for_controllers ({:private=>['announcements']}) head_link_path "admin_announcements_path" + + context_link 'announcement.all_articles', + :link_path=>"admin_announcements_path" , + :priority=>1, + :active_for_action=>{:bulletins=>:index}, + :available_for => [:all] + context_link 'announcement.add_new', + :link_path=>"new_admin_announcement_path" , + :priority=>2, + :active_for_action=>{:bulletins=>:new}, + :available_for => [:sub_manager] + # context_link 'announcement.categories', + # :link_path=>"admin_announcement_path(get_module_app)" , + # :priority=>3, + # :active_for_category => 'Announcement', + # :available_for => [:manager] + # context_link 'tags', + # :link_path=>"admin_announcement_path(module_app_id: get_module_app)" , + # :priority=>4, + # :active_for_tag => 'Announcement', + # :available_for => [:manager] end + end end end