This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod_bulk_commands.erl
137 lines (120 loc) · 5.24 KB
/
mod_bulk_commands.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
%% @author Arthur Clemens <arthur@visiblearea.com>
%% @copyright 2013 Arthur Clemens
%% Date: 2013-12-25
%% @doc Admin add-on to delete or change the state of multiple items at once.
-module(mod_bulk_commands).
-author("Arthur Clemens <arthur@visiblearea.com>").
-mod_title("Bulk admin commands").
-mod_description("Admin add-on to delete or change the state of multiple items at once.").
-mod_prio(500).
-mod_depends([admin]).
-include_lib("zotonic.hrl").
-include_lib("modules/mod_admin/include/admin_menu.hrl").
-export([
init/1,
observe_postback_notify/2
]).
%% Set the config value which pages to add bulk commands to
%% This is a comma-separated list of context variables "selected"
init(Context) ->
PagesConfigSet = m_config:get(?MODULE, pages, Context),
case PagesConfigSet of
undefined -> m_config:set_value(?MODULE, pages, "admin_overview_rsc,admin_media", Context);
_ -> undefined
end,
CommandsConfigSet = m_config:get(?MODULE, commands, Context),
case CommandsConfigSet of
undefined -> m_config:set_value(?MODULE, commands, "delete,published,featured,protected", Context);
_ -> undefined
end,
ok.
observe_postback_notify(#postback_notify{message="bulk_delete"}, Context) ->
bulk_commands(delete, [], Context);
observe_postback_notify(#postback_notify{message="published_true"}, Context) ->
bulk_commands(is_published, [{state, true}], Context);
observe_postback_notify(#postback_notify{message="published_false"}, Context) ->
bulk_commands(is_published, [{state, false}], Context);
observe_postback_notify(#postback_notify{message="featured_true"}, Context) ->
bulk_commands(is_featured, [{state, true}], Context);
observe_postback_notify(#postback_notify{message="featured_false"}, Context) ->
bulk_commands(is_featured, [{state, false}], Context);
observe_postback_notify(#postback_notify{message="protected_true"}, Context) ->
bulk_commands(is_protected, [{state, true}], Context);
observe_postback_notify(#postback_notify{message="protected_false"}, Context) ->
bulk_commands(is_protected, [{state, false}], Context);
observe_postback_notify(_, _Context) ->
undefined.
bulk_commands(Command, Args, Context) ->
Json = z_context:get_q("data", Context),
JsonData = mochijson2:decode(Json),
{ok, Pattern} = re:compile("(\\d+)"),
Actions = lists:foldl(
fun({struct, [{_, HrefBin}, {_, RowIdBin}]}, Acc) ->
Href = z_convert:to_list(HrefBin),
case (re:run(Href, Pattern, [{capture, first, list}])) of
{match,[PageIdStr]} ->
PageId = z_convert:to_integer(PageIdStr),
RowId = z_convert:to_list(RowIdBin),
case m_rsc:exists(PageId, Context) of
true ->
Args1 = lists:append(Args, [
{pageId, PageId},
{rowId, RowId}
]),
Action = action_command(Command, Args1, Context),
[Action | Acc];
false ->
Action = {growl, [{text, ?__("This page does no longer exist.", Context)}, {type, "error"}]},
[Action | Acc]
end;
nomatch -> undefined
end
end,
[done_action(Command, Context)],
JsonData
),
z_render:wire(Actions, Context).
done_action(Command, Context) ->
TableId = z_context:get_q("tableId", Context),
{done, [
{command, Command},
{tableId, TableId}
]}.
action_command(Command, Args, Context) when Command == delete ->
PageId = proplists:get_value(pageId, Args),
case z_acl:rsc_deletable(PageId, Context) of
true ->
RowId = proplists:get_value(rowId, Args),
ok = m_rsc:delete(PageId, Context),
{remove, [{fadeout, true}, {speed, 200}, {target, RowId}]};
false ->
{growl, [{text, ?__(io_lib:format("You are not allowed to delete page ~p. It may be protected.", [PageId]), Context)}, {type, "error"}]}
end;
action_command(Command, Args, Context) when Command == is_published ->
change_state(Command, Args, Context);
action_command(Command, Args, Context) when Command == is_featured ->
change_state(Command, Args, Context);
action_command(Command, Args, Context) when Command == is_protected ->
change_state(Command, Args, Context);
action_command(_, _, Context) ->
Context.
change_state(Command, Args, Context) ->
PageId = proplists:get_value(pageId, Args),
case z_acl:rsc_editable(PageId, Context) of
true ->
State = proplists:get_value(state, Args),
RowId = proplists:get_value(rowId, Args),
Props = [
{Command, State}
],
m_rsc:update(PageId, Props, Context),
{highlight, [{speed, 200}, {rowId, RowId}, {Command, bool_to_integer(State)}]};
false ->
{growl, [{text, ?__(io_lib:format("You are not allowed to change this property of page ~p.", [PageId]), Context)}, {type, "error"}]}
end.
bool_to_integer(V) when V == true ->
1;
bool_to_integer(V) when V == false ->
0;
bool_to_integer(_) ->
0.