2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
class CampaignsController < ApplicationController
|
2018-08-08 21:31:42 +00:00
|
|
|
include Controllers::CampaignHelper
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
helper_method :current_campaign_editor?
|
2019-07-30 21:29:24 +00:00
|
|
|
before_action :authenticate_confirmed_user!, only: %i[create name_and_id duplicate]
|
|
|
|
before_action :authenticate_campaign_editor!, only: %i[update soft_delete]
|
|
|
|
before_action :check_nonprofit_status, only: %i[index show]
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
def index
|
|
|
|
@nonprofit = current_nonprofit
|
2019-07-30 21:29:24 +00:00
|
|
|
if current_nonprofit_user?
|
2019-02-06 17:38:53 +00:00
|
|
|
@campaigns = @nonprofit.campaigns.includes(:nonprofit).not_deleted.order('created_at desc')
|
|
|
|
@deleted_campaigns = @nonprofit.campaigns.includes(:nonprofit).deleted.order('created_at desc')
|
|
|
|
else
|
|
|
|
@campaigns = @nonprofit.campaigns.includes(:nonprofit).not_deleted.not_a_child.order('created_at desc')
|
|
|
|
@deleted_campaigns = @nonprofit.campaigns.includes(:nonprofit).deleted.not_a_child.order('created_at desc')
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html do
|
|
|
|
@active_campaigns = @campaigns.active
|
|
|
|
@past_campaigns = @campaigns.past
|
|
|
|
@unpublished_campaigns = @campaigns.unpublished
|
|
|
|
end
|
|
|
|
|
|
|
|
format.json do
|
|
|
|
@campaigns = @campaigns.limit(params[:limit]) unless params[:limit].blank?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
@campaign = current_campaign
|
|
|
|
@timezone = Format::Timezone.to_proxy(current_nonprofit.timezone)
|
|
|
|
if @campaign.deleted && !current_campaign_editor?
|
|
|
|
redirect_to nonprofit_path(current_nonprofit)
|
|
|
|
flash[:notice] = "Sorry, we couldn't find that campaign"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@nonprofit = current_nonprofit
|
|
|
|
@url = Format::Url.concat(root_url, @campaign.url)
|
|
|
|
|
2018-05-30 17:45:51 +00:00
|
|
|
if @campaign.child_campaign?
|
2018-05-16 19:25:16 +00:00
|
|
|
@parent_campaign = @campaign.parent_campaign
|
|
|
|
@peer_to_peer_campaign_param = @parent_campaign.id
|
|
|
|
else
|
|
|
|
@peer_to_peer_campaign_param = @campaign.id
|
|
|
|
end
|
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
@campaign_background_image = FetchBackgroundImage.with_model(@campaign)
|
|
|
|
end
|
|
|
|
|
|
|
|
def activities
|
2018-11-21 19:47:27 +00:00
|
|
|
@campaign = current_campaign
|
|
|
|
render json: QueryDonations.for_campaign_activities(@campaign.id)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2018-11-09 00:29:00 +00:00
|
|
|
render json: CreateCampaign.create(params, current_nonprofit)
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
Time.use_zone(current_nonprofit.timezone || 'UTC') do
|
|
|
|
params[:campaign][:end_datetime] = Chronic.parse(params[:campaign][:end_datetime]) if params[:campaign][:end_datetime].present?
|
|
|
|
end
|
|
|
|
current_campaign.update_attributes params[:campaign]
|
|
|
|
json_saved current_campaign, 'Successfully updated!'
|
|
|
|
end
|
|
|
|
|
|
|
|
# post 'nonprofits/:np_id/campaigns/:campaign_id/duplicate'
|
|
|
|
def duplicate
|
2019-07-30 21:29:24 +00:00
|
|
|
render_json do
|
2018-03-25 17:30:42 +00:00
|
|
|
InsertDuplicate.campaign(current_campaign.id, current_user.profile.id)
|
2019-07-30 21:29:24 +00:00
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def soft_delete
|
|
|
|
current_campaign.update_attribute(:deleted, params[:delete])
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def metrics
|
|
|
|
render json: QueryCampaignMetrics.on_donations(current_campaign.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def timeline
|
|
|
|
render json: QueryCampaigns.timeline(current_campaign.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# returns supporters count as well as total cents for one time, recurring, offsite and the previous three combined. used on campaign dashboard
|
|
|
|
def totals
|
|
|
|
render json: QueryCampaigns.totals(current_campaign.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def name_and_id
|
|
|
|
render json: QueryCampaigns.name_and_id(current_nonprofit.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def peer_to_peer
|
2019-07-30 21:29:24 +00:00
|
|
|
session[:donor_signup_url] = request.env['REQUEST_URI']
|
2018-05-30 19:58:41 +00:00
|
|
|
@nonprofit = Nonprofit.find_by_id(params[:npo_id])
|
|
|
|
@parent_campaign = Campaign.find_by_id(params[:campaign_id])
|
2018-07-05 14:11:08 +00:00
|
|
|
|
|
|
|
if params[:campaign_id].present? && !@parent_campaign
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ActionController::RoutingError, 'Not Found'
|
2018-07-05 14:11:08 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if current_user
|
|
|
|
@profile = current_user.profile
|
2019-07-30 21:29:24 +00:00
|
|
|
if @parent_campaign
|
2018-11-12 17:25:43 +00:00
|
|
|
@child_campaign = Campaign.where(
|
|
|
|
profile_id: @profile.id,
|
|
|
|
parent_campaign_id: @parent_campaign.id
|
|
|
|
).first
|
|
|
|
end
|
2018-07-05 14:11:08 +00:00
|
|
|
end
|
2018-05-16 19:25:16 +00:00
|
|
|
end
|
|
|
|
|
2018-03-25 17:30:42 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def check_nonprofit_status
|
|
|
|
if !current_role?(:super_admin) && !current_nonprofit.published
|
2019-07-30 21:29:24 +00:00
|
|
|
raise ActionController::RoutingError, 'Not Found'
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|