PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 6%% Copyright 2009 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
20-module(action_admin_config_dialog_config_new).
21-author("Marc Worrell <marc@worrell.nl").
22
23%% interface functions
24-export([
25    render_action/4,
26    event/2
27]).
28
29-include("zotonic.hrl").
30
31render_action(TriggerId, TargetId, Args, Context) ->
32    OnSuccess = proplists:get_all_values(on_success, Args),
33    Postback = {config_new_dialog, OnSuccess},
34	{PostbackMsgJS, _PickledPostback} = z_render:make_postback(Postback, click, TriggerId, TargetId, ?MODULE, Context),
35	{PostbackMsgJS, Context}.
36
37
38%% @doc Fill the dialog with the new group form. The form will be posted back to this module.
39%% @spec event(Event, Context1) -> Context2
40event({postback, {config_new_dialog, OnSuccess}, _TriggerId, _TargetId}, Context) ->
41    Vars = [
42        {delegate, atom_to_list(?MODULE)},
43        {on_success, OnSuccess}
44    ],
45    z_render:dialog("Add configuration key.", "_action_dialog_config_new.tpl", Vars, Context);
46
47
48event({submit, {config_new, Args}, _TriggerId, _TargetId}, Context) ->
49    case z_acl:is_allowed(use, mod_admin_config, Context) of
50        true ->
51            Module = z_string:to_name(z_context:get_q_validated("module", Context)),
52            Key = z_string:to_name(z_context:get_q_validated("key", Context)),
53            Value = z_context:get_q("val", Context, ""),
54            OnSuccess = proplists:get_all_values(on_success, Args),
55
56            case m_config:get_id(Module, Key, Context) of
57                undefined ->
58                    m_config:set_value(Module, Key, Value, Context),
59                    z_render:wire([{dialog_close, []} | OnSuccess], Context);
60                _ ->
61                    z_render:growl_error("The config key already exists, please choose another key name.", Context)
62            end;
63        false ->
64            z_render:growl_error("Only an administrator can add configuration keys.", Context)
65    end.
66