| rev | 
   line source | 
| 
bsw/jbe@0
 | 
     1 local issue = param.get("issue", "table")
 | 
| 
bsw/jbe@0
 | 
     2 
 | 
| 
bsw/jbe@19
 | 
     3 local initiatives_selector = param.get("initiatives_selector", "table")
 | 
| 
bsw@51
 | 
     4 
 | 
| 
bsw@274
 | 
     5 local highlight_initiative = param.get("highlight_initiative", "table")
 | 
| 
bsw@274
 | 
     6 
 | 
| 
bsw@285
 | 
     7 local for_member = param.get("for_member", "table") or app.session.member
 | 
| 
bsw@285
 | 
     8 
 | 
| 
bsw@11
 | 
     9 initiatives_selector
 | 
| 
bsw/jbe@19
 | 
    10   :join("issue", nil, "issue.id = initiative.issue_id")
 | 
| 
bsw@11
 | 
    11 
 | 
| 
bsw@51
 | 
    12 if app.session.member_id then
 | 
| 
bsw@51
 | 
    13   initiatives_selector
 | 
| 
bsw@285
 | 
    14     :left_join("initiator", "_initiator", { "_initiator.initiative_id = initiative.id AND _initiator.member_id = ? AND _initiator.accepted", for_member.id } )
 | 
| 
bsw@285
 | 
    15     :left_join("supporter", "_supporter", { "_supporter.initiative_id = initiative.id AND _supporter.member_id = ?", for_member.id} )
 | 
| 
bsw@285
 | 
    16     :left_join("delegating_interest_snapshot", "_delegating_interest_snapshot", { "_delegating_interest_snapshot.issue_id = initiative.issue_id AND _delegating_interest_snapshot.member_id = ? AND _delegating_interest_snapshot.event = issue.latest_snapshot_event", for_member.id} )
 | 
| 
bsw@285
 | 
    17     :left_join("direct_supporter_snapshot", "_direct_supporter_snapshot", "_direct_supporter_snapshot.initiative_id = initiative.id AND _direct_supporter_snapshot.member_id = _delegating_interest_snapshot.delegate_member_ids[array_upper(_delegating_interest_snapshot.delegate_member_ids, 1)] AND _direct_supporter_snapshot.event = issue.latest_snapshot_event")
 | 
| 
bsw@285
 | 
    18 
 | 
| 
bsw@51
 | 
    19     :add_field("(_initiator.member_id NOTNULL)", "is_initiator")
 | 
| 
bsw@285
 | 
    20     :add_field({"(_supporter.member_id NOTNULL) AND NOT EXISTS(SELECT 1 FROM opinion WHERE opinion.initiative_id = initiative.id AND opinion.member_id = ? AND ((opinion.degree = 2 AND NOT fulfilled) OR (opinion.degree = -2 AND fulfilled)))", for_member.id }, "is_supporter")
 | 
| 
bsw@285
 | 
    21     :add_field({"EXISTS(SELECT 1 FROM opinion WHERE opinion.initiative_id = initiative.id AND opinion.member_id = ? AND ((opinion.degree = 2 AND NOT fulfilled) OR (opinion.degree = -2 AND fulfilled)))", for_member.id }, "is_potential_supporter")
 | 
| 
bsw@285
 | 
    22 
 | 
| 
bsw@285
 | 
    23     :add_field("_direct_supporter_snapshot.member_id NOTNULL", "is_supporter_via_delegation")
 | 
| 
bsw@51
 | 
    24 end
 | 
| 
bsw@11
 | 
    25 
 | 
| 
bsw/jbe@19
 | 
    26 local initiatives_count = initiatives_selector:count()
 | 
| 
bsw@11
 | 
    27 
 | 
| 
bsw/jbe@19
 | 
    28 local limit = param.get("limit", atom.number)
 | 
| 
bsw/jbe@19
 | 
    29 local no_sort = param.get("no_sort", atom.boolean)
 | 
| 
bsw/jbe@19
 | 
    30 
 | 
| 
bsw@345
 | 
    31 local more_initiatives_count
 | 
| 
bsw@345
 | 
    32 if limit then
 | 
| 
bsw@345
 | 
    33   if initiatives_count > limit then
 | 
| 
bsw@345
 | 
    34     more_initiatives_count = initiatives_count - limit
 | 
| 
bsw/jbe@19
 | 
    35   end
 | 
