/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. %% Copyright 2009-2010 Marc Worrell, Arjan Scherpenisse
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(resource_admin_edit).
  18. -author("Marc Worrell <marc@worrell.nl>").
  19. -export([
  20. resource_exists/2,
  21. is_authorized/2,
  22. event/2,
  23. filter_props/1,
  24. ensure_id/1
  25. ]).
  26. -include_lib("resource_html.hrl").
  27. %% @todo Change this into "visible" and add a view instead of edit template.
  28. is_authorized(ReqData, Context) ->
  29. {Context2, Id} = ensure_id(?WM_REQ(ReqData, Context)),
  30. z_acl:wm_is_authorized([{use, mod_admin}, {view, Id}], Context2).
  31. resource_exists(ReqData, Context) ->
  32. {Context2, Id} = ensure_id(?WM_REQ(ReqData, Context)),
  33. case Id of
  34. undefined -> ?WM_REPLY(false, Context2);
  35. _N -> ?WM_REPLY(m_rsc:exists(Id, Context2), Context2)
  36. end.
  37. html(Context) ->
  38. Vars = [
  39. {id, z_context:get(id, Context)}
  40. ],
  41. Html = z_template:render({cat, "admin_edit.tpl"}, Vars, Context),
  42. z_context:output(Html, Context).
  43. %% @doc Fetch the (numerical) page id from the request
  44. ensure_id(Context) ->
  45. Context2 = z_context:ensure_all(Context),
  46. case z_context:get(id, Context2) of
  47. N when is_integer(N) ->
  48. {Context2, N};
  49. undefined ->
  50. try
  51. {ok, IdN} = m_rsc:name_to_id(z_context:get_q("id", Context2), Context2),
  52. {z_context:set(id, IdN, Context2), IdN}
  53. catch
  54. _:_ -> {Context2, undefined}
  55. end
  56. end.
  57. %% @doc Handle the submit of the resource edit form
  58. event(#submit{message=rscform}, Context) ->
  59. Post = z_context:get_q_all_noz(Context),
  60. Props = filter_props(Post),
  61. Id = z_convert:to_integer(proplists:get_value("id", Props)),
  62. Props1 = proplists:delete("id", Props),
  63. CatBefore = m_rsc:p(Id, category_id, Context),
  64. case m_rsc:update(Id, Props1, Context) of
  65. {ok, _} ->
  66. case proplists:is_defined("save_view", Post) of
  67. true ->
  68. PageUrl = m_rsc:p(Id, page_url, Context),
  69. z_render:wire({redirect, [{location, PageUrl}]}, Context);
  70. false ->
  71. case m_rsc:p(Id, category_id, Context) of
  72. CatBefore ->
  73. Context1 = z_render:set_value("field-name", m_rsc:p(Id, name, Context), Context),
  74. Context2 = z_render:set_value("field-uri", m_rsc:p(Id, uri, Context1), Context1),
  75. Context3 = z_render:set_value("field-page-path", m_rsc:p(Id, page_path, Context1), Context2),
  76. Context4 = z_render:set_value("slug", m_rsc:p(Id, slug, Context3), Context3),
  77. Context4b= z_render:set_value("visible_for", integer_to_list(m_rsc:p(Id, visible_for, Context4)), Context4),
  78. Context5 = case z_convert:to_bool(m_rsc:p(Id, is_protected, Context4b)) of
  79. true -> z_render:wire("delete-button", {disable, []}, Context4b);
  80. false -> z_render:wire("delete-button", {enable, []}, Context4b)
  81. end,
  82. Title = ?__(m_rsc:p(Id, title, Context5), Context5),
  83. Context6 = z_render:growl(["Saved â&#x20AC;&#x153;", Title, "â&#x20AC;?."], Context5),
  84. case proplists:is_defined("save_duplicate", Post) of
  85. true ->
  86. z_render:wire({dialog_duplicate_rsc, [{id, Id}]}, Context6);
  87. false ->
  88. Context6
  89. end;
  90. _CatOther ->
  91. z_render:wire({reload, []}, Context)
  92. end
  93. end;
  94. {error, duplicate_uri} ->
  95. z_render:growl_error("Error, duplicate uri. Please change the uri.", Context);
  96. {error, duplicate_name} ->
  97. z_render:growl_error("Error, duplicate name. Please change the name.", Context);
  98. {error, eacces} ->
  99. z_render:growl_error("You don't have permission to edit this page.", Context);
  100. {error, invalid_query} ->
  101. z_render:growl_error("Your search query is invalid. Please correct it before saving.", Context);
  102. {error, _Reason} ->
  103. z_render:growl_error("Something went wrong. Sorry.", Context)
  104. end;
  105. event(#postback{message={reload_media, Opts}}, Context) ->
  106. RscId = proplists:get_value(rsc_id, Opts),
  107. DivId = proplists:get_value(div_id, Opts),
  108. {Html, Context1} = z_template:render_to_iolist({cat, "_edit_media.tpl"}, [{id,RscId},{div_id,DivId}], Context),
  109. z_render:update(DivId, Html, Context1);
  110. event(#sort{items=Sorted, drop={dragdrop, {object_sorter, Props}, _, _}}, Context) ->
  111. RscId = proplists:get_value(id, Props),
  112. Predicate = proplists:get_value(predicate, Props),
  113. EdgeIds = [ EdgeId || {dragdrop, EdgeId, _, _ElementId} <- Sorted ],
  114. m_edge:update_sequence_edge_ids(RscId, Predicate, EdgeIds, Context),
  115. Context;
  116. %% Previewing the results of a query in the admin edit
  117. event(#postback{message={query_preview, Opts}}, Context) ->
  118. DivId = proplists:get_value(div_id, Opts),
  119. try
  120. Q = search_query:parse_query_text(z_context:get_q("triggervalue", Context)),
  121. S = z_search:search({'query', Q}, Context),
  122. {Html, Context1} = z_template:render_to_iolist("_admin_query_preview.tpl", [{result,S}], Context),
  123. z_render:update(DivId, Html, Context1)
  124. catch
  125. _: {error, {Kind, Arg}} ->
  126. z_render:growl_error(["There is an error in your query: ", Kind, " - ", Arg], Context)
  127. end.
  128. %% @doc Remove some properties that are part of the postback
  129. filter_props(Fs) ->
  130. Remove = [
  131. "triggervalue",
  132. "postback",
  133. "z_trigger_id",
  134. "z_pageid",
  135. "z_submitter",
  136. "trigger_value",
  137. "save_view",
  138. "save_duplicate",
  139. "save_stay"
  140. ],
  141. lists:foldl(fun(P, Acc) -> proplists:delete(P, Acc) end, Fs, Remove).
  142. %[ {list_to_existing_atom(K), list_to_binary(V)} || {K,V} <- Props ].