PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/controllers/search_controller.erl

http://github.com/AF83/ucengine
Erlang | 102 lines | 74 code | 11 blank | 17 comment | 0 complexity | 09a2fe57a8b32f9b323e2b424ecbf8e2 MD5 | raw file
  1. %%
  2. %% U.C.Engine - Unified Collaboration Engine
  3. %% Copyright (C) 2011 af83
  4. %%
  5. %% This program is free software: you can redistribute it and/or modify
  6. %% it under the terms of the GNU Affero General Public License as published by
  7. %% the Free Software Foundation, either version 3 of the License, or
  8. %% (at your option) any later version.
  9. %%
  10. %% This program is distributed in the hope that it will be useful,
  11. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. %% GNU Affero General Public License for more details.
  14. %%
  15. %% You should have received a copy of the GNU Affero General Public License
  16. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. %%
  18. -module(search_controller).
  19. -export([init/0, search/4]).
  20. -include("uce.hrl").
  21. -include_lib("yaws/include/yaws_api.hrl").
  22. init() ->
  23. [#uce_route{method='GET',
  24. path=["search", '_'],
  25. callback={?MODULE, search,
  26. [{"uid", required, string},
  27. {"sid", required, string},
  28. {"searchTerms", "", string},
  29. {"startIndex", 0, integer},
  30. {"startPage", 1, integer},
  31. {"count", 10, integer},
  32. {"order", "asc", string}]}}].
  33. extract_terms(SearchTerms, [Term|Terms], [Default|Defaults]) ->
  34. {ok, Regexp} = re:compile("(^|.* )" ++ Term ++ ":([^ ]+)(.*)"),
  35. case re:run(SearchTerms, Regexp, [{capture, all, list}]) of
  36. {match, [_, Start, Value, End]} ->
  37. [{Term, Value}] ++ extract_terms(Start ++ End, Terms, Defaults);
  38. nomatch ->
  39. [{Term, Default}] ++ extract_terms(SearchTerms, Terms, Defaults);
  40. Error ->
  41. ?ERROR_MSG("search: ~p", [Error]),
  42. throw({error, bad_parameters})
  43. end;
  44. extract_terms(SearchTerms, [], []) ->
  45. [{"keywords", string:tokens(SearchTerms, " ")}].
  46. search(Domain, [], [Uid, Sid, SearchTerms, StartIndex, StartPage, Count, Order], Arg) ->
  47. {ok, true} = uce_presence:assert(Domain, Uid, Sid),
  48. [{"type", Type},
  49. {"start", DateStart},
  50. {"end", DateEnd},
  51. {"location", Location},
  52. {"from", From},
  53. {"to", _To},
  54. {"parent", Parent},
  55. {"keywords", Keywords}] =
  56. extract_terms(SearchTerms,
  57. ["type", "start", "end", "location", "from", "to", "parent"],
  58. ["", "0", infinity, "", "", "", ""]),
  59. {ok, true} = uce_access:assert(Domain, Uid, Location, "event", "list",
  60. [{"from", From}]),
  61. DateEndInt = case DateEnd of
  62. infinity ->
  63. infinity;
  64. A when is_list(A) ->
  65. list_to_integer(A)
  66. end,
  67. Start = uce_paginate:index(Count, StartIndex, StartPage),
  68. {ok, NumTotal, Events} = uce_event:search(Domain,
  69. Location,
  70. Keywords,
  71. From,
  72. string:tokens(Type, ","),
  73. Uid,
  74. list_to_integer(DateStart),
  75. DateEndInt,
  76. Parent,
  77. Start,
  78. Count,
  79. list_to_atom(Order)),
  80. {abs_path, Path} = Arg#arg.req#http_request.path,
  81. Link = lists:concat(["http://", Arg#arg.headers#headers.host, Path]),
  82. Entries = json_helpers:to_json(Domain, Events),
  83. Feed = {struct, [{'link', Link},
  84. {'totalResults', NumTotal},
  85. {'startIndex', StartIndex},
  86. {'itemsPerPage', Count},
  87. {'Query', {struct, [{role, "request"},
  88. {searchTerms, SearchTerms},
  89. {startPage, StartPage}]}},
  90. {'entries', Entries}]},
  91. json_helpers:json(Domain, Feed).