Module: Decidim::Admin::NewslettersHelper

Included in:
NewslettersController
Defined in:
app/helpers/decidim/admin/newsletters_helper.rb

Overview

This module includes helpers to manage newsletters in admin layout

Instance Method Summary collapse

Instance Method Details

#find_verification_types_for_select(organization) ⇒ Object



7
8
9
10
11
12
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 7

def find_verification_types_for_select(organization)
  available_verifications = organization.available_authorizations
  available_verifications.map do |verification_type|
    [t("decidim.authorization_handlers.#{verification_type}.name"), verification_type]
  end
end

#newsletter_attention_callout_announcementObject



154
155
156
157
158
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 154

def newsletter_attention_callout_announcement
  {
    body: t("warning", scope: "decidim.admin.newsletters.select_recipients_to_deliver").html_safe
  }
end

#newsletter_recipients_count_callout_announcementObject



160
161
162
163
164
165
166
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 160

def newsletter_recipients_count_callout_announcement
  spinner = "<span id='recipients_count_spinner' class='loading-spinner hide'></span>"
  body = "#{t("recipients_count", scope: "decidim.admin.newsletters.select_recipients_to_deliver", count: recipients_count_query)} #{spinner}"
  {
    body:
  }
end

#organization_participatory_space(manifest_name) ⇒ Object



119
120
121
122
123
124
125
126
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 119

def organization_participatory_space(manifest_name)
  @organization_participatory_spaces ||= {}
  @organization_participatory_spaces[manifest_name] ||= Decidim
                                                        .find_participatory_space_manifest(manifest_name)
                                                        .participatory_spaces.call(current_organization)
                                                        .published
                                                        .sort_by { |space| [space.try(:closed?) ? 1 : 0, space.title[current_locale]] }
end

#parse_ids(ids) ⇒ Object



168
169
170
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 168

def parse_ids(ids)
  ids.size == 1 && ids.first.is_a?(String) ? ids.first.split.map(&:strip) : ids
end

#participatory_space_title(space_type) ⇒ Object



34
35
36
37
38
39
40
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 34

def participatory_space_title(space_type)
  return unless space_type

   :h4 do
    t("activerecord.models.decidim/#{space_type.manifest_name.singularize}.other")
  end
end

#participatory_space_types_form_object(form_object, space_type) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 22

def participatory_space_types_form_object(form_object, space_type)
  return if spaces_user_can_admin[space_type.manifest_name.to_sym].blank?

  html = ""
  form_object.fields_for "participatory_space_types[#{space_type.manifest_name}]", space_type do |ff|
    html += participatory_space_title(space_type)
    html += ff.hidden_field :manifest_name, value: space_type.manifest_name
    html += select_tag_participatory_spaces(space_type.manifest_name, spaces_for_select(space_type.manifest_name.to_sym), ff)
  end
  html.html_safe
end

#participatory_spaces_for_select(form_object) ⇒ Object



14
15
16
17
18
19
20
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 14

def participatory_spaces_for_select(form_object)
   :div do
    @form.participatory_space_types.each do |space_type|
      concat participatory_space_types_form_object(form_object, space_type)
    end
  end
end

#select_tag_participatory_spaces(manifest_name, spaces, child_form) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 42

def select_tag_participatory_spaces(manifest_name, spaces, child_form)
  return unless spaces

  raw(cell("decidim/admin/multi_select_picker", nil, context: {
             select_id: "#{manifest_name}-spaces-select",
             field_name: "#{child_form.object_name}[ids][]",
             options_for_select: spaces,
             selected_values: selected_options(:participatory_space_types)[manifest_name] || [],
             placeholder: t("select_recipients_to_deliver.select_#{manifest_name}", scope: "decidim.admin.newsletters"),
             class: "mb-2"
           }))
end

#selected_verification_types(newsletter) ⇒ Object



172
173
174
175
176
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 172

def selected_verification_types(newsletter)
  newsletter.sent_to_users_with_verification_types&.map do |type|
    I18n.t("decidim.authorization_handlers.#{type}.name")
  end&.join(", ")
end

#selective_newsletter_to(newsletter) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 62

