liquid_feedback_frontend
view app/main/initiative/_list.lua @ 278:fecd4c13054a
Code/css clean up and minor enhancements
author | bsw |
---|---|
date | Mon Feb 13 01:53:41 2012 +0100 (2012-02-13) |
parents | aec9df5b4cd3 |
children | 6c88b4bfb56c |
line source
1 local issue = param.get("issue", "table")
3 local initiatives_selector = param.get("initiatives_selector", "table")
5 local highlight_initiative = param.get("highlight_initiative", "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 initiatives_selector:add_where{ "initiative.id != ?", show_for_initiative.id }
58 execute.view{
59 module = "initiative",
60 view = "_list_element",
61 params = {
62 initiative = show_for_initiative,
63 }
64 }
65 if show_for_issue then
66 slot.put("<br />")
67 ui.container{
68 attr = { style = "font-weight: bold;" },
69 content = function()
70 slot.put(_"Alternative initiatives")
71 end
72 }
73 end
74 elseif show_for_issue then
75 ui.container{
76 attr = { style = "font-weight: bold;" },
77 content = function()
78 slot.put(_"Alternative initiatives")
79 end
80 }
81 end
83 if not show_for_initiative or initiatives_count > 1 then
86 local more_initiatives_count
87 if limit then
88 limit = limit - (show_for_initiative and 1 or 0)
89 if initiatives_count > limit then
90 more_initiatives_count = initiatives_count - limit
91 end
92 initiatives_selector:limit(limit)
93 end
95 local issue = param.get("issue", "table")
97 local name = "initiative_list"
98 if issue then
99 name = "issue_" .. tostring(issue.id) .. "_initiative_list"
100 end
102 ui.add_partial_param_names{ name }
104 local order_filter = {
105 name = name,
106 label = _"Order by"
107 }
109 if issue and issue.ranks_available then
110 order_filter[#order_filter+1] = {
111 name = "rank",
112 label = _"Rank",
113 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
114 }
115 end
117 order_filter[#order_filter+1] = {
118 name = "potential_support",
119 label = _"Potential support",
120 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
121 }
123 order_filter[#order_filter+1] = {
124 name = "support",
125 label = _"Support",
126 selector_modifier = function(selector) selector:add_order_by("initiative.satisfied_supporter_count::float / issue.population::float DESC, initiative.id") end
127 }
129 order_filter[#order_filter+1] = {
130 name = "newest",
131 label = _"Newest",
132 selector_modifier = function(selector) selector:add_order_by("initiative.created DESC, initiative.id") end
133 }
135 order_filter[#order_filter+1] = {
136 name = "oldest",
137 label = _"Oldest",
138 selector_modifier = function(selector) selector:add_order_by("initiative.created, initiative.id") end
139 }
141 ui_filters = ui.filters
143 if no_sort then
144 ui_filters = function(args) args.content() end
145 if issue.ranks_available then
146 initiatives_selector:add_order_by("initiative.rank, initiative.admitted DESC, vote_ratio(initiative.positive_votes, initiative.negative_votes) DESC, initiative.id")
147 else
148 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")
149 end
150 end
152 ui_filters{
153 label = _"Change order",
154 order_filter,
155 selector = initiatives_selector,
156 content = function()
157 ui.paginate{
158 name = issue and "issue_" .. tostring(issue.id) .. "_page" or nil,
159 selector = initiatives_selector,
160 per_page = param.get("per_page", atom.number) or limit,
161 content = function()
162 local initiatives = initiatives_selector:exec()
163 if highlight_initiative then
164 local highlight_initiative_found
165 for i, initiative in ipairs(initiatives) do
166 if initiative.id == highlight_initiative.id then
167 highhighlight_initiative_found = true
168 end
169 end
170 if not highhighlight_initiative_found then
171 initiatives[#initiatives+1] = highlight_initiative
172 if more_initiatives_count then
173 more_initiatives_count = more_initiatives_count - 1
174 end
175 end
176 end
177 for i, initiative in ipairs(initiatives) do
178 execute.view{
179 module = "initiative",
180 view = "_list_element",
181 params = {
182 initiative = initiative,
183 selected = highlight_initiative and highlight_initiative.id == initiative.id or nil,
184 }
185 }
186 end
187 end
188 }
189 end
190 }
192 if more_initiatives_count then
193 local text
194 if more_initiatives_count == 1 then
195 text = _("and one more initiative")
196 else
197 text = _("and #{count} more initiatives", { count = more_initiatives_count })
198 end
199 ui.link{
200 attr = { class = "more_initiatives_link" },
201 content = text,
202 module = "issue",
203 view = "show",
204 id = issue.id,
205 }
206 end
208 end
210 if show_for_issue then
211 slot.put("<br />")
213 if issue and initiatives_count == 1 then
214 ui.container{
215 content = function()
216 if issue.fully_frozen or issue.closed then
217 slot.put(_"There were no more alternative initiatives.")
218 else
219 slot.put(_"There are no more alternative initiatives currently.")
220 end
221 end
222 }
223 end
225 end