Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quick hack for multi select filters #218

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/kaffy/resource_form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ defmodule Kaffy.ResourceForm do
end
end

def form_field(changeset, form, {field, options}, opts) do
type = Kaffy.ResourceSchema.field_type(changeset.data.__struct__, field)
build_html_input(changeset.data, form, {field, options}, type, opts)
end
# def form_field(changeset, form, {field, options}, opts) do
# type = Kaffy.ResourceSchema.field_type(changeset.data.__struct__, field)
# build_html_input(changeset.data, form, {field, options}, type, opts)
# end

defp build_html_input(schema, form, {field, options}, type, opts, readonly \\ false) do
data = schema
Expand Down
157 changes: 148 additions & 9 deletions lib/kaffy/resource_query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ defmodule Kaffy.ResourceQuery do
search = Map.get(params, "search", "") |> String.trim()
search_fields = Kaffy.ResourceAdmin.search_fields(resource)
filtered_fields = get_filter_fields(params, resource)
hardcoded_filtered_fields = get_hardcoded_filter_fields(params, resource)
ordering = get_ordering(resource, params)

current_offset = (page - 1) * per_page
schema = resource[:schema]

{all, paged} =
build_query(
schema,
resource,
search_fields,
filtered_fields,
hardcoded_filtered_fields,
search,
per_page,
ordering,
Expand Down Expand Up @@ -102,16 +104,31 @@ defmodule Kaffy.ResourceQuery do
end)
end

defp get_hardcoded_filter_fields(params, _resource) do
schema_fields = [
"map_marker_1_lat",
"map_marker_1_lon",
"map_marker_2_lat",
"map_marker_2_lon",
"user_min_age",
"user_max_age"
]

filtered_fields = Enum.filter(params, fn {k, v} -> k in schema_fields and v != "" end)
Enum.into(filtered_fields, %{})
end

defp build_query(
schema,
resource,
search_fields,
filtered_fields,
hardcoded_filtered_fields,
search,
per_page,
ordering,
current_offset
) do
query = from(s in schema)
query = from(s in resource[:schema])

query =
cond do
Expand Down Expand Up @@ -141,27 +158,149 @@ defmodule Kaffy.ResourceQuery do
end)
end

query = build_filtered_fields_query(query, filtered_fields)
query = build_filtered_fields_query(resource, query, filtered_fields)

query = build_hardcoded_filtered_fields_query(resource, query, hardcoded_filtered_fields)

limited_query =
from(s in query, limit: ^per_page, offset: ^current_offset, order_by: ^ordering)

{query, limited_query}
end

defp build_filtered_fields_query(query, []), do: query
defp build_filtered_fields_query(_resource, query, []), do: query

defp build_filtered_fields_query(resource, query, [filter | rest]) do
IO.puts("MSP build_filtered_fields_query ---------------------------------")

IO.puts(
"resource: #{inspect(resource)}, query: #{inspect(query)}, filter: #{inspect(filter)}, rest: #{inspect(rest)}"
)

defp build_filtered_fields_query(query, [filter | rest]) do
query =
case filter.value == "" do
true ->
query

false ->
field_name = String.to_existing_atom(filter.name)
from(s in query, where: field(s, ^field_name) == ^filter.value)
field_name = String.to_atom(filter.name)

{type, operator} = get_field_type_and_operator(resource, field_name)

case type do
:date ->
case operator do
:ltoe ->
from(
s in query,
where: field(s, ^field_name) <= ^filter.value
)

:gtoe ->
from(
s in query,
where: field(s, ^field_name) >= ^filter.value
)

_ ->
from(
s in query,
where: field(s, ^field_name) == ^filter.value
)
end

_ ->
filter_values = String.split(filter.value, ",")

from(
s in query,
where: field(s, ^field_name) in ^filter_values
)
end
end

build_filtered_fields_query(query, rest)
build_filtered_fields_query(resource, query, rest)
end

defp build_hardcoded_filtered_fields_query(_resource, query, filters) do
IO.puts("MSP build_hardcoded_filtered_fields_query ---------------------------------")
IO.puts("query: #{inspect(query)}, filters: #{inspect(filters)}")

if Enum.count(filters) > 0 do
query =
if Map.has_key?(filters, "map_marker_1_lat") &&
Map.has_key?(filters, "map_marker_1_lon") &&
Map.has_key?(filters, "map_marker_2_lat") &&
Map.has_key?(filters, "map_marker_2_lon") do
map_marker_1_lat = Map.get(filters, "map_marker_1_lat")
map_marker_1_lon = Map.get(filters, "map_marker_1_lon")
map_marker_2_lat = Map.get(filters, "map_marker_2_lat")
map_marker_2_lon = Map.get(filters, "map_marker_2_lon")

from(
s in query,
where: field(s, :home_lat) >= ^map_marker_2_lat,
where: field(s, :home_lat) <= ^map_marker_1_lat,
where: field(s, :home_long) >= ^map_marker_1_lon,
where: field(s, :home_long) <= ^map_marker_2_lon
)
else
query
end

query =
if Map.has_key?(filters, "user_min_age") &&
Map.has_key?(filters, "user_max_age") do
user_min_age = Map.get(filters, "user_min_age", 0)
user_max_age = Map.get(filters, "user_max_age", 200)

today = DateTime.utc_now()
one_year_in_seconds = 365 * 24 * 60 * 60

min_age =
DateTime.add(
today,
String.to_integer(user_min_age) *
one_year_in_seconds * -1,
:second
)

max_age =
DateTime.add(
today,
String.to_integer(user_max_age) *
one_year_in_seconds * -1,
:second
)

from(
s in query,
where: field(s, :dob) <= ^min_age,
where: field(s, :dob) >= ^max_age
)
else
query
end

