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