bsw/jbe@19: ui.script{ script = "lf_initiative_expanded = {};" }
bsw@11: 
bsw/jbe@0: local issue = param.get("issue", "table")
bsw/jbe@0: 
bsw/jbe@19: local initiatives_selector = param.get("initiatives_selector", "table")
bsw@51: 
bsw@11: initiatives_selector
bsw/jbe@19:   :join("issue", nil, "issue.id = initiative.issue_id")
bsw@11: 
bsw@51: if app.session.member_id then
bsw@51:   initiatives_selector
bsw@57:     :left_join("initiator", "_initiator", { "_initiator.initiative_id = initiative.id AND _initiator.member_id = ? AND _initiator.accepted", app.session.member.id} )
bsw@51:     :left_join("supporter", "_supporter", { "_supporter.initiative_id = initiative.id AND _supporter.member_id = ?", app.session.member.id} )
bsw@51:   
bsw@51:     :add_field("(_initiator.member_id NOTNULL)", "is_initiator")
bsw@51:     :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)))", app.session.member.id }, "is_supporter")
bsw@51:     :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)))", app.session.member.id }, "is_potential_supporter")
bsw@51: end
bsw@11: 
bsw/jbe@19: local initiatives_count = initiatives_selector:count()
bsw@11: 
bsw/jbe@19: local limit = param.get("limit", atom.number)
bsw/jbe@19: local no_sort = param.get("no_sort", atom.boolean)
bsw/jbe@19: 
bsw/jbe@19: local show_for_issue = param.get("show_for_issue", atom.boolean)
bsw/jbe@19: 
bsw/jbe@19: local show_for_initiative
bsw/jbe@19: 
bsw/jbe@19: local show_for_initiative_id = param.get("for_initiative_id", atom.number)
bsw/jbe@19: 
bsw/jbe@19: if show_for_initiative_id then
bsw/jbe@19:   show_for_initiative = Initiative:by_id(show_for_initiative_id)
bsw/jbe@19: 
bsw/jbe@19: elseif not show_for_initiative_id and show_for_issue and issue and issue.ranks_available then
bsw/jbe@19:   winning_initiative = Initiative:new_selector()
bsw/jbe@19:     :add_where{ "issue_id = ?", issue.id }
bsw/jbe@19:     :add_where("rank = 1")
bsw@24:     :optional_object_mode()
bsw/jbe@19:     :exec()
bsw/jbe@19:   if winning_initiative then
bsw/jbe@19:     show_for_initiative = winning_initiative
bsw/jbe@19:     ui.container{
bsw/jbe@19:       attr = { class = "admitted_info" },
bsw/jbe@19:       content = _"This issue has been finished with the following winning initiative:"
bsw/jbe@19:     }
bsw/jbe@19:   else
bsw/jbe@19:     ui.container{
bsw/jbe@19:       attr = { class = "not_admitted_info" },
bsw/jbe@19:       content = _"This issue has been finished without any winning initiative."
bsw/jbe@19:     }
bsw/jbe@19:   end
bsw/jbe@19: end
bsw/jbe@19: 
bsw/jbe@19: 
bsw/jbe@19: if show_for_initiative then
bsw/jbe@19:   ui.script{ script = "lf_initiative_expanded['initiative_content_" .. tostring(show_for_initiative.id) .. "'] = true;" }
bsw/jbe@19:   initiatives_selector:add_where{ "initiative.id != ?", show_for_initiative.id }
bsw/jbe@19: 
bsw/jbe@19:   execute.view{
bsw/jbe@19:     module = "initiative",
bsw/jbe@19:     view = "_list_element",
bsw/jbe@19:     params = {
bsw/jbe@19:       initiative = show_for_initiative,
bsw/jbe@19:       expanded = true,
bsw/jbe@19:       expandable = true
bsw/jbe@19:     }
bsw/jbe@19:   }
bsw/jbe@19:   if show_for_issue then
bsw/jbe@19:     slot.put("
")
bsw/jbe@19:     ui.container{
bsw/jbe@19:       attr = { style = "font-weight: bold;" },
bsw/jbe@0:       content = function()
bsw/jbe@19:         slot.put(_"Alternative initiatives")
bsw/jbe@0:       end
bsw/jbe@0:     }
bsw/jbe@0:   end
bsw/jbe@19: elseif show_for_issue then
bsw/jbe@19:   ui.container{
bsw/jbe@19:     attr = { style = "font-weight: bold;" },
bsw/jbe@19:     content = function()
bsw/jbe@19:       slot.put(_"Alternative initiatives")
bsw/jbe@19:     end
bsw@11:   }
bsw@11: end
bsw/jbe@19: 
bsw/jbe@19: if not show_for_initiative or initiatives_count > 1 then
bsw/jbe@19: 
bsw/jbe@19: 
bsw/jbe@19:   local more_initiatives_count
bsw/jbe@19:   if limit then
bsw/jbe@19:     limit = limit - (show_for_initiative and 1 or 0)
bsw/jbe@19:     if initiatives_count > limit then
bsw/jbe@19:       more_initiatives_count = initiatives_count - limit
bsw/jbe@19:     end
bsw/jbe@19:     initiatives_selector:limit(limit)
bsw/jbe@19:   end
bsw/jbe@19: 
bsw/jbe@19:   local expandable = param.get("expandable", atom.boolean)
bsw/jbe@19: 
bsw/jbe@19:   local issue = param.get("issue", "table")
bsw/jbe@19: 
bsw/jbe@19:   local name = "initiative_list"
bsw/jbe@19:   if issue then
bsw/jbe@19:     name = "issue_" .. tostring(issue.id) ..  "_initiative_list"
bsw/jbe@19:   end
bsw/jbe@19: 
bsw/jbe@19:   ui.add_partial_param_names{ name }
bsw/jbe@19: 
bsw/jbe@19:   local order_filter = {
bsw/jbe@19:     name = name,
bsw/jbe@19:     label = _"Order by"
bsw/jbe@19:   }
bsw/jbe@19: 
bsw/jbe@19:   if issue and issue.ranks_available then
bsw/jbe@19:     order_filter[#order_filter+1] = {
bsw/jbe@19:       name = "rank",
bsw/jbe@19:       label = _"Rank",
bsw/jbe@19:       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/jbe@19:     }
bsw/jbe@19:   end
bsw/jbe@19: 
bsw/jbe@19:   order_filter[#order_filter+1] = {
bsw/jbe@19:     name = "potential_support",
bsw/jbe@19:     label = _"Potential support",
bsw/jbe@19:     selector_modifier = function(selector) selector:add_order_by("initiative.supporter_count::float / issue.population::float DESC, initiative.id") end
bsw/jbe@19:   }
bsw/jbe@19: 
bsw/jbe@19:   order_filter[#order_filter+1] = {
bsw/jbe@19:     name = "support",
bsw/jbe@19:     label = _"Support",
bsw/jbe@19:     selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
bsw/jbe@19:   }
bsw/jbe@19: 
bsw/jbe@19:   order_filter[#order_filter+1] = {
bsw/jbe@19:     name = "newest",
bsw/jbe@19:     label = _"Newest",
bsw/jbe@19:     selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
bsw/jbe@19:   }
bsw/jbe@19: 
bsw/jbe@19:   order_filter[#order_filter+1] = {
bsw/jbe@19:     name = "oldest",
bsw/jbe@19:     label = _"Oldest",
bsw/jbe@19:     selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
bsw/jbe@19:   }
bsw/jbe@19: 
bsw/jbe@19:   ui_filters = ui.filters
bsw/jbe@19: 
bsw/jbe@19:   if no_sort then
bsw/jbe@19:     ui_filters = function(args) args.content() end
bsw/jbe@19:     if issue.ranks_available then
bsw/jbe@19:       initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
bsw/jbe@19:     else
bsw/jbe@19:       initiatives_selector:add_order_by("initiative.supporter_count::float / issue.population::float DESC, initiative.id")
bsw/jbe@19:     end
bsw/jbe@19:   end
bsw/jbe@19: 
bsw/jbe@19:   ui_filters{
bsw/jbe@19:     label = _"Change order",
bsw/jbe@19:     order_filter,
bsw/jbe@19:     selector = initiatives_selector,
bsw/jbe@19:     content = function()
bsw/jbe@19:       ui.paginate{
bsw/jbe@19:         name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
bsw/jbe@19:         selector = initiatives_selector,
bsw/jbe@19:         per_page = param.get("per_page", atom.number),
bsw/jbe@19:         content = function()
bsw/jbe@19:           local initiatives = initiatives_selector:exec()
bsw/jbe@19:           for i, initiative in ipairs(initiatives) do
bsw/jbe@19:             local expanded = config.user_tab_mode == "accordeon_all_expanded" and expandable or
bsw/jbe@19:               show_for_initiative and initiative.id == show_for_initiative.id
bsw/jbe@19:             if expanded then
bsw/jbe@19:               ui.script{ script = "lf_initiative_expanded['initiative_content_" .. tostring(initiative.id) .. "'] = true;" }
bsw/jbe@19:             end
bsw/jbe@19:             execute.view{
bsw/jbe@19:               module = "initiative",
bsw/jbe@19:               view = "_list_element",
bsw/jbe@19:               params = {
bsw/jbe@19:                 initiative = initiative,
bsw/jbe@19:                 expanded = expanded,
bsw/jbe@19:                 expandable = expandable
bsw/jbe@19:               }
bsw/jbe@19:             }
bsw/jbe@19:           end
bsw/jbe@19:         end
bsw/jbe@19:       }
bsw/jbe@19:     end
bsw/jbe@19:   }
bsw/jbe@19: 
bsw/jbe@19:   if more_initiatives_count then
bsw/jbe@19:     ui.link{
bsw/jbe@19:       attr = { style = "font-size: 75%; font-style: italic;" },
bsw/jbe@19:       content = _("and #{count} more initiatives", { count = more_initiatives_count }),
bsw/jbe@19:       module = "issue",
bsw/jbe@19:       view = "show",
bsw/jbe@19:       id = issue.id,
bsw/jbe@19:     }
bsw/jbe@19:   end
bsw/jbe@19: 
bsw/jbe@19: end
bsw/jbe@19: 
bsw/jbe@19: if show_for_issue then
bsw/jbe@19:   slot.put("
")
bsw/jbe@19:   
bsw/jbe@19:   if issue and initiatives_count == 1 then
bsw/jbe@19:     ui.container{
bsw/jbe@19:       content = function()
bsw/jbe@19:         if issue.fully_frozen or issue.closed then
bsw/jbe@19:           slot.put(_"There were no more alternative initiatives.")
bsw/jbe@19:         else
bsw/jbe@19:           slot.put(_"There are no more alternative initiatives currently.")
bsw/jbe@19:         end
bsw/jbe@19:       end
bsw/jbe@19:     }
bsw/jbe@19:   end
bsw/jbe@19: 
bsw@51:   if app.session.member_id and not (issue.fully_frozen or issue.closed) then
bsw/jbe@19:     slot.put(" ")
bsw/jbe@19:     ui.link{
bsw/jbe@19:       content = function()
bsw/jbe@19:         ui.image{ static = "icons/16/script_add.png" }
bsw/jbe@19:         slot.put(" ")
bsw/jbe@19:         slot.put(_"Create alternative initiative")
bsw/jbe@19:       end,
bsw/jbe@19:       module = "initiative",
bsw/jbe@19:       view = "new",
bsw/jbe@19:       params = { issue_id = issue.id }
bsw/jbe@19:     }
bsw/jbe@19:   end
bsw/jbe@19: end