PageRenderTime 33ms CodeModel.GetById 11ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/mod_admin/resources/resource_admin_edit.erl

http://github.com/zotonic/zotonic
Erlang | 160 lines | 120 code | 18 blank | 22 comment | 1 complexity | c122dfbbdbf63c23f27370cee0f6ae4b MD5 | raw file
  1%% @author Marc Worrell <marc@worrell.nl>
  2%% @copyright 2009-2010 Marc Worrell, Arjan Scherpenisse
  3%% @doc Admin webmachine_resource.
  4
  5%% Copyright 2009-2010 Marc Worrell, Arjan Scherpenisse
  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_admin_edit).
 20-author("Marc Worrell <marc@worrell.nl>").
 21
 22-export([
 23    resource_exists/2,
 24    is_authorized/2,
 25    event/2,
 26    filter_props/1,
 27	ensure_id/1
 28]).
 29
 30-include_lib("resource_html.hrl").
 31
 32%% @todo Change this into "visible" and add a view instead of edit template.
 33is_authorized(ReqData, Context) ->
 34    {Context2, Id} = ensure_id(?WM_REQ(ReqData, Context)),
 35    z_acl:wm_is_authorized([{use, mod_admin}, {view, Id}], Context2).
 36
 37
 38resource_exists(ReqData, Context) ->
 39    {Context2, Id} = ensure_id(?WM_REQ(ReqData, Context)),
 40    case Id of
 41        undefined -> ?WM_REPLY(false, Context2);
 42        _N -> ?WM_REPLY(m_rsc:exists(Id, Context2), Context2)
 43    end.
 44        
 45
 46html(Context) ->
 47    Vars = [
 48        {id, z_context:get(id, Context)}
 49    ],
 50    Html = z_template:render({cat, "admin_edit.tpl"}, Vars, Context),
 51	z_context:output(Html, Context).
 52
 53
 54%% @doc Fetch the (numerical) page id from the request
 55ensure_id(Context) ->
 56    Context2 = z_context:ensure_all(Context),
 57    case z_context:get(id, Context2) of
 58        N when is_integer(N) ->
 59            {Context2, N};
 60        undefined ->
 61            try
 62                {ok, IdN} = m_rsc:name_to_id(z_context:get_q("id", Context2), Context2),
 63                {z_context:set(id, IdN, Context2), IdN}
 64            catch
 65                _:_ -> {Context2, undefined}
 66            end
 67    end.
 68
 69
 70%% @doc Handle the submit of the resource edit form
 71event(#submit{message=rscform}, Context) ->
 72    Post = z_context:get_q_all_noz(Context),
 73    Props = filter_props(Post),
 74    Id = z_convert:to_integer(proplists:get_value("id", Props)),
 75    Props1 = proplists:delete("id", Props),
 76    CatBefore = m_rsc:p(Id, category_id, Context),
 77    case m_rsc:update(Id, Props1, Context) of
 78        {ok, _} -> 
 79            case proplists:is_defined("save_view", Post) of
 80                true ->
 81                    PageUrl = m_rsc:p(Id, page_url, Context),
 82                    z_render:wire({redirect, [{location, PageUrl}]}, Context);
 83                false ->
 84                    case m_rsc:p(Id, category_id, Context) of
 85                        CatBefore ->
 86                            Context1 = z_render:set_value("field-name", m_rsc:p(Id, name, Context), Context),
 87                            Context2 = z_render:set_value("field-uri",  m_rsc:p(Id, uri, Context1), Context1),
 88                            Context3 = z_render:set_value("field-page-path",  m_rsc:p(Id, page_path, Context1), Context2),
 89                            Context4 = z_render:set_value("slug",  m_rsc:p(Id, slug, Context3), Context3),
 90                            Context4b= z_render:set_value("visible_for", integer_to_list(m_rsc:p(Id, visible_for, Context4)), Context4),
 91                            Context5 = case z_convert:to_bool(m_rsc:p(Id, is_protected, Context4b)) of
 92                                true ->  z_render:wire("delete-button", {disable, []}, Context4b);
 93                                false -> z_render:wire("delete-button", {enable, []}, Context4b)
 94                            end,
 95                            Title = ?__(m_rsc:p(Id, title, Context5), Context5),
 96                            Context6 = z_render:growl(["Saved â&#x20AC;&#x153;", Title, "â&#x20AC;?."], Context5),
 97                            case proplists:is_defined("save_duplicate", Post) of
 98                                true ->
 99                                    z_render:wire({dialog_duplicate_rsc, [{id, Id}]}, Context6);
100                                false ->
101                                    Context6
102                            end;
103                        _CatOther ->
104                            z_render:wire({reload, []}, Context)
105                    end
106            end;
107        {error, duplicate_uri} ->
108            z_render:growl_error("Error, duplicate uri. Please change the uri.", Context);
109        {error, duplicate_name} ->
110            z_render:growl_error("Error, duplicate name. Please change the name.", Context);
111        {error, eacces} ->
112            z_render:growl_error("You don't have permission to edit this page.", Context);
113        {error, invalid_query} ->
114            z_render:growl_error("Your search query is invalid. Please correct it before saving.", Context);
115        {error, _Reason} ->
116            z_render:growl_error("Something went wrong. Sorry.", Context)
117    end;
118
119event(#postback{message={reload_media, Opts}}, Context) ->
120    RscId = proplists:get_value(rsc_id, Opts),
121    DivId = proplists:get_value(div_id, Opts),
122    {Html, Context1} = z_template:render_to_iolist({cat, "_edit_media.tpl"}, [{id,RscId},{div_id,DivId}], Context),
123    z_render:update(DivId, Html, Context1);
124
125event(#sort{items=Sorted, drop={dragdrop, {object_sorter, Props}, _, _}}, Context) ->
126    RscId     = proplists:get_value(id, Props),
127    Predicate = proplists:get_value(predicate, Props),
128    EdgeIds   = [ EdgeId || {dragdrop, EdgeId, _, _ElementId} <- Sorted ],
129    m_edge:update_sequence_edge_ids(RscId, Predicate, EdgeIds, Context),
130    Context;
131
132%% Previewing the results of a query in the admin edit
133event(#postback{message={query_preview, Opts}}, Context) ->
134    DivId = proplists:get_value(div_id, Opts),
135    try
136        Q = search_query:parse_query_text(z_context:get_q("triggervalue", Context)),
137        S = z_search:search({'query', Q}, Context),
138        {Html, Context1} = z_template:render_to_iolist("_admin_query_preview.tpl", [{result,S}], Context),
139        z_render:update(DivId, Html, Context1)
140    catch
141        _: {error, {Kind, Arg}} ->
142            z_render:growl_error(["There is an error in your query: ", Kind, " - ", Arg], Context)
143    end.
144
145
146%% @doc Remove some properties that are part of the postback
147filter_props(Fs) ->
148    Remove = [
149        "triggervalue",
150        "postback",
151        "z_trigger_id",
152        "z_pageid",
153        "z_submitter",
154        "trigger_value",
155        "save_view",
156        "save_duplicate",
157        "save_stay"
158    ],
159    lists:foldl(fun(P, Acc) -> proplists:delete(P, Acc) end, Fs, Remove).
160    %[ {list_to_existing_atom(K), list_to_binary(V)} || {K,V} <- Props ].