Module: Decidim::Admin::NewslettersHelper

Included in:
NewslettersController
Defined in:
decidim-admin/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

#newsletter_attention_callout_announcementObject


134
135
136
137
138
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 134

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

#newsletter_recipients_count_callout_announcementObject


140
141
142
143
144
145
146
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 140

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: body
  }
end

#organization_participatory_space(manifest_name) ⇒ Object


99
100
101
102
103
104
105
106
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 99

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.closed? ? 1 : 0, space.title[current_locale]] }
end

#participatory_space_types_form_object(form_object, space_type) ⇒ Object


15
16
17
18
19
20
21
22
23
24
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 15

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 += 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


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

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


26
27
28
29
30
31
32
33
34
35
36
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 26

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

   :div, class: "#{manifest_name}-block spaces-block-tag cell small-12 medium-6" do
    child_form.select :ids, options_for_select(spaces),
                      { prompt: t("select_recipients_to_deliver.none", scope: "decidim.admin.newsletters"),
                        label: t("activerecord.models.decidim/#{manifest_name.singularize}.other"),
                        include_hidden: false },
                      multiple: true, size: spaces.size > 10 ? 10 : spaces.size, class: "chosen-select"
  end
end

#selective_newsletter_to(newsletter) ⇒ Object


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

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?

   :div do
    concat sent_to_users newsletter
    concat sent_to_spaces newsletter
    concat sent_to_scopes newsletter
  end
end

#sent_to_scopes(newsletter) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 86

def sent_to_scopes(newsletter)
   :p, style: "margin-bottom:0;" do
    concat t("index.segmented_to", scope: "decidim.admin.newsletters", subject: nil)
    if newsletter.sent_scopes.any?
      newsletter.sent_scopes.each do |scope|
        concat (:strong, (translated_attribute scope.name).to_s)
      end
    else
      concat (:strong, t("index.no_scopes", scope: "decidim.admin.newsletters"))
    end
  end
end

#sent_to_spaces(newsletter) ⇒ Object


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 66

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

    html += t("index.segmented_to", scope: "decidim.admin.newsletters", subject: t("activerecord.models.decidim/#{type["manifest_name"].singularize}.other"))
    if type["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: type["ids"]).each do |space|
        html += "<strong>#{translated_attribute space.title}</strong>"
      end
    end
    html += "<br/>"
  end
  html += "</p>"
  html.html_safe
end

#sent_to_users(newsletter) ⇒ Object


56
57
58
59
60
61
62
63
64
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 56

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")
    concat (:strong, t("index.all_users", scope: "decidim.admin.newsletters")) if newsletter.sended_to_all_users?
    concat (:strong, t("index.followers", scope: "decidim.admin.newsletters")) if newsletter.sended_to_followers?
    concat t("index.and", scope: "decidim.admin.newsletters") if newsletter.sended_to_followers? && newsletter.sended_to_participants?
    concat (:strong, t("index.participants", scope: "decidim.admin.newsletters")) if newsletter.sended_to_participants?
  end
end

#space_as_option_for_select(space) ⇒ Object


124
125
126
127
128
129
130
131
132
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 124

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


38
39
40
41
42
43
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 38

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


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'decidim-admin/app/helpers/decidim/admin/newsletters_helper.rb', line 108

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