PageRenderTime 36ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/modules/mod_mailinglist/scomps/scomp_mailinglist_mailinglist_subscribe.erl

https://code.google.com/p/zotonic/
Erlang | 115 lines | 89 code | 8 blank | 18 comment | 0 complexity | 4241d2e26535928752926a396736fb68 MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2010 Marc Worrell
  3%%
  4%% Show a form to subscribe to a mailinglist.  Prefill the form with the account details
  5%% of the current user (if any).
  6
  7%% Copyright 2010 Marc Worrell
  8%%
  9%% Licensed under the Apache License, Version 2.0 (the "License");
 10%% you may not use this file except in compliance with the License.
 11%% You may obtain a copy of the License at
 12%% 
 13%%     http://www.apache.org/licenses/LICENSE-2.0
 14%% 
 15%% Unless required by applicable law or agreed to in writing, software
 16%% distributed under the License is distributed on an "AS IS" BASIS,
 17%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18%% See the License for the specific language governing permissions and
 19%% limitations under the License.
 20
 21-module(scomp_mailinglist_mailinglist_subscribe).
 22-behaviour(gen_scomp).
 23
 24-export([vary/2, render/3, event/2]).
 25
 26-include("zotonic.hrl").
 27
 28vary(_Params, _Context) -> nocache.
 29render(Params, _Vars, Context) ->
 30    Id = m_rsc:rid(proplists:get_value(id, Params), Context),
 31    RcptId = proplists:get_value(recipient_id, Params),
 32    InAdmin = proplists:get_value(in_admin, Params, false),
 33    Template = proplists:get_value(template, Params, "_scomp_mailinglist_subscribe.tpl"),
 34    UserId = z_acl:user(Context),
 35    Props = [
 36        {id, Id},
 37        {recipient_id, RcptId},
 38        {user_id, UserId},
 39        {in_admin, InAdmin},
 40        {delegate, ?MODULE}
 41    ],
 42    {ok, z_template:render(Template, Props, Context)}.
 43
 44
 45event({submit, {recipient_add, Props}, _TriggerId, _TargetId}, Context) ->
 46    Id = proplists:get_value(id, Props),
 47    InAdmin = z_convert:to_bool(proplists:get_value(in_admin, Props)),
 48	case z_acl:rsc_visible(Id, Context) of
 49		true ->
 50			Email = z_context:get_q_validated(email, Context),
 51			Notification = case not InAdmin orelse z_convert:to_bool(z_context:get_q(send_welcome, Context)) of
 52				true -> send_welcome;
 53				false -> silent
 54			end,
 55			RecipientProps = [
 56			    {user_id, undefined},
 57			    {in_admin, InAdmin},
 58			    {name_first, z_string:trim(z_context:get_q(name_first, Context, ""))},
 59			    {name_surname_prefix, z_string:trim(z_context:get_q(name_surname_prefix, Context, ""))},
 60			    {name_surname, z_string:trim(z_context:get_q(name_surname, Context, ""))}
 61			],
 62			case m_mailinglist:insert_recipient(Id, Email, RecipientProps, Notification, Context) of
 63				ok -> 
 64				    case InAdmin of
 65				        true ->
 66        					z_render:wire([	{growl, [{text, "Added the recipient."}]}, 
 67        									{dialog_close, []},
 68        									{reload, []}], Context);
 69        				false ->
 70					        z_render:wire([ {slide_fade_in, [{target, "mailinglist_subscribe_done"}]},
 71					                        {slide_fade_out, [{target, "mailinglist_subscribe_form"}]}], Context)
 72        			end;
 73				{error, _Reason} ->
 74				    case InAdmin of
 75				        true ->
 76					        z_render:growl_error("Could not add the recipient.", Context);
 77					    false ->
 78					        z_render:wire([ {slide_fade_in, [{target, "mailinglist_subscribe_error"}]}], Context)
 79					end
 80			end;
 81		false ->
 82		    case InAdmin of
 83		        true ->
 84			        z_render:growl_error("You are not allowed to add or enable recipients.", Context);
 85			    false ->
 86			        z_render:wire([ {slide_fade_in, [{target, "mailinglist_subscribe_error"}]}], Context)
 87			end
 88	end;
 89
 90event({submit, {recipient_edit, Props}, _TriggerId, _TargetId}, Context) ->
 91    Id = proplists:get_value(id, Props),
 92    RcptId = proplists:get_value(recipient_id, Props),
 93    InAdmin = z_convert:to_bool(proplists:get_value(in_admin, Props)),
 94	case z_acl:rsc_visible(Id, Context) of
 95		true ->
 96			RecipientProps = [
 97                {email, z_context:get_q_validated(email, Context)},
 98			    {user_id, undefined},
 99			    {in_admin, InAdmin},
100			    {name_first, z_string:trim(z_context:get_q(name_first, Context, ""))},
101			    {name_surname_prefix, z_string:trim(z_context:get_q(name_surname_prefix, Context, ""))},
102			    {name_surname, z_string:trim(z_context:get_q(name_surname, Context, ""))}
103			],
104            ok = m_mailinglist:update_recipient(RcptId, RecipientProps, Context),
105            z_render:wire([	{growl, [{text, "Updated the recipient."}]}, 
106                            {dialog_close, []},
107                            {reload, []}], Context);
108		false ->
109		    case InAdmin of
110		        true ->
111			        z_render:growl_error("You are not allowed to edit recipients.", Context);
112			    false ->
113			        z_render:wire([ {slide_down, [{target, "mailinglist_subscribe_error"}]}], Context)
114			end
115	end.