From 0857039fc5de619bd84a747b2b3c17024aa8e07c Mon Sep 17 00:00:00 2001 From: Vencislav Atanasov Date: Sun, 16 Oct 2016 21:50:09 +0300 Subject: [PATCH] Split code to functions, add basic config --- schedule/config.php | 17 +++ schedule/index.php | 18 ++- schedule/load.php | 118 +++++++-------- schedule/parse.php | 353 +++++++++++++++++++++----------------------- 4 files changed, 254 insertions(+), 252 deletions(-) create mode 100644 schedule/config.php diff --git a/schedule/config.php b/schedule/config.php new file mode 100644 index 0000000..d0ab77b --- /dev/null +++ b/schedule/config.php @@ -0,0 +1,17 @@ + 'bg', + 'cfp_url' => 'https://cfp.openfest.org', + 'cut_len' => 70, + ]; + + $config = [ + 2015 => [ + 'allowedHallIds' => [6, 7, 8], + + ], + ]; + + return array_merge($globalConfig, $config[$year]); +} diff --git a/schedule/index.php b/schedule/index.php index 6b9875c..ba026a3 100644 --- a/schedule/index.php +++ b/schedule/index.php @@ -2,7 +2,14 @@ error_reporting(~0); 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); ?> @@ -11,19 +18,14 @@ define('SCHED_LANG', 'bg'); -
-
-		
- + diff --git a/schedule/load.php b/schedule/load.php index c6311d1..2466763 100644 --- a/schedule/load.php +++ b/schedule/load.php @@ -1,21 +1,4 @@ '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) { $valA = &$a[$key]; $valB = &$b[$key]; @@ -23,52 +6,65 @@ function compareKeys($a, $b, $key) { 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) { - $curl = new SmartCurl($base_url); - $json = $curl->getUrl($filename); + $filenames = [ + 'events' => 'events.json', + 'speakers' => 'speakers.json', + 'tracks' => 'tracks.json', + 'event_types' => 'event_types.json', + 'halls' => 'halls.json', + 'slots' => 'slots.json', + ]; - if ($json === false) { - echo 'get failed: ', $filename, PHP_EOL; - exit; - } - - $decoded = json_decode($json, true); + $data = []; - if ($decoded === false) { - echo 'decode failed: ', $filename, PHP_EOL; - exit; + foreach ($filenames as $name => $filename) { + $curl = new SmartCurl($base_url); + $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; - - 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; + + 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 ($config) { + return in_array($key, $config['allowedHallIds']); + }, ARRAY_FILTER_USE_KEY); + + return $data; } - -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; diff --git a/schedule/parse.php b/schedule/parse.php index 3acd021..f195336 100644 --- a/schedule/parse.php +++ b/schedule/parse.php @@ -1,183 +1,170 @@ - array( - 'name' => 'English', - 'locale' => 'en_US.UTF8' - ), - 'bg' => array( - 'name' => 'Български', - 'locale' => 'bg_BG.UTF8' - ) -); - -/* We need to set these so we actually parse properly the dates. WP fucks up both. */ -date_default_timezone_set('Europe/Sofia'); -setlocale(LC_TIME, $languages[SCHED_LANG]['locale']); - -foreach ($data['slots'] as $slot_id => $slot) { - $slotTime = $slot['starts_at']; - $slotDate = date('d', $slotTime); - - if ($slotDate !== $date) { - $lines[] = ''; - $lines[] = ''; - $lines[] = ''; - $lines[] = ''; - - $date = $slotDate; - } - - if ($slotTime !== $time) { - if ($time !== 0) { - $lines[] = ''; - } - - $lines[] = ''; - $lines[] = ''; - - $time = $slotTime; - } - - $eid = &$slot['event_id']; - - if (!array_key_exists($eid, $data['events'])) { - continue; - } - - $event = &$data['events'][$eid]; - - if (is_null($eid)) { - $lines[] = ''; - } - else { - $title = mb_substr($event['title'], 0, $cut_len) . (mb_strlen($event['title']) > $cut_len ? '...' : ''); - $speakers = ''; - - if (count($event['participant_user_ids']) > 0) { - $speakers = json_encode($event['participant_user_ids']) . '
'; - - $spk = array(); - $speaker_name = array(); - 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 ($uid == 4 || empty ($data['speakers'][$uid])) { - continue; - } else { - /* TODO: fix the URL */ - $name = $data['speakers'][$uid]['first_name'] . ' ' . $data['speakers'][$uid]['last_name']; - $spk[$uid] = '' . $name . ''; - } - } - $speakers = implode (', ', $spk); - } - - - /* Hack, we don't want language for the misc track. This is the same for all years. */ - if ('misc' !== $data['tracks'][$event['track_id']]['name']['en']) { - $csslang = "schedule-".$event['language']; - } else { - $csslang = ""; - } - $cssclass = &$data['tracks'][$event['track_id']]['css_class']; - $style = ' class="' . $cssclass . ' ' . $csslang . '"'; - $content = '' . htmlspecialchars($title) . '
' . $speakers; - - - /* these are done by $eid, as otherwise we get some talks more than once (for example the lunch) */ - $fulltalks[$eid] = ''; - $fulltalks[$eid] .= '
'; - /* We don't want '()' when we don't have a speaker name */ - $fulltalk_spkr = strlen($speakers)>1 ? ' (' . $speakers . ')' : ''; - $fulltalks[$eid] .= '

