Split code to functions, add basic config

This commit is contained in:
Vencislav Atanasov 2016-10-16 21:50:09 +03:00
parent ca4f0e0f73
commit 0857039fc5
4 changed files with 254 additions and 252 deletions

17
schedule/config.php Normal file
View File

@ -0,0 +1,17 @@
<?php
function getSchedConfig($year = 2015) {
$globalConfig = [
'lang' => 'bg',
'cfp_url' => 'https://cfp.openfest.org',
'cut_len' => 70,
];
$config = [
2015 => [
'allowedHallIds' => [6, 7, 8],
],
];
return array_merge($globalConfig, $config[$year]);
}

View File

@ -2,7 +2,14 @@
error_reporting(~0); error_reporting(~0);
ini_set('display_errors', 1); ini_set('display_errors', 1);
define('SCHED_LANG', 'bg'); $requirePath = __DIR__ . DIRECTORY_SEPARATOR;
require $requirePath . 'class.SmartCurl.php';
require $requirePath . 'config.php';
require $requirePath . 'load.php';
require $requirePath . 'parse.php';
$sched_config = getSchedConfig();
$data = loadData($sched_config);
$content = parseData($sched_config, $data);
?> ?>
<html> <html>
<head> <head>
@ -11,19 +18,14 @@ define('SCHED_LANG', 'bg');
<link rel="stylesheet" type="text/css" href="http://www.openfest.org/2014/wp-content/themes/initfest/style.css" /> <link rel="stylesheet" type="text/css" href="http://www.openfest.org/2014/wp-content/themes/initfest/style.css" />
</head> </head>
<body> <body>
<pre>
<?php
$content = require __DIR__ . DIRECTORY_SEPARATOR . 'parse.php';
?>
</pre>
<table border="1" style="text-align: center;"> <table border="1" style="text-align: center;">
<thead> <thead>
<tr> <tr>
<td>&nbsp;</td> <td>&nbsp;</td>
<?php <?php
foreach ($content['halls'] as $hall_name) { foreach ($data['halls'] as $hall_name) {
?> ?>
<td><?php echo htmlspecialchars($hall_name[SCHED_LANG]); ?></td> <td><?php echo htmlspecialchars($hall_name[$sched_config['lang']]); ?></td>
<?php <?php
} }
?> ?>

View File

