PageRenderTime 26ms CodeModel.GetById 9ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 1ms

/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
  7%% Copyright 2009 Marc Worrell
  8%%
  9%% Licensed under the Apache License, Version 2.0 (the "License");
 10%% you may not use this file except in compliance with the License.
 11%% You may obtain a copy of the License at
 12%% 
 13%%     http://www.apache.org/licenses/LICENSE-2.0
 14%% 
 15%% Unless required by applicable law or agreed to in writing, software
 16%% distributed under the License is distributed on an "AS IS" BASIS,
 17%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 18%% See the License for the specific language governing permissions and
 19%% limitations under the License.
 20
 21-module(m_site).
 22-author("Marc Worrell <marc@worrell.nl").
 23
 24-behaviour(gen_model).
 25
 26%% interface functions
 27-export([
 28    m_find_value/3,
 29    m_to_list/2,
 30    m_value/2,
 31    all/1,
 32    get/2,
 33    get/3,
 34    get_all/2
 35]).
 36
 37-include_lib("zotonic.hrl").
 38
 39%% @doc Fetch the value for the key from a model source
 40%% @spec m_find_value(Key, Source, Context) -> term()
 41m_find_value(all, #m{value=undefined} = M, _Context) ->
 42    M#m{value=all};
 43m_find_value(Key, #m{value=all}, Context) ->
 44    get_all(Key, Context);
 45m_find_value(hostname_no_port, #m{value=undefined}, Context) ->
 46    z_dispatcher:drop_port(get(hostname, Context));
 47m_find_value(document_domain, #m{value=undefined}, Context) ->
 48    z_context:document_domain(Context);
 49m_find_value(Key, #m{value=undefined}, Context) ->
 50    get(Key, Context).
 51
 52%% @doc Transform a m_config value to a list, used for template loops
 53%% @spec m_to_list(Source, Context) -> All
 54m_to_list(#m{value=undefined}, Context) ->
 55    all(Context).
 56
 57%% @doc Transform a model value so that it can be formatted or piped through filters
 58%% @spec m_value(Source, Context) -> term()
 59m_value(#m{value=undefined}, Context) ->
 60    all(Context).
 61
 62
 63%% @doc Return the complete site configuration
 64all(Context) ->
 65    F = fun() ->
 66        z_sites_manager:get_site_config(Context#context.host)
 67    end,
 68    z_depcache:memo(F, site_config, Context).
 69
 70%% @doc Fetch a key from the site configuration
 71get(Key, Context) when is_atom(Key) ->
 72    case z_depcache:get(site_config, Key, Context) of
 73        {ok, undefined} ->
 74            undefined;
 75        {ok, none} when Key == hostname ->
 76	    case z_context:is_request(Context) of
 77		true -> sanitize_host(z_context:get_req_header("host", Context));
 78		false -> undefined
 79	    end;
 80        {ok, Cs} ->
 81            Cs;
 82        undefined ->
 83	    All = all(Context),
 84	    proplists:get_value(Key, All)
 85    end.
 86    
 87    sanitize_host(Host) ->
 88        sanitize_host(Host, []).
 89
 90    sanitize_host([], Acc) ->
 91        lists:reverse(Acc);
 92    sanitize_host([C|Rest], Acc)
 93        when (C >= $a andalso C =< $z)
 94        orelse (C >= $A andalso C =< $Z)
 95        orelse (C >= $0 andalso C =< $9)
 96        orelse C =:= $-
 97        orelse C =:= $: ->
 98            sanitize_host(Rest, [C|Acc]);
 99    sanitize_host(_, _Acc) ->
100        [].
101
102        
103
104%% @doc Fetch a nested key from the site configuration
105get(site, Key, Context) when is_atom(Key) ->
106    get(Key, Context);
107get(Module, Key, Context) when is_atom(Key) ->
108	case get(Module, Context) of
109		undefined -> undefined;
110		L when is_list(L) -> proplists:get_value(Key, L)
111	end.
112
113%% @doc Fetch all values for a key, eg for the hostalias key.
114get_all(Key, Context) ->
115    proplists:get_all_values(Key, all(Context)).