/modules/mod_admin_config/actions/action_admin_config_dialog_config_new.erl

https://code.google.com/p/zotonic/ · Erlang · 66 lines · 35 code · 11 blank · 20 comment · 0 complexity · 6611c7e1b025d865f407443260b0e1f2 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %% Date: 2009-08-07
  4. %% @doc Open a dialog with some fields to make a new configuration.
  5. %% Copyright 2009 Marc Worrell
  6. %%
  7. %% Licensed under the Apache License, Version 2.0 (the "License");
  8. %% you may not use this file except in compliance with the License.
  9. %% You may obtain a copy of the License at
  10. %%
  11. %% http://www.apache.org/licenses/LICENSE-2.0
  12. %%
  13. %% Unless required by applicable law or agreed to in writing, software
  14. %% distributed under the License is distributed on an "AS IS" BASIS,
  15. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. %% See the License for the specific language governing permissions and
  17. %% limitations under the License.
  18. -module(action_admin_config_dialog_config_new).
  19. -author("Marc Worrell <marc@worrell.nl").
  20. %% interface functions
  21. -export([
  22. render_action/4,
  23. event/2
  24. ]).
  25. -include("zotonic.hrl").
  26. render_action(TriggerId, TargetId, Args, Context) ->
  27. OnSuccess = proplists:get_all_values(on_success, Args),
  28. Postback = {config_new_dialog, OnSuccess},
  29. {PostbackMsgJS, _PickledPostback} = z_render:make_postback(Postback, click, TriggerId, TargetId, ?MODULE, Context),
  30. {PostbackMsgJS, Context}.
  31. %% @doc Fill the dialog with the new group form. The form will be posted back to this module.
  32. %% @spec event(Event, Context1) -> Context2
  33. event({postback, {config_new_dialog, OnSuccess}, _TriggerId, _TargetId}, Context) ->
  34. Vars = [
  35. {delegate, atom_to_list(?MODULE)},
  36. {on_success, OnSuccess}
  37. ],
  38. z_render:dialog("Add configuration key.", "_action_dialog_config_new.tpl", Vars, Context);
  39. event({submit, {config_new, Args}, _TriggerId, _TargetId}, Context) ->
  40. case z_acl:is_allowed(use, mod_admin_config, Context) of
  41. true ->
  42. Module = z_string:to_name(z_context:get_q_validated("module", Context)),
  43. Key = z_string:to_name(z_context:get_q_validated("key", Context)),
  44. Value = z_context:get_q("val", Context, ""),
  45. OnSuccess = proplists:get_all_values(on_success, Args),
  46. case m_config:get_id(Module, Key, Context) of
  47. undefined ->
  48. m_config:set_value(Module, Key, Value, Context),
  49. z_render:wire([{dialog_close, []} | OnSuccess], Context);
  50. _ ->
  51. z_render:growl_error("The config key already exists, please choose another key name.", Context)
  52. end;
  53. false ->
  54. z_render:growl_error("Only an administrator can add configuration keys.", Context)
  55. end.