' . $event['title'] . ' ' . $fulltalk_spkr . '

'; - $fulltalks[$eid] .= '

' . $event['abstract'] . '

'; - $fulltalks[$eid] .= '
'; - - if ($slot['event_id'] === $prev_event_id) { - array_pop($lines); - $lines[] = '' . $content . ''; - } - else { - $lines[] = '' . $content . ''; - $colspan = 1; - } - } - - $prev_event_id = $slot['event_id']; -} - -$lines[] = ''; - -/* create the legend */ -$legend = []; - -foreach($data['tracks'] as $track) { - $legend[] = ''; -} - -foreach ($languages as $code => $lang) { - $legend[] = ''; -} - -$gspk = []; -$fspk = []; -$types = []; -$types['twitter']['url']='https://twitter.com/'; -$types['twitter']['class']='fa fa-twitter'; -$types['github']['url']='https://github.com/'; -$types['github']['class']='fa fa-github'; -$types['email']['url']='mailto:'; -$types['email']['class']='fa fa-envelope'; - -$gspk[] = '
'; - -foreach ($data['speakers'] as $speaker) { - $name = $speaker['first_name'] . ' ' . $speaker['last_name']; - - $gspk[] = ''; - - $fspk[] = '
'; - $fspk[] = '' . $name .''; - $fspk[] = '

' . $name . '

'; - $fspk[] = '
'; - foreach ($types as $type => $parm) { - if (!empty($speaker[$type])) { - $fspk[] = ''; - } - } - $fspk[] = '
'; - $fspk[] = '

' . $speaker['biography'] . '

