Skip to content

Commit

Permalink
Added support for subscribing persons in the database to a mailinglist.
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Feb 12, 2011
1 parent c02ce6c commit 415458d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 53 deletions.
48 changes: 30 additions & 18 deletions modules/mod_mailinglist/mod_mailinglist.erl
Expand Up @@ -3,7 +3,7 @@
%% @date 2009-11-23
%% @doc Mailinglist implementation. Enables to send pages to a list of recipients.

%% Copyright 2009 Marc Worrell
%% Copyright 2009-2011 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -267,14 +267,15 @@ send_mailing(ListId, PageId, Context) ->
end.

send_mailing_process(ListId, PageId, Context) ->
Recipients = m_mailinglist:get_enabled_recipients(ListId, Context),
{Direct,Queued} = split_list(20, Recipients),
FromEmail = case m_rsc:p(ListId, mailinglist_reply_to, Context) of
Empty when Empty =:= undefined; Empty =:= <<>> ->
z_convert:to_list(m_config:get_value(mod_emailer, email_from, Context));
RT ->
z_convert:to_list(RT)
end,
Recipients = m_mailinglist:get_enabled_recipients(ListId, Context),
SubscribersOf = m_edge:subjects(ListId, subscriberof, Context),
{Direct,Queued} = split_list(20, Recipients ++ SubscribersOf),
FromEmail = case m_rsc:p(ListId, mailinglist_reply_to, Context) of
Empty when Empty =:= undefined; Empty =:= <<>> ->
z_convert:to_list(m_config:get_value(mod_emailer, email_from, Context));
RT ->
z_convert:to_list(RT)
end,
FromName = case m_rsc:p(ListId, mailinglist_sender_name, Context) of
undefined -> [];
<<>> -> [];
Expand All @@ -285,15 +286,26 @@ send_mailing_process(ListId, PageId, Context) ->
{id,PageId}, {list_id, ListId}, {email_from, From}
],

[
z_email:send_render(Email, {cat, "mailing_page.tpl"}, [{email,Email}|Options], Context)
|| Email <- Direct
],
[
z_email:sendq_render(Email, {cat, "mailing_page.tpl"}, [{email,Email}|Options], Context)
|| Email <- Queued
],
ok.
[ send(true, Email, Options, Context) || Email <- Direct ],
[ send(false, Email, Options, Context) || Email <- Queued ],
ok.


send(_IsDirect, undefined, _Options, _Context) ->
skip;
send(IsDirect, Id, Options, Context) when is_integer(Id) ->
send(IsDirect, m_rsc:p_no_acl(Id, email, Context), [{recipient_id,Id}|Options], Context);
send(IsDirect, Email, Options, Context) ->
case z_convert:to_list(z_string:trim(Email)) of
[] -> skip;
Email1 ->
Options1 = [{email,Email1}|Options],
case IsDirect of
true -> z_email:send_render(Email, {cat, "mailing_page.tpl"}, Options1, Context);
false -> z_email:send_qrender(Email, {cat, "mailing_page.tpl"}, Options1, Context)
end
end.


split_list(N, List) ->
split_list(N, List, []).
Expand Down
32 changes: 18 additions & 14 deletions modules/mod_mailinglist/models/m_mailinglist.erl
Expand Up @@ -184,7 +184,7 @@ get_confirm_key(ConfirmKey, Context) ->

%% @doc Insert a recipient in the mailing list, send a message to the recipient when needed.
insert_recipient(ListId, Email, WelcomeMessageType, Context) ->
insert_recipient(ListId, Email, [{user_id, undefined}], WelcomeMessageType, Context).
insert_recipient(ListId, Email, [], WelcomeMessageType, Context).

