-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions.display.php
executable file
·263 lines (225 loc) · 9.28 KB
/
functions.display.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
257
258
259
260
261
262
263
<?php
$filter_tag = intval( &$_COOKIE['filter_tag'] );
$filter_tag_label = '';
if ( $filter_tag )
$filter_tag_label = $db->get_var( "select name from tags where id = {$filter_tag}" );
$filter_constituency_labels = array(
'0' => false,
'constituent' => 'constituent',
'not_constituent' => 'not constituent',
'unjudged' => 'unjudged'
);
$filter_constituency = &$_COOKIE['filter_constituency'];
if ( !isset($filter_constituency_labels[$filter_constituency]) )
$filter_constituency = '0';
$filter_constituency_label = $filter_constituency_labels[$filter_constituency];
function formatDisplayEntry($content, $text, $url) {
$content = splitSentences($content);
$esc_link = preg_quote(trim($text), '!');
$esc_url = preg_quote($url, '!');
$esc_link = preg_replace('!\s!', '\s', $esc_link);
$esc_link = str_replace('\.', '\.(\n?)', $esc_link);
$regex = "!^.*<a href=['\"]{$esc_url}['\"]>\s*{$esc_link}\s*</a>.*$!im";
preg_match($regex, $content, $match);
if (isset($match[0]))
$text = $match[0];
// make the desired link colored.
$text = preg_replace("!<a href=[\"']{$esc_url}[\"']!", '$0 class="desired-link"', $text);
// process text slightly to remove unsightly things
$text = trim($text);
$text = preg_replace('/^<br>/', '', $text);
return $text;
}
function permalink($entry, $id) {
$args = array(
'entry' => $entry,
'id' => $id
);
if ( isset($_GET['debug']) )
$args['debug'] = true;
if ( isset($_GET['tables']) )
$args['tables'] = $_GET['tables'];
return 'display.php?' . http_build_query($args);
}
function filterSql() {
global $db, $filter_tag, $filter_constituency, $filter_sql;
$sql = "select base.entry, base.id from links as base";
if ( $filter_constituency == 'unjudged' )
$sql .= " left join link_constituency as lc on (base.entry = lc.entry and base.id = lc.id) where lc.constituency is null";
if ( $filter_tag ) {
$sql = "select base.entry, base.id as id from tags_xref as base";
if ( $filter_constituency == 'unjudged' )
$sql .= " left join link_constituency as lc on (base.entry = lc.entry and base.id = lc.id and tid = {$filter_tag}) where lc.constituency is null and tid = {$filter_tag}";
else
$sql .= " where tid = {$filter_tag}";
}
if ( $filter_constituency && $filter_constituency != 'unjudged' ) {
$sql = "select base.entry, base.id from (select * from (select * from link_constituency order by date desc) as raw_lc group by entry, id) as base";
if ( $filter_tag )
$sql .= " join tags_xref on (base.entry = tags_xref.entry and base.id = tags_xref.id and tid = {$filter_tag})";
$sql .= " where base.constituency = '$filter_constituency'";
}
$filter_sql = $sql;
return $sql;
}
function randomLink() {
global $db;
$sql = filterSql();
$sql .= " order by RAND() limit 1";
$args = $db->get_row( $sql, ARRAY_A );
if ( empty($args) )
return '#';
if ( isset($_GET['debug']) )
$args['debug'] = 1;
return 'display.php?' . http_build_query($args);
}
// $dir == 'next' or 'prev'
function getNextPrevLink( $entry, $id, $dir ) {
global $db;
$compare = $dir == 'next' ? '>' : '<';
$order = $compare == '>' ? 'asc' : 'desc';
$sql = filterSql();
$sql .= " and base.entry $compare $entry or (base.entry = $entry and base.id $compare $id) order by base.entry $order, base.id $order limit 1";
$result = $db->get_row( $sql );
if ( $result === false || empty($result) )
return false;
return permalink($result->entry, $result->id);
}
function head($type, $title) {
?>
<head>
<title>The <a>constituent</a> Project | <?php echo $title; ?></title>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
<link type="text/css" rel="stylesheet" href="display.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-tabs.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-dropdown.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-alerts.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-twipsy.js"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/1.4.0/bootstrap-modal.js"></script>
<script type="text/javascript" src="js/jquery.cookie.js"></script>
<?php if ( file_exists("$type.js") ) {
echo "<script type='text/javascript' src='$type.js'></script>";
} ?>
<script type="text/javascript">
$(function() {
$('.tabs').tabs();
$('.topbar').dropdown();
$('[rel=twipsy], a').twipsy();
$('.modal').modal({backdrop: true, keyboard: true});
});
</script>
<?php
if ( stristr($_SERVER['HTTP_HOST'], 'mit.edu') !== false ):?>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19567124-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<?php endif;?>
</head>
<?php
}
function nav($type, $subtype = false) {
global $random, $id, $entry, $filter_tag_label, $filter_constituency_label, $parse_type, $db;
?>
<div class="topbar">
<div class="topbar-inner">
<div class="container">
<a class="brand" href="http://constituency.mit.edu/"><a>constituent</a></a>
<ul class="nav">
<?php if ( $type == 'display' ): ?>
<li class="dropdown active">
<a class="dropdown-toggle" href="#">#<?php echo $entry; ?>:<?php echo $id; ?></a>
<ul class="dropdown-menu">
<!--<li><a href='<?php echo esc_url(permalink($entry, $id)); ?>' title='<?php echo esc_attr("Entry #$entry, link #$id"); ?>'>Permalink</a></li>-->
<li><a href='<?php echo esc_url("http://metafilter.com/$entry"); ?>' title='<?php echo esc_attr("Entry #$entry, link #$id"); ?>'>on MetaFilter</a></li>
<li class="divider"></li>
<li><a id='random-link' href="<?php echo randomLink(); ?>">Random <span class="accelerator">(R)</span></a></li>
</ul>
</li>
<?php else: ?>
<li><a href='display.php' title='Judgement'>Judgement</a></li>
<?php endif; ?>
<li class="dropdown<?php if ( $type == 'reports' ) echo " active";?>">
<a class="dropdown-toggle" href="#">Reports</a>
<ul class="dropdown-menu">
<li<?php if ( $type == 'reports' && $subtype == 'nonconstituents' ) echo ' class="active"'; ?>><a href="nonconstituents.php">Nonconstituents</a></li>
<li<?php if ( $type == 'reports' && $subtype == 'nonconstituents-tags' ) echo ' class="active"'; ?>><a href="nonconstituents-tags.php">Nonconstituents by Type</a></li>
</ul>
</li>
<li<?php if ( $type == 'history' ) echo " class='active'";?>><a href='history.php' title='User history'>History</a></li>
</ul>
<p class="pull-left">
<?php
$filters = array();
if ( $filter_tag_label )
$filters[] = $filter_tag_label;
if ( $filter_constituency_label )
$filters[] = $filter_constituency_label;
if ( $type == 'display' ) {
if ( !empty($filters) )
echo "<span class='label' data-controls-modal='options' style='margin-right: 10px;'>" . join(', ', $filters) . "</span>";
else
echo "<span class='label' data-controls-modal='options' style='margin-right: 10px;'>set options</span>";
}
?>
Logged in as <?php echo USERNAME; ?></p>
<ul class='nav secondary-nav'>
<?php if ( $type == 'display' ): ?>
<li class="dropdown" id='parse-control'>
<a class="dropdown-toggle" href="#">Parse: <?php echo $parse_type ? esc_html($parse_type) : 'None'; ?></a>
<ul class="dropdown-menu">
<?php foreach ( $db->get_col('select type from parses group by type') as $possible_type ): ?>
<li<?php if ( $possible_type === $parse_type ) echo ' class="active"'; ?> data-parse_type='<?php echo esc_attr($possible_type); ?>'><a href="#"><?php echo esc_html($possible_type); ?></a></li>
<?php endforeach; ?>
<li class="divider"></li>
<li<?php if ( false === $parse_type ) echo ' class="active"'; ?> data-parse_type=''><a href="#">None</a></li>
</ul>
</li>
<?php endif; ?>
</ul>
</div>
</div>
</div>
<div class="modal fade" id="options">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Options</h3>
</div>
<div class="modal-body">
<div class='clearfix'>
Auto-advance: <select id="move_forward">
<option value="off">off</option>
<option value="random">random</option>
<option value="next">next</option>
</select>
</div>
<div class='clearfix'>
Filters:
<select id="filter_tag">
<option value="0">No filter</option>
<?php foreach ( $db->get_results("select * from tags where parse_specific = 0") as $tag ):?>
<option value="<?php echo esc_attr($tag->id); ?>"><?php echo esc_html($tag->name); ?></option>
<?php endforeach; ?>
</select>
<select id="filter_constituency">
<option value="0">No filter</option>
<option value="constituent">Constituent</option>
<option value="not_constituent">Not constituent</option>
<option value="unjudged">Unjudged</option>
</select>
</div>
</div>
<!--<div class="modal-footer">
<a href="#" class="btn btn-primary">Save changes</a>
<a href="#" class="btn">Close</a>
</div>-->
</div>
<?php
}