'; - $fspk[] = '
'; -} - -$gspk[] = '
'; - -return array_merge($data, compact('lines', 'fulltalks', 'gspk', 'fspk', 'legend')); + array( + 'name' => 'English', + 'locale' => 'en_US.UTF8' + ), + 'bg' => array( + 'name' => 'Български', + 'locale' => 'bg_BG.UTF8' + ) + ); + + /* We need to set these so we actually parse properly the dates. WP fucks up both. */ + date_default_timezone_set('Europe/Sofia'); + setlocale(LC_TIME, $languages[$config['lang']]['locale']); + + foreach ($data['slots'] as $slot_id => $slot) { + if (!in_array($slot['hall_id'], $config['allowedHallIds'])) { + continue; + } + + $slotTime = $slot['starts_at']; + $slotDate = date('d', $slotTime); + + if ($slotDate !== $date) { + $lines[] = ''; + $lines[] = ''; + $lines[] = ''; + $lines[] = ''; + + $date = $slotDate; + } + + if ($slotTime !== $time) { + if ($time !== 0) { + $lines[] = ''; + } + + $lines[] = ''; + $lines[] = ''; + + $time = $slotTime; + } + + $eid = &$slot['event_id']; + + if (!array_key_exists($eid, $data['events'])) { + continue; + } + + $event = &$data['events'][$eid]; + + if (is_null($eid)) { + $lines[] = ''; + } + else { + $title = mb_substr($event['title'], 0, $config['cut_len']) . (mb_strlen($event['title']) > $config['cut_len'] ? '...' : ''); + $speakers = ''; + + if (count($event['participant_user_ids']) > 0) { + $speakers = json_encode($event['participant_user_ids']) . '
'; + + $spk = array(); + $speaker_name = array(); + 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 ($uid == 4 || empty ($data['speakers'][$uid])) { + continue; + } else { + /* TODO: fix the URL */ + $name = $data['speakers'][$uid]['first_name'] . ' ' . $data['speakers'][$uid]['last_name']; + $spk[$uid] = '' . $name . ''; + } + } + $speakers = implode (', ', $spk); + } + + + /* Hack, we don't want language for the misc track. This is the same for all years. */ + if ('misc' !== $data['tracks'][$event['track_id']]['name']['en']) { + $csslang = "schedule-".$event['language']; + } else { + $csslang = ""; + } + $cssclass = &$data['tracks'][$event['track_id']]['css_class']; + $style = ' class="' . $cssclass . ' ' . $csslang . '"'; + $content = '' . htmlspecialchars($title) . '
' . $speakers; + + + /* these are done by $eid, as otherwise we get some talks more than once (for example the lunch) */ + $fulltalks[$eid] = ''; + $fulltalks[$eid] .= '
'; + /* We don't want '()' when we don't have a speaker name */ + $fulltalk_spkr = strlen($speakers)>1 ? ' (' . $speakers . ')' : ''; + $fulltalks[$eid] .= '

' . $event['title'] . ' ' . $fulltalk_spkr . '

'; + $fulltalks[$eid] .= '

' . $event['abstract'] . '

'; + $fulltalks[$eid] .= '
'; + + if ($slot['event_id'] === $prev_event_id) { + array_pop($lines); + $lines[] = '' . $content . ''; + } + else { + $lines[] = '' . $content . ''; + $colspan = 1; + } + } + + $prev_event_id = $slot['event_id']; + } + + $lines[] = ''; + + /* create the legend */ + $legend = []; + + foreach($data['tracks'] as $track) { + $legend[] = ''; + } + + foreach ($languages as $code => $lang) { + $legend[] = ''; + } + + $gspk = []; + $fspk = []; + $types = []; + $types['twitter']['url']='https://twitter.com/'; + $types['twitter']['class']='fa fa-twitter'; + $types['github']['url']='https://github.com/'; + $types['github']['class']='fa fa-github'; + $types['email']['url']='mailto:'; + $types['email']['class']='fa fa-envelope'; + + $gspk[] = '
'; + + foreach ($data['speakers'] as $speaker) { + $name = $speaker['first_name'] . ' ' . $speaker['last_name']; + + $gspk[] = ''; + + $fspk[] = '
'; + $fspk[] = '' . $name .''; + $fspk[] = '

' . $name . '

'; + $fspk[] = '
'; + foreach ($types as $type => $parm) { + if (!empty($speaker[$type])) { + $fspk[] = ''; + } + } + $fspk[] = '
'; + $fspk[] = '

' . $speaker['biography'] . '

'; + $fspk[] = '
'; + } + + $gspk[] = '
'; + + return compact('lines', 'fulltalks', 'gspk', 'fspk', 'legend'); +}
 
' . strftime('%d %B - %A', $slotTime) . ' 
' . date('H:i', $slot['starts_at']) . ' - ' . date('H:i', $slot['ends_at']) . 'TBA
' . $track['name'][SCHED_LANG] . '
' . $lang['name'] . '
' . strftime('%d %B - %A', $slotTime) . ' 
' . date('H:i', $slot['starts_at']) . ' - ' . date('H:i', $slot['ends_at']) . 'TBA
' . $track['name'][$config['lang']] . '
' . $lang['name'] . '