query
else
query
end
end

defp get_field_type_and_operator(resource, field_name) do
IO.puts("MSP get_field_type_and_operator ---------------------------------")
schema = resource[:schema]
admin_fields = Kaffy.ResourceAdmin.index(resource)
field_options = admin_fields |> Map.new() |> Map.get(field_name)

IO.puts("field_options: #{inspect(field_options)}")

{_field, type, operator, _multiple, _filters} =
Kaffy.ResourceSchema.kaffy_field_filters(schema, {field_name, field_options})

IO.puts("field_name: #{field_name}, type: #{type}, operator: #{operator}")

{type, operator}
end
end
15 changes: 12 additions & 3 deletions lib/kaffy/resource_schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,22 @@ defmodule Kaffy.ResourceSchema do
Enum.map(admin_fields, fn f -> kaffy_field_filters(resource[:schema], f) end)

Enum.any?(fields_with_filters, fn
{_, filters} -> filters
_ -> false
{_, _, _, _, filters} ->
filters

_ ->
false
end)
end

def kaffy_field_filters(_schema, {field, options}) do
{field, Map.get(options || %{}, :filters, false)}
{
field,
Map.get(options || %{}, :filter_type, :select),
Map.get(options || %{}, :filter_operator, :equals),
Map.get(options || %{}, :filter_multiple, false),
Map.get(options || %{}, :filters, false)
}
end

def kaffy_field_filters(_, _), do: false
Expand Down
4 changes: 4 additions & 0 deletions lib/kaffy_web/controllers/resource_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ defmodule KaffyWeb.ResourceController do
put_flash(conn, :success, "Action performed successfully")
|> redirect(to: Kaffy.Utils.router().kaffy_resource_path(conn, :index, context, resource))

{:ok, redirect_url} ->
put_flash(conn, :success, "Action performed successfully, redirecting back")
|> redirect(external: redirect_url)

{:error, error_msg} ->
put_flash(conn, :error, error_msg)
|> redirect(to: Kaffy.Utils.router().kaffy_resource_path(conn, :index, context, resource))
Expand Down
1 change: 1 addition & 0 deletions lib/kaffy_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<script src="/kaffy/assets/js/select-all-checkbox.js?v=20"></script>
<script src="/kaffy/assets/js/phoenix_html.js"></script>
<script src="/kaffy/assets/js/dashboard.js?v=20"></script>
<script src="/kaffy/assets/js/map.js"></script>
<%= for js <- Kaffy.Utils.extensions(@conn).javascripts do %>
<%= js %>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion lib/kaffy_web/templates/resource/_list_table.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= if Kaffy.ResourceSchema.has_field_filters?(@my_resource) do %>
<tr>
<%= for {field, index} <- Enum.with_index(@fields) do %>
<% {field, filters} = Kaffy.ResourceSchema.kaffy_field_filters(@my_resource[:schema], field) %>
<% {field, _type, _operator, _multiple, filters} = Kaffy.ResourceSchema.kaffy_field_filters(@my_resource[:schema], field) %>
<%= if filters do %>
<th class="bg-light">
<select class="kaffy-filter custom-select" id="kaffy-field-<%= index %>" data-field-name="<%= Kaffy.ResourceSchema.kaffy_field_name(@my_resource[:schema], field) %>">
Expand Down
2 changes: 1 addition & 1 deletion lib/kaffy_web/templates/resource/_table.html.eex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<table class="table table-striped">
<table class="table table-striped mt-4">
<thead>
<%= render KaffyWeb.ResourceView, "_table_header.html", my_resource: @my_resource, fields: @fields, params: @params %>
</thead>
Expand Down
30 changes: 23 additions & 7 deletions lib/kaffy_web/templates/resource/_table_header.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,31 @@
<tr>
<th class="bg-light"></th>
<%= for {field, index} <- Enum.with_index(@fields) do %>
<% {field, filters} = Kaffy.ResourceSchema.kaffy_field_filters(@my_resource[:schema], field) %>
<% {field, type, _operator, multiple, filters} = Kaffy.ResourceSchema.kaffy_field_filters(@my_resource[:schema], field) %>
<%= if filters do %>
<th class="bg-light">
<select class="kaffy-filter custom-select" id="kaffy-field-<%= index %>" data-field-name="<%= field %>">
<option value="">All</option>
<%= for {human, machine} <- filters do %>
<option value="<%= machine %>"<%= if Map.get(@params, to_string(field)) == to_string(machine) do %> selected<% end %>><%= human %></option>
<% end %>
</select>
<%= if type == :select do %>
<select class="kaffy-filter custom-select" id="kaffy-field-<%= index %>" data-field-name="<%= field %>" <%= if multiple do %> multiple<% end %>>
<option value="">All</option>
<%= for {human, machine} <- filters do %>
<% params = Map.get(@params, to_string(field)) || "" %>
<% params = String.split(params, ",") %>

<option value="<%= machine %>"<%= if Enum.member?(params, to_string(machine)) do %> selected<% end %>><%= human %></option>
<% end %>
</select>
<% end %>

<%= if type == :date do %>
<div class="input-group mb-2 flatpickr flatpickr-wrap-date">
<div class="input-group-prepend">
<div class="input-group-text" data-clear="">❌</div>
</div>
<div class="input-group-prepend">
<div class="input-group-text" data-toggle="">🗓️</div>
</div>
<input class="kaffy-filter form-control flatpickr-input" id="kaffy-field-<%= index %>" data-field-name="<%= field %>" value="<%= Map.get(@params, to_string(field)) %>" data-input="" placeholder="Select Date..." type="text" readonly="readonly"></div>
<% end %>
</th>
<% else %>
<th class="bg-light"></th>
Expand Down
Loading