Class: Admin::MailchimpController

Inherits:
AdminController
  • Object
show all
Defined in:
app/controllers/admin/mailchimp_controller.rb

Instance Method Summary collapse

Instance Method Details

#callbackObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/admin/mailchimp_controller.rb', line 72

def callback
  key = Preference.get_cached(_sid, 'mailchimp_callback_key')
  if key.is_blank?
    render :text=>"no key set"
    return
  end

  if params[:key] != key
    Event.store("mailchimp callback", request, nil, nil, nil)
    head :bad_request
    return
  end

  type = params[:type]

  if type=='unsubscribe' || type=='subscribe'
    u = User.sys(_sid).where(:email=>params[:data][:email]).first

    head :ok and return unless u
    if type=='unsubscribe'
      u.update_attributes(:subscribe_newsletter=>0)
#      elsif type=='upemail'
#        u.update_attributes(:email=>params[:data][:new_email])
    elsif type=='subscribe'
      u.update_attributes(:subscribe_newsletter=>1)
    end
  end

  head :ok
end

#importObject



3
4
5
6
7
8
9
10
11
# File 'app/controllers/admin/mailchimp_controller.rb', line 3

def import
  gb = mailchimp_connect

  User.where("email <> '#{Preference.get_cached(_sid,'master_user_email')}'").find_each do |user|
    gb.list_subscribe({:id=>Preference.get_cached(user.system_id, "mailchimp_all_user_list"), :email_address=>user.email, :double_optin=>false, :merge_vars=>{:GROUPINGS=>[{:name=>"KIT Groups", :groups=>user.groups.collect {|g| g.name}.join(',')}]} }) rescue nil
  end

  redirect_to "/admin/mailchimp"
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/admin/mailchimp_controller.rb', line 14

def index
  throw "No access" unless current_user.admin?

    mailchimp_connect
    @lists = @gibbon.lists["data"]
    
    if params[:make_default]
      # when making a list the default we must make sure that (a) it has a callback hook and (b) it has a group called KIT Groups.  that list must be the only one with those two things.
      Preference.set(_sid, "mailchimp_all_user_list", params[:make_default])
      Activity.add(_sid, "All User List set to #{params[:make_default]}", 0, "Mailchimp")
      @lists.each do |list|
        hooks = @gibbon.list_webhooks({:id=>list["id"]})
        found = false
        hooks.each do |hook|
          if hook["url"] =~ /mailchimp/
            found = true
            break
          end
        end

        if !found && list["id"] == params[:make_default]
#            @gibbon.list_webhook_add({:id=>list["id"], :url=>"http://requestb.in/qwlqxqqw", :campaign=>false})
          @gibbon.list_webhook_add({:id=>list["id"], :url=>"#{Preference.get_cached(_sid, "host")}/api/mailchimp?key=#{Preference.get_cached(_sid, 'mailchimp_callback_key')}", :campaign=>false})
        elsif found && list["id"] != params[:make_default]
          @gibbon.list_webhook_del({:id=>list["id"], :url=>"#{Preference.get_cached(_sid, "host")}/api/mailchimp?key=#{Preference.get_cached(_sid, 'mailchimp_callback_key')}"})
          #@gibbon.list_webhook_del({:id=>list["id"], :url=>"http://requestb.in/qwlqxqqw"})
        end 

        found_group = nil
        begin
          groups = @gibbon.list_interest_groupings({:id=>list["id"]}) 
        rescue 
          logger.debug "**** No interest groupings at the moment"
          groups = nil
        end
        if groups
          groups.each do |group|
            if group["name"] == "KIT Groups"
              found_group = group["id"]
              logger.debug found_group
              break  
            end
          end
          if found_group && list["id"] != params[:make_default]
            @gibbon.list_interest_grouping_del({:id=>list["id"], :grouping_id=>found_group})
          end
        end
        if found_group==nil && list["id"] == params[:make_default]
              logger.debug found_group
          @gibbon.list_interest_grouping_add({:id=>list["id"], :name=>"KIT Groups", :type=>"checkboxes", :groups=>Group.mailchimp_groups(_sid)}) 
        end
      end
      redirect_to "/admin/mailchimp" and return  
    end

    @default_list = Preference.get_cached(_sid, "mailchimp_all_user_list")
end