2013-01-05 21:21:11 +00:00
|
|
|
class TopicsController < ApplicationController
|
2014-01-29 03:46:58 +00:00
|
|
|
include TopicsHelper
|
|
|
|
|
2014-02-05 17:37:21 +00:00
|
|
|
before_filter :require_user, only: [:create, :update, :removefrommap, :destroy]
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
respond_to :html, :js, :json
|
2014-02-05 17:37:21 +00:00
|
|
|
|
2014-01-29 03:46:58 +00:00
|
|
|
# GET /topics/autocomplete_topic
|
|
|
|
def autocomplete_topic
|
|
|
|
@current = current_user
|
|
|
|
term = params[:term]
|
|
|
|
if term && !term.empty?
|
2014-02-14 03:32:51 +00:00
|
|
|
# !connor term here needs to have .downcase
|
2014-02-15 17:52:56 +00:00
|
|
|
@topics = Topic.where('LOWER("name") like ?', term.downcase + '%').order('"name"')
|
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
|
|
|
|
@topics.delete_if {|t| t.permission == "private" && (!authenticated? || (authenticated? && @current.id != t.user_id)) }
|
2014-01-29 03:46:58 +00:00
|
|
|
else
|
2014-02-05 01:28:06 +00:00
|
|
|
@topics = []
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2014-02-05 01:28:06 +00:00
|
|
|
render json: autocomplete_array_json(@topics)
|
2014-01-29 03:46:58 +00:00
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
# GET topics/:id
|
|
|
|
def show
|
|
|
|
@current = current_user
|
|
|
|
@topic = Topic.find(params[:id]).authorize_to_show(@current)
|
|
|
|
|
|
|
|
if @topic
|
|
|
|
@relatives = @topic.network_as_json(@current).html_safe
|
|
|
|
else
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@topic, @user) }
|
|
|
|
format.json { respond_with(@relatives) }
|
|
|
|
end
|
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
|
|
|
|
# GET topics/:id/json
|
|
|
|
def json
|
|
|
|
@current = current_user
|
|
|
|
@topic = Topic.find(params[:id]).authorize_to_show(@current)
|
|
|
|
|
|
|
|
if not @topic
|
|
|
|
redirect_to root_url and return
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.json { render :json => @topic.self_as_json }
|
|
|
|
end
|
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
# POST topics
|
|
|
|
def create
|
|
|
|
|
|
|
|
@user = current_user
|
|
|
|
|
|
|
|
# if the topic exists grab it and return it
|
|
|
|
if params[:topic][:grabTopic] != "null"
|
|
|
|
@topic = Topic.find(params[:topic][:grabTopic])
|
|
|
|
# if the topic doesn't exist yet, create it
|
|
|
|
else
|
|
|
|
@topic = Topic.new()
|
|
|
|
@topic.name = params[:topic][:name]
|
|
|
|
@topic.desc = ""
|
|
|
|
@topic.link = ""
|
|
|
|
@topic.permission = 'commons'
|
|
|
|
@topic.metacode = Metacode.find_by_name(params[:topic][:metacode])
|
|
|
|
@topic.user = @user
|
2013-01-18 22:08:06 +00:00
|
|
|
|
|
|
|
#if being created on a map, set topic by default to whatever permissions the map is
|
|
|
|
if params[:topic][:map]
|
|
|
|
@map = Map.find(params[:topic][:map])
|
|
|
|
@topic.permission = @map.permission
|
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
@topic.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# pass on to the topic create js whether it's being created with a synapse
|
|
|
|
@synapse = "false"
|
|
|
|
if params[:topic][:addSynapse] == "true"
|
|
|
|
@synapse = "true"
|
2013-02-16 01:00:05 +00:00
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
|
|
|
|
# also create an object to return the position to the canvas
|
|
|
|
@position = Hash.new()
|
|
|
|
@position['x'] = params[:topic][:x]
|
|
|
|
@position['y'] = params[:topic][:y]
|
|
|
|
|
|
|
|
# set this for the case where the topic is being created on a map.
|
2013-02-17 00:03:21 +00:00
|
|
|
@mapping = nil
|
2013-01-05 21:21:11 +00:00
|
|
|
if params[:topic][:map]
|
2013-01-18 22:08:06 +00:00
|
|
|
@map = Map.find(params[:topic][:map])
|
2014-02-26 20:23:29 +00:00
|
|
|
@map.touch(:updated_at)
|
2013-01-18 22:08:06 +00:00
|
|
|
|
2013-02-17 00:03:21 +00:00
|
|
|
@mapping = Mapping.new()
|
2013-01-05 21:21:11 +00:00
|
|
|
@mapping.category = "Topic"
|
|
|
|
@mapping.user = @user
|
2013-01-18 22:08:06 +00:00
|
|
|
@mapping.map = @map
|
2013-01-05 21:21:11 +00:00
|
|
|
@mapping.topic = @topic
|
|
|
|
@mapping.xloc = params[:topic][:x]
|
|
|
|
@mapping.yloc = params[:topic][:y]
|
|
|
|
@mapping.save
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
#push add to map to realtime viewers of the map
|
2014-02-04 17:08:09 +00:00
|
|
|
@mapping.message 'create',@user.id
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { respond_with(@user, location: topic_url(@topic)) }
|
|
|
|
format.js { respond_with(@topic, @mapping, @synapse, @position) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# PUT topics/:id
|
|
|
|
def update
|
|
|
|
@current = current_user
|
|
|
|
@topic = Topic.find(params[:id]).authorize_to_edit(@current)
|
|
|
|
|
|
|
|
if @topic
|
|
|
|
if params[:topic]
|
2013-04-26 04:07:29 +00:00
|
|
|
@permissionBefore = @topic.permission
|
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
@topic.name = params[:topic][:name] if params[:topic][:name]
|
2013-01-18 22:08:06 +00:00
|
|
|
@topic.desc = params[:topic][:desc] if params[:topic][:desc]
|
|
|
|
@topic.link = params[:topic][:link] if params[:topic][:link]
|
|
|
|
@topic.permission = params[:topic][:permission] if params[:topic][:permission]
|
2013-01-05 21:21:11 +00:00
|
|
|
@topic.metacode = Metacode.find_by_name(params[:topic][:metacode]) if params[:topic][:metacode]
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
@permissionAfter = @topic.permission
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
@topic.save
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
#push notify to anyone viewing this topic on a map in realtime (see mapping.rb to understand the 'message' action)
|
|
|
|
# if the topic was private and is being switched to PU or CO it is the same as being created for other viewers
|
|
|
|
if @permissionBefore == "private" and @permissionAfter != "private"
|
2014-02-04 17:08:09 +00:00
|
|
|
@topic.message 'create',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
elsif @permissionBefore != "private" and @permissionAfter == "private"
|
2014-02-04 17:08:09 +00:00
|
|
|
@topic.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
else
|
2014-02-04 17:08:09 +00:00
|
|
|
@topic.message 'update',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
end
|
2013-01-18 22:08:06 +00:00
|
|
|
end
|
2014-02-09 23:54:45 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js { render :json => @topic.self_as_json }
|
2014-02-11 00:32:05 +00:00
|
|
|
format.json { render :json => @topic.self_as_json }
|
2014-02-09 23:54:45 +00:00
|
|
|
end
|
2013-01-05 21:21:11 +00:00
|
|
|
end
|
|
|
|
|
2014-02-26 20:23:29 +00:00
|
|
|
# POST topics/:map_id/:topic_id/removefrommap
|
2013-01-05 21:21:11 +00:00
|
|
|
def removefrommap
|
|
|
|
@current = current_user
|
|
|
|
@mapping = Mapping.find_by_topic_id_and_map_id(params[:topic_id],params[:map_id])
|
|
|
|
|
|
|
|
@map = Map.find(params[:map_id])
|
2014-02-26 20:23:29 +00:00
|
|
|
@map.touch(:updated_at)
|
2013-01-05 21:21:11 +00:00
|
|
|
@topic = Topic.find(params[:topic_id])
|
|
|
|
@mappings = @map.mappings.select{|m|
|
|
|
|
if m.synapse != nil
|
|
|
|
m.synapse.topic1 == @topic || m.synapse.topic2 == @topic
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
@mappings.each do |m|
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
#push notify to anyone viewing same map in realtime (see mapping.rb to understand the 'message' action)
|
2014-02-04 17:08:09 +00:00
|
|
|
m.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
m.delete
|
|
|
|
end
|
|
|
|
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
#push notify to anyone viewing same map in realtime (see mapping.rb to understand the 'message' action)
|
2014-01-29 04:34:50 +00:00
|
|
|
#@mapping.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
@mapping.delete
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# DELETE topics/:id
|
|
|
|
def destroy
|
|
|
|
@current = current_user
|
|
|
|
@topic = Topic.find(params[:id]).authorize_to_edit(@current)
|
|
|
|
|
|
|
|
if @topic
|
|
|
|
@synapses = @topic.synapses
|
|
|
|
@mappings = @topic.mappings
|
|
|
|
|
|
|
|
@synapses.each do |synapse|
|
2013-04-26 04:07:29 +00:00
|
|
|
synapse.mappings.each do |m|
|
2014-02-26 20:23:29 +00:00
|
|
|
|
|
|
|
@map = m.map
|
|
|
|
@map.touch(:updated_at)
|
|
|
|
|
2013-04-26 04:07:29 +00:00
|
|
|
#push notify to anyone viewing same map in realtime (see mapping.rb to understand the 'message' action)
|
2014-02-04 17:08:09 +00:00
|
|
|
m.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
|
|
|
|
m.delete
|
|
|
|
end
|
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
synapse.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@mappings.each do |mapping|
|
2014-02-26 20:23:29 +00:00
|
|
|
|
|
|
|
@map = mapping.map
|
|
|
|
@map.touch(:updated_at)
|
|
|
|
|
2013-04-26 04:07:29 +00:00
|
|
|
#push notify to anyone viewing a map with this topic in realtime (see mapping.rb to understand the 'message' action)
|
2014-02-04 17:08:09 +00:00
|
|
|
mapping.message 'destroy',@current.id
|
2013-04-26 04:07:29 +00:00
|
|
|
|
2013-01-05 21:21:11 +00:00
|
|
|
mapping.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
@topic.delete
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.js
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|