| 
bsw@345
 | 
    36   initiatives_selector:limit(limit)
 | 
| 
bsw/jbe@19
 | 
    37 end
 | 
| 
bsw/jbe@19
 | 
    38 
 | 
| 
bsw@345
 | 
    39 local issue = param.get("issue", "table")
 | 
| 
bsw/jbe@19
 | 
    40 
 | 
| 
bsw@345
 | 
    41 local name = "initiative_list"
 | 
| 
bsw@345
 | 
    42 if issue then
 | 
| 
bsw@345
 | 
    43   name = "issue_" .. tostring(issue.id) ..  "_initiative_list"
 | 
| 
bsw@345
 | 
    44 end
 | 
| 
bsw@345
 | 
    45 
 | 
| 
bsw@345
 | 
    46 ui.add_partial_param_names{ name }
 | 
| 
bsw/jbe@19
 | 
    47 
 | 
| 
bsw@345
 | 
    48 local order_filter = {
 | 
| 
bsw@345
 | 
    49   name = name,
 | 
| 
bsw@345
 | 
    50   label = _"Order by"
 | 
| 
bsw@345
 | 
    51 }
 | 
| 
bsw@345
 | 
    52 
 | 
| 
bsw@345
 | 
    53 if issue and issue.ranks_available then
 | 
| 
bsw@345
 | 
    54   order_filter[#order_filter+1] = {
 | 
| 
bsw@345
 | 
    55     name = "rank",
 | 
| 
bsw@345
 | 
    56     label = _"Rank",
 | 
| 
bsw@345
 | 
    57     selector_modifier = function(selector) selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id") end
 | 
| 
bsw@11
 | 
    58   }
 | 
| 
bsw@11
 | 
    59 end
 | 
| 
bsw/jbe@19
 | 
    60 
 | 
| 
bsw@345
 | 
    61 order_filter[#order_filter+1] = {
 | 
| 
bsw@345
 | 
    62   name = "potential_support",
 | 
| 
bsw@345
 | 
    63   label = _"Potential support",
 | 
| 
bsw@345
 | 
    64   selector_modifier = function(selector) selector:add_order_by("CASE WHEN issue.population = 0 THEN 0 ELSE initiative.supporter_count::float / issue.population::float END DESC, initiative.id") end
 | 
| 
bsw@345
 | 
    65 }
 | 
| 
bsw/jbe@19
 | 
    66 
 | 
| 
bsw@345
 | 
    67 order_filter[#order_filter+1] = {
 | 
| 
bsw@345
 | 
    68   name = "support",
 | 
| 
bsw@345
 | 
    69   label = _"Support",
 | 
| 
bsw@345
 | 
    70   selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
 | 
| 
bsw@345
 | 
    71 }
 | 
| 
bsw/jbe@19
 | 
    72 
 | 
| 
bsw@345
 | 
    73 order_filter[#order_filter+1] = {
 | 
| 
bsw@345
 | 
    74   name = "newest",
 | 
| 
bsw@345
 | 
    75   label = _"Newest",
 | 
| 
bsw@345
 | 
    76   selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
 | 
| 
bsw@345
 | 
    77 }
 | 
| 
bsw/jbe@19
 | 
    78 
 | 
| 
bsw@345
 | 
    79 order_filter[#order_filter+1] = {
 | 
| 
bsw@345
 | 
    80   name = "oldest",
 | 
| 
bsw@345
 | 
    81   label = _"Oldest",
 | 
| 
bsw@345
 | 
    82   selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
 | 
| 
bsw@345
 | 
    83 }
 | 
| 
bsw/jbe@19
 | 
    84 
 | 
| 
bsw@345
 | 
    85 ui_filters = ui.filters
 | 
| 
bsw/jbe@19
 | 
    86 
 | 
| 
bsw@345
 | 
    87 if no_sort then
 | 
| 
bsw@345
 | 
    88   ui_filters = function(args) args.content() end
 | 
| 
bsw@345
 | 
    89   if issue.ranks_available then
 | 
| 
bsw@345
 | 
    90     initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
 | 
| 
bsw@345
 | 
    91   else
 | 
| 
bsw@345
 | 
    92     initiatives_selector:add_order_by("CASE WHEN issue.population = 0 OR initiative.supporter_count = 0 OR initiative.supporter_count ISNULL THEN 0 ELSE initiative.supporter_count::float / issue.population::float END DESC, initiative.id")
 | 
