source: t29-www/lib/template.php @ 390

Last change on this file since 390 was 390, checked in by sven, 11 years ago
  • Vor/Zurück-Link bei Lernprojekten aktiviert
  • Anzeige fehlender englischer Übersetzungen wieder aktiviert (aufgehübscht; mit Infobox)
  • Property svn:keywords set to Id
File size: 18.2 KB
RevLine 
[251]1<?php
2/**
[347]3 * technikum29v6 Page Template.
4 * Initially written 08.01.2012, Sven Koeppel
[251]5 *
[347]6 * This file contains the t29v6 HTML5 template (header, footer, structure, ...).
7 *
[251]8 * Global vars:
9 *  $lang = de | en
10 *  $seiten_id = kurzkennung der aktuellen seite
11 *  $root = Seiten-Root fuer URLs ($root/de, $root/shared, etc.)
[254]12 *  $titel = Seitentitel
13 *  $header_cache_file, $footer_cache_file.
[251]14 **/
[273]15
[254]16class t29Template {
[297]17        public $conf, $menu, $msg;
[255]18        public $body_classes = array();
19        public $javascript_config = array();
[259]20        public $page_relations, $interlang_links;
[275]21        public $log; // lightweight logging system
[255]22
[347]23        /**
24         * The t29Template constructor.
25         *
26         * The template class is embedded into the t29v6 class framework. It
27         * uses t29Log for logging, t29Messages for any localisation strings,
28         * t29RessourceLoader for resolving URLs for CSS and JavaScript ressources.
29         * t29Menu is a helper class considered for parsing and extracting
30         * any relations between pages and the menu from navigation.xml.
31         *
32         * From the t29v6 entrypoint, technikum29.php, this class is instanced
33         * at the end, if no caching has worked. That call looks like
34         *
35         *   $template = new t29Template($GLOBALS);
36         *
37         * which means that our configuration $this->conf will be set up from the
38         * former global namespace. For correct working, at least some global vars
39         * like
40         *   $lib
41         *   $lang
42         *   $host
43         *   ...
44         * are considered as present (see above for a list).
45         *
46         **/
[254]47        function __construct($conf_array) {
48                $this->conf = $conf_array;
[275]49               
[297]50                // fetch the lightweight logging object:
[275]51                require_once $this->conf['lib'].'/logging.php';
[297]52                $this->log = t29Log::get();
[347]53               
[254]54                // create a menu:
55                require_once $this->conf['lib'].'/menu.php';
56                $this->menu = new t29Menu($this->conf);
[255]57
58                // create localisation class:
59                require_once $this->conf['lib'].'/messages.php';
60                $this->msg = new t29Messages($this->conf['lang']);
[301]61               
62                // create the ressourceloaders:
63                require_once $this->conf['lib'].'/ressourceloader.php';
64                $this->rl = array();
65                foreach(array('js','css') as $type)
66                        $this->rl[$type] = t29RessourceLoader::create_from_type($type, $this->conf);
[255]67
[259]68                // fill up configuration
[347]69               
[297]70                // optional html headers which can be filled by hooks or parts
[296]71                if(!isset($this->conf['header_prepend']))
72                        $this->conf['header_prepend'] = array(); // list
[297]73
[347]74                // ask t29Host for configuration fillup
[297]75                $this->conf['host']->fillup_template_conf($this->conf);
[296]76               
[273]77                // Path names in messages
[289]78                foreach(array('footer-legal-file', 'topnav-search-page') as $msg_id)
79                        $this->msg->set($msg_id, $this->conf['lang_path'].$this->msg->_($msg_id));
[259]80
[279]81                // store informations about the current page
82                $this->conf['seiten_link'] = $this->menu->get_link();
83                $this->conf['seite_in_nav'] = $this->menu->get_link_navigation_class($this->conf['seiten_link']);
84                $this->conf['seite_in_ul'] = $this->menu->get_link_ul_classes($this->conf['seiten_link']);
85
[255]86                // setup body classes:
[279]87                $body_classprefixes = array(
88                        // css prefix => configuration array value
89                        'lang-' => 'lang',
90                        'page-' => 'seiten_id',
91                        'in-nav-' => 'seite_in_nav',
92                        'in-' => 'seite_in_ul',
93                );
94                foreach($body_classprefixes as $prefix => $key) {
95                        if(is_array($this->conf[$key]))
96                                // append each element of array conf values
97                                foreach($this->conf[$key] as $x)
98                                        $this->body_classes[] = $prefix . $x;
99                        elseif($this->conf[$key]) // skip null/false/empty conf values
100                                $this->body_classes[] = $prefix . $this->conf[$key];
101                }
[255]102               
103                // setup javascript configuration
[279]104                $javascript_transfer = array('lang', 'seiten_id', 'seite_in_nav', 'seite_in_ul');
105                foreach($javascript_transfer as $key)
106                        $this->javascript_config[$key] = $this->conf[$key];
[357]107                // also collect data from other classes, e.g. t29Host:
108                $this->javascript_config['web_prefix'] = $this->conf['host']->web_prefix;
[259]109               
[347]110                // get all kind of relations. Pages can afterwards be overwritten with t29Template
111                // methods (see below).
[259]112                $this->page_relations = $this->menu->get_page_relations();
113                $this->interlang_links = $this->menu->get_interlanguage_link();
[390]114                $this->current_link_classes = $this->menu->get_link_classes();
[261]115               
116                // check and load additional css
117                $this->conf['pagecss'] = '/shared/css-v6/pagestyles/'.$this->conf['seiten_id'].'.css';
118                $this->conf['has_pagecss'] = file_exists($this->conf['webroot'].$this->conf['pagecss']);
119                // FIXME: There is no caching check yet for this setting
120                //        (new pagecss file won't be detected and wont purge the tmpl cache)
[279]121               
122                // setup html title
123                $this->conf['html_title'] = '';
124                if(isset($this->conf['titel']) && !empty($this->conf['titel']))
125                        $this->conf['html_title'] = $this->conf['titel'] . ' - ';
[299]126                // Startseite macht ihren Titel jetzt selbst (SEO):
127                //elseif($this->conf['seiten_id'] == $this->msg->_('homepage-pagename'))
128                //      {} // nop: Startseitentitel soll nur sein "technikum29"
[279]129                elseif($this->conf['seiten_link'])
130                        // Titel vom Menu nehmen
131                        $this->conf['html_title'] = $this->conf['seiten_link'] . ' - ';
132                $this->conf['html_title'] .= $this->msg->_('html-title');
[347]133               
134                // Unfortunately mostly a t29Template instance won't be visible to a page
135                // handled by technikum29.php. Therefore there is this small "future" trick:
136                if(isset($this->conf['template_callback']))
137                        $this->conf['template_callback']($this);
138                // Now you can use code like
139                // $template_callback = function($template) {
140                //    $template->set_page_relation("next", "/de/example", "foo");
141                //    $template->menu->... read and modify anything ... etc
142                // }
143                // so the callback function is called at the end of the template constructor.
144                // This can be considered whenever giving a static configuration variables
145                // is not enough.
[254]146        }
147       
148        /**
[347]149         * Overwrite the page relations given by the t29Menu.
150         * By setting $relation to "prev" or "next", you can overwrite the
151         * relations which has been set up by construction by $this->menu->get_page_relations().
152         * Thus any page can state any relations. For sure they are only one-directional,
153         * the other pages don't know anything about such relations because no vice-versa
154         * introspection can be done.
155         **/
156        function set_page_relation($relation, $href, $label) {
157                // good values for $relation are: "prev", "next".
158                // Link is composed as <a href="$href">$label</a>.
159                $this->page_relations[$relation] = t29Menu::dom_new_link($href, $label);
[390]160                //print_r($this->page_relations);
[347]161        }
162
163        /**
164         * Overwrite the interlanguage link list given by the t29Menu.
165         * This does the same as set_page_relation() only for interlanguage links.
166         **/
167        function set_interlang_link($lang, $href, $label) {
168                // good values for $lang are: "de", "en".
169                // Link is composed as <a href="$href">$label</a>.
170                $this->interlang_links[$lang] = t29Menu::dom_new_link($href, $label);
171        }
172       
173        /**
[254]174         * Main caching and output system.
175         * Parameters (global configuration):
176         *    skip_cache  -  if true, skips writing output to cache file
177         *    purge_cache -  if true, forces creation of new cache file
178         *                   (does not change behaviour of this file's code)
179         **/
[264]180        function create_cache($cache_object) {
[357]181                $cache_object->start_cache(array(
182                        'shutdown_func' => array($this, 'print_footer'), // print the footer with it
183                        'filter_func'   => $this->conf['host']->has_web_prefix ? 
184                                array($this, 'rewrite_page_prefix_links') : null, // Entrypoint for URL and content rewriting!
185                ));
186                // directly start header printing
[254]187                $this->print_header();
188        }
[273]189       
190        /**
191         * Write header and footer in separate cache files.
192         **/
193        function create_separate_caches($header_cache, $footer_cache) {
[357]194                $header_cache->start_cache(array(
195                         // start with no shutdown, filter, nor writing
196                        'filter_func' => $this->conf['host']->has_web_prefix ? array($this, 'rewrite_page_prefix_links') : null,
197                        'write_cache' => false,
198                ));
[273]199                $this->print_header();
200                $header_cache->write_cache(); // will also print out header immediately.
201               
[357]202                $footer_cache->start_cache(array(
203                         // start with no shutdown, filter, nor writing
204                        'filter_func' => $this->conf['host']->has_web_prefix ? array($this, 'rewrite_page_prefix_links') : null,
205                        'write_cache' => false,
206                ));
[273]207                $this->print_footer();
208                $footer_content = $footer_cache->write_cache(null, true); // don't print footer immediately.
209               
210                // print footer on exit.
211                register_shutdown_function(function() use ($footer_content) {
212                        print $footer_content;
213                });
214        }
[254]215
216        function print_header() {
[347]217                $p = $this->msg->get_shorthand_printer(); // t29Messages gettext printer
218                $_ = $this->msg->get_shorthand_returner(); // t29Messages gettext
219                $href = $this->conf['host']->get_shorthand_link_returner(); // t29Host link rewriter
[251]220?>
221<!doctype html>
[255]222<html class="no-js" lang="<?php echo $this->conf['lang']; ?>">
[251]223<head>
224  <meta charset="utf-8">
[279]225  <title><?php echo $this->conf['html_title']; ?></title>
[289]226  <meta name="author" content="technikum29-Team">
[343]227  <meta name="generator" content="<?php print $this->conf['host']; ?>">
[254]228  <meta name="t29.cachedate" content="<?php print date('r'); ?>">
[264]229  <?php
[297]230        foreach($this->conf['header_prepend'] as $h) print $h."\n  ";
[390]231       
232        if($this->conf['ajax']) print "\n  <meta name='t29.ajax' content='true'>";
[296]233 
[264]234        if(isset($this->conf['version'])) printf('<meta name="t29.version" content="%s">', $this->conf['version']);
[277]235        if(isset($_GET['debug']))
236                foreach(explode(' ','debug rl_debug skip_cache purge_cache verbose_cache') as $x)
237                        printf("\n  <meta name='t29.template.data-%s' content='%s'>", $x, isset($_GET[$x])?'true':'false');
[264]238  ?>
[259]239 
240  <?php
[347]241        foreach(array_merge(
242                array("first" => t29Menu::dom_new_link($this->conf['lang_path'], $_('head-rel-first'))),
243                $this->page_relations
244        ) as $rel => $a) {
[259]245                if($rel == 'start') continue; // not in standard
246                printf("\n  <link rel='%s' href='%s' title='%s' />",
[347]247                        $rel, $href($a['href']), sprintf($_('head-rel-'.$rel), $this->relational_link_to_string($a))
[259]248                );
249        }
250  ?>
251 
[348]252  <link rel="copyright" href="<?php print $href($_('footer-legal-file')); ?>" title="<?php $p('footer-legal-link'); ?>">
253  <link rel="search" type="application/opensearchdescription+xml" href="<?php print $href($_('topnav-search-page')); print '?action=opensearch-desc&amp;lang='.$this->conf['lang']; ?>" title="<?php $p('opensearch-desc'); ?>">
[259]254  <?php
255        // print interlanguage links for all languages except the active one
256        foreach($this->interlang_links as $lang => $a) {
[276]257                if($lang != $this->conf['lang'] && !is_null($a)) {
[259]258                        printf('<link rel="alternate" href="%s" hreflang="%s" title="%s">',
[347]259                                $href($a['href']), $lang, $this->relational_link_to_string($a)
[259]260                        );
261                }
262        }
263  ?>
264 
[251]265  <meta name="viewport" content="width=device-width,initial-scale=1">
[261]266  <?php
[301]267        $this->print_ressourceloader_links('css', PHP_EOL . '  <link rel="stylesheet" href="%s">');
[261]268  ?>
[251]269
270  <script src="/shared/js-v6/libs/modernizr-2.0.6.min.js"></script>
271</head>
272
[255]273<body class="<?php echo implode(' ', $this->body_classes) ?>">
[251]274  <div id="container">
[259]275        <h1 role="banner"><a href="/" title="<?php $p('head-h1-title'); ?>"><?php $p('head-h1'); ?></a></h1>
[251]276        <div id="background-color-container"><!-- helper -->
277        <section class="main content" role="main" id="content">
[347]278                <ul class="messages panel <?php if($this->log->is_empty()) echo 'empty'; ?> nolist">
279                <?php
280                        // This prints out error messages collected by the log only until this point
281                        $this->log->print_all();
282                        // All log entries generated until final processing will be flushed out at the
283                        // end; a userspace javascript helper will then move them here.
[275]284                ?>
[347]285                </ul>   
[251]286                <!--<header class="teaser">
287                        <h2 id="pdp8L">Wissenschaftliche Rechner und Minicomputer</h2>
288                        <img width=880 src="http://www.technikum29.de/shared/photos/rechnertechnik/univac/panorama-rechts.jpg">
289                </header>-->
[254]290        <!-- start content -->
[251]291<?php 
292} // function print_header().
293
[259]294        function print_footer() {
[347]295                $p = $this->msg->get_shorthand_printer(); // t29Messages gettext printer
296                $_ = $this->msg->get_shorthand_returner(); // t29Messages gettext
297                $href = $this->conf['host']->get_shorthand_link_returner(); // t29Host link rewriter
298               
[259]299        ?>
[254]300        <!-- end content -->
[251]301        </section>
302        <hr>
[265]303        <section class="sidebar top">
[255]304                        <h2 class="visuallyhidden"><?php $p("sidebar-h2-tour"); ?></h2>
[251]305                        <nav class="side">
[347]306                                <?php $this->menu->print_menu(t29Menu::sidebar_menu, $this->conf['host']); ?>
[251]307                        </nav>
[255]308                        <!-- menu changing buttons are made with javascript -->
[251]309        </section>
[265]310        <section class="sidebar bottom">
311                <!-- inhalte die unten ueber dem header sind -->
312        </section>
[251]313        </div><!-- div id="background-color-container" helper end -->
314        <hr>
315        <header class="banner">
[255]316                <h2 class="visuallyhidden"><?php $p("sidebar-h2-mainnav"); ?></h2>
[251]317                <nav class="horizontal">
[347]318                        <?php $this->menu->print_menu(t29Menu::horizontal_menu, $this->conf['host']); ?>
[251]319                </nav>
320                <nav class="top">
[255]321                        <h3 class="visuallyhidden"><?php $p("sidebar-h2-lang"); ?></h3>
[251]322                        <ul>
[255]323                                <?php
[259]324                                        foreach($this->interlang_links as $lang => $a) {
[276]325                                                $is_current_lang = $lang == $this->conf['lang'];
326                                                if(is_null($a)) {
327                                                        // when interlanguage link not present (null) = no translation exists
[390]328                                                        $backtitle = isset($this->conf['titel']) ? $this->conf['titel'] : null;
329                                                       
330                                                        $a = t29Menu::dom_new_link(
331                                                                $_('topnav-interlang-nonexistent-page') . '?'
332                                                                   . htmlentities(http_build_query(array(
333                                                                        'backurl' => $_SERVER['REQUEST_URI'],
334                                                                        'backtitle' => $backtitle ? $backtitle : null,
335                                                                     ))),
336                                                                'not present'
337                                                        );
[276]338                                                        $title = sprintf($_('topnav-interlang-nonexistent', $lang));
339                                                        $class = 'nonexistent';
340                                                } elseif($is_current_lang) {
341                                                        $title = sprintf($_('topnav-interlang-active'), $a);
342                                                        $class = 'active';
343                                                } else {
344                                                        // ordinary interlang link
345                                                        $title = sprintf($_('topnav-interlang-title', $lang), $a);
346                                                        $class = '';
347                                                }
[255]348                                                printf("\t\t\t\t<li%s><a href='%s' title='%s'>%s</a></li>\n",
[276]349                                                        (empty($class) ? '' : " class='$class'"),
[347]350                                                        $href($a['href']), htmlspecialchars($title),
[259]351                                                        $this->conf['languages'][$lang][0] // verbose language name
[255]352                                                );
353                                        }
354                                ?>
[251]355                        </ul>
[347]356                        <form method="get" action="<?php $href($p('topnav-search-page')); ?>">
[268]357                                <span class="no-js"><?php $p('topnav-search-label'); ?>:</span>
358                                <input type="text" value="" data-defaultvalue="<?php $p('topnav-search-label'); ?>" name="q" class="text">
359                                <input type="submit" value="<? $p('topnav-search-label'); ?>" class="button">
[251]360                        </form>
361                </nav>
362    </header>
363        <hr>
[307]364        <?php
[347]365                // only print menu when in sidebar where it applies.
366                // it can also be forced with a global setting $force_footer_menu = 1
367                $print_footer_menu = ($this->conf['seite_in_nav'] == 'side') || isset($this->conf['force_footer_menu']);
[350]368               
369                // print next or prev entry when the current page has a
370                // "show-rel-next" or "show-rel-prev" class entry
[390]371                $show_rel_next = in_array('show-rel-next', $this->current_link_classes);
372                $show_rel_prev = in_array('show-rel-prev', $this->current_link_classes);
373               
[307]374        ?>
375    <footer class="in-sheet <? if(!$print_footer_menu) print "empty-footer"; ?>">
[251]376                <nav class="guide">
377                        <!-- hier wird nav.side die Liste per JS reinkopiert -->
378                </nav>
379                <nav class="rel clearfix">
380                <ul>
[259]381                        <?php
[390]382                          //if($print_footer_menu)
[259]383                                foreach($this->page_relations as $rel => $a) {
[350]384                                        // only show the links wanted to be shown. Only relevant if
385                                        // the "show-rel-*"-magic is working.
[390]386                                        if( $print_footer_menu ||
[350]387                                            (!$print_footer_menu && $rel == "prev" && $show_rel_prev) ||
388                                            (!$print_footer_menu && $rel == "next" && $show_rel_next)) {
[390]389                                       
[350]390                                                printf("\t<li class='%s'><a href='%s' title='%s'>%s <strong>%s</strong></a>\n",
391                                                        $rel, $href($a['href']), sprintf($_('head-rel-'.$rel), $this->relational_link_to_string($a)),
392                                                        $_('nav-rel-'.$rel), $this->relational_link_to_string($a)
393                                                );
[390]394                                        } // endif
[350]395                                } // endfor
[259]396                        ?>
[251]397                </ul>
398                </nav>
399                <div class="right">
[289]400                        <!-- text der rechts unten steht -->
[251]401                </div>
402    </footer>
403  </div> <!--! end of #container -->
[289]404  <footer class="attached">
[291]405    <div class="legacy"><?php $p('footer-legacy-text'); ?></div>
406        <!--
[289]407        <ul class="clearfix">
408        <li class="logo">
[347]409                <a href="<?php $href($p('footer-legal-file')); ?>" class="img" title="technikum29 Logo">Logo</a>
[289]410                <p><?php $p('footer-copyright-tag'); ?>
[347]411                   <br><?php printf('<a href="%s">%s</a>', $href($_('footer-legal-file')), $_('footer-legal-link')); ?>
[289]412                </p>
413        </li>
414        <li class="copy">
[347]415                <a href="<?php $href($p('footer-legal-file')); ?>#image-copyright" class="img">CC</a>
416                <p>Viele Bilder können unter einer <a href="<?php $href($p('footer-legal-file')); ?>#image-copyright">CreativeCommons-Lizenz</a>
417                   verwendet werden. <a href="<?php $href($p('footer-legal-file')); ?>#image-copyright">Erkundigen Sie sich</a>.</p>
[289]418        </li>
419        </ul>
[291]420        -->
[347]421        <?php
422                // pending log messages
423                if(!$this->log->is_empty()) {
424                        echo '<ul class="messages footer nolist">';
425                        $this->log->print_all();
426                        echo '</ul>';
427                }
428        ?>
[289]429  </footer>
[277]430</div><!-- end of div id="footer-background-container" helper -->
[251]431
[306]432  <? /* JavaScript at the bottom for fast page loading */ ?>
[301]433  <script src="/shared/js-v6/libs/jquery-1.7.2.min.js"></script>
[260]434  <script>window.t29={'conf': <?php print json_encode($this->javascript_config); ?>};</script>
[273]435  <?php
[301]436        $this->print_ressourceloader_links('js', '  <script src="%s"></script>'.PHP_EOL);
[273]437  ?>
[306]438  <? /* Piwik Noscript, Script selbst wird asynchron im JS-Bereich aufgerufen */ ?>
439  <noscript><img src="<? $p("js-piwik-noscript-imgsrc"); ?>" alt="" /></noscript>
[251]440</body>
441</html>
442<?php
[254]443        } // function print_footer()
444       
[266]445        // Hilfsfunktionen
446        private function relational_link_to_string($a) {
447                // wenn es bei einem relationalen Link einen Titel gibt, diesen ausgeben, ansonsten die
448                // Linkbeschreibung. Die Links sind XML-Elemente in der Navigation.
449                return isset($a['title']) ? $a['title'] : $a;
450        }
[273]451
[301]452        function print_ressourceloader_links($type, $template='<!-- RL: %s -->') {
453                $rl = $this->rl[$type];
454                $rl_links = $rl->get_urls( isset($_GET['rl_debug']) );
455                $rl_pagespecific_links = $rl->get_page_specific_urls($this->conf['seiten_id']);
456
[357]457                foreach(array($rl_links, $rl_pagespecific_links) as $rls) {
458                        foreach($rls as $link) {
459                                // do the host link renaming conversion. This is more important if
460                                // there is a web_prefix than for the suffix rewriting.
461                                //$link = $this->conf['host']->rewrite_link($link, true);
[301]462                                printf($template, $link);
[357]463                        }
464                }
[273]465        }
[357]466       
467        function rewrite_page_prefix_links($content) {
468                // called by cache: rewrite the page contents
469                return preg_replace('#(href|src|action)=("|\')/#i', '\\1=\\2'.$this->conf['host']->web_prefix.'/', $content);
470        }
[273]471
[301]472
[343]473} // class t29Template
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License