diff --git a/app/controllers/admin/faqs_controller.rb b/app/controllers/admin/faqs_controller.rb index 4bf2f63..bcf6744 100644 --- a/app/controllers/admin/faqs_controller.rb +++ b/app/controllers/admin/faqs_controller.rb @@ -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 diff --git a/app/controllers/faqs_controller.rb b/app/controllers/faqs_controller.rb new file mode 100644 index 0000000..5acb529 --- /dev/null +++ b/app/controllers/faqs_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/qa.rb b/app/models/qa.rb index 409e92c..3bd7267 100644 --- a/app/models/qa.rb +++ b/app/models/qa.rb @@ -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 \ No newline at end of file diff --git a/app/models/qa_file.rb b/app/models/qa_file.rb new file mode 100644 index 0000000..30b694e --- /dev/null +++ b/app/models/qa_file.rb @@ -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 diff --git a/app/models/qa_link.rb b/app/models/qa_link.rb new file mode 100644 index 0000000..77a242f --- /dev/null +++ b/app/models/qa_link.rb @@ -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 diff --git a/app/views/admin/faqs/_form.html.erb b/app/views/admin/faqs/_form.html.erb new file mode 100644 index 0000000..e1d1ddb --- /dev/null +++ b/app/views/admin/faqs/_form.html.erb @@ -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 %> +
+<% content_for :page_specific_javascript do %> + +<% end %> + diff --git a/app/views/admin/faqs/_form_qa_file.html.erb b/app/views/admin/faqs/_form_qa_file.html.erb new file mode 100644 index 0000000..3262693 --- /dev/null +++ b/app/views/admin/faqs/_form_qa_file.html.erb @@ -0,0 +1,55 @@ +<% if form_qa_file.new_record? %> ++ <%= qa.status_for_table %> + | ++ <%= qa.category.title %> + | +
+ <%= qa.title %>
+
+
+
+ |
+ + <% qa.tags.each do |tag| %> + <%= tag.name %> + <% end %> + | +
Find me in app/views/admin/faqs/index.html.erb
+<% content_for :right_nav do %> + +