/src/models/m_session.erl

https://code.google.com/p/zotonic/ · Erlang · 51 lines · 17 code · 9 blank · 25 comment · 0 complexity · d82a0cac8ddf5012431b20f510ada439 MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2009 Marc Worrell
  3. %% Date: 2009-11-20
  4. %%
  5. %% @doc Model for accessing the session variables from a template.
  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_session).
  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. ]).
  28. -include_lib("zotonic.hrl").
  29. %% @doc Fetch the value for the key from a model source
  30. %% @spec m_find_value(Key, Source, Context) -> term()
  31. m_find_value(session_id, #m{value=undefined}, Context) ->
  32. z_context:get(session_id, Context);
  33. m_find_value(Key, #m{value=undefined}, Context) ->
  34. z_context:get_session(Key, Context).
  35. %% @doc Transform a m_config value to a list, used for template loops
  36. %% @spec m_to_list(Source, Context) -> List
  37. m_to_list(#m{value=undefined}, _Context) ->
  38. [].
  39. %% @doc Transform a model value so that it can be formatted or piped through filters
  40. %% @spec m_value(Source, Context) -> term()
  41. m_value(#m{value=undefined}, _Context) ->
  42. undefined.