Introduce Models abstracting the Config away from Views & Templates

This commit is contained in:
MaZderMind 2015-03-08 13:42:58 +01:00
parent 844a7d53f2
commit cb48ba9b70
9 changed files with 292 additions and 133 deletions

209
index.php
View file

@ -1,115 +1,132 @@
<?php
require_once('config.php');
require_once('lib/PhpTemplate.php');
require_once('lib/Exceptions.php');
require_once('lib/helper.php');
require_once('model/Overview.php');
require_once('model/Room.php');
$route = @$_GET['route'];
$route = rtrim($route, '/');
$GLOBALS['ROUTE'] = $route;
$GLOBALS['tpl'] = new PhpTemplate('template/page.phtml');
$GLOBALS['tpl']->set(array(
$tpl = new PhpTemplate('template/page.phtml');
$tpl->set(array(
'baseurl' => baseurl(),
'assemblies' => './template/assemblies/',
));
if($route == '')
{
include('view/overview.php');
try {
if($route == '')
{
include('view/overview.php');
}
else if(preg_match('@^about$@', $route, $m))
{
include('view/about.php');
}
else if(preg_match('@^program.json$@', $route, $m))
{
if(!has('SCHEDULE'))
return include('view/404.php');
include('view/program-json.php');
}
else if(preg_match('@^feedback$@', $route, $m))
{
if(!has('FEEDBACK'))
return include('view/404.php');
include('view/feedback.php');
}
else if(preg_match('@^feedback/read$@', $route, $m))
{
if(!has('FEEDBACK'))
return include('view/404.php');
include('view/feedback-read.php');
}
else if(preg_match('@^relive/([0-9]+)$@', $route, $m))
{
if(!has('OVERVIEW.RELIVE_JSON'))
return include('view/404.php');
$_GET = array(
'id' => $m[1],
);
include('view/relive-player.php');
}
else if(preg_match('@^relive$@', $route, $m))
{
if(!has('OVERVIEW.RELIVE_JSON'))
return include('view/404.php');
include('view/relive.php');
}
else if(preg_match('@^([^/]+)$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => '',
'language' => 'native',
);
include('view/room.php');
}
else if(preg_match('@^([^/]+)/translated$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => '',
'language' => 'translated',
);
include('view/room.php');
}
else if(preg_match('@^([^/]+)/(sd|audio|slides)$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => $m[2],
'language' => 'native',
);
include('view/room.php');
}
else if(preg_match('@^([^/]+)/(sd|audio|slides)/translated$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => $m[2],
'language' => 'translated',
);
include('view/room.php');
}
else
{
throw new NotFoundException();
}
}
else if(preg_match('@^about$@', $route, $m))
{
include('view/about.php');
}
else if(preg_match('@^program.json$@', $route, $m))
{
if(!has('SCHEDULE'))
return include('view/404.php');
include('view/program-json.php');
}
else if(preg_match('@^feedback$@', $route, $m))
{
if(!has('FEEDBACK'))
return include('view/404.php');
include('view/feedback.php');
}
else if(preg_match('@^feedback/read$@', $route, $m))
{
if(!has('FEEDBACK'))
return include('view/404.php');
include('view/feedback-read.php');
}
else if(preg_match('@^relive/([0-9]+)$@', $route, $m))
{
if(!has('OVERVIEW.RELIVE_JSON'))
return include('view/404.php');
$_GET = array(
'id' => $m[1],
);
include('view/relive-player.php');
}
else if(preg_match('@^relive$@', $route, $m))
{
if(!has('OVERVIEW.RELIVE_JSON'))
return include('view/404.php');
include('view/relive.php');
}
else if(preg_match('@^([^/]+)$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => '',
'language' => 'native',
);
include('view/room.php');
}
else if(preg_match('@^([^/]+)/translated$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => '',
'language' => 'translated',
);
include('view/room.php');
}
else if(preg_match('@^([^/]+)/(sd|audio|slides)$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => $m[2],
'language' => 'native',
);
include('view/room.php');
}
else if(preg_match('@^([^/]+)/(sd|audio|slides)/translated$@', $route, $m))
{
$_GET = array(
'room' => $m[1],
'selection' => $m[2],
'language' => 'translated',
);
include('view/room.php');
}
else
catch(NotFoundException $e)
{
include('view/404.php');
}
catch(Exception $e)
{
header("HTTP/1.1 500 Internal Server Error");
die($e);
}

3
lib/Exceptions.php Normal file
View file

@ -0,0 +1,3 @@
<?php
class NotFoundException extends Exception {}

View file

@ -2,16 +2,6 @@
require_once('program.php');
function link_index()
{
return '';
}
function link_room($room)
{
return rawurlencode($room).'/';
}
function link_player($room, $format = 'video', $translated = false)
{
$defaultformat = room_has_hd($room) ? 'hd' : 'sd';

52
model/Overview.php Normal file
View file

@ -0,0 +1,52 @@
<?php
require_once('model/Room.php');
class Overview
{
public function getGroups() {
$groups = array();
foreach(get('OVERVIEW.GROUPS') as $group => $rooms)
{
foreach($rooms as $room)
{
try {
$groups[$group][] = Room::get($room);
}
catch(NotFountException $e)
{
// just ignore unknown rooms
continue;
}
}
}
return $groups;
}
public function getReleasesUrl() {
return get('OVERVIEW.RELEASES');
}
public function getReliveUrl() {
if(has('OVERVIEW.RELIVE'))
return get('OVERVIEW.RELIVE');
elseif(has('OVERVIEW.RELIVE_JSON'))
return 'relive/';
else
return null;
}
public function hasRelive() {
return has('OVERVIEW.RELIVE') || has('OVERVIEW.RELIVE_JSON');
}
public function hasReleases() {
return has('OVERVIEW.RELEASES');
}
}

49
model/Room.php Normal file
View file

@ -0,0 +1,49 @@
<?php
class Room {
private $slug;
public static function get($slug)
{
return new Room($slug);
}
private function __construct($slug)
{
if(! has('ROOMS.'.$slug))
throw new NotFoundException('Room '.$slug);
$this->slug = $slug;
}
public function getSlug() {
return $this->slug;
}
public function getThumb() {
return 'thumbs/'.$this->getStream().'.png';
}
public function getLink() {
return rawurlencode($this->getSlug()).'/';
}
public function getStream() {
return get('ROOMS.'.$this->getSlug().'.STREAM', $this->getSlug());
}
public function getDisplay() {
return get('ROOMS.'.$this->getSlug().'.DISPLAY', $this->getSlug());
}
public function hasPreview() {
return get('ROOMS.'.$this->getSlug().'.PREVIEW');
}
public function hasSchedule() {
return get('ROOMS.'.$this->getSlug().'.SCHEDULE') && has('SCHEDULE');
}
}

5
model/Stream.php Normal file
View file

@ -0,0 +1,5 @@
<?php
class Room {
}

28
model/StreamList.php Normal file
View file

@ -0,0 +1,28 @@
<?php
class StreamList implements AggregateIterator {
private $streams = array();
public function getIterator() {
return new ArrayIterator($this->streams);
}
public function hasTranslation() {
}
public function hasRTMP() {
}
public function hasHLS() {
}
public function hasWebM() {
}
public function hasHD() {
}
public function hasSD() {
}
}

View file

@ -7,7 +7,7 @@
</div>
</div>
<? foreach(get('OVERVIEW.GROUPS') as $group => $rooms): ?>
<? foreach($overview->getGroups() as $group => $rooms): ?>
<div class="row room-group">
<div class="col-xs-12">
<h2><?=h($group)?></h2>
@ -16,49 +16,60 @@
<? $count = count($rooms); ?>
<? foreach($rooms as $idx => $room): ?>
<div class="
room room-<?=h($room)?> clearfix
room
room-<?=h($room->getSlug())?>
clearfix
<? /* when the count is odd and this is the last item - make it full width */ ?>
<? if($count % 2 == 1 && $idx == $count - 1): ?>
col-xs-12 wide
<? if( ($count % 2 == 1) && ($idx == $count - 1) ): ?>
wide
col-xs-12
<? else: ?>
<? if(get("ROOMS.$room.PREVIEW") && get("ROOMS.$room.SCHEDULE") && has("SCHEDULE")): ?>
narrow
<? if($room->hasPreview() && $room->hasSchedule()): ?>
col-md-6
<? else: ?>
col-sm-6
<? endif ?>
narrow
<? endif ?>
<? if(get("ROOMS.$room.PREVIEW")): ?>
<? if($room->hasPreview()): ?>
has-preview
<? endif ?>
<? if(get("ROOMS.$room.SCHEDULE") && has("SCHEDULE")): ?>
<? if($room->hasSchedule()): ?>
has-schedule
<? endif ?>
">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
<a href="<?=h(link_room($room))?>">
<?=h(get("ROOMS.$room.DISPLAY"))?>
<a href="<?=h($room->getLink())?>">
<?=h($room->getDisplay())?>
</a>
</div>
</div>
<div class="panel-body">
<? if(get("ROOMS.$room.PREVIEW")): ?>
<a href="<?=h(link_room($room))?>">
<img class="preview" src="thumbs/<?=h(get("ROOMS.$room.STREAM"))?>.png" alt="" width="213" height="120" />
<? if($room->hasPreview()): ?>
<a href="<?=h($room->getLink())?>">
<img
class="preview"
src="<?=h($room->getThumb())?>"
alt="<?=h($room->getDisplay())?>"
width="213" height="120"
/>
</a>
<? endif ?>
<a href="<?=h(link_room($room))?>" class="title">
<?=h(get("ROOMS.$room.DISPLAY"))?>
<a href="<?=h($room->getLink())?>" class="title">
<?=h($room->getDisplay())?>
</a>
<? if(get("ROOMS.$room.SCHEDULE") && has("SCHEDULE")): ?>
<? if($room->hasSchedule()): ?>
<div class="program-schedule">
<div class="talk current-talk">
<strong>Now:</strong>
@ -78,18 +89,25 @@
</div>
<? endforeach ?>
<? if(has('OVERVIEW.RELEASES') || has('OVERVIEW.RELIVE') || has('OVERVIEW.RELIVE_JSON')): ?>
<? $class = has('OVERVIEW.RELEASES') && (has('OVERVIEW.RELIVE') || has('OVERVIEW.RELIVE_JSON')) ? 'col-sm-6 col-xs-12' : 'col-xs-12' ?>
<? if($overview->hasReleases() || $overview->hasRelive()): ?>
<?
$class = ($overview->hasReleases() && $overview->hasRelive()) ?
// both enabled
'col-sm-6 col-xs-12' :
// only one of both enabled
'col-xs-12';
?>
<div class="row recordings">
<div class="col-xs-12">
<h2>Recordings</h2>
</div>
<? if(has('OVERVIEW.RELEASES')): ?>
<? if($overview->hasReleases()): ?>
<div class="<?=h($class)?>">
<div class="panel panel-primary">
<div class="panel-body">
<a href="<?=h(get('OVERVIEW.RELEASES'))?>">
<a href="<?=h($overview->getReleasesUrl())?>">
<span class="fa fa-video-camera"></span> Releases
</a>
</div>
@ -97,16 +115,11 @@
</div>
<? endif ?>
<? if(has('OVERVIEW.RELIVE') || has('OVERVIEW.RELIVE_JSON')): ?>
<? if($overview->hasRelive()): ?>
<div class="<?=h($class)?>">
<div class="panel panel-primary">
<div class="panel-body">
<? if(has('OVERVIEW.RELIVE')): ?>
<a href="<?=h(get('OVERVIEW.RELIVE'))?>">
<? else: ?>
<a href="relive/">
<? endif ?>
<a href="<?=h($overview->getReliveUrl())?>">
<span class="fa fa-play-circle"></span> ReLive
</a>
</div>

View file

@ -3,4 +3,6 @@
echo $tpl->render(array(
'page' => 'overview',
'title' => 'Live-Streams',
'overview' => new Overview(),
));