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 %> + + +
+ <%#= render partial: 'admin/member_selects/email_selection_box', locals: {field: 'bulletin[email_user_ids][]', users: @email_users} %> +
++ <%= hidden_field_tag 'bulletin_link_field_count', @bulletin.bulletin_links.count %> + <%= t(:add) %> +
+ ++ <%= hidden_field_tag 'bulletin_file_field_count', @bulletin.bulletin_files.count %> + <%= t(:add) %> +
+ +<%= b.status_for_table %> | +<%= b.category.title %> | +
+ <%= b.title %>
+
+
+
+ |
+ <%= format_value b.postdate %> | +<%= format_value b.deadline %> | +<%= b.update_user.user_name %> | +