| 
bsw/jbe@19
 | 
    93   end
 | 
| 
bsw@345
 | 
    94 end
 | 
| 
bsw/jbe@19
 | 
    95 
 | 
| 
bsw@345
 | 
    96 ui_filters{
 | 
| 
bsw@345
 | 
    97   label = _"Change order",
 | 
| 
bsw@345
 | 
    98   order_filter,
 | 
| 
bsw@345
 | 
    99   selector = initiatives_selector,
 | 
| 
bsw@345
 | 
   100   content = function()
 | 
| 
bsw@345
 | 
   101     ui.paginate{
 | 
| 
bsw@345
 | 
   102       name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
 | 
| 
bsw@345
 | 
   103       selector = initiatives_selector,
 | 
| 
bsw@345
 | 
   104       per_page = param.get("per_page", atom.number) or limit,
 | 
| 
bsw@345
 | 
   105       content = function()
 | 
| 
bsw@345
 | 
   106         local initiatives = initiatives_selector:exec()
 | 
| 
bsw@345
 | 
   107         if highlight_initiative then
 | 
| 
bsw@345
 | 
   108           local highlight_initiative_found
 | 
| 
bsw@345
 | 
   109           for i, initiative in ipairs(initiatives) do
 | 
| 
bsw@345
 | 
   110             if initiative.id == highlight_initiative.id then
 | 
| 
bsw@345
 | 
   111               highhighlight_initiative_found = true
 | 
| 
bsw@274
 | 
   112             end
 | 
| 
bsw@274
 | 
   113           end
 | 
| 
bsw@345
 | 
   114           if not highhighlight_initiative_found then
 | 
| 
bsw@345
 | 
   115             initiatives[#initiatives+1] = highlight_initiative
 | 
| 
bsw@345
 | 
   116             if more_initiatives_count then
 | 
| 
bsw@345
 | 
   117               more_initiatives_count = more_initiatives_count - 1
 | 
| 
bsw@345
 | 
   118             end
 | 
| 
bsw/jbe@19
 | 
   119           end
 | 
| 
bsw/jbe@19
 | 
   120         end
 | 
| 
bsw@345
 | 
   121         for i, initiative in ipairs(initiatives) do
 | 
| 
bsw@345
 | 
   122           execute.view{
 | 
| 
bsw@345
 | 
   123             module = "initiative",
 | 
| 
bsw@345
 | 
   124             view = "_list_element",
 | 
| 
bsw@345
 | 
   125             params = {
 | 
| 
bsw@345
 | 
   126               initiative = initiative,
 | 
| 
bsw@345
 | 
   127               selected = highlight_initiative and highlight_initiative.id == initiative.id or nil,
 | 
| 
bsw@345
 | 
   128             }
 | 
| 
bsw@345
 | 
   129           }
 | 
| 
bsw/jbe@19
 | 
   130         end
 | 
| 
bsw/jbe@19
 | 
   131       end
 | 
| 
bsw/jbe@19
 | 
   132     }
 | 
| 
bsw/jbe@19
 | 
   133   end
 | 
| 
bsw@345
 | 
   134 }
 | 
| 
bsw/jbe@19
 | 
   135 
 | 
| 
bsw@345
 | 
   136 if more_initiatives_count and more_initiatives_count > 0 then
 | 
| 
bsw@345
 | 
   137   local text
 | 
| 
bsw@345
 | 
   138   if more_initiatives_count == 1 then
 | 
| 
bsw@345
 | 
   139     text = _("and one more initiative")
 | 
| 
bsw@345
 | 
   140   else
 | 
| 
bsw@345
 | 
   141     text = _("and #{count} more initiatives", { count = more_initiatives_count })
 | 
| 
bsw@345
 | 
   142   end
 | 
| 
bsw@345
 | 
   143   ui.link{
 | 
| 
bsw@345
 | 
   144     attr = { class = "more_initiatives_link" },
 | 
| 
bsw@345
 | 
   145     content = text,
 | 
| 
bsw@345
 | 
   146     module = "issue",
 | 
| 
bsw@345
 | 
   147     view = "show",
 | 
| 
bsw@345
 | 
   148     id = issue.id,
 | 
| 
bsw@345
 | 
   149   }
 | 
| 
bsw/jbe@19
 | 
   150 end
 |