diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a0e166e..d489611 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -75,4 +75,4 @@ class ApplicationController < ActionController::Base !!(current_user && current_user.confirmed?) end -end \ No newline at end of file +end diff --git a/app/controllers/blogposts_controller.rb b/app/controllers/blogposts_controller.rb index 79c9e5d..7a9851d 100644 --- a/app/controllers/blogposts_controller.rb +++ b/app/controllers/blogposts_controller.rb @@ -75,4 +75,4 @@ class BlogpostsController < ApplicationController end end -end \ No newline at end of file +end diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rb index 486d21c..761a86b 100644 --- a/app/controllers/forums_controller.rb +++ b/app/controllers/forums_controller.rb @@ -1,4 +1,5 @@ class ForumsController < ApplicationController + before_filter :check_permission, only: [:show, :edit, :update, :destroy] def index @@ -77,7 +78,6 @@ class ForumsController < ApplicationController redirect_to forums_path end - private def check_permission diff --git a/app/controllers/forumthreads_controller.rb b/app/controllers/forumthreads_controller.rb index b1dffd9..e21c6d4 100644 --- a/app/controllers/forumthreads_controller.rb +++ b/app/controllers/forumthreads_controller.rb @@ -3,9 +3,14 @@ class ForumthreadsController < ApplicationController before_filter :check_permission, only: [:show, :edit, :update, :destroy] def index - redirect_to forum_path(@thread.forum.forumgroup, f) + if params[:label] && !Label.where("lower(name) = ?", params[:label].downcase).try(:first) && params[:label].downcase != "no label" + flash[:alert] = "'#{params[:label]}' is not a valid label." + redirect_to forumthreads_path(params.except(:label, :controller, :action)) + return + end + @threads = Forumthread.filter(current_user, params[:title], params[:content], params[:reply], params[:label], User.where("lower(ign) = ?", params[:author].to_s.downcase).try(:first), params[:query], Forum.where(id: params[:id]).try(:first)) + .page(params[:page]).per(30) end - def show if params[:reverse] @replies = @thread.replies.reverse_order.page(params[:page]) @@ -80,6 +85,20 @@ class ForumthreadsController < ApplicationController redirect_to @thread.forum end + def search + end + + def search_redirect + params.each do |key, value| + params[key] = nil if params[key] == "" + end + params[:id] = nil if params[:id] == "Search All Threads" + params[:label] = nil if params[:label] && params[:label].downcase == "label" + params[:author] = params[:author].tr("@ ", "") if params[:author] + params_list = Hash[params.except(:commit, :utf8, :authenticity_token)] + redirect_to forumthreads_path(params_list) + end + private def check_permission diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 16f42d3..6b31d22 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,7 +4,7 @@ class UsersController < ApplicationController include MailerHelper include ERB::Util - before_filter :set_user, except: [:index, :new, :create, :lost_password, :reset_password, :suggestions] + before_filter :set_user, except: [:index, :new, :create, :lost_password, :reset_password, :suggestions, :search_redirect] def index if params[:role] @@ -13,7 +13,7 @@ class UsersController < ApplicationController else if role = Role.get(params[:role]) @users = User.joins(:role).where(role: role) - else + elsif params[:search] == nil flash[:alert] = "role '#{params[:role]}' does not exist!" redirect_to users_path return @@ -30,6 +30,7 @@ class UsersController < ApplicationController else @users = User.joins(:role).where.not(id: User.first.id) #Remove first user end + @users = User.search(@users, params[:search]) if params[:search] @users = @users.order("roles.value desc", "confirmed desc", :name) unless params[:badge] @count = @users.size @users = @users.page(params[:page]).per(100) @@ -339,6 +340,14 @@ class UsersController < ApplicationController end end + def search_redirect + params.each do |key, value| + params[key] = nil if params[key] == "" + end + params_list = Hash[params.except(:commit, :utf8, :authenticity_token)] + redirect_to users_path(params_list) + end + private def validate_token(uuid, email, token) diff --git a/app/helpers/mailer_helper.rb b/app/helpers/mailer_helper.rb index dbacf81..5e5649c 100644 --- a/app/helpers/mailer_helper.rb +++ b/app/helpers/mailer_helper.rb @@ -24,4 +24,4 @@ module MailerHelper end end end -end \ No newline at end of file +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 2ce1765..7ad99d8 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -52,4 +52,4 @@ module UsersHelper end end -end \ No newline at end of file +end diff --git a/app/models/forum.rb b/app/models/forum.rb index 39e8f2a..a239dbc 100644 --- a/app/models/forum.rb +++ b/app/models/forum.rb @@ -32,4 +32,4 @@ class Forum < ActiveRecord::Base def to_param [id, to_s.parameterize].join("-") end -end \ No newline at end of file +end diff --git a/app/models/forumthread.rb b/app/models/forumthread.rb index 905e4d3..86823ac 100644 --- a/app/models/forumthread.rb +++ b/app/models/forumthread.rb @@ -65,4 +65,47 @@ class Forumthread < ActiveRecord::Base def to_param [id, to_s.parameterize].join("-") end + + def self.filter (user, title, content, reply, label, author, query, forum) + userid = user.try(:id).to_i + role = user.try(:role).to_i + + can_read = "COALESCE(forum_role_read.value, 0) <= ? AND COALESCE(forumgroup_role_read.value, 0) <= ?" + sticky_can_write = "sticky = true AND (COALESCE(forum_role_write.value, 0) <= ? OR COALESCE(forumgroup_role_write.value, 0) <= ?)" + + threads = forum.try(:forumthreads) || Forumthread + threads = threads.where("forumthreads.user_author_id = ? OR (#{can_read}) OR (#{sticky_can_write})", userid, role, role, role, role) + .joins("LEFT JOIN threadreplies ON forumthreads.id = threadreplies.forumthread_id") + .joins(forum: :forumgroup) + .joins("LEFT JOIN roles as forum_role_read ON forums.role_read_id = forum_role_read.id") + .joins("LEFT JOIN roles as forum_role_write ON forums.role_write_id = forum_role_write.id") + .joins("LEFT JOIN roles as forumgroup_role_read ON forumgroups.role_read_id = forumgroup_role_read.id") + .joins("LEFT JOIN roles as forumgroup_role_write ON forumgroups.role_write_id = forumgroup_role_write.id") + + if [content, title, reply, label, author, query].any? + label_o = Label.find_by(name: label) + if label_o + threads = threads.where(label: label_o) + elsif label.try(:downcase) == "no label" + threads = threads.where(label: nil) + end + + threads = threads.where(user_author: author) if author + + if query + threads = threads.where("MATCH (title, forumthreads.content) AGAINST (?) OR MATCH (threadreplies.content) AGAINST (?)", query, query) + elsif [title, content, reply].any? + query = [title, content, reply].select(&:present?).join(" ") + threads = threads.where("MATCH (title) AGAINST (?)", title) if title + threads = threads.where("MATCH (forumthreads.content) AGAINST (?)", content) if content + threads = threads.where("MATCH (threadreplies.content) AGAINST (?)", reply) if reply + threads = threads.group("threadreplies.id", "forumthreads.id") + threads = threads.order("(MATCH (title, forumthreads.content) AGAINST ('#{query}')) DESC") + end + end + + threads = threads.order("sticky desc", "threadreplies.created_at desc", "forumthreads.created_at desc") if threads.order_values.empty? + + threads + end end diff --git a/app/models/role.rb b/app/models/role.rb index 708fb40..e780b8c 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -53,4 +53,4 @@ class Role < ActiveRecord::Base Role.order(:value).select {|r| r >= from}.select {|r| r <= to} end -end \ No newline at end of file +end diff --git a/app/models/user.rb b/app/models/user.rb index ab7471e..a96410a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -174,4 +174,8 @@ class User < ActiveRecord::Base def set_email_token self.email_token ||= SecureRandom.hex(16) end + + def self.search (users, search) + return users.where("users.name like ? OR ign like ?", "%#{User.send(:sanitize_sql_like, search)}%", "%#{User.send(:sanitize_sql_like, search)}%") + end end diff --git a/app/views/application/_md_editor_user.html.erb b/app/views/application/_md_editor_user.html.erb new file mode 100644 index 0000000..25f63a4 --- /dev/null +++ b/app/views/application/_md_editor_user.html.erb @@ -0,0 +1,8 @@ +
<%= link_to "New thread", new_forumthread_path(forum: @forum), class: "btn blue" %>
++ <%= link_to "New thread", new_forumthread_path(forum: @forum), class: "btn blue" %> + <% params[:id] = params[:id].split("-")[0] %> +
<% end %> <% if @forum.role_read && @forum.role_write && @forum.role_write < @forum.role_read %> @@ -51,4 +58,4 @@| Forum | +<%= select_tag "id", options_for_select(["Search All Threads"] + forums, params[:id]) %> | +
| Label | ++ <%= select_tag "label", options_for_select(label_list, params[:label]), class: "auto-width" %> + | +
| Title | ++ <%= text_field_tag "title", params[:title], placeholder: "Search Titles" %> + | +
| Content | ++ <%= text_field_tag "content", params[:content], placeholder: "Search Contents" %> + | +
| Author | ++ <%= render partial: "md_editor_user", locals: {name: "author", content: params[:author]} %> + | +Replies | ++ <%= text_field_tag "reply", params[:reply], placeholder: "Search Replies" %> + | + +
| + <%= submit_tag "Go", class: "btn blue", style: "width:50px" %> + | +