@ -1,21 +1,4 @@
<?php <?php
require __DIR__ . DIRECTORY_SEPARATOR . 'class.SmartCurl.php';
$base_url = 'https://cfp.openfest.org/api/conferences/2/';
$filenames = [
'events' => 'events.json',
'speakers' => 'speakers.json',
'tracks' => 'tracks.json',
'event_types' => 'event_types.json',
'halls' => 'halls.json',
'slots' => 'slots.json',
];
if (empty($allowedhallids)) {
$allowedhallids = array(6, 7, 8);
}
function compareKeys($a, $b, $key) { function compareKeys($a, $b, $key) {
$valA = &$a[$key]; $valA = &$a[$key];
$valB = &$b[$key]; $valB = &$b[$key];
@ -23,52 +6,65 @@ function compareKeys($a, $b, $key) {
return ($valA < $valB) ? -1 : (($valA > $valB) ? 1 : 0); return ($valA < $valB) ? -1 : (($valA > $valB) ? 1 : 0);
} }
$data = []; function loadData($config) {
$base_url = $config['cfp_url'] . '/api/conferences/2/';
foreach ($filenames as $name => $filename) { $filenames = [
$curl = new SmartCurl($base_url); 'events' => 'events.json',
$json = $curl->getUrl($filename); 'speakers' => 'speakers.json',
'tracks' => 'tracks.json',
'event_types' => 'event_types.json',
'halls' => 'halls.json',
'slots' => 'slots.json',
];
if ($json === false) { $data = [];
echo 'get failed: ', $filename, PHP_EOL;
exit;
}
$decoded = json_decode($json, true);
if ($decoded === false) { foreach ($filenames as $name => $filename) {
echo 'decode failed: ', $filename, PHP_EOL; $curl = new SmartCurl($base_url);
exit; $json = $curl->getUrl($filename);
if ($json === false) {
echo 'get failed: ', $filename, PHP_EOL;
exit;
}
$decoded = json_decode($json, true);
if ($decoded === false) {
echo 'decode failed: ', $filename, PHP_EOL;
exit;
}
$add = true;
switch ($name) {
case 'halls':
$decoded = array_map(function($el) {
return $el['name'];
}, $decoded);
break;
case 'slots':
$decoded = array_map(function($el) {
foreach (['starts_at', 'ends_at'] as $key) {
$el[$key] = strtotime($el[$key]);
}
return $el;
}, $decoded);
break;
}
$data[$name] = $decoded;
} }
$add = true; uasort($data['slots'], function($a, $b) {
return compareKeys($a, $b, 'starts_at') ?: compareKeys($a, $b, 'hall_id');
switch ($name) { });
case 'halls':
$decoded = array_map(function($el) { $data['halls'] = array_filter($data['halls'], function($key) use ($config) {
return $el['name']; return in_array($key, $config['allowedHallIds']);
}, $decoded); }, ARRAY_FILTER_USE_KEY);
break;
case 'slots': return $data;
$decoded = array_map(function($el) {
foreach (['starts_at', 'ends_at'] as $key) {
$el[$key] = strtotime($el[$key]);
}
return $el;
}, $decoded);
break;
}
$data[$name] = $decoded;
} }
uasort($data['slots'], function($a, $b) {
return compareKeys($a, $b, 'starts_at') ?: compareKeys($a, $b, 'hall_id');
});
$data['halls'] = array_filter($data['halls'], function($key) use ($allowedhallids) {
return in_array($key, $allowedhallids);
}, ARRAY_FILTER_USE_KEY);
return $data;

View File

@ -1,183 +1,170 @@
<?php <?php
// 'halfnarp_friendly' function parseData($config, $data) {
// 'events' $time = 0;
// 'speakers' $date = 0;
// 'tracks' [en/bg] $lines = [];
// 'event_types' [en/bg] $fulltalks = [];
// 'halls' $prev_event_id = 0;
// 'slots' $colspan = 1;
$data = require __DIR__ . DIRECTORY_SEPARATOR . 'load.php'; $languages = array(
'en' => array(
if (!defined('SCHED_LANG')) { 'name' => 'English',
define('SCHED_LANG', 'bg'); 'locale' => 'en_US.UTF8'
} ),
'bg' => array(
$cut_len = 70; 'name' => 'Български',
$cfp_url = 'http://cfp.openfest.org'; 'locale' => 'bg_BG.UTF8'
$time = 0; )
$date = 0; );
$lines = [];
$fulltalks = []; /* We need to set these so we actually parse properly the dates. WP fucks up both. */
$prev_event_id = 0; date_default_timezone_set('Europe/Sofia');
$colspan = 1; setlocale(LC_TIME, $languages[$config['lang']]['locale']);
$hall_ids = array_keys($data['halls']);
$first_hall_id = min($hall_ids); foreach ($data['slots'] as $slot_id => $slot) {
$last_hall_id = max($hall_ids); if (!in_array($slot['hall_id'], $config['allowedHallIds'])) {
continue;
$languages = array( }
'en' => array(
'name' => 'English', $slotTime = $slot['starts_at'];
'locale' => 'en_US.UTF8' $slotDate = date('d', $slotTime);
),
'bg' => array( if ($slotDate !== $date) {
'name' => 'Български', $lines[] = '<tr>';
'locale' => 'bg_BG.UTF8' $lines[] = '<td>' . strftime('%d %B - %A', $slotTime) . '</td>';
) $lines[] = '<td colspan="3">&nbsp;</td>';
); $lines[] = '</tr>';
/* We need to set these so we actually parse properly the dates. WP fucks up both. */ $date = $slotDate;
date_default_timezone_set('Europe/Sofia'); }
setlocale(LC_TIME, $languages[SCHED_LANG]['locale']);
if ($slotTime !== $time) {
foreach ($data['slots'] as $slot_id => $slot) { if ($time !== 0) {
$slotTime = $slot['starts_at']; $lines[] = '</tr>';
$slotDate = date('d', $slotTime); }
if ($slotDate !== $date) { $lines[] = '<tr>';
$lines[] = '<tr>'; $lines[] = '<td>' . date('H:i', $slot['starts_at']) . ' - ' . date('H:i', $slot['ends_at']) . '</td>';
$lines[] = '<td>' . strftime('%d %B - %A', $slotTime) . '</td>';
$lines[] = '<td colspan="3">&nbsp;</td>'; $time = $slotTime;
$lines[] = '</tr>'; }
$date = $slotDate; $eid = &$slot['event_id'];
}
if (!array_key_exists($eid, $data['events'])) {
if ($slotTime !== $time) { continue;
if ($time !== 0) { }
$lines[] = '</tr>';
} $event = &$data['events'][$eid];
$lines[] = '<tr>'; if (is_null($eid)) {
$lines[] = '<td>' . date('H:i', $slot['starts_at']) . ' - ' . date('H:i', $slot['ends_at']) . '</td>'; $lines[] = '<td>TBA</td>';
}
$time = $slotTime; else {
} $title = mb_substr($event['title'], 0, $config['cut_len']) . (mb_strlen($event['title']) > $config['cut_len'] ? '...' : '');
$speakers = '';
$eid = &$slot['event_id'];
if (count($event['participant_user_ids']) > 0) {
if (!array_key_exists($eid, $data['events'])) { $speakers = json_encode($event['participant_user_ids']) . '<br>';
continue;
} $spk = array();
$speaker_name = array();
$event = &$data['events'][$eid]; foreach ($event['participant_user_ids'] as $uid) {
/* The check for uid==4 is for us not to show the "Opefest Team" as a presenter for lunches, etc. */
if (is_null($eid)) { if ($uid == 4 || empty ($data['speakers'][$uid])) {
$lines[] = '<td>TBA</td>'; continue;
} } else {
else { /* TODO: fix the URL */
$title = mb_substr($event['title'], 0, $cut_len) . (mb_strlen($event['title']) > $cut_len ? '...' : ''); $name = $data['speakers'][$uid]['first_name'] . ' ' . $data['speakers'][$uid]['last_name'];
$speakers = ''; $spk[$uid] = '<a class="vt-p" href="SPKURL#'. $name . '">' . $name . '</a>';
}
if (count($event['participant_user_ids']) > 0) { }
$speakers = json_encode($event['participant_user_ids']) . '<br>'; $speakers = implode (', ', $spk);
}
$spk = array();
$speaker_name = array();
foreach ($event['participant_user_ids'] as $uid) { /* Hack, we don't want language for the misc track. This is the same for all years. */
/* The check for uid==4 is for us not to show the "Opefest Team" as a presenter for lunches, etc. */ if ('misc' !== $data['tracks'][$event['track_id']]['name']['en']) {
if ($uid == 4 || empty ($data['speakers'][$uid])) { $csslang = "schedule-".$event['language'];
continue; } else {
} else { $csslang = "";
/* TODO: fix the URL */ }
$name = $data['speakers'][$uid]['first_name'] . ' ' . $data['speakers'][$uid]['last_name']; $cssclass = &$data['tracks'][$event['track_id']]['css_class'];
$spk[$uid] = '<a class="vt-p" href="SPKURL#'. $name . '">' . $name . '</a>'; $style = ' class="' . $cssclass . ' ' . $csslang . '"';
} $content = '<a href=#lecture-' . $eid . '>' . htmlspecialchars($title) . '</a> <br>' . $speakers;
}
$speakers = implode (', ', $spk);
} /* these are done by $eid, as otherwise we get some talks more than once (for example the lunch) */
$fulltalks[$eid] = '';
$fulltalks[$eid] .= '<section id="lecture-' . $eid . '">';
/* Hack, we don't want language for the misc track. This is the same for all years. */ /* We don't want '()' when we don't have a speaker name */
if ('misc' !== $data['tracks'][$event['track_id']]['name']['en']) { $fulltalk_spkr = strlen($speakers)>1 ? ' (' . $speakers . ')' : '';
$csslang = "schedule-".$event['language']; $fulltalks[$eid] .= '<p><strong>' . $event['title'] . ' ' . $fulltalk_spkr . '</strong></p>';
} else { $fulltalks[$eid] .= '<p>' . $event['abstract'] . '</p>';
$csslang = ""; $fulltalks[$eid] .= '<div class="separator"></div></section>';
}
$cssclass = &$data['tracks'][$event['track_id']]['css_class']; if ($slot['event_id'] === $prev_event_id) {
$style = ' class="' . $cssclass . ' ' . $csslang . '"'; array_pop($lines);
$content = '<a href=#lecture-' . $eid . '>' . htmlspecialchars($title) . '</a> <br>' . $speakers; $lines[] = '<td' . $style . ' colspan="' . ++$colspan . '">' . $content . '</td>';
}
else {
/* these are done by $eid, as otherwise we get some talks more than once (for example the lunch) */ $lines[] = '<td' . $style . '>' . $content . '</td>';
$fulltalks[$eid] = ''; $colspan = 1;
$fulltalks[$eid] .= '<section id="lecture-' . $eid . '">'; }
/* We don't want '()' when we don't have a speaker name */ }
$fulltalk_spkr = strlen($speakers)>1 ? ' (' . $speakers . ')' : '';
$fulltalks[$eid] .= '<p><strong>' . $event['title'] . ' ' . $fulltalk_spkr . '</strong></p>'; $prev_event_id = $slot['event_id'];
$fulltalks[$eid] .= '<p>' . $event['abstract'] . '</p>'; }
$fulltalks[$eid] .= '<div class="separator"></div></section>';
$lines[] = '</tr>';
if ($slot['event_id'] === $prev_event_id) {
array_pop($lines); /* create the legend */
$lines[] = '<td' . $style . ' colspan="' . ++$colspan . '">' . $content . '</td>'; $legend = [];
}
else { foreach($data['tracks'] as $track) {
$lines[] = '<td' . $style . '>' . $content . '</td>'; $legend[] = '<tr><td class="' . $track['css_class'] . '">' . $track['name'][$config['lang']] . '</td></tr>';
$colspan = 1; }
}
} foreach ($languages as $code => $lang) {
$legend[] = '<tr><td class="schedule-' . $code . '">' . $lang['name'] . '</td></tr>';
$prev_event_id = $slot['event_id']; }
}
$gspk = [];
$lines[] = '</tr>'; $fspk = [];
$types = [];
/* create the legend */ $types['twitter']['url']='https://twitter.com/';
$legend = []; $types['twitter']['class']='fa fa-twitter';
$types['github']['url']='https://github.com/';
foreach($data['tracks'] as $track) { $types['github']['class']='fa fa-github';
$legend[] = '<tr><td class="' . $track['css_class'] . '">' . $track['name'][SCHED_LANG] . '</td></tr>'; $types['email']['url']='mailto:';
} $types['email']['class']='fa fa-envelope';
foreach ($languages as $code => $lang) { $gspk[] = '<div class="grid members">';
$legend[] = '<tr><td class="schedule-' . $code . '">' . $lang['name'] . '</td></tr>';
} foreach ($data['speakers'] as $speaker) {
$name = $speaker['first_name'] . ' ' . $speaker['last_name'];
$gspk = [];
$fspk = []; $gspk[] = '<div class="member col4">';
$types = []; $gspk[] = '<a href="#' . $name . '">';
$types['twitter']['url']='https://twitter.com/'; $gspk[] = '<img width="100" height="100" src="' . $config['cfp_url'] . $speaker['picture']['schedule']['url'].'" class="attachment-100x100 wp-post-image" alt="' . $name .'" />';
$types['twitter']['class']='fa fa-twitter'; $gspk[] = '</a> </div>';
$types['github']['url']='https://github.com/';
$types['github']['class']='fa fa-github'; $fspk[] = '<div class="speaker" id="' . $name . '">';
$types['email']['url']='mailto:'; $fspk[] = '<img width="100" height="100" src="' . $config['cfp_url'] . $speaker['picture']['schedule']['url'].'" class="attachment-100x100 wp-post-image" alt="' . $name .'" />';
$types['email']['class']='fa fa-envelope'; $fspk[] = '<h3>' . $name . '</h3>';
$fspk[] = '<div class="icons">';
$gspk[] = '<div class="grid members">'; foreach ($types as $type => $parm) {
if (!empty($speaker[$type])) {
foreach ($data['speakers'] as $speaker) { $fspk[] = '<a href="'. $parm['url'] . $speaker[$type] . '"><i class="' . $parm['class'] . '"></i></a>';
$name = $speaker['first_name'] . ' ' . $speaker['last_name']; }
}
$gspk[] = '<div class="member col4">'; $fspk[] = '</div>';
$gspk[] = '<a href="#' . $name . '">'; $fspk[] = '<p>' . $speaker['biography'] . '</p>';
$gspk[] = '<img width="100" height="100" src="' . $cfp_url . $speaker['picture']['schedule']['url'].'" class="attachment-100x100 wp-post-image" alt="' . $name .'" />'; $fspk[] = '</div><div class="separator"></div>';
$gspk[] = '</a> </div>'; }
$fspk[] = '<div class="speaker" id="' . $name . '">'; $gspk[] = '</div>';
$fspk[] = '<img width="100" height="100" src="' . $cfp_url . $speaker['picture']['schedule']['url'].'" class="attachment-100x100 wp-post-image" alt="' . $name .'" />';
$fspk[] = '<h3>' . $name . '</h3>'; return compact('lines', 'fulltalks', 'gspk', 'fspk', 'legend');
$fspk[] = '<div class="icons">'; }
foreach ($types as $type => $parm) {
if (!empty($speaker[$type])) {
$fspk[] = '<a href="'. $parm['url'] . $speaker[$type] . '"><i class="' . $parm['class'] . '"></i></a>';
}
}
$fspk[] = '</div>';
$fspk[] = '<p>' . $speaker['biography'] . '</p>';
$fspk[] = '</div><div class="separator"></div>';
}
$gspk[] = '</div>';
return array_merge($data, compact('lines', 'fulltalks', 'gspk', 'fspk', 'legend'));