PageRenderTime 41ms CodeModel.GetById 2ms app.highlight 34ms RepoModel.GetById 2ms app.codeStats 0ms

/priv/sites/zotonic_status/resources/resource_zotonic_status.erl

https://code.google.com/p/zotonic/
Erlang | 136 lines | 91 code | 22 blank | 23 comment | 3 complexity | c62af4999c734537ea52282883e3c959 MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2010 Marc Worrell
  3%% @doc Resource to serve the zotonic fallback site templates.
  4
  5%% Copyright 2010 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
 19-module(resource_zotonic_status).
 20-author("Marc Worrell <marc@worrell.nl>").
 21
 22-export([init/1, service_available/2, charsets_provided/2, content_types_provided/2]).
 23-export([
 24	provide_content/2,
 25	event/2,
 26	updater/2
 27]).
 28
 29-include_lib("webmachine_resource.hrl").
 30-include_lib("include/zotonic.hrl").
 31
 32
 33init(DispatchArgs) -> {ok, DispatchArgs}.
 34
 35service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
 36    Context  = z_context:new(ReqData, ?MODULE),
 37    Context1 = z_context:set(DispatchArgs, Context),
 38    ?WM_REPLY(true, Context1).
 39
 40charsets_provided(ReqData, Context) ->
 41    {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
 42
 43content_types_provided(ReqData, Context) ->
 44    case z_context:get(content_type, Context) of
 45        undefined ->
 46            {[{"text/html", provide_content}], ReqData, Context};
 47        Mime -> 
 48            {[{Mime, provide_content}], ReqData, Context}
 49    end.
 50
 51provide_content(ReqData, Context) ->
 52    Context1 = ?WM_REQ(ReqData, Context),
 53    Context2 = z_context:ensure_all(Context1),
 54    Template = z_context:get(template, Context2),
 55    SitesStatus = z_sites_manager:get_sites_status(),
 56    Vars = [
 57        {has_user, z_acl:user(Context2)},
 58        {configs, [ {Site, z_sites_manager:get_site_config(Site)} || Site <- z_sites_manager:get_sites_all(), Site /= zotonic_status ]},
 59        {sites, SitesStatus}
 60        | z_context:get_all(Context2)
 61    ],
 62    Vars1 = z_notifier:foldl(zotonic_status_init, Vars, Context),
 63    Rendered = z_template:render(Template, Vars1, Context2),
 64    {Output, OutputContext} = z_context:output(Rendered, Context2),
 65    start_stream(SitesStatus, OutputContext),
 66    ?WM_REPLY(Output, OutputContext).
 67
 68
 69%% -----------------------------------------------------------------------------------------------
 70%% Handle all events
 71%% -----------------------------------------------------------------------------------------------
 72
 73event({submit, [], TriggerId, _TargetId}, Context) ->
 74    case z_context:get_q(password, Context) == z_config:get(password) of
 75        true ->
 76            {ok, ContextAuth} = z_auth:logon(1, Context),
 77            z_render:wire({reload, []}, ContextAuth);
 78        false ->
 79            z_render:wire([
 80                        {set_class, [{target,TriggerId},{class,"error-pw"}]}, 
 81                        {set_value, [{target,"password"},{value, ""}]}], Context)
 82    end;
 83event({postback, {logoff, []}, _TriggerId, _TargetId}, Context) ->
 84    z_render:wire({reload, []}, z_auth:logoff(Context));
 85event({postback, {site_start, [{site, Site}]}, _TriggerId, _TargetId}, Context) ->
 86    true = z_auth:is_auth(Context),
 87    z_sites_manager:start(Site),
 88    Context;
 89event({postback, {site_restart, [{site, Site}]}, _TriggerId, _TargetId}, Context) ->
 90    true = z_auth:is_auth(Context),
 91    z_sites_manager:restart(Site),
 92    Context;
 93event({postback, {site_stop, [{site, Site}]}, _TriggerId, _TargetId}, Context) ->
 94    true = z_auth:is_auth(Context),
 95    z_sites_manager:stop(Site),
 96    Context;
 97event({postback, {site_flush, [{site, Site}]}, _TriggerId, _TargetId}, Context) ->
 98    true = z_auth:is_auth(Context),
 99    z:flush(z_context:new(Site)),
100    Context.
101
102
103%% -----------------------------------------------------------------------------------------------
104%% Stream process to update the page when data changes
105%% -----------------------------------------------------------------------------------------------
106
107start_stream(SitesStatus, Context) ->
108    z_session_page:spawn_link(?MODULE, updater, [SitesStatus, Context], Context).
109
110
111% @todo Instead of polling we should observe the system wide notifications (that will be implemented)
112updater(SitesStatus, Context) ->
113    Context1 = z_auth:logon_from_session(Context),
114    timer:sleep(1000),
115    z_sites_manager:upgrade(),
116    NewStatus = z_sites_manager:get_sites_status(),
117    case NewStatus /= SitesStatus of
118        true ->
119            Context2 = render_update(NewStatus, Context1),
120            ?MODULE:updater(NewStatus, Context2);
121        false ->
122            ?MODULE:updater(SitesStatus, Context1)
123    end.
124
125
126render_update(SitesStatus, Context) ->
127    Vars = [
128        {has_user, z_acl:user(Context)},
129        {configs, [ {Site, z_sites_manager:get_site_config(Site)} || Site <- z_sites_manager:get_sites_all(), Site /= zotonic_status ]},
130        {sites, SitesStatus}
131    ],
132    Vars1 = z_notifier:foldl(zotonic_status_init, Vars, Context),
133    Context1 = z_render:update("sites", #render{template="_sites.tpl", vars=Vars1}, Context),
134    z_session_page:add_script(Context1).
135    
136