Current section

Files

Jump to
zotonic_mod_admin src actions action_admin_delete_rsc.erl
Raw

src/actions/action_admin_delete_rsc.erl

%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2009-2026 Marc Worrell
%% @doc Delete a resource, no confirmation.
%% @end
%% Copyright 2009-2026 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
-module(action_admin_delete_rsc).
-moduledoc("
Delete a [resource](/id/doc_glossary#term-resource), without confirmation.
Todo
Extend documentation
").
-author("Marc Worrell <marc@worrell.nl").
%% interface functions
-export([
render_action/4,
event/2
]).
-include_lib("zotonic_core/include/zotonic.hrl").
render_action(TriggerId, TargetId, Args, Context) ->
Id = z_convert:to_integer(proplists:get_value(id, Args)),
OnSuccess = proplists:get_all_values(on_success, Args),
Postback = {delete_rsc, Id, OnSuccess},
{PostbackMsgJS, _PickledPostback} = z_render:make_postback(Postback, click, TriggerId, TargetId, ?MODULE, Context),
{PostbackMsgJS, Context}.
%% @doc Delete a resource. After the deletion the user is redirected, and/or some items on the page are faded out.
-spec event(term(), z:context()) -> z:context().
event(#postback{message={delete_rsc, Id, OnSuccess}}, Context) ->
case z_acl:rsc_deletable(Id, Context) of
true ->
ok = m_rsc:delete(Id, Context),
z_render:wire(OnSuccess, Context);
false ->
z_render:growl_error(?__("You are not allowed to delete this page.", Context), Context)
end.