insert_recipient(ListId, Email, Props, WelcomeMessageType, Context) ->
true = z_acl:rsc_visible(ListId, Context),
Expand Down Expand Up @@ -369,22 +369,21 @@ install(Context) ->
true ->
case z_db:column_names(mailinglist_recipient, Context) of
[confirm_key,email,id,is_enabled,mailinglist_id,timestamp] ->
z_db:q("alter table mailinglist_recipient
add column user_id int default null,
add column props bytea,
add constraint fk_mailinglist_user_id FOREIGN KEY (user_id) REFERENCES rsc (id) ON UPDATE CASCADE ON DELETE CASCADE",
Context),
z_db:equery("create index fk_mailinglist_recipient_user_id on mailinglist_recipient(user_id)", Context),
z_db:q("alter table mailinglist_recipient
add column props bytea", Context),
z_db:flush(Context),
ok;
[confirm_key,email,id,is_enabled,mailinglist_id,props,timestamp,user_id] ->
z_db:q("alter table mailinglist_recipient
drop column user_id int default null", Context),
z_db:flush(Context);
[confirm_key,email,id,is_enabled,mailinglist_id,props,timestamp] ->
ok
end;
false ->
z_db:q("
CREATE TABLE mailinglist_recipient (
id serial NOT NULL,
user_id int default NULL,
mailinglist_id INT NOT NULL,
email character varying (200) NOT NULL,
is_enabled boolean NOT NULL default true,
Expand All @@ -397,14 +396,9 @@ install(Context) ->
CONSTRAINT confirm_key_key UNIQUE (confirm_key),
CONSTRAINT fk_mailinglist_id FOREIGN KEY (mailinglist_id)
REFERENCES rsc (id)
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT fk_mailinglist_user_id FOREIGN KEY (user_id)
REFERENCES rsc (id)
ON UPDATE CASCADE ON DELETE CASCADE
ON UPDATE CASCADE ON DELETE CASCADE
)", Context),

z_db:equery("create index fki_mailinglist_recipient_usr_id on mailinglist_recipient(user_id)", Context),

z_db:q("
CREATE TABLE mailinglist_scheduled (
page_id INT NOT NULL,
Expand All @@ -428,6 +422,16 @@ datamodel() ->
{summary, "Mailing lists are used to send pages to groups of people."}
]}
]},

% Any resource with an e-mail address can be a subscriber of a mailinglist
{predicates, [
{subscriberof,
[{title, <<"Subscriber of">>}],
[{person, mailinglist}, {location, mailinglist}]},
{exsubscriberof,
[{title, <<"Ex-subscriber of">>}],
[{person, mailinglist}, {location, mailinglist}]}
]},

{resources, [
{mailinglist_test, mailinglist, [
Expand Down
7 changes: 3 additions & 4 deletions modules/mod_mailinglist/templates/admin_mailinglist.tpl
Expand Up @@ -11,9 +11,8 @@
{% button text=_"New mailing list" action={dialog_new_rsc cat="mailinglist"} %}

<hr class="clear" />
<p>
{_ Any page can be send as a mailing. You can send a mailing from any edit page, here you can add or remove mailing lists and recipients. _}
</p>
<p>{_ Any page can be send as a mailing. You can send a mailing from any edit page, here you can add or remove mailing lists and recipients. _}<br/>
{_ Recipients can be e-mail only via a simple signup form, or subscribed person pages in the system. _}</p>

<h3 class="above-list ">{_ Mailing list overview _}</h3>
<ul class="short-list">
Expand All @@ -31,7 +30,7 @@
<span class="zp-20">{{ title|default:"untitled" }}</span>
<span class="zp-40">{{ m.rsc[id].summary|default:"-" }}</span>
{% with m.mailinglist.stats[id] as stats %}
<span class="zp-10">{{ stats[1]|format_number }}</span>
<span class="zp-10">{{ (stats[1] + id.s.subscriberof|length)|format_number }}</span>
<span class="zp-10">{{ stats[2]|length|format_number }}</span>
{% endwith %}
<span class="zp-20">
Expand Down
Expand Up @@ -19,7 +19,10 @@
{% button text=_"Upload file" title=_"Upload a list of recipients." action={dialog_open template="_dialog_mailinglist_recipients_upload.tpl" id=id} %}

<hr class="clear" />
<p>{_ All recipients of the mailing list. You can upload or download this list, which must be a file with one e-mail address per line. _}<br/><a href="{% url admin_edit_rsc id=id %}">{_ Edit the mailing list &raquo; _}</a></p>
<p>{_ All recipients of the mailing list. You can upload or download this list, which must be a file with one e-mail address per line. _}<br/>
<a href="{% url admin_edit_rsc id=id %}">{_ Edit the mailing list &raquo; _}</a><br/>
<a href="{% url admin_referrers id=id %}">{_ See the _} {{ id.s.subscriberof|length }} {_ subscribing persons &raquo; _}</a>.
</p>
{% with m.search.paged[{mailinglist_recipients id=id pagelen=150 page=q.page}] as recipients %}
{% pager result=recipients dispatch="admin_mailinglist_recipients" id=id qargs %}
{% for list in recipients|vsplit_in:3 %}
Expand Down
4 changes: 2 additions & 2 deletions modules/mod_mailinglist/templates/email_page.tpl
@@ -1,10 +1,10 @@
{% extends "email_base.tpl" %}

{% block title %}{{ m.rsc[id].title }}{% endblock %}
{% block title %}{{ id.title }}{% endblock %}

{% block body %}
<p>{_ This mail was sent to you because someone visiting _} {{ m.site.hostname }} {_ thought you would be interested. _}</p>
<p>{_ You can _} <a href="{{ m.rsc[id].page_url }}">{_ read the full article online. _}</a></p>
<p>{_ You can _} <a href="http://{{ m.site.hostname }}{{ id.page_url }}">{_ read the full article online. _}</a></p>

<hr/>

Expand Down
25 changes: 11 additions & 14 deletions modules/mod_mailinglist/templates/mailing_page.tpl
@@ -1,27 +1,24 @@
{% extends "email_base.tpl" %}

{% block title %}{{ m.rsc[id].title }}{% endblock %}
{% block title %}{{ id.title }}{% endblock %}

{% block body %}
<p style="text-align: center"><a href="{{ m.rsc[id].page_url }}">{_ Click here when you can’t read the message below. _}</a></p>
<p style="text-align: center"><a href="http://{{ m.site.hostname }}{{ id.page_url }}">{_ Click here when you can’t read the message below. _}</a></p>

<hr/>

<h1>{{ m.rsc[id].title }}</h1>
<h1>{{ id.title }}</h1>

{% if m.rsc[id].body %}
<p><strong>{{ m.rsc[id].summary|linebreaksbr }}</strong></p>
{{ m.rsc[id].body }}
{% if id.body %}
{% if id.summary %}
<p><strong>{{ id.summary|linebreaksbr }}</strong></p>
{% endif %}
{{ id.body }}
{% else %}
<p>{{ m.rsc[id].summary|linebreaksbr }}</p>
<p>{{ id.summary|linebreaksbr }}</p>
{% endif %}

<p><a href="{{ m.rsc[id].page_url }}">{_ Read this page on the web. _}</a></p>

<hr/>

{% with m.mailinglist.subscription[list_id][email] as sub %}
<p style="color: #666">{_ You received this mail because you are subscribed to the mailing list _} <a href="{{ m.rsc[list_id].page_url }}">{{ m.rsc[list_id].title }}</a>. <a href="{% url mailinglist_unsubscribe confirm_key=sub.confirm_key %}">{_ Please unsubscribe _}</a> {_ when you don't want to receive any further mail from this list. _} </p>
{% endwith %}
<p><a href="http://{{ m.site.hostname }}{{ m.rsc[id].page_url }}">{_ Read this page on the web. _}</a></p>

{% include "_mailing_footer.tpl" %}
{% endblock %}

0 comments on commit 415458d

Please sign in to comment.