This repository has been archived by the owner on Jun 26, 2018. It is now read-only.
/
list.php
256 lines (234 loc) · 9.74 KB
/
list.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?
// list.php:
// List all pledges.
//
// Copyright (c) 2005 UK Citizens Online Democracy. All rights reserved.
// Email: francis@mysociety.org. WWW: http://www.mysociety.org
//
// $Id: list.php,v 1.51 2008-01-02 19:08:22 matthew Exp $
require_once "../phplib/pb.php";
require_once '../phplib/fns.php';
require_once '../commonlib/phplib/importparams.php';
define('PAGE_SIZE', 50);
$err = importparams(
array('offset', '/^(0|[1-9]\d*)$/', '', 0),
array('sort', '/^(' . join('|', array_keys(microsites_list_sort_options())) . ')\/?$/', '', 'default'),
array('type', '/^[a-z_]*$/', '', 'all')
);
if ($err) {
err(_('Illegal offset or sort parameter passed'));
}
$viewsarray = microsites_list_views();
$viewsarray_keys = array_keys($viewsarray);
if ($q_type == 'all') $q_type = $viewsarray_keys[0];
$rss = get_http_var('rss') ? true : false;
// Old postcode RSS feed
if ($rss && get_http_var('postcode')) {
header("Location: /rss/search?q=" . urlencode(get_http_var('postcode')));
exit;
}
// Strip any trailing '/'.
$q_sort = preg_replace("#/$#", "", $q_sort);
if ($q_type == 'failed') {
$open = '<'; $succeeded = '<';
$open_byarea = '<'; $succeeded_byarea = ' = 0';
if ($q_sort == "default") $q_sort = "creationtime";
} elseif ($q_type == 'succeeded_closed') {
$open = '<'; $succeeded = '>=';
$open_byarea = '<'; $succeeded_byarea = ' > 0';
if ($q_sort == "default") $q_sort = $rss ? "whensucceeded" : "creationtime";
} elseif ($q_type == 'succeeded_open') {
$open = '>='; $succeeded = '>=';
$open_byarea = '>='; $succeeded_byarea = ' < 0'; # never allowed
if ($q_sort == "default") $q_sort = $rss ? "whensucceeded" : "date";
} elseif ($q_type == 'succeeded') {
$open = null; $succeeded = '>=';
$open_byarea = null; $succeeded_byarea = ' > 0';
if ($q_sort == "default") $q_sort = $rss ? "whensucceeded" : "creationtime";
} else { // open
$open = '>='; $succeeded = '<';
$open_byarea = '>='; $succeeded_byarea = null;
if ($q_sort == "default") $q_sort = $rss ? "creationtime" : "percentcomplete";
}
$date_range_clause = ($open ? " AND date $open '$pb_today' " : "");
if ($succeeded)
$signers_range_clause = "AND (SELECT count(*) FROM signers WHERE signers.pledge_id = pledges.id) $succeeded target";
else
$signers_range_clause = "";
$byarea_clause = ($open_byarea ? " date $open_byarea '$pb_today' AND " : "")
. ($succeeded_byarea ? "(SELECT count(*) FROM byarea_location WHERE
byarea_location.pledge_id = pledges.id AND byarea_location.whensucceeded IS NOT NULL)
$succeeded_byarea" : " (1=1)");
$page_clause = "AND ( (pledges.target_type = 'overall' $signers_range_clause $date_range_clause)
OR (pledges.target_type = 'byarea' AND $byarea_clause) )";
$limit_to_category = intval(get_http_var('category'));
if ($limit_to_category) {
$category_clause = ' AND (SELECT count(*) from pledge_category where pledge_category.pledge_id = pledges.id and pledge_category.category_id = '.$limit_to_category.') > 0 ';
} else {
$category_clause = '';
}
$sql_params = array();
$locale_clause = "(".
pb_site_pledge_filter_main($sql_params) .
' OR ' . pb_site_pledge_filter_general($sql_params).
")";
$query = "
SELECT count(pledges.id)
FROM pledges LEFT JOIN location ON location.id = pledges.location_id
WHERE $locale_clause AND pin IS NULL
$page_clause
$category_clause
AND (".microsites_normal_prominences()." OR cached_prominence = 'frontpage')";
$ntotal = db_getOne($query, $sql_params);
if ($ntotal < $q_offset) {
$q_offset = $ntotal - PAGE_SIZE;
if ($q_offset < 0)
$q_offset = 0;
}
if ($q_sort == 'percentcomplete' && microsites_no_target())
$q_sort = 'signers';
if ($q_sort == 'creationtime' || $q_sort == 'whensucceeded' || $q_sort == 'signers') {
$sort_phrase = "$q_sort DESC";
} elseif ($q_sort == 'percentcomplete') {
$sort_phrase = "(
(SELECT count(*) FROM signers WHERE signers.pledge_id = pledges.id)::numeric
/ target) DESC";
} elseif ($q_sort == 'category') {
$sort_phrase = "coalesce ((SELECT name FROM pledge_category, category WHERE
pledge_category.category_id = category.id AND parent_category_id IS NULL AND
pledge_category.pledge_id = pledges.id LIMIT 1), '"._("Miscellaneous")."')";
} else {
$sort_phrase = $q_sort;
}
$sql_params[] = PAGE_SIZE;
$qrows = db_query("
SELECT pledges.*, '$pb_today' <= pledges.date AS open,
(SELECT count(*) FROM signers WHERE signers.pledge_id = pledges.id) AS signers,
person.email AS email, address_1, address_postcode,
country, state, description, method, latitude, longitude
FROM pledges
LEFT JOIN person ON person.id = pledges.person_id
LEFT JOIN location ON location.id = pledges.location_id
WHERE $locale_clause
AND pin IS NULL
$page_clause
$category_clause
AND (".microsites_normal_prominences()." OR cached_prominence = 'frontpage')
ORDER BY $sort_phrase,pledges.id LIMIT ? OFFSET $q_offset", $sql_params);
/* PG bug: mustn't quote parameter of offset */
if ($q_type == 'open') {
$heading = _("Pledges which need signers");
if ($rss)
$heading = _('New pledges');
} elseif ($q_type == 'succeeded_open') {
$heading = _("Successful pledges, open to new signers");
} elseif ($q_type == 'succeeded_closed') {
$heading = _("Successful pledges, closed to new signers");
} elseif ($q_type == 'succeeded') {
$heading = _("Successful pledges");
} elseif ($q_type == 'failed') {
$heading = _("Failed pledges");
} else {
header("Location: /list");
exit;
}
if ($rss)
rss_header($heading, $heading, array());
else {
page_header($heading, array('id'=>'all',
'rss'=> array(
$heading => pb_domain_url(array('explicit'=>true, 'path'=>'/rss'.$_SERVER['REQUEST_URI']))
),
'cache-max-age' => 60,
));
}
if ($limit_to_category) {
$heading .= " " . _("(one category only)");
}
if (!$rss) {
?><a href="<?=pb_domain_url(array('explicit'=>true, 'path'=>"/rss".$_SERVER['REQUEST_URI']))?>"><img align="right" border="0" src="/rss.gif" alt="<?=_('RSS feed of ') . $heading ?>"></a><?
print '<div style="float:left;width:57%">';
print '<h2 class="head_with_mast">' . $heading . '</h2>';
pb_print_filter_link_main_general('class="head_mast" style="font-style:italic"');
print '</div>';
$views = '';
$c = 0;
foreach ($viewsarray as $s => $desc) {
$c++;
if ($q_type == $s) continue;
$views .= "<li><a href=\"/list/$s\">" . str_replace(' ', ' ', $desc) . '</a>';
}
$sort = ($q_sort) ? '&sort=' . $q_sort : '';
$off = ($q_offset) ? '&offset=' . $q_offset : '';
$prev = '<span class="greyed">« '._('Previous page').'</span>'; $next = '<span class="greyed">'._('Next page').' »</span>';
if ($q_offset > 0) {
$n = $q_offset - PAGE_SIZE;
if ($n < 0) $n = 0;
$prev = "<a href=\"?offset=$n$sort\">« "._('Previous page')."</a>";
}
if ($q_offset + PAGE_SIZE < $ntotal) {
$n = $q_offset + PAGE_SIZE;
$next = "<a href=\"?offset=$n$sort\">"._('Next page')." »</a>";
}
$navlinks1 = '<ul style="margin-top:0; float:right; width:30%">' . $views . "</ul>\n";
$navlinks2 = '';
$navlinks3 = '';
if ($ntotal > 0) {
$navlinks2 = '<p align="center" style="margin:0.5em 0;font-size: 89%">' . _('Sort by'). ': ';
$arr = microsites_list_sort_options();
$c = 0;
foreach ($arr as $s => $desc) {
$c ++;
if ($q_sort != $s) $navlinks2 .= "<a href=\"?sort=$s$off\">$desc</a>"; else $navlinks2 .= $desc;
if ($c != count($arr)) $navlinks2 .= ' | ';
}
$navlinks2 .= '</p>';
$navlinks3 = '<p align="center" style="margin:0.5em 0;">';
$navlinks3 .= $prev . ' | '._('Pledges'). ' ' . ($q_offset + 1) . ' – ' .
($q_offset + PAGE_SIZE > $ntotal ? $ntotal : $q_offset + PAGE_SIZE) . ' of ' .
$ntotal . ' | ' . $next;
$navlinks3 .= '</p>';
}
print $navlinks1.'<div style="clear:both"></div>'.$navlinks2.$navlinks3;
}
$rss_items = array();
if ($ntotal > 0) {
$c = 0;
$lastdivision = 'none';
while ($row = db_fetch_array($qrows)) {
$pledge = new Pledge($row);
if ($q_sort == "category") {
$categories = $pledge->categories();
$thiscategory = _("Miscellaneous");
$thiscategory_no = -1;
foreach ($categories as $thiscategory_no => $thiscategory) {
# just let variables fill in with last one
}
if ($lastdivision <> $thiscategory) {
if (!$rss)
print '<h2 style="clear:both">' . _($thiscategory) . "</h2> <!-- $thiscategory_no -->";
$c = 0;
$lastdivision = $thiscategory;
}
}
$arr = array('class'=>"pledge-".$c%2, 'href' => $pledge->url_main() );
if ($rss)
$rss_items[] = $pledge->rss_entry();
else
$pledge->render_box($arr);
$c++;
}
if (!$rss)
print "<br style=\"clear: both;\">$navlinks3$navlinks2";
} else {
if (!$rss)
print p(_('There are currently none.'));
}
if ($rss) {
rss_footer($rss_items);
} else {
if (microsites_new_pledges_prominence() != 'frontpage')
print '<p style="clear: both" align="center"><small>'._('New pledges are not shown here. <a href="/faq#allpledges">Read our FAQ</a> for details of when they appear.').'</small></p>';
page_footer();
}
?>