-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateDEM.php
349 lines (317 loc) · 12.2 KB
/
StateDEM.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
include_once('Graph.php');
require_once("$nodeViz_config[application_path]/config.php");
$global_edges = array();
setlocale(LC_MONETARY, 'en_US.UTF-8');
$db = dbconnect();
/*
creates the graph data structure that will be used to pass data among components. Structure must not change
*/
class StateDEM extends Graph {
function __construct() {
parent::__construct();
// gives the classes of nodes
$this->data['nodetypes'] = array('candidates', 'donors', 'zerocontribs'); //FIXME - add properties for types
// gives the classes of edges, and which nodes types they will connect
$this->data['edgetypes'] = array( 'donations' => array('donors', 'candidates'), 'zeroes'=>array('donors', 'donors'));
// graph level properties
$this->data['properties'] = array(
//sets the scaling of the elements in the gui
//'minSize' => array('donors'=>'.5', 'candidates' => '.5', 'donations'=>'10'),
//'maxSize' => array('donors'=>'4', 'candidates' => '4', 'donations' =>'90'),
'minSize' => array('donors'=>'1', 'candidates' => '1', 'donations'=>'10'),
'maxSize' => array('donors'=>'5', 'candidates' => '5', 'donations' =>'90'),
'log_scaling' => 0,
'state'=>'AL',
'cycle'=>'2006',
'chamber'=>'state:upper',
'valueMin'=>'0',
'edgeid'=>'',
'candidate_ids'=>'',
'company_ids'=>''
);
// special properties for controling layout software
//BROKEN, USE GV PARAMS
$this->data['graphvizProperties'] = array(
'graph'=> array(
'bgcolor'=>'#FFFFFF',
'size' => '6.52,6.52!',
'fontsize'=>2,
'splines'=>'curved',
'fontcolor'=>'blue',
'start'=>'15',
'layoutEngine'=>'neato',
'overlap'=>'false',
//'overlap'=>'vpsc', //It's complaining about this for some reason
//'sep'=>"+10"
//'maxiter' => '100000', //turning this off speeds things up, but does it mean that some might not converge?
),
'node'=> array('imagescale'=>'true','fixedsize'=>1, 'style'=> 'setlinewidth(4), filled', 'regular'=>'true','fontsize'=>2, 'margin'=>0,0),
'edge'=>array('arrowhead'=>'none', 'color'=>'#99999966', 'len'=>4, 'minlen'=>4, 'style'=>'tapered', 'tailclip'=>'false')
);
srand(20); //Don't copy this - this just makes sure that we are generating the same 'random' values each time
}
/**
There must be a <nodetype_fetchNodes() function for each defined node type.
It returns the ids of the nodes for the type.
**/
function candidates_fetchNodes() {
global $min_cycle;
global $max_cycle;
$nodes = array();
$props = $this->data['properties'];
$where = "a.state='".$props['state']."'";
if ($props['chamber'] != 'state:all') {
$where .= " and a.seat = '$props[chamber]'";
} else {
$where .= " and a.seat != 'state:governor' ";
}
if ($props['cycle'] != 'all') {
$where .= " and a.term=".$props['cycle'];
} else {
$where .= " and a.term between $min_cycle and $max_cycle ";
}
if ($props['candidate_ids']) {
$canids = arrayToInString(explode(',', $props['candidate_ids']));
$where = "a.imsp_candidate_id in ($canids)";
}
$query = "select imsp_candidate_id as id, a.state, a.term, a.district, a.party, full_name candidate_name, concat(last_name, ', ', first_name) as lastfirst, image, lifetime_total, twitter, facebook, action_link from legislator_terms a join legislators b on imsp_candidate_id = nimsp_candidate_id left join action_links on entity_id = nimsp_candidate_id and company=0 where $where";
writelog($query);
$this->addquery('fetch_candidates', $query);
foreach(dbLookupArray($query) as $can) {
$can['total_dollars'] = 0;
$nodes[$can['id']] = $can;
}
return $nodes;
}
function donors_fetchNodes() {
$nodes = array();
global $global_edges;
global $min_cycle;
global $max_cycle;
$props = $this->data['properties'];
$cans = $this->getNodesByType('candidates');
$cycle = "cycle = '$props[cycle]' and ";
if ($props['cycle'] == 'all') {
$cycle = " cycle between $min_cycle and $max_cycle and ";
//if ($props['chamber'] != 'state:governor') {
// $cycle .= "a.seat='$props[chamber]' and ";
//}
}
//($props['candidate_ids'] || ($props['cycle'] == 'all' && $props['chamber'] != 'state:governor')) ? " cycle between $min_cycle and $max_cycle and a.seat='$props[chamber]' and " : "cycle='$props[cycle]' and";
$companies = $props['company_ids'] ? "company_id in (".arrayToInString(explode(',', $props['company_ids'])).") and" : "";
$trim_terms = $props['cycle'] == 'all' ? " join legislator_terms l on recipient_ext_id = imsp_candidate_id and cycle = term and l.seat='$props[chamber]' " : "";
$basicContribQuery = "
select transaction_id, company_id, c.name as company_name, sum(amount) as amount, recipient_ext_id, b.name as industry, c.image_name as image, d.dem_type as sitecode, contributor_type, (d.coal_related + d.oil_related + d.carbon_related) as lifetime_total, action_link from contributions_dem a join catcodes b on code = contributor_category join companies c on c.id = a.company_id join companies_state_details d using(company_id) $trim_terms left join action_links on entity_id = company_id and company = 1 where $cycle $companies recipient_ext_id in (".arrayToInString($cans, 1).") and company_name != '' group by a.company_id, recipient_name order by sum(amount) desc
";
$this->addquery('fetch_donors', $basicContribQuery);
writelog('before donor query');
$contribs = dbLookupArray($basicContribQuery);
writelog('after donor query');
foreach ($contribs as $donor) {
$donor['amount'] = round($donor['amount']);
$id = 'co-'.trim($donor['company_id']);
if (isset($nodes[$id])) {
$nodes[$id]['total_dollars'] += $donor['amount'];
} else {
$nodes[$id] = $donor;
$nodes[$id]['id'] = $id;
$nodes[$id]['total_dollars'] = $donor['amount'];
}
if ($nodes[$id]['image']) {
if (! stristr($nodes[$id]['image'], 'com_images')) {
$nodes[$id]['image'] = "../www/com_images/c".$nodes[$id]['image'].".png";
}
} else {
$nodes[$id]['image'] = "../www/com_images/cunknown_".$nodes[$id]['sitecode']."_co.png";
}
if(isset($this->data['nodes'][$donor['recipient_ext_id']])) {
$this->data['nodes'][$donor['recipient_ext_id']]['total_dollars'] += $donor['amount'];
}
$edgeid = $donor['recipient_ext_id'].'_'.$id;
if (isset($global_edges[$edgeid])) {
$global_edges[$edgeid]['value'] += $donor['amount'];
} else {
$global_edges[$edgeid] = array(
'toId'=>$donor['recipient_ext_id'],
'fromId'=>$id,
'value'=>$donor['amount'],
'id'=>$edgeid
);
}
$id = null;
$edgeid = null;
$donor = null;
}
$contribs = null;
return $nodes;
}
function zerocontribs_fetchNodes() {
$nodes = array();
$nodes['zerocontribs'] = array('id'=>'zerocontribs', 'label'=>'','value'=>0, 'shape'=>'circle', 'size'=>0, 'area'=>0, 'type'=>'companies');
return $nodes;
}
function candidates_nodeProperties($nodes) {
foreach ($nodes as &$node) {
//if ($node['total_dollars'] <= $this->data['properties']['valueMin']) { writelog($node['id']); unset($nodes[$node['id']]); continue; }
$node['value'] = $node['total_dollars'];
$node['color'] = colorize($node['party']);
$node['title'] = $node['candidate_name'];
$node['fillcolor'] = '#ffffffff';
$node['tooltip'] = $node['title'];
#$node['fill-opacity'] = .5;
$node['shape'] = 'square';
#$node['label_zoom_level'] = '10';
//$node['click'] = "this.selectNode('".$node['id']."'); this.panToNode('".$node['id']."');";
$node['image'] = $node['image'] ? "../www/can_images/$node[id].jpg" : "../www/can_images/unknownCandidate.jpg";
if($node['value'] == 0) { $node['class'] = 'zero'; }
}
//$nodes = $this->scaleSizes($nodes, 'candidates', 'value');
return $nodes;
}
function donors_nodeProperties($nodes) {
foreach ($nodes as $node) {
//if ($node['total_dollars'] <= $this->data['properties']['valueMin']) { writelog($node['id']); unset($nodes[$node['id']]); continue; }
$node['title'] = ucwords(trim($node['company_name']));
$node['value'] = $node['total_dollars'];
$node['dir'] = getcwd();
//if ($node['contributor_type'] == 'I') {
$node['color'] = colorize($node['sitecode']);
$node['shape'] = 'circle';
//} elseif ($node['contributor_type'] == 'C') {
// $node['color'] = 'purple';
// $node['shape'] = 'polygon';
// $node['sides'] = '9';
//} else {
// $node['color'] = 'orange';
// $node['shape'] = 'triangle';
//}
$node['fillcolor'] = "#ffffffff";
$node['tooltip'] = $node['title'];
#$node['label_zoom_level'] = '9';
//$node['click'] = "this.selectNode('".$node['id']."'); this.panToNode('".$node['id']."');";
$nodes[$node['id']] = $node;
$node = null;
}
//$nodes = $this->scaleSizes($nodes, 'donors', 'value');
return $nodes;
}
function donations_fetchEdges() {
global $global_edges;
$edges = $global_edges;
$global_edges = null;
return $edges;
}
function zeroes_fetchEdges() {
return array();
}
function zeroes_edgeProperties() {
$edges = array();
foreach ($this->data['nodes'] as $node) {
if ($node['value'] ==0 && $node['type'] == 'candidates') {
$id = "zerocontribs_$node[id]";
$edge = array(
'id'=>$id,
'toId'=>$node['id'],
'fromId'=>'zerocontribs',
'len'=>'',
'value'=>0,
'weight'=>0,
'style'=>'invis',
'type'=>'zeroes'
);
$edges[$id] = $edge;
}
}
return $edges;
}
function donations_edgeProperties($edges) {
#print "".memory_get_usage()." edgetype\n";
$x = 0;
foreach ($edges as $edge) {
unset($edges[$edge['id']]);
if (! isset($this->data['nodes'][$edge['toId']]) || ! isset($this->data['nodes'][$edge['fromId']])) {
continue;
}
if (! $edge['value']) { continue; } //we don't need $0 edges
$edge['weight'] = $edge['value'];
$edge['tooltip'] = money_format('%.0n', $edge['value'])." from ".$this->data['nodes'][$edge['fromId']]['title']." to ".$this->data['nodes'][$edge['toId']]['title'];
$edges[$edge['id']] = $edge;
$x++;
}
#print "$x\n";
#print "".memory_get_usage()." edgetype\n";
//$edges = $this->scaleSizes($edges, 'donations', 'value');
return $edges;
}
function preProcessGraph() {
foreach ($this->data['properties'] as &$prop) {
if(gettype($prop) == "string") {
$prop = dbEscape($prop);
}
}
}
function postProcessGraph() {
writelog("Process");
$nodes = $this->data['nodes'];
#print "post ".memory_get_usage()."\n";
$nodes = $this->scaleSizes($nodes, 'donors', 'value');
$nodes = $this->scaleSizes($nodes, 'candidates', 'value');
uasort($nodes, function($a, $b) { return $a['value'] > $b['value']; }) ;
$this->data['nodes'] = $nodes;
$edges = $this->data['edges'];
$edges = $this->scaleSizes($edges, 'donations', 'value');
uasort($edges, function($a, $b) { return $a['value'] > $b['value']; }) ;
$this->data['edges'] = $edges;
global $edgestore_tag;
if($edgestore_tag != '') {
$name = $this->graphname();
foreach(array_keys($this->data['edges']) as $edgeid) {
$edge = $this->data['edges'][$edgeid];
$setstring = "tag='$edgestore_tag', graphname='$name', type='$edge[type]', toid='$edge[toId]', fromid='$edge[fromId]', value=$edge[value]";
dbwrite("insert into edgestore set $setstring on duplicate key update $setstring" );
}
}
}
function getSubgraphs() {
return;
$nodes = &$this->data['nodes'];
$subnodes = array();
foreach ($nodes as $node) {
if ($node['value'] == 0) {
$subnodes[] = $node['id'];
}
}
$this->data['subgraphs']['cluster_zerocontribs'] = array();
$this->data['subgraphs']['cluster_zerocontribs']['properties'] = array(
'pad'=>'500',
'rank'=>'min',
'style'=>'rounded',
//'label'=>"Accepted No DEM"
);
$this->data['subgraphs']['cluster_zerocontribs']['nodes'] = $subnodes;
}
function graphname() {
$props = $this->data['properties'];
return implode('_', array($props['state'], $props['cycle'], $props['chamber']));
}
}
function colorize($value){
$value = strtoupper(trim($value));
$colors = array(
"REPUBLICAN"=>"#cc6666ff",
"R"=>"#cc6666ff",
"DEMOCRAT"=>"#6699ccff",
"D"=>"#6699ccff",
"GREEN"=>"#33cc33ff",
"G"=>"#33cc33ff",
"LIBERTARIAN"=>"#cc33ccff",
"L"=>"#cc33ccff",
"PEACE & FREEDOM"=>"#33ccccff",
'OIL'=>'#6d8f9dff',
'COAL'=>'#958d63ff',
'CARBON'=>'#666666ff'
);
$color = isset($colors[$value]) ? $colors[$value] : "#CCCC33ff";
return($color);
}