Class: Plugins::CamaSubscriber::PromotionsController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/plugins/cama_subscriber/promotions_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



11
12
13
14
15
16
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 11

def create
  @item = current_site.subscriber_promotions.create(item_params)
  @item.add_groups(params[:groups])
  flash[:notice] = t('.saved', default: 'The promotions was saved')
  redirect_to action: :index
end

#destroyObject



36
37
38
39
40
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 36

def destroy
  @item = current_site.subscriber_promotions.find(params[:id]).destroy
  flash[:notice] = t('.destroyed', default: 'The promotion was destroyed')
  redirect_to action: :index
end

#editObject



18
19
20
21
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 18

def edit
  @item ||= current_site.subscriber_promotions.find(params[:id])
  render 'form', layout: false
end

#form_testObject



67
68
69
70
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 67

def form_test
  @promotion = current_site.subscriber_promotions.find(params[:promotion_id])
  render layout: false
end

#indexObject



2
3
4
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 2

def index
  @items = current_site.subscriber_promotions
end

#newObject



6
7
8
9
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 6

def new
  @item = current_site.subscriber_promotions.new
  render 'form', layout: false
end

#send_campaignObject

send the campaign to items of the groups



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 43

def send_campaign
  promotion = current_site.subscriber_promotions.find(params[:promotion_id])
  if promotion.groups.present?
    i = 0
    promotion.items.active.uniq.each do |item|
      sent_promo = promotion.sent_promo_items.create(item_id: item.id)
      key = Base64.encode64("#{promotion.id}/#{sent_promo.id}")
      cama_send_email(item.email, promotion.subject, {
          from: promotion.email_from,
          cc_to: promotion.email_cc.split(","),
          template: promotion.template,
          layout_name: promotion.layout,
          content: promotion.content.to_s.gsub('unsubscribe_url', plugins_cama_subscriber_unsubscribe_url(key: key)).gsub('unsubscribe_all_url', plugins_cama_subscriber_unsubscribe_all_url(key: key)) + "<img src='#{plugins_cama_subscriber_image_email_url(key: key)}'>"
      })
      i += 1
    end
    flash[:notice] = t('.success_send', default: "The campaign was sent successfully to #{i} subscribers", qty: i)
    promotion.sent!
  else
    flash[:error] = t('.error_send', default: 'You need to define at least one group')
  end
  redirect_to action: :index
end

#send_testObject



72
73
74
75
76
77
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 72

def send_test
  promotion = current_site.subscriber_promotions.find(params[:promotion_id])
  cama_send_email(params[:email], promotion.subject, {from: promotion.email_from, cc_to: promotion.email_cc.split(","), template: promotion.template, layout_name: promotion.layout, content: promotion.content})
  flash[:notice] = t('.success_test_send', default: 'The test campaign was sent successfully')
  redirect_to action: :index
end

#showObject



31
32
33
34
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 31

def show
  @item = current_site.subscriber_promotions.find(params[:id])
  render layout: false
end

#updateObject



23
24
25
26
27
28
29
# File 'app/controllers/plugins/cama_subscriber/promotions_controller.rb', line 23

def update
  @item = current_site.subscriber_promotions.find(params[:id])
  @item.update(item_params)
  @item.add_groups(params[:groups])
  flash[:notice] = t('.updated', default: 'The promotion was udpated')
  redirect_to action: :index
end