def selective_newsletter_to(newsletter)
  return (:strong, t("index.not_sent", scope: "decidim.admin.newsletters"), class: "text-warning") unless newsletter.sent?
  return (:strong, t("index.all_users", scope: "decidim.admin.newsletters"), class: "text-success") if newsletter.sent? && newsletter.extended_data.blank?
  return sent_to_verified_users(newsletter) if newsletter.sent_to_verified_users?

   :div do
    concat sent_to_users newsletter
    concat sent_to_spaces newsletter
  end
end

#sent_to_spaces(newsletter) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 97

def sent_to_spaces(newsletter)
  html = "<p style='margin-bottom:0;'> "
  newsletter.sent_to_participatory_spaces.try(:each) do |type|
    next if type["ids"].blank?

    ids = parse_ids(type["ids"])

    html += t("index.segmented_to", scope: "decidim.admin.newsletters", subject: t("activerecord.models.decidim/#{type["manifest_name"].singularize}.other"))
    if ids.include?("all")
      html += "<strong> #{t("index.all", scope: "decidim.admin.newsletters")} </strong>"
    else
      Decidim.find_participatory_space_manifest(type["manifest_name"].to_sym)
             .participatory_spaces.call(current_organization).where(id: ids).each do |space|
        html += "<strong>#{decidim_escape_translated(space.title)}</strong>"
      end
    end
    html += "<br/>"
  end
  html += "</p>"
  html.html_safe
end

#sent_to_users(newsletter) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 73

def sent_to_users(newsletter)
   :p, style: "margin-bottom:0;" do
    concat (:strong, t("index.has_been_sent_to", scope: "decidim.admin.newsletters"), class: "text-success")

    recipients = []

    recipients << (:strong, t("index.all_users", scope: "decidim.admin.newsletters")) if newsletter.sent_to_all_users?
    recipients << (:strong, t("index.verified_users", scope: "decidim.admin.newsletters")) if newsletter.sent_to_verified_users?
    recipients << (:strong, t("index.followers", scope: "decidim.admin.newsletters")) if newsletter.sent_to_followers?
    recipients << (:strong, t("index.participants", scope: "decidim.admin.newsletters")) if newsletter.sent_to_participants?
    recipients << (:strong, t("index.private_members", scope: "decidim.admin.newsletters")) if newsletter.sent_to_private_members?

    concat recipients.join(t("index.and", scope: "decidim.admin.newsletters")).html_safe
  end
end

#sent_to_verified_users(newsletter) ⇒ Object



89
90
91
92
93
94
95
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 89

def sent_to_verified_users(newsletter)
   :p, style: "margin-bottom:0;" do
    concat (:strong, t("index.has_been_sent_to", scope: "decidim.admin.newsletters"), class: "text-success")
    concat (:strong, t("index.verified_users", scope: "decidim.admin.newsletters"))
    concat (:p, t("index.verification_types", scope: "decidim.admin.newsletters", types: selected_verification_types(newsletter)))
  end
end

#space_as_option_for_select(space) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 144

def space_as_option_for_select(space)
  return unless space

  [
    translated_attribute(space.title),
    space.id,
    { class: space.try(:closed?) ? "red" : "green", title: translated_attribute(space.title).to_s }
  ]
end

#spaces_for_select(manifest_name) ⇒ Object



55
56
57
58
59
60
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 55

def spaces_for_select(manifest_name)
  return unless Decidim.participatory_space_manifests.map(&:name).include?(manifest_name)
  return spaces_user_can_admin[manifest_name] unless current_user.admin?

  [[I18n.t("select_recipients_to_deliver.all_spaces", scope: "decidim.admin.newsletters"), "all"]] + spaces_user_can_admin[manifest_name]
end

#spaces_user_can_adminObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/helpers/decidim/admin/newsletters_helper.rb', line 128

def spaces_user_can_admin
  @spaces_user_can_admin ||= {}
  Decidim.participatory_space_manifests.each do |manifest|
    organization_participatory_space(manifest.name)&.each do |space|
      next unless space.admins.exists?(id: current_user.id)

      @spaces_user_can_admin[manifest.name] ||= []
      space_as_option_for_select_data = space_as_option_for_select(space)
      @spaces_user_can_admin[manifest.name] << space_as_option_for_select_data unless @spaces_user_can_admin[manifest.name].detect do |x|
        x == space_as_option_for_select_data
      end
    end
  end
  @spaces_user_can_admin
end