/src/models/m_site.erl

https://code.google.com/p/zotonic/ · Erlang · 115 lines · 69 code · 17 blank · 29 comment · 1 complexity · 3d9a63d229f8e45ca4112beffa98aae9 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %% Date: 2009-04-09
  4. %%
  5. %% @doc Model for the zotonic site configuration
  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. -module(m_site).
  20. -author("Marc Worrell <marc@worrell.nl").
  21. -behaviour(gen_model).
  22. %% interface functions
  23. -export([
  24. m_find_value/3,
  25. m_to_list/2,
  26. m_value/2,
  27. all/1,
  28. get/2,
  29. get/3,
  30. get_all/2
  31. ]).
  32. -include_lib("zotonic.hrl").
  33. %% @doc Fetch the value for the key from a model source
  34. %% @spec m_find_value(Key, Source, Context) -> term()
  35. m_find_value(all, #m{value=undefined} = M, _Context) ->
  36. M#m{value=all};
  37. m_find_value(Key, #m{value=all}, Context) ->
  38. get_all(Key, Context);
  39. m_find_value(hostname_no_port, #m{value=undefined}, Context) ->
  40. z_dispatcher:drop_port(get(hostname, Context));
  41. m_find_value(document_domain, #m{value=undefined}, Context) ->
  42. z_context:document_domain(Context);
  43. m_find_value(Key, #m{value=undefined}, Context) ->
  44. get(Key, Context).
  45. %% @doc Transform a m_config value to a list, used for template loops
  46. %% @spec m_to_list(Source, Context) -> All
  47. m_to_list(#m{value=undefined}, Context) ->
  48. all(Context).
  49. %% @doc Transform a model value so that it can be formatted or piped through filters
  50. %% @spec m_value(Source, Context) -> term()
  51. m_value(#m{value=undefined}, Context) ->
  52. all(Context).
  53. %% @doc Return the complete site configuration
  54. all(Context) ->
  55. F = fun() ->
  56. z_sites_manager:get_site_config(Context#context.host)
  57. end,
  58. z_depcache:memo(F, site_config, Context).
  59. %% @doc Fetch a key from the site configuration
  60. get(Key, Context) when is_atom(Key) ->
  61. case z_depcache:get(site_config, Key, Context) of
  62. {ok, undefined} ->
  63. undefined;
  64. {ok, none} when Key == hostname ->
  65. case z_context:is_request(Context) of
  66. true -> sanitize_host(z_context:get_req_header("host", Context));
  67. false -> undefined
  68. end;
  69. {ok, Cs} ->
  70. Cs;
  71. undefined ->
  72. All = all(Context),
  73. proplists:get_value(Key, All)
  74. end.
  75. sanitize_host(Host) ->
  76. sanitize_host(Host, []).
  77. sanitize_host([], Acc) ->
  78. lists:reverse(Acc);
  79. sanitize_host([C|Rest], Acc)
  80. when (C >= $a andalso C =< $z)
  81. orelse (C >= $A andalso C =< $Z)
  82. orelse (C >= $0 andalso C =< $9)
  83. orelse C =:= $-
  84. orelse C =:= $: ->
  85. sanitize_host(Rest, [C|Acc]);
  86. sanitize_host(_, _Acc) ->
  87. [].
  88. %% @doc Fetch a nested key from the site configuration
  89. get(site, Key, Context) when is_atom(Key) ->
  90. get(Key, Context);
  91. get(Module, Key, Context) when is_atom(Key) ->
  92. case get(Module, Context) of
  93. undefined -> undefined;
  94. L when is_list(L) -> proplists:get_value(Key, L)
  95. end.
  96. %% @doc Fetch all values for a key, eg for the hostalias key.
  97. get_all(Key, Context) ->
  98. proplists:get_all_values(Key, all(Context)).