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

/modules/mod_admin_modules/mod_admin_modules.erl

https://code.google.com/p/zotonic/
Erlang | 77 lines | 43 code | 11 blank | 23 comment | 1 complexity | bdfcbaf1530b8fe0cccd22ffb0a23c8e MD5 | raw file
 1%% @author Marc Worrell <marc@worrell.nl>
 2%% @copyright 2009 Marc Worrell
 3%% Date: 2009-06-03
 4%% @doc Add a module management screen to the admin.
 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(mod_admin_modules).
21-author("Marc Worrell <marc@worrell.nl>").
22
23-mod_title("Admin module support").
24-mod_description("Manages modules. Adds an admin interface to activate and deactivate modules.").
25-mod_prio(700).
26
27%% interface functions
28-export([
29    all/1
30]).
31
32-include("zotonic.hrl").
33
34
35%% @spec all(context()) -> ModuleDescriptions
36%% @doc Fetch a list of all modules available, including their description as a propertylist. The module list is sorted
37%% on the name of the module.
38all(Context) ->
39    Active  = z_module_manager:active(Context),
40    Modules = z_module_manager:scan(Context),
41    Descrs  = [ add_sort_key({z_module_manager:prio(M), M, [{is_active, lists:member(M, Active)}, {path, Path} | descr(M)]}) || {M, Path} <- Modules ],
42    lists:sort(Descrs).
43
44
45    add_sort_key({Prio, M, Props}) ->
46        SortKey = case atom_to_list(M) of
47                    "mod_" ++ _ -> {not proplists:get_value(is_active, Props), 2, z_string:to_name(proplists:get_value(mod_title, Props))};
48                    _ -> {not proplists:get_value(is_active, Props), 1, proplists:get_value(mod_title, Props)}
49                  end,
50        {SortKey, Prio, M, Props}.
51        
52
53%% @spec descr(ModuleName) -> proplist()
54%% @doc Return a property list with the title and other attributes of the module.
55descr(Module) ->
56    Descr = case z_module_manager:module_exists(Module) of
57        true ->
58            try
59                erlang:get_module_info(Module, attributes)
60            catch 
61                _M:E -> [{error, E}]
62            end;
63        false ->
64            [{error, enoent}]
65    end,
66    case proplists:get_value(title, Descr) of
67        undefined ->
68            Title = case atom_to_list(Module) of
69                        "mod_" ++ T -> 
70                            string:join(string:tokens(T, "_"), " ");
71                        T ->
72                            string:join(string:tokens(T, "_"), " ")
73                    end,
74            [{title, Title} | Descr];
75        _Title ->
76            Descr
77    end.