forked from nicoknoll/BLNFMCalSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blnfmcalsync.php
315 lines (240 loc) · 9.39 KB
/
blnfmcalsync.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
<?php
/*
Plugin Name: BLN.FM Cal Sync
Description:
Author: Nico Knoll
Version: 2.2
Author URI: http://nico.is
*/
$blnfmSyncWarnings = array();
function getSpreadsheets() {
return array(
'1o1tSB-z8BWgmz1xifLCOzIGRv12S9cNuaR2D7WJS9Wg',
'13pRV-83bORr2lTjyvJo_byN093I00ysfx3l6RbdUWJI'
);
}
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function worksheetsToArray($id) {
$entries = array();
$i = 1;
while(true) {
$url = 'https://spreadsheets.google.com/feeds/list/'.$id.'/'.$i.'/public/full?alt=json';
$data = @json_decode(file_get_contents_curl($url), true);
$starttag = substr($data['feed']['title']["\$t"], 0, strpos($data['feed']['title']["\$t"], " "));
if(!isset($data['feed']['entry'])) break;
foreach($data['feed']['entry'] as $entryKey => $entry) {
$data['feed']['entry'][$entryKey]['gsx$starttag']["\$t"] = $starttag;
}
$entries = array_merge($entries, $data['feed']['entry']);
$i++;
}
return $entries;
}
function spreadsheetToArray($id) {
$entries = worksheetsToArray($id);
$return = array();
foreach($entries as $event) {
$return_event = array();
foreach($event as $key => $value) {
if(preg_match("%^gsx\\$%Uis", $key)) {
$key = str_replace('gsx$', '', $key);
if($key == '_cn6ca') $key = 'starttag';
if($key == 'starttag' || $key == 'endtag') {
$tmp = explode('.', $value["\$t"]);
if($value["\$t"] != '') $return_event[$key] = @date('Y-m-d', mktime(0,0,0,(int)$tmp[1],(int)$tmp[0],(int)$tmp[2]));
else $return_event[$key] = $value["\$t"];
} elseif($key == 'startzeit' || $key == 'endzeit') {
$tmp = explode(':', $value["\$t"]);
if($value["\$t"] != '') $return_event[$key] = @date('H:i:s', mktime((int)$tmp[0],(int)$tmp[1],0,0,0,0));
else $return_event[$key] = $value["\$t"];
} elseif($key == 'ganztägig' || $key == 'fortlaufend' || $key == 'recommended' || $key == 'promoted' || $key == 'team' || $key == 'ausverkauft') {
$return_event[$key] = ((trim($value["\$t"]) != '' && $value["\$t"] != 0) ? true : false);
} else {
$return_event[$key] = $value["\$t"];
}
}
}
$return[] = processData($return_event);
}
return $return;
}
function processData($eventData) {
global $blnfmSyncWarnings;
$id = '<b>'.$eventData['id'].'</b>';
if($eventData['endtag'] == '') {
$blnfmSyncWarnings[] = $id.': Endtag fehlt. Es wird jetzt angenommen, dass Endtag = Starttag ist.';
$eventData['endtag'] = $eventData['starttag'];
}
if($eventData['startzeit'] == '') {
$blnfmSyncWarnings[] = $id.': Startzeit fehlt und wird auf 23h gesetzt.';
// Todo:check which type the event is
$eventData['startzeit'] = @date('H:i:s', mktime(23,0,0,0,0,0));
}
if($eventData['endzeit'] == '') {
$blnfmSyncWarnings[] = $id.': Endzeit fehlt und wird auf 6h gesetzt.';
// Todo:check which type the event is
$eventData['endzeit'] = @date('H:i:s', mktime(6,0,0,0,0,0));
}
if(isset($eventData['ausverkauft'])) {
$blnfmSyncWarnings[] = $id.': Ausverkauft, Status wird auf "ausverkauft" gesetzt.';
$eventData['status'] = 'ausverkauft';
}
if(isset($eventData['verschoben'])) {
$blnfmSyncWarnings[] = $id.': Verschoben, Status wird auf "verschoben" gesetzt.';
$eventData['status'] = 'verschoben';
}
return $eventData;
}
/*
function addLocation($name) {
$em_location = new EM_Location();
$em_location->location_name = $name;
$em_location->location_address = $name;
$em_location->location_town = 'Berlin';
$em_location->location_state = 'Berlin';
$em_location->location_country = 'DE';
$em_location->save();
$em_location->save_meta();
return $em_location->location_id;
}
function getLocationIDs() {
$EM_Locations = EM_Locations::get();
$return = array();
foreach($EM_Locations as $EM_Location){
@$return[$EM_Location->name] = @$EM_Location->id;
}
return $return;
}
*/
function getPostIdByMetaValue($key, $value) {
global $wpdb;
$meta = $wpdb->get_results("SELECT * FROM `".$wpdb->postmeta."` WHERE meta_key='".esc_sql($key)."' AND meta_value='".esc_sql($value)."'");
if (is_array($meta) && !empty($meta) && isset($meta[0])) {
$meta = $meta[0];
}
if (is_object($meta)) {
return $meta->post_id;
} else {
return false;
}
}
function updateEvent($data) {
$em_event = em_get_event(getPostIdByMetaValue('_ss_id', $data['id']), 'post_id');
$check = true;
if(isset($data['command']) && $data['command'] == 'UPDATE') {
if(!($em_event->event_id)) $em_event = new EM_Event();
$em_event->event_start_date = $data["starttag"];
$em_event->event_start_time = $data["startzeit"];
$em_event->event_end_date = $data["endtag"];
$em_event->event_end_time = $data["endzeit"];
$em_event->start = strtotime($em_event->event_start_date." ".$em_event->event_start_time);
$em_event->end = strtotime($em_event->event_end_date." ".$em_event->event_end_time);
$em_event->location_id = (isset($data["venueid"]) ? $data["venueid"] : '');
$em_event->post_title = $data["titel"];
$em_event->event_name = $data["titel"];
//$em_event->body = (($data["kurzbeschreibung"]) ? $data["kurzbeschreibung"] : '');
$em_event->post_content = (isset($data["kurzbeschreibung"]) ? $data["kurzbeschreibung"] : '');
$em_event->post_excerpt = (isset($data["auszug"]) ? $data["auszug"] : '');
$em_event->post_tags = @$data["tags"];
// meta
$em_event->event_attributes = array(
'Status' => $data['status'],
'Line Up' => $data['lineup'],
'Stil' => (isset($data["stil"]) ? $data["stil"] : ''),
'Preis' => $data['preis'],
'Parent' => (isset($data["parentid"]) ? $data["parentid"] : ''),
'Team' => $data['team'],
'Recommended' => $data['recommended'],
'Promoted' => $data['promoted'],
'Gewinnspiel' => $data['gewinnspiel'],
'Kurzbeschreibung' => $data["kurzbeschreibung"]
);
$em_event->group_id = 0;
$em_event->event_date_modified = date('Y-m-d H:i:s', time());
$em_event->event_all_day = ($data['ganztägig'] ? 1 : 0);
$em_event->event_rsvp = 0;
$check = $em_event->save();
add_post_meta($em_event->post_id, '_ss_id', $data['id']);
// add category
$categories = array();
$type = new EM_Category(strtolower($data["veranstaltungstyp"]));
if(!$type->term_id) $type = new EM_Category("sonstiges");
array_push($categories, ($type->term_id));
if($data["recommended"]) array_push($categories, get_cat_ID('tipp'));
if($data["promoted"]) array_push($categories, get_cat_ID('sponsored'));
if($data["team"]) array_push($categories, get_cat_ID('team'));
if($data["veranstaltungstyp"]) array_push($categories, get_cat_ID(strtolower($data["veranstaltungstyp"])));
if($data["gewinnspiel"]) array_push($categories, get_cat_ID('gewinnspiel'));
if($data["preis"] != '' && $data["preis"] == 0) array_push($categories, get_cat_ID('kostenlos'));
if(count($categories)) wp_set_post_terms($em_event->post_id, $categories, 'event-categories', false);
// add tags
$tags = array();
if($data["tags"]) array_push($tags, $data["tags"]);
//if($data["ausverkauft"]) array_push($tags, "ausverkauft");
if($data["openair"]) array_push($tags, "open air");
if($data['lineup']) $tags = array_merge($tags, explode(',', $data['lineup']));
if($data['tags']) $tags = array_merge($tags, explode(',', $data['tags']));
if(count($tags)) wp_set_post_terms($em_event->post_id, $tags, 'event-tags', false);
} elseif(isset($data['command']) && $data['command'] == 'DELETE') {
if($em_event->event_id) $check = $em_event->delete(true);
}
return $check;
}
function updateEvents() {
foreach(getSpreadsheets() as $spreadsheet) {
$events = spreadsheetToArray($spreadsheet);
foreach($events as $event) {
// reload locations as we dynamically create them if missing
/*
$locations = getLocationIDs();
$event['locationid'] = @$locations[$event['venue']];
if($event['locationid'] == '') $event['locationid'] = addLocation($event['venue']);
*/
updateEvent($event);
}
}
}
function blnfmcalsync_page_function() {
global $blnfmSyncWarnings;
echo '<div class="wrap">';
if(@$_POST['syncnow']) {
echo '<div class="updated">
<p>Veranstaltungen sollten jetzt synchronisiert sein.</p>
</div>';
update_option( 'blnfmcalsync-lastsync', time());
updateEvents();
}
foreach($blnfmSyncWarnings as $blnfmSyncWarning) {
echo '<div class="notice notice-warning">
<p>'.$blnfmSyncWarning.'</p>
</div>';
}
echo '</div>';
$lastSynced = get_option( 'blnfmcalsync-lastsync' );
echo '<div class="wrap">
<h1>Mit Google Spreadsheet synchronisieren</h1>
<p><b>Spreadsheet URLs:</b> </p>
<ul>
';
foreach(getSpreadsheets() as $spreadsheet) echo '<li>'.$spreadsheet.'</li>';
echo '
<p>Letztes mal synchronisiert: '.date('d.m.Y H:i', $lastSynced).' Uhr</p>
<p>Button klicken um die Synchronisation zu starten.</p>
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
<input type="submit" id="dbem_options_submit" class="button-primary" name="syncnow" value="Jetzt synchronisieren">
</form>';
}
function blnfmcalsync_page() {
add_plugins_page( 'BLN.FM Cal Sync', 'BLN.FM Cal Sync', 'None', 'BLNFMCalSync', 'blnfmcalsync_page_function');
add_submenu_page( 'edit.php?post_type=event', 'BLN.FM Cal Sync', 'BLN.FM Cal Sync', 'manage_options', 'BLNFMCalSync', 'blnfmcalsync_page_function');
}
add_action('admin_menu', 'blnfmcalsync_page');
?>