-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwhereAreReaders.php
151 lines (145 loc) · 4.14 KB
/
whereAreReaders.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
<?php
/*
Template Name: Where are readers
*/
?>
<?php
$us_state_abbrevs_names = array(
'AL'=>'Alabama',
'AK'=>'Alaska',
'AS'=>'American Samoa',
'AZ'=>'Arizona',
'AR'=>'Arkansas',
'CA'=>'California',
'CO'=>'Colorado',
'CT'=>'Connecticut',
'DE'=>'Delaware',
'DC'=>'District of Columbia',
'FM'=>'Federated States of Micronesia',
'FL'=>'Florida',
'GA'=>'Georgia',
'GU'=>'Guam Gu',
'HI'=>'Hawaii',
'ID'=>'Idaho',
'IL'=>'Illinois',
'IN'=>'Indiana',
'IA'=>'Iowa',
'KS'=>'Kansas',
'KY'=>'Kentucky',
'LA'=>'Louisiana',
'ME'=>'Maine',
'MH'=>'Marshall Islands',
'MD'=>'Maryland',
'MA'=>'Massachusetts',
'MI'=>'Michigan',
'MN'=>'Minnesota',
'MS'=>'Mississippi',
'MO'=>'Missouri',
'MT'=>'Montana',
'NE'=>'Nebraska',
'NV'=>'Nevada',
'NH'=>'New Hampshire',
'NJ'=>'New Jersey',
'NM'=>'New Mexico',
'NY'=>'New York',
'NC'=>'North Carolina',
'ND'=>'North Dakota',
'MP'=>'Northern Mariana Islands',
'OH'=>'Ohio',
'OK'=>'Oklahoma',
'OR'=>'Oregon',
'PW'=>'Palau',
'PA'=>'Pennsylvania',
'PR'=>'Puerto Rico',
'RI'=>'Rhode Island',
'SC'=>'South Carolina',
'SD'=>'South Dakota',
'TN'=>'Tennessee',
'TX'=>'Texas',
'UT'=>'Utah',
'VT'=>'Vermont',
'VI'=>'Virgin Islands',
'VA'=>'Virginia',
'WA'=>'Washington',
'WV'=>'West Virginia',
'WI'=>'Wisconsin',
'WY'=>'Wyoming',
'AE'=>'Armed Forces Africa \ Canada \ Europe \ Middle East',
'AA'=>'Armed Forces America (except Canada)',
'AP'=>'Armed Forces Pacific'
);
$userID = get_current_user_id();
$mine = getParam('mine', 0, '/1/') == '1';
$base = ABSPATH;
if ($userID && $mine) {
$mapname = "$base/Maps/map_$userID.json";
} else {
$mapname = "$base/Maps/map_0.json";
}
$exists = file_exists($mapname);
$view = array(
'user' => $userID,
'mine' => $mine,
'exists' => $exists
);
if ($exists) {
$content = file_get_contents($mapname);
$map = json_decode($content, true);
$countries = array();
foreach($map['countries'] as $country=>$count) {
$countries[] = array('name' => $country, 'count' => number_format($count));
}
sort($countries);
$states = array();
foreach($map['states'] as $state=>$count) {
$states[] = array('name' => $us_state_abbrevs_names[$state], 'count' => number_format($count));
}
sort($states);
$view['countries'] = $countries;
$view['country_count'] = count($countries);
$view['states'] = $states;
$view['state_count'] = count($states);
$view['total'] = number_format($map['total']);
$view['date'] = $map['date'];
$image = $map['image'] . '?bust=' . $map['total'];
if (strpos($image, '/var/tmp') === 0) {
$image = substr($image, 8);
}
$view['image'] = $image;
if ($mine == 0) {
global $wpdb;
$sql = "
select s.language, s.reviewed, count(s.language) as count
from wpreader_posts p join wpreader_book_search s on p.ID = s.ID
where p.post_status = 'publish' group by s.language, s.reviewed;";
$rows = $wpdb->get_results($sql);
$langName = array();
foreach($Templates['languages'] as $lang)
$langName[$lang['value']] = $lang['label'];
$totals = array();
$tb = 0;
$tr = 0;
foreach($rows as $row) {
$l = $langName[$row->language];
if (!array_key_exists($l, $totals)) {
$totals[$l] = array('language'=>$l, 'total'=>0, 'reviewed'=>0);
}
$totals[$l]['total'] += $row->count;
$tb += $row->count;
if ($row->reviewed == 'R') {
$totals[$l]['reviewed'] += $row->count;
$tr += $row->count;
}
}
function compare_lang($a, $b) {
return strnatcmp($a['language'], $b['language']);
}
usort($totals, 'compare_lang');
$totals['Total'] = array('language'=>'Total', 'total'=>$tb, 'reviewed'=>$tr);
$view['languages'] = array_values($totals);
}
}
thr_header('map-page');
echo template_render('whereAreReaders', $view);
thr_footer();
?>