forked from awaln/datapress
/
head-start-exhibit.js.php
195 lines (169 loc) · 5.89 KB
/
head-start-exhibit.js.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
<?php
if (!$guessurl = site_url())
$guessurl = wp_guess_url();
$baseuri = $guessurl;
$exhibituri = $baseuri . '/wp-content/plugins/datapress';
require_once('wp-exhibit-geocoder.php');
global $exhibits_to_show;
?>
// Start Exhibit Manually
Exhibit.Functions["contains"] = {
f: function(args) {
var result = args[0].size > 0;
var set = args[0].getSet();
args[1].forEachValue(function(v) {
if (!set.contains(v)) {
result = false;
return true;
}
});
return new Exhibit.Expression._Collection([ result ? "true" : "false" ], "boolean");
}
};
SimileAjax.jQuery(document).ready(function() {
<?php
$needGeocoding = false;
if (isset($exhibits_to_show) && (count($exhibits_to_show) > 0)) {
foreach ($exhibits_to_show as $exhibit_to_show) {
if(WpExhibitGeocoder::doesExhibitContainGeocodedData($exhibit_to_show->get('id'))) {
$needGeocoding = true;
}
}
}
?>
window.database = Exhibit.Database.create();
// If we don't need geocoding, load data links with onAllDataLoaded callback.
// IF we do need geocoding, load data links with updateGeocode callback
<?php if ($needGeocoding) { ?>
window.database.loadDataLinks(updateGeocode);
<?php } else { ?>
window.database.loadDataLinks(onAllDataLoaded);
<?php } ?>
});
function getItemProps(propertyName) {
var ret = new Array();
var items = window.database.getAllItems();
items.visit(function(item) {
var obj = window.database.getObject(item, propertyName);
if(obj != null) {
ret[item] = obj;
}
// else {
// ret[item] = "";
// }
});
return ret;
}
function updateGeocode() {
var geoExId = "";
var geoAddressField = "";
var geoIds = [];
var geoAddresses = [];
<?php
/* Step 0: Figure out how many calls to the geocoder we will make. */
$callBacks = 0;
if (isset($exhibits_to_show) && (count($exhibits_to_show) > 0)) {
foreach ($exhibits_to_show as $exhibit_to_show) {
$exhibit_id = $exhibit_to_show->get('id');
$fields = WpExhibitGeocoder::getGeocodedFieldsForExhibit($exhibit_id);
foreach ($fields as $field) {
$callBacks += 1;
}
}
}
?>
var callBacks = <?php echo $callBacks ?>;
<?php
/* Step 1: Figure out what fields we need to geocode. */
if (isset($exhibits_to_show) && (count($exhibits_to_show) > 0)) {
foreach ($exhibits_to_show as $exhibit_to_show) {
$exhibit_id = $exhibit_to_show->get('id');
$fields = WpExhibitGeocoder::getGeocodedFieldsForExhibit($exhibit_id);
foreach ($fields as $field) {
/* Step 2: Build up a big list of all the data (itemID, field, value) for these fields */
?>
geoExId = "<?php echo $exhibit_id ?>";
geoAddressField = "<?php echo $field ?>";
geoIds = Array();
geoAddresses = Array();
// TOOD: getItemProps
var itemProps = getItemProps(geoAddressField);
var i = 0;
for(key2 in itemProps) {
geoAddresses[i] = itemProps[key2];
geoIds[i] = key2;
i++;
}
// Step 3: Call wp-exhibit-geocoder.php with that data
try {
var payload = {'exhibitid': geoExId, 'datumids[]': geoIds, 'addresses[]': geoAddresses, 'addressField': geoAddressField};
console.log(payload);
SimileAjax.jQuery.post(
<? echo("'$exhibituri/wp-exhibit-geocode.php'"); ?>,
payload,
function(data) {
SimileAjax. jQuery('head').append("<?php echo("<link href='$exhibituri/wp-exhibit-geocode.php?exhibit-id=$exhibit_id' type='application/json' rel='exhibit/data' alt='geocoded_data' />") ?>");
callBacks -= 1;
if (callBacks == 0) {
window.database = Exhibit.Database.create();
window.database.loadDataLinks(onAllDataLoaded);
}
}
);
} catch(e) {
console.log(e);
} // Try catch
<?php
} // Foreach field
} // For each exhibit
} // If exhibits
?>
};
function onAllDataLoaded() {
window.exhibit = Exhibit.create();
createCollections();
window.exhibit.configureFromDOM();
};
function createCollections() {
var auto_union = new Exhibit.Collection.create2("auto_union", {}, window.exhibit.getUIContext());
var collection_all = new Exhibit.Collection("collection_all", window.database);
window.exhibit.setCollection("auto_union", auto_union);
window.exhibit.setCollection("collection_all", collection_all);
/*
* For each type of item, create a collection
*/
var types = window.database._types;
var collections = {};
for (var key in types) {
// if (key != "Item") {
var id = types[key].getID();
collections[id] = Exhibit.Collection.create2(
'collection_'+id,
{
baseCollectionID: 'auto_union',
expression:"filter(value, contains(.type, '" + id + "'))"
},
window.exhibit.getUIContext()
);
window.exhibit.setCollection('collection_' + id, collections[id]);
// }
}
collection_all._update = function() {
var dunnit = false;
for (var key in collections) {
if (!dunnit) {
this._items = new Exhibit.Set(collections[key].getRestrictedItems());
dunnit = true;
}
else {
this._items.addSet(collections[key].getRestrictedItems());
}
}
this._onRootItemsChanged();
};
collection_all._listener = { onItemsChanged: function() { collection_all._update(); } };
for (var key in types) {
collections[id].addListener(collection_all._listener);
}
collection_all._update();
};