liquid_feedback_frontend
view app/main/initiative/_list.lua @ 98:227f3e98bdde
Added tag beta27 for changeset b3a3d734cc8d
| author | bsw | 
|---|---|
| date | Mon Aug 30 21:56:38 2010 +0200 (2010-08-30) | 
| parents | 4f39f0a0d5b5 | 
| children | bed32bf10a0b | 
 line source
     1 ui.script{ script = "lf_initiative_expanded = {};" }
     3 local issue = param.get("issue", "table")
     5 local initiatives_selector = param.get("initiatives_selector", "table")
     7 initiatives_selector
     8   :join("issue", nil, "issue.id = initiative.issue_id")
    10 if app.session.member_id then
    11   initiatives_selector
    12     :left_join("initiator", "_initiator", { "_initiator.initiative_id = initiative.id AND _initiator.member_id = ? AND _initiator.accepted", app.session.member.id} )
    13     :left_join("supporter", "_supporter", { "_supporter.initiative_id = initiative.id AND _supporter.member_id = ?", app.session.member.id} )
    15     :add_field("(_initiator.member_id NOTNULL)", "is_initiator")
    16     :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")
    17     :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")
    18 end
    20 local initiatives_count = initiatives_selector:count()
    22 local limit = param.get("limit", atom.number)
    23 local no_sort = param.get("no_sort", atom.boolean)
    25 local show_for_issue = param.get("show_for_issue", atom.boolean)
    27 local show_for_initiative
    29 local show_for_initiative_id = param.get("for_initiative_id", atom.number)
    31 if show_for_initiative_id then
    32   show_for_initiative = Initiative:by_id(show_for_initiative_id)
    34 elseif not show_for_initiative_id and show_for_issue and issue and issue.ranks_available then
    35   winning_initiative = Initiative:new_selector()
    36     :add_where{ "issue_id = ?", issue.id }
    37     :add_where("rank = 1")
    38     :optional_object_mode()
    39     :exec()
    40   if winning_initiative then
    41     show_for_initiative = winning_initiative
    42     ui.container{
    43       attr = { class = "admitted_info" },
    44       content = _"This issue has been finished with the following winning initiative:"
    45     }
    46   else
    47     ui.container{
    48       attr = { class = "not_admitted_info" },
    49       content = _"This issue has been finished without any winning initiative."
    50     }
    51   end
    52 end
    55 if show_for_initiative then
    56   ui.script{ script = "lf_initiative_expanded['initiative_content_" .. tostring(show_for_initiative.id) .. "'] = true;" }
    57   initiatives_selector:add_where{ "initiative.id != ?", show_for_initiative.id }
    59   execute.view{
    60     module = "initiative",
    61     view = "_list_element",
    62     params = {
    63       initiative = show_for_initiative,
    64       expanded = true,
    65       expandable = true
    66     }
    67   }
    68   if show_for_issue then
    69     slot.put("<br />")
    70     ui.container{
    71       attr = { style = "font-weight: bold;" },
    72       content = function()
    73         slot.put(_"Alternative initiatives")
    74       end
    75     }
    76   end
    77 elseif show_for_issue then
    78   ui.container{
    79     attr = { style = "font-weight: bold;" },
    80     content = function()
    81       slot.put(_"Alternative initiatives")
    82     end
    83   }
    84 end
    86 if not show_for_initiative or initiatives_count > 1 then
    89   local more_initiatives_count
    90   if limit then
    91     limit = limit - (show_for_initiative and 1 or 0)
    92     if initiatives_count > limit then
    93       more_initiatives_count = initiatives_count - limit
    94     end
    95     initiatives_selector:limit(limit)
    96   end
    98   local expandable = param.get("expandable", atom.boolean)
   100   local issue = param.get("issue", "table")
   102   local name = "initiative_list"
   103   if issue then
   104     name = "issue_" .. tostring(issue.id) ..  "_initiative_list"
   105   end
   107   ui.add_partial_param_names{ name }
   109   local order_filter = {
   110     name = name,
   111     label = _"Order by"
   112   }
   114   if issue and issue.ranks_available then
   115     order_filter[#order_filter+1] = {
   116       name = "rank",
   117       label = _"Rank",
   118       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
   119     }
   120   end
   122   order_filter[#order_filter+1] = {
   123     name = "potential_support",
   124     label = _"Potential support",
   125     selector_modifier = function(selector) selector:add_order_by("initiative.supporter_count::float / issue.population::float DESC, initiative.id") end
   126   }
   128   order_filter[#order_filter+1] = {
   129     name = "support",
   130     label = _"Support",
   131     selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
   132   }
   134   order_filter[#order_filter+1] = {
   135     name = "newest",
   136     label = _"Newest",
   137     selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
   138   }
   140   order_filter[#order_filter+1] = {
   141     name = "oldest",
   142     label = _"Oldest",
   143     selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
   144   }
   146   ui_filters = ui.filters
   148   if no_sort then
   149     ui_filters = function(args) args.content() end
   150     if issue.ranks_available then
   151       initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
   152     else
   153       initiatives_selector:add_order_by("initiative.supporter_count::float / issue.population::float DESC, initiative.id")
   154     end
   155   end
   157   ui_filters{
   158     label = _"Change order",
   159     order_filter,
   160     selector = initiatives_selector,
   161     content = function()
   162       ui.paginate{
   163         name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
   164         selector = initiatives_selector,
   165         per_page = param.get("per_page", atom.number),
   166         content = function()
   167           local initiatives = initiatives_selector:exec()
   168           for i, initiative in ipairs(initiatives) do
   169             local expanded = config.user_tab_mode == "accordeon_all_expanded" and expandable or
   170               show_for_initiative and initiative.id == show_for_initiative.id
   171             if expanded then
   172               ui.script{ script = "lf_initiative_expanded['initiative_content_" .. tostring(initiative.id) .. "'] = true;" }
   173             end
   174             execute.view{
   175               module = "initiative",
   176               view = "_list_element",
   177               params = {
   178                 initiative = initiative,
   179                 expanded = expanded,
   180                 expandable = expandable
   181               }
   182             }
   183           end
   184         end
   185       }
   186     end
   187   }
   189   if more_initiatives_count then
   190     ui.link{
   191       attr = { style = "font-size: 75%; font-style: italic;" },
   192       content = _("and #{count} more initiatives", { count = more_initiatives_count }),
   193       module = "issue",
   194       view = "show",
   195       id = issue.id,
   196     }
   197   end
   199 end
   201 if show_for_issue then
   202   slot.put("<br />")
   204   if issue and initiatives_count == 1 then
   205     ui.container{
   206       content = function()
   207         if issue.fully_frozen or issue.closed then
   208           slot.put(_"There were no more alternative initiatives.")
   209         else
   210           slot.put(_"There are no more alternative initiatives currently.")
   211         end
   212       end
   213     }
   214   end
   216   if app.session.member_id and not (issue.fully_frozen or issue.closed) then
   217     slot.put(" ")
   218     ui.link{
   219       content = function()
   220         ui.image{ static = "icons/16/script_add.png" }
   221         slot.put(" ")
   222         slot.put(_"Create alternative initiative")
   223       end,
   224       module = "initiative",
   225       view = "new",
   226       params = { issue_id = issue.id }
   227     }
   228   end
   229 end
