javascriptcursus/php/page.php

<?php require_once 'geshi/geshi.php'; require_once 'chapter_titles.php'; require_once 'assignments.php'; class page { // Public: public function __construct($title = 'Geen titel') { $this->title = $title; $this->content = null; $this->extraHeaders = null; $this->jsBuffer = null; $this->htmlBuffer = null; $this->displayPlaygroundButton = true; $this->assignmentsClient = new assignmentsClient(); } public function printAll() { $this->flushCode(); echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'. '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl">'. '<head>'. '<title>JavaScript - '.$this->title.'</title>'. '<link rel="shortcut icon" href="favicon.ico" />'. '<link rel="stylesheet" type="text/css" href="css/main.css" />'. '<script type="text/javascript" src="js/main.js"></script>'. $this->extraHeaders. '</head>'. '<body>'. '<div id="page">'; $buffer = '<img id="logo" src="img/logo.png" alt="JavaScript - logo" />'; $buffer .= $this->generateMenu(); $buffer .= '<table id="content-table"><tr>'. '<td id="index"><div class="white-block-content"></div>'; $buffer .= $this->generateIndex(); $buffer .= '</td><td id="content-table-empty"></td><td id="content"><div class="white-block-content"></div>'; $buffer .= $this->generateH1(); $buffer .= $this->content; $buffer .= '</td></tr></table>'; echo $this->createWhiteBlock($buffer); echo '<div>&nbsp;</div>'; echo $this->createWhiteBlock($this->generateFooter(), 'footer'); echo '</div>'. // ID: page '</body></html>'; } public function addHeader($header) { $this->extraHeaders .= $header; } public function addContent($content) { $this->flushCode(); $this->content .= $content; } public function addJS($js, $displayPlaygroundButton = true) { $this->jsBuffer = $js; $this->displayPlaygroundButton = $displayPlaygroundButton; } public function addHTML($html, $insertHtmlBufferInHead = false, $displayPlaygroundButton = true) { $this->htmlBuffer = $html; $this->insertHtmlBufferInHead = $insertHtmlBufferInHead; $this->displayPlaygroundButton = $displayPlaygroundButton; } public function flushCode() { $buffer = null; if($this->jsBuffer == null && $this->htmlBuffer == null) return; else if($this->jsBuffer != null) { $jsGeshi = new GeSHi($this->jsBuffer, 'javascript'); $jsGeshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); $buffer .= '<b>JavaScript:</b>'.$jsGeshi->parse_code(); if($this->htmlBuffer != null) { $htmlGeshi = new GeSHi($this->htmlBuffer, 'html4strict'); $htmlGeshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); $buffer .= '<hr /><b>HTML:</b>'.$htmlGeshi->parse_code(); } } else if($this->htmlBuffer != null) { $htmlGeshi = new GeSHi($this->htmlBuffer, 'html4strict'); $htmlGeshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); $buffer .= '<b>HTML:</b>'.$htmlGeshi->parse_code(); } if($this->displayPlaygroundButton) $buffer .= '<form class="view-in-playground" action="playground.php" target="_blank" method="post">'. '<input type="hidden" name="code" value="'.htmlentities($this->formatCode(), ENT_QUOTES).'" />'. '<input type="submit" value="Bekijk in Playground" />'. '</form>'; $this->content .= $this->createWhiteBlock($buffer); $this->jsBuffer = ($this->htmlBuffer = null); $this->insertHtmlBufferInHead = false; } public function inlineJS($js) { return '<samp class="javascript-inline">'.htmlentities($js).'</samp>'; } public function inlineHTML($html) { return '<samp class="html-inline">'.htmlentities($html).'</samp>'; } public function getAdressOfH($chapter, $subChapter) { return "hoofdstuk_$chapter.php#H$chapter.$subChapter"; } public function generateLinkToH($chapter, $subChapter=0, $content=null, $extra=null) { if($content == null) $content = "H$chapter".($subChapter==0 ? null : ".$subChapter"); return '<a href="'.$this->getAdressOfH($chapter, $subChapter).'" '.$extra.'>'.$content.'</a>'; } public function addAssignments($chapter) { $this->flushCode(); $assignments = $this->assignmentsClient->getAssignmentsByChapter($chapter, true); if(!$assignments || count($assignments) == 0) $this->content .= '<p>Er zijn (nog) geen opdrachten voor dit hoofdstuk!</p>'; else { $this->content .= '<ol class="assignments">'; foreach($assignments as $assignment) { $this->content .= "<li>{$assignment->assignment} (".$this->generateLinkToH($assignment->chapter, $assignment->subChapter).')'; if($assignment->viewAnswer || $assignment->exampleCode != null) $this->content .= '<br />'; if($assignment->exampleCode != null) $this->content .= "<a href=\"voorbeeld.php?id={$assignment->id}\">Voorbeeld</a>".($assignment->viewAnswer ? ' - ' : null); if($assignment->viewAnswer) $this->content .= "<a href=\"opdrachten.php?id={$assignment->id}\">Antwoord</a>"; $this->content .= '</li>'; } $this->content .= '</ol>'; } } public function createWhiteBlock($content, $id = null, $padding = true) { return '<div class="white-block-top">'. '<div class="white-block-right">'. '<div class="white-block-down">'. '<div class="white-block-left">'. '<div class="white-block-content"'.($id==null ? null : " id=\"$id\"").($padding ? null : ' style="padding:0px;"').'>'. $content. '</div></div></div></div></div>'; } // Protected: protected $title; protected $content; protected $extraHeaders; protected $jsBuffer; protected $htmlBuffer; protected $insertHtmlBufferInHead; protected $displayPlaygroundButton; protected $assignmentsClient; protected function generateMenu() { return '<table id="menu"><tr>'. '<td><a href="index.php">Home</a></td>'. '<td><a href="inhoudsopgave.php">Inhoudsopgave</a></td>'. '<td><a href="playground.php">Playground</a></td>'. '<td><a href="opdrachten.php">Opdrachten</a></td>'. '<td><a href="links.php">Links</a></td>'. '</tr></table>'; } protected function generateIndex() { $out = '<h2>Inhoud</h2><ol id="index-list">'; for($i = 1; $i < count($GLOBALS['chapter_titles']); ++$i) { $chapter = $GLOBALS['chapter_titles'][$i]; $hasSubChapters = (count($chapter) > 1); $out .= "<li>"; if($hasSubChapters) $out .= "<img id=\"h$i\" class=\"index-arrow\" src=\"img/arrow-right.png\" alt=\"Geeft paragrafen weer\" title=\"Geeft paragrafen weer\" /> "; else $out .= "<img id=\"h$i\" src=\"img/arrow-right.png\" alt=\"Geen paragrafen\" title=\"Geen paragrafen\" /> "; $out.="<a href=\"hoofdstuk_$i.php\">H$i - $chapter[0]</a>"; if($hasSubChapters) { $out .= "<ol id=\"h$i-list\">"; for($j = 1; $j<count($chapter); ++$j) $out .= "<li id=\"index-h$i.$j\"><a href=\"hoofdstuk_$i.php#H$i.$j\">&sect;$j. $chapter[$j]</a></li>"; $out .= '</ol>'; } $out .= '</li>'; } $out .= '</ol>'; return $out; } protected function generateFooter() { if(date('Y') == 2011) return '&copy; Mark Kamsma 2011</a>'; return '&copy; Mark Kamsma 2011-'.date('Y').'</a>'; } protected function generateH1() { return '<h1>'.htmlentities($this->title).'</h1>'; } protected function formatCode() { if($this->jsBuffer != null) { if($this->htmlBuffer == null) { return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> <head> <title>'.htmlentities($this->title).'</title> <script type="text/javascript"> '.str_replace("\n", "\n ", $this->jsBuffer).' </script> </head> <body> </body> </html>'; } else { $pos = 0; if(($pos = strpos($this->htmlBuffer, '<head>'))==false) { $tmpHTML = "\n ".str_replace("\n", "\n ", $this->htmlBuffer); return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> <head> <title>'.htmlentities($this->title).'</title>'.($this->insertHtmlBufferInHead ? $tmpHTML : null).' <script type="text/javascript"> '.str_replace("\n", "\n ", $this->jsBuffer).' </script> </head> <body>'.(!$this->insertHtmlBufferInHead ? $tmpHTML : null).' </body> </html>'; } else { return substr_replace($this->htmlBuffer, "<script type=\"text/javascript\">\n ".str_replace("\n", "\n ", $this->jsBuffer)."\n </script>", $pos, 0); } } } else if($this->htmlBuffer != null) { if(strpos($this->htmlBuffer, '<body>')!=false) return $this->htmlBuffer; else { $tmpHTML = "\n ".str_replace("\n", "\n ", $this->htmlBuffer); return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="nl" lang="nl"> <head> <title>'.htmlentities($this->title).'</title>'.($this->insertHtmlBufferInHead ? $tmpHTML : null).' </head> <body>'.(!$this->insertHtmlBufferInHead ? $tmpHTML : null).' </body> </html>'; } } return null; } }; ?>

Resultaat

Made by Thijs Aarnoudse