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