Skip to content
This repository
Browse code

Only make queries with the 'is_query_live' flag a live query. Also so…

…me extra updates to m_rsc_update for later REST API work.

Fixes #344
  • Loading branch information...
commit 3a51c400707eb5f7551516dae3b1bd5dcaf41a88 1 parent 6b70f53
Marc Worrell authored June 14, 2012
19  modules/mod_admin/templates/_admin_edit_content.query.tpl
@@ -15,11 +15,20 @@
15 15
 		<a href="http://zotonic.com/documentation/761/the-query-search-model">documentation on the query arguments</a> on the Zotonic website. _}
16 16
 	</div>
17 17
 
18  
-	<div class="form-item clearfix">
19  
-		<label for="query">{_ Query _}</label>
20  
-		<textarea id="{{ #query }}" name="query" rows="15">{{ r.query }}</textarea>
21  
-		{% wire id=#query type="change" postback={query_preview rsc_id=id div_id=#querypreview} delegate="resource_admin_edit" %}
22  
-	</div>
  18
+    <div class="control-group">
  19
+    	<label class="control-label" for="query">{_ Query _}</label>
  20
+    	<div class="controls">
  21
+    	    <textarea id="{{ #query }}" name="query" rows="15">{{ r.query }}</textarea>
  22
+    		{% wire id=#query type="change" postback={query_preview rsc_id=id div_id=#querypreview} delegate="resource_admin_edit" %}
  23
+    	</div>
  24
+    </div>
  25
+    
  26
+    <div class="control-group">
  27
+    	<label class="checkbox">
  28
+    	    <input type="checkbox" id="is_query_live" name="is_query_live" {% if r.is_query_live %}checked{% endif %}/>
  29
+    	    {_ Live query, send notifications when matching items are updated or inserted. _}
  30
+    	</label>
  31
+    </div>
23 32
 
24 33
 	<h3>{_ Query preview _}</h3>
25 34
 	<div class="query-results" id="{{ #querypreview }}">
1  modules/mod_search/support/search_query.erl
@@ -38,6 +38,7 @@ search(Query, Context) ->
38 38
     case parse_query(Query1, Context, Start) of
39 39
         [] -> #search_result{};
40 40
         #search_result{} = Result -> Result;
  41
+        Start -> #search_result{};
41 42
         R ->
42 43
             %% add any sort terms
43 44
             R1 = parse_sort(Query1, R),
9  modules/mod_search/support/search_query_notify.erl
@@ -46,8 +46,13 @@ watches_update(Id, Watches, Context) ->
46 46
         undefined ->
47 47
             proplists:delete(Id, Watches);
48 48
         Q ->
49  
-            Props = search_query:parse_query_text(Q),
50  
-            [{Id, Props} | proplists:delete(Id, Watches)]
  49
+            case z_convert:to_bool(m_rsc:p(Id, is_query_live, Context)) of
  50
+                true ->
  51
+                    Props = search_query:parse_query_text(Q),
  52
+                    [{Id, Props} | proplists:delete(Id, Watches)];
  53
+                false ->
  54
+                    proplists:delete(Id, Watches)
  55
+            end
51 56
     end.
52 57
 
53 58
 watches_remove(Id, Watches, _Context) ->
80  src/models/m_rsc_update.erl
@@ -160,6 +160,7 @@ update(Id, Props, Options, Context) when is_integer(Id) orelse Id == insert_rsc
160 160
     EscapeTexts = proplists:get_value(escape_texts, Options, true),
161 161
     AclCheck = proplists:get_value(acl_check, Options, true),
162 162
     IsImport = proplists:is_defined(is_import, Options),
  163
+    % IsTransMerge = proplists:is_defined(is_trans_merge, Options, false),
163 164
     IsEditable = Id == insert_rsc orelse z_acl:rsc_editable(Id, Context) orelse not(AclCheck),
164 165
 
165 166
     case IsEditable of
@@ -167,7 +168,7 @@ update(Id, Props, Options, Context) when is_integer(Id) orelse Id == insert_rsc
167 168
             DateProps = recombine_dates(Props),
168 169
             TextProps = recombine_languages(DateProps, Context),
169 170
             BlockProps = recombine_blocks(TextProps),
170  
-            AtomProps = [ {z_convert:to_atom(P), V} || {P, V} <- BlockProps ],
  171
+            AtomProps = [ {map_property_name(P), V} || {P, V} <- BlockProps ],
171 172
             FilteredProps = props_filter(props_trim(AtomProps), [], Context),
172 173
             EditableProps = props_filter_protected(FilteredProps),
173 174
             AclCheckedProps = case z_acl:rsc_update_check(Id, EditableProps, Context) of
@@ -421,15 +422,14 @@ preflight_check(Id, [_H|T], Context) ->
421 422
 
422 423
 %% @doc Remove whitespace around some predefined fields
423 424
 props_trim(Props) ->
424  
-    props_trim(Props, []).
425  
-
426  
-props_trim([], Acc) ->
427  
-    lists:reverse(Acc);
428  
-props_trim([{P,V}=Prop|T], Acc) ->
429  
-    case is_trimmable(P,V) of
430  
-        true -> props_trim(T, [{P, z_string:trim(V)}|Acc]);
431  
-        false -> props_trim(T, [Prop|Acc])
432  
-    end.
  425
+    [
  426
+        case is_trimmable(P,V) of
  427
+            true -> {P, z_string:trim(V)};
  428
+            false -> {P,V}
  429
+        end
  430
+        || {P,V} <- Props
  431
+    ].
  432
+
433 433
 
434 434
 %% @doc Remove properties the user is not allowed to change and convert some other to the correct data type
435 435
 %% @spec props_filter(Props1, Acc, Context) -> Props2
@@ -477,7 +477,6 @@ props_filter([{page_path, Path}|T], Acc, Context) ->
477 477
         false ->
478 478
             props_filter(T, Acc, Context)
479 479
     end;
480  
-
481 480
 props_filter([{slug, undefined}|T], Acc, Context) ->
482 481
     props_filter(T, [{slug, []} | Acc], Context);
483 482
 props_filter([{slug, <<>>}|T], Acc, Context) ->
@@ -490,7 +489,6 @@ props_filter([{custom_slug, P}|T], Acc, Context) ->
490 489
     props_filter(T, [{custom_slug, z_convert:to_bool(P)} | Acc], Context);
491 490
 props_filter([{is_published, P}|T], Acc, Context) ->
492 491
     props_filter(T, [{is_published, z_convert:to_bool(P)} | Acc], Context);
493  
-
494 492
 props_filter([{is_authoritative, P}|T], Acc, Context) ->
495 493
     case z_acl:is_allowed(use, mod_admin_config, Context) of
496 494
         true ->
@@ -500,7 +498,8 @@ props_filter([{is_authoritative, P}|T], Acc, Context) ->
500 498
     end;
501 499
 props_filter([{is_featured, P}|T], Acc, Context) ->
502 500
     props_filter(T, [{is_featured, z_convert:to_bool(P)} | Acc], Context);
503  
-
  501
+props_filter([{is_query_live, P}|T], Acc, Context) ->
  502
+    props_filter(T, [{is_query_live, z_convert:to_bool(P)} | Acc], Context);
504 503
 props_filter([{visible_for, Vis}|T], Acc, Context) ->
505 504
     VisibleFor = z_convert:to_integer(Vis),
506 505
     case VisibleFor of
@@ -568,42 +567,43 @@ props_defaults(Props, _Context) ->
568 567
 props_filter_protected(Props) ->
569 568
     lists:filter(fun({K,_V}) -> not is_protected(K) end, Props).
570 569
 
  570
+
  571
+%% @doc Map property names to an atom, fold pivot and computed fields together for later filtering.
  572
+map_property_name(P) when not is_list(P) -> map_property_name(z_convert:to_list(P));
  573
+map_property_name("computed_"++_) -> computed_xxx;
  574
+map_property_name("pivot_"++_) -> pivot_xxx;
  575
+map_property_name(P) when is_list(P) -> erlang:list_to_existing_atom(P).
  576
+
  577
+
571 578
 %% @doc Properties that can't be updated with m_rsc_update:update/3 or m_rsc_update:insert/2
572  
-is_protected(created)                    -> true;
573  
-is_protected(creator_id)                 -> true;
574  
-is_protected(id)                         -> true;
575  
-is_protected(modified)                   -> true;
576  
-is_protected(modifier_id)                -> true;
577  
-is_protected(pivot_category_nr)          -> true;
578  
-is_protected(pivot_city)                 -> true;
579  
-is_protected(pivot_country)              -> true;
580  
-is_protected(pivot_date_end)             -> true;
581  
-is_protected(pivot_date_end_month_day)   -> true;
582  
-is_protected(pivot_date_start)           -> true;
583  
-is_protected(pivot_date_start_month_day) -> true;
584  
-is_protected(pivot_first_name)           -> true;
585  
-is_protected(pivot_gender)               -> true;
586  
-is_protected(pivot_geocode)              -> true;
587  
-is_protected(pivot_postcode)             -> true;
588  
-is_protected(pivot_rtsv)                 -> true;
589  
-is_protected(pivot_state)                -> true;
590  
-is_protected(pivot_street)               -> true;
591  
-is_protected(pivot_surname)              -> true;
592  
-is_protected(pivot_title)                -> true;
593  
-is_protected(pivot_tsv)                  -> true;
594  
-is_protected(props)                      -> true;
595  
-is_protected(version)                    -> true;
596  
-is_protected(_)                          -> false.
  579
+is_protected(id)            -> true;
  580
+is_protected(created)       -> true;
  581
+is_protected(creator_id)    -> true;
  582
+is_protected(modified)      -> true;
  583
+is_protected(modifier_id)   -> true;
  584
+is_protected(props)         -> true;
  585
+is_protected(version)       -> true;
  586
+is_protected(page_url)      -> true;
  587
+is_protected(medium)        -> true;
  588
+is_protected(pivot_xxx)     -> true;
  589
+is_protected(computed_xxx)  -> true;
  590
+is_protected(_)             -> false.
597 591
 
598 592
 
599 593
 is_trimmable(_, V) when not is_binary(V) and not is_list(V) -> false;
  594
+is_trimmable(title, _)       -> true;
  595
+is_trimmable(title_short, _) -> true;
  596
+is_trimmable(summary, _)     -> true;
  597
+is_trimmable(chapeau, _)     -> true;
  598
+is_trimmable(subtitle, _)    -> true;
600 599
 is_trimmable(email, _)       -> true;
  600
+is_trimmable(uri, _)         -> true;
  601
+is_trimmable(website, _)     -> true;
601 602
 is_trimmable(page_path, _)   -> true;
  603
+is_trimmable(name, _)        -> true;
602 604
 is_trimmable(slug, _)        -> true;
603 605
 is_trimmable(custom_slug, _) -> true;
604 606
 is_trimmable(category, _)    -> true;
605  
-is_trimmable(title, _)       -> true;
606  
-is_trimmable(title_short, _) -> true;
607 607
 is_trimmable(_, _)           -> false.
608 608
 
609 609
 

0 notes on commit 3a51c40

Please sign in to comment.
Something went wrong with that request. Please try again.