2012-09-23 02:39:12 +00:00
|
|
|
class MainController < ApplicationController
|
2013-01-01 22:45:35 +00:00
|
|
|
include TopicsHelper
|
2014-01-29 03:46:58 +00:00
|
|
|
include MapsHelper
|
|
|
|
include UsersHelper
|
2014-02-05 01:28:06 +00:00
|
|
|
include SynapsesHelper
|
2016-03-11 14:10:31 +00:00
|
|
|
|
|
|
|
after_action :verify_authorized, except: :index
|
|
|
|
after_action :verify_policy_scoped, only: :index
|
2012-10-29 17:52:29 +00:00
|
|
|
|
2014-07-30 05:24:20 +00:00
|
|
|
respond_to :html, :json
|
2012-09-23 02:39:12 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# home page
|
2013-01-08 04:03:41 +00:00
|
|
|
def home
|
2016-02-28 08:53:59 +00:00
|
|
|
@maps = Map.where("maps.permission != ?", "private").order("updated_at DESC").page(1).per(20)
|
2014-08-10 17:06:58 +00:00
|
|
|
respond_to do |format|
|
2014-08-12 17:45:29 +00:00
|
|
|
format.html {
|
|
|
|
if authenticated?
|
2016-02-28 08:53:59 +00:00
|
|
|
render 'main/home'
|
2014-08-12 17:45:29 +00:00
|
|
|
else
|
2016-02-28 08:53:59 +00:00
|
|
|
render 'maps/activemaps'
|
2014-08-12 17:45:29 +00:00
|
|
|
end
|
|
|
|
}
|
2014-08-10 17:06:58 +00:00
|
|
|
end
|
2013-01-08 04:03:41 +00:00
|
|
|
end
|
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
### SEARCHING ###
|
|
|
|
|
|
|
|
# get /search/topics?term=SOMETERM
|
|
|
|
def searchtopics
|
|
|
|
term = params[:term]
|
2014-02-02 19:56:07 +00:00
|
|
|
user = params[:user] ? params[:user] : false
|
2014-01-29 03:46:58 +00:00
|
|
|
|
|
|
|
if term && !term.empty? && term.downcase[0..3] != "map:" && term.downcase[0..6] != "mapper:" && term.downcase != "topic:"
|
|
|
|
|
|
|
|
#remove "topic:" if appended at beginning
|
|
|
|
term = term[6..-1] if term.downcase[0..5] == "topic:"
|
|
|
|
|
2014-02-05 01:28:06 +00:00
|
|
|
#if desc: search desc instead
|
|
|
|
desc = false
|
|
|
|
if term.downcase[0..4] == "desc:"
|
|
|
|
term = term[5..-1]
|
|
|
|
desc = true
|
|
|
|
end
|
|
|
|
|
|
|
|
#if link: search link instead
|
|
|
|
link = false
|
|
|
|
if term.downcase[0..4] == "link:"
|
|
|
|
term = term[5..-1]
|
|
|
|
link = true
|
|
|
|
end
|
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
#check whether there's a filter by metacode as part of the query
|
|
|
|
filterByMetacode = false
|
|
|
|
Metacode.all.each do |m|
|
|
|
|
lOne = m.name.length+1
|
|
|
|
lTwo = m.name.length
|
|
|
|
|
|
|
|
if term.downcase[0..lTwo] == m.name.downcase + ":"
|
|
|
|
term = term[lOne..-1]
|
|
|
|
filterByMetacode = m
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if filterByMetacode
|
|
|
|
if term == ""
|
|
|
|
@topics = []
|
|
|
|
else
|
2014-02-19 04:33:09 +00:00
|
|
|
search = term.downcase + '%'
|
2014-02-02 19:56:07 +00:00
|
|
|
|
2015-10-01 04:33:38 +00:00
|
|
|
if user
|
|
|
|
@topics = Set.new(Topic.where('LOWER("name") like ?', search).where('metacode_id = ? AND user_id = ?', filterByMetacode.id, user).order('"name"'))
|
|
|
|
@topics2 = Set.new(Topic.where('LOWER("name") like ?', '%' + search).where('metacode_id = ? AND user_id = ?', filterByMetacode.id, user).order('"name"'))
|
|
|
|
@topics3 = Set.new(Topic.where('LOWER("desc") like ?', '%' + search).where('metacode_id = ? AND user_id = ?', filterByMetacode.id, user).order('"name"'))
|
|
|
|
@topics4 = Set.new(Topic.where('LOWER("link") like ?', '%' + search).where('metacode_id = ? AND user_id = ?', filterByMetacode.id, user).order('"name"'))
|
|
|
|
else
|
|
|
|
@topics = Set.new(Topic.where('LOWER("name") like ?', search).where('metacode_id = ?', filterByMetacode.id).order('"name"'))
|
|
|
|
@topics2 = Set.new(Topic.where('LOWER("name") like ?', '%' + search).where('metacode_id = ?', filterByMetacode.id).order('"name"'))
|
|
|
|
@topics3 = Set.new(Topic.where('LOWER("desc") like ?', '%' + search).where('metacode_id = ?', filterByMetacode.id).order('"name"'))
|
|
|
|
@topics4 = Set.new(Topic.where('LOWER("link") like ?', '%' + search).where('metacode_id = ?', filterByMetacode.id).order('"name"'))
|
2014-02-02 19:56:07 +00:00
|
|
|
end
|
2015-10-01 04:33:38 +00:00
|
|
|
|
|
|
|
#get unique elements only through the magic of Sets
|
|
|
|
@topics = (@topics + @topics2 + @topics3 + @topics4).to_a
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
elsif desc
|
|
|
|
search = '%' + term.downcase + '%'
|
|
|
|
if !user
|
|
|
|
@topics = Topic.where('LOWER("desc") like ?', search).order('"name"')
|
|
|
|
elsif user
|
|
|
|
@topics = Topic.where('LOWER("desc") like ?', search).where('user_id = ?', user).order('"name"')
|
|
|
|
end
|
|
|
|
elsif link
|
2014-01-29 03:46:58 +00:00
|
|
|
search = '%' + term.downcase + '%'
|
2014-02-02 19:56:07 +00:00
|
|
|
if !user
|
2014-02-05 01:28:06 +00:00
|
|
|
@topics = Topic.where('LOWER("link") like ?', search).order('"name"')
|
|
|
|
elsif user
|
|
|
|
@topics = Topic.where('LOWER("link") like ?', search).where('user_id = ?', user).order('"name"')
|
|
|
|
end
|
|
|
|
else #regular case, just search the name
|
2014-02-19 04:33:09 +00:00
|
|
|
search = term.downcase + '%'
|
2014-02-05 01:28:06 +00:00
|
|
|
if !user
|
|
|
|
@topics = Topic.where('LOWER("name") like ?', search).order('"name"')
|
2014-02-19 04:33:09 +00:00
|
|
|
@topics2 = Topic.where('LOWER("name") like ?', '%' + search).order('"name"')
|
|
|
|
@topics3 = Topic.where('LOWER("desc") like ?', '%' + search).order('"name"')
|
|
|
|
@topics4 = Topic.where('LOWER("link") like ?', '%' + search).order('"name"')
|
|
|
|
@topics = @topics + (@topics2 - @topics)
|
|
|
|
@topics = @topics + (@topics3 - @topics)
|
|
|
|
@topics = @topics + (@topics4 - @topics)
|
2014-02-05 01:28:06 +00:00
|
|
|
elsif user
|
|
|
|
@topics = Topic.where('LOWER("name") like ?', search).where('user_id = ?', user).order('"name"')
|
2014-02-19 04:33:09 +00:00
|
|
|
@topics2 = Topic.where('LOWER("name") like ?', '%' + search).where('user_id = ?', user).order('"name"')
|
|
|
|
@topics3 = Topic.where('LOWER("desc") like ?', '%' + search).where('user_id = ?', user).order('"name"')
|
|
|
|
@topics4 = Topic.where('LOWER("link") like ?', '%' + search).where('user_id = ?', user).order('"name"')
|
|
|
|
@topics = @topics + (@topics2 - @topics)
|
|
|
|
@topics = @topics + (@topics3 - @topics)
|
|
|
|
@topics = @topics + (@topics4 - @topics)
|
2014-02-02 19:56:07 +00:00
|
|
|
end
|
2013-01-03 08:53:25 +00:00
|
|
|
end
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
|
|
|
@topics = []
|
2012-12-23 06:12:56 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
|
|
|
|
#read this next line as 'delete a topic if its private and you're either 1. logged out or 2. logged in but not the topic creator
|
2015-11-08 15:14:53 +00:00
|
|
|
@topics.to_a.delete_if {|t| t.permission == "private" && (!authenticated? || (authenticated? && current_user.id != t.user_id)) }
|
2014-02-05 01:28:06 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
render json: autocomplete_array_json(@topics)
|
|
|
|
end
|
|
|
|
|
|
|
|
# get /search/maps?term=SOMETERM
|
|
|
|
def searchmaps
|
|
|
|
term = params[:term]
|
2014-02-02 19:56:07 +00:00
|
|
|
user = params[:user] ? params[:user] : nil
|
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
if term && !term.empty? && term.downcase[0..5] != "topic:" && term.downcase[0..6] != "mapper:" && term.downcase != "map:"
|
2013-01-03 23:21:46 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
#remove "map:" if appended at beginning
|
|
|
|
term = term[4..-1] if term.downcase[0..3] == "map:"
|
|
|
|
|
2014-02-05 01:28:06 +00:00
|
|
|
#if desc: search desc instead
|
|
|
|
desc = false
|
|
|
|
if term.downcase[0..4] == "desc:"
|
|
|
|
term = term[5..-1]
|
|
|
|
desc = true
|
|
|
|
end
|
2014-02-19 03:44:10 +00:00
|
|
|
search = '%' + term.downcase + '%'
|
2014-02-05 01:28:06 +00:00
|
|
|
query = desc ? 'LOWER("desc") like ?' : 'LOWER("name") like ?'
|
2014-02-02 19:56:07 +00:00
|
|
|
if !user
|
2014-02-14 03:22:01 +00:00
|
|
|
# !connor why is the limit 5 done here and not above? also, why not limit after sorting alphabetically?
|
2014-02-05 01:28:06 +00:00
|
|
|
@maps = Map.where(query, search).limit(5).order('"name"')
|
2014-02-02 19:56:07 +00:00
|
|
|
elsif user
|
2014-06-10 21:41:34 +00:00
|
|
|
@maps = Map.where(query, search).where('user_id = ?', user).order('"name"')
|
2014-02-02 19:56:07 +00:00
|
|
|
end
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
|
|
|
@maps = []
|
2012-12-23 06:12:56 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
|
|
|
|
#read this next line as 'delete a map if its private and you're either 1. logged out or 2. logged in but not the map creator
|
2015-11-08 15:14:53 +00:00
|
|
|
@maps.to_a.delete_if {|m| m.permission == "private" && (!authenticated? || (authenticated? && current_user.id != m.user_id)) }
|
2014-02-05 01:28:06 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
render json: autocomplete_map_array_json(@maps)
|
2012-12-23 06:12:56 +00:00
|
|
|
end
|
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# get /search/mappers?term=SOMETERM
|
|
|
|
def searchmappers
|
|
|
|
term = params[:term]
|
|
|
|
if term && !term.empty? && term.downcase[0..3] != "map:" && term.downcase[0..5] != "topic:" && term.downcase != "mapper:"
|
|
|
|
|
|
|
|
#remove "mapper:" if appended at beginning
|
|
|
|
term = term[7..-1] if term.downcase[0..6] == "mapper:"
|
2014-06-10 21:41:34 +00:00
|
|
|
@mappers = User.where('LOWER("name") like ?', term.downcase + '%').order('"name"')
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
|
|
|
@mappers = []
|
2012-10-29 17:52:29 +00:00
|
|
|
end
|
2014-01-29 03:46:58 +00:00
|
|
|
render json: autocomplete_user_array_json(@mappers)
|
2014-02-05 01:28:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# get /search/synapses?term=SOMETERM OR
|
|
|
|
# get /search/synapses?topic1id=SOMEID&topic2id=SOMEID
|
|
|
|
def searchsynapses
|
|
|
|
term = params[:term]
|
|
|
|
topic1id = params[:topic1id]
|
|
|
|
topic2id = params[:topic2id]
|
2015-10-02 09:39:53 +00:00
|
|
|
|
2014-02-05 01:28:06 +00:00
|
|
|
if term && !term.empty?
|
2015-10-02 09:39:53 +00:00
|
|
|
@synapses = Synapse.where('LOWER("desc") like ?', '%' + term.downcase + '%').order('"desc"')
|
2014-11-16 17:35:06 +00:00
|
|
|
|
|
|
|
# remove any duplicate synapse types that just differ by
|
|
|
|
# leading or trailing whitespaces
|
|
|
|
collectedDesc = []
|
2015-10-02 09:39:53 +00:00
|
|
|
@synapses.to_a.uniq(&:desc).delete_if {|s|
|
2014-11-16 17:35:06 +00:00
|
|
|
desc = s.desc == nil || s.desc == "" ? "" : s.desc.strip
|
|
|
|
if collectedDesc.index(desc) == nil
|
|
|
|
collectedDesc.push(desc)
|
|
|
|
boolean = false
|
|
|
|
else
|
|
|
|
boolean = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
#limit to 5 results
|
|
|
|
@synapses = @synapses.slice(0,5)
|
2014-02-05 01:28:06 +00:00
|
|
|
elsif topic1id && !topic1id.empty?
|
|
|
|
@one = Synapse.where('node1_id = ? AND node2_id = ?', topic1id, topic2id)
|
|
|
|
@two = Synapse.where('node2_id = ? AND node1_id = ?', topic1id, topic2id)
|
|
|
|
@synapses = @one + @two
|
2015-10-01 04:33:38 +00:00
|
|
|
@synapses.sort! {|s1,s2| s1.desc <=> s2.desc }.to_a
|
2014-02-05 01:28:06 +00:00
|
|
|
|
2015-10-01 04:33:38 +00:00
|
|
|
#permissions
|
|
|
|
@synapses.delete_if {|s| s.permission == "private" && !authenticated? }
|
2015-11-08 15:14:53 +00:00
|
|
|
@synapses.delete_if {|s| s.permission == "private" && authenticated? && current_user.id != s.user_id }
|
2014-02-05 01:28:06 +00:00
|
|
|
else
|
|
|
|
@synapses = []
|
|
|
|
end
|
2015-10-01 04:33:38 +00:00
|
|
|
|
2015-10-02 07:46:48 +00:00
|
|
|
render json: autocomplete_synapse_array_json(@synapses)
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2012-09-23 02:39:12 +00:00
|
|
|
end
|