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

Last change on this file since 410 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
Line 
1<?php
2/**
3 * technikum29v6 Page Template.
4 * Initially written 08.01.2012, Sven Koeppel
5 *
6 * This file contains the t29v6 HTML5 template (header, footer, structure, ...).
7 *
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.)
12 *  $titel = Seitentitel
13 *  $header_cache_file, $footer_cache_file.
14 **/
15
16class t29Template {
17        public $conf, $menu, $msg;
18        public $body_classes = array();
19        public $javascript_config = array();
20        public $page_relations, $interlang_links;
21        public $log; // lightweight logging system
22
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         **/
47        function __construct($conf_array) {
48                $this->conf = $conf_array;
49               
50                // fetch the lightweight logging object:
51                require_once $this->conf['lib'].'/logging.php';
52                $this->log = t29Log::get();
53               
54                // create a menu:
55                require_once $this->conf['lib'].'/menu.php';
56                $this->menu = new t29Menu($this->conf);
57
58                // create localisation class:
59                require_once $this->conf['lib'].'/messages.php';
60                $this->msg = new t29Messages($this->conf['lang']);
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);
67
68                // fill up configuration
69               
70                // optional html headers which can be filled by hooks or parts
71                if(!isset($this->conf['header_prepend']))
72                        $this->conf['header_prepend'] = array(); // list
73
74                // ask t29Host for configuration fillup
75                $this->conf['host']->fillup_template_conf($this->conf);
76               
77                // Path names in messages
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));
80
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
86                // setup body classes:
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                }
102               
103                // setup javascript configuration
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];
107                // also collect data from other classes, e.g. t29Host:
108                $this->javascript_config['web_prefix'] = $this->conf['host']->web_prefix;
109               
110                // get all kind of relations. Pages can afterwards be overwritten with t29Template
111                // methods (see below).
112                $this->page_relations = $this->menu->get_page_relations();
113                $this->interlang_links = $this->menu->get_interlanguage_link();
114                $this->current_link_classes = $this->menu->get_link_classes();
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)
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'] . ' - ';
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"
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');
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.
146        }
147       
148        /**
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);
160                //print_r($this->page_relations);
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        /**
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         **/
180        function create_cache($cache_object) {
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
187                $this->print_header();
188        }
189       
190        /**
191         * Write header and footer in separate cache files.
192         **/
193        function create_separate_caches($header_cache, $footer_cache) {
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                ));
199                $this->print_header();
200                $header_cache->write_cache(); // will also print out header immediately.
201               
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                ));
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        }
215
216        function print_header() {
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
220?>
221<!doctype html>
222<html class="no-js" lang="<?php echo $this->conf['lang']; ?>">
223<head>
224  <meta charset="utf-8">
225  <title><?php echo $this->conf['html_title']; ?></title>
226  <meta name="author" content="technikum29-Team">
227  <meta name="generator" content="<?php print $this->conf['host']; ?>">
228  <meta name="t29.cachedate" content="<?php print date('r'); ?>">
229  <?php
230        foreach($this->conf['header_prepend'] as $h) print $h."\n  ";
231       
232        if($this->conf['ajax']) print "\n  <meta name='t29.ajax' content='true'>";
233 
234        if(isset($this->conf['version'])) printf('<meta name="t29.version" content="%s">', $this->conf['version']);
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');
238  ?>
239 
240  <?php
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) {
245                if($rel == 'start') continue; // not in standard
246                printf("\n  <link rel='%s' href='%s' title='%s' />",
247                        $rel, $href($a['href']), sprintf($_('head-rel-'.$rel), $this->relational_link_to_string($a))
248                );
249        }
250  ?>
251 
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'); ?>">
254  <?php
255        // print interlanguage links for all languages except the active one
256        foreach($this->interlang_links as $lang => $a) {
257                if($lang != $this->conf['lang'] && !is_null($a)) {
258                        printf('<link rel="alternate" href="%s" hreflang="%s" title="%s">',
259                                $href($a['href']), $lang, $this->relational_link_to_string($a)
260                        );
261                }
262        }
263  ?>
264 
265  <meta name="viewport" content="width=device-width,initial-scale=1">
266  <?php
267        $this->print_ressourceloader_links('css', PHP_EOL . '  <link rel="stylesheet" href="%s">');
268  ?>
269
270  <script src="/shared/js-v6/libs/modernizr-2.0.6.min.js"></script>
271</head>
272
273<body class="<?php echo implode(' ', $this->body_classes) ?>">
274  <div id="container">
275        <h1 role="banner"><a href="/" title="<?php $p('head-h1-title'); ?>"><?php $p('head-h1'); ?></a></h1>
276        <div id="background-color-container"><!-- helper -->
277        <section class="main content" role="main" id="content">
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.
284                ?>
285                </ul>   
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>-->
290        <!-- start content -->
291<?php 
292} // function print_header().
293
294        function print_footer() {
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               
299        ?>
300        <!-- end content -->
301        </section>
302        <hr>
303        <section class="sidebar top">
304                        <h2 class="visuallyhidden"><?php $p("sidebar-h2-tour"); ?></h2>
305                        <nav class="side">
306                                <?php $this->menu->print_menu(t29Menu::sidebar_menu, $this->conf['host']); ?>
307                        </nav>
308                        <!-- menu changing buttons are made with javascript -->
309        </section>
310        <section class="sidebar bottom">
311                <!-- inhalte die unten ueber dem header sind -->
312        </section>
313        </div><!-- div id="background-color-container" helper end -->
314        <hr>
315        <header class="banner">
316                <h2 class="visuallyhidden"><?php $p("sidebar-h2-mainnav"); ?></h2>
317                <nav class="horizontal">
318                        <?php $this->menu->print_menu(t29Menu::horizontal_menu, $this->conf['host']); ?>
319                </nav>
320                <nav class="top">
321                        <h3 class="visuallyhidden"><?php $p("sidebar-h2-lang"); ?></h3>
322                        <ul>
323                                <?php
324                                        foreach($this->interlang_links as $lang => $a) {
325                                                $is_current_lang = $lang == $this->conf['lang'];
326                                                if(is_null($a)) {
327                                                        // when interlanguage link not present (null) = no translation exists
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                                                        );
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                                                }
348                                                printf("\t\t\t\t<li%s><a href='%s' title='%s'>%s</a></li>\n",
349                                                        (empty($class) ? '' : " class='$class'"),
350                                                        $href($a['href']), htmlspecialchars($title),
351                                                        $this->conf['languages'][$lang][0] // verbose language name
352                                                );
353                                        }
354                                ?>
355                        </ul>
356                        <form method="get" action="<?php $href($p('topnav-search-page')); ?>">
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">
360                        </form>
361                </nav>
362    </header>
363        <hr>
364        <?php
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']);
368               
369                // print next or prev entry when the current page has a
370                // "show-rel-next" or "show-rel-prev" class entry
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               
374        ?>
375    <footer class="in-sheet <? if(!$print_footer_menu) print "empty-footer"; ?>">
376                <nav class="guide">
377                        <!-- hier wird nav.side die Liste per JS reinkopiert -->
378                </nav>
379                <nav class="rel clearfix">
380                <ul>
381                        <?php
382                          //if($print_footer_menu)
383                                foreach($this->page_relations as $rel => $a) {
384                                        // only show the links wanted to be shown. Only relevant if
385                                        // the "show-rel-*"-magic is working.
386                                        if( $print_footer_menu ||
387                                            (!$print_footer_menu && $rel == "prev" && $show_rel_prev) ||
388                                            (!$print_footer_menu && $rel == "next" && $show_rel_next)) {
389                                       
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                                                );
394                                        } // endif
395                                } // endfor
396                        ?>
397                </ul>
398                </nav>
399                <div class="right">
400                        <!-- text der rechts unten steht -->
401                </div>
402    </footer>
403  </div> <!--! end of #container -->
404  <footer class="attached">
405    <div class="legacy"><?php $p('footer-legacy-text'); ?></div>
406        <!--
407        <ul class="clearfix">
408        <li class="logo">
409                <a href="<?php $href($p('footer-legal-file')); ?>" class="img" title="technikum29 Logo">Logo</a>
410                <p><?php $p('footer-copyright-tag'); ?>
411                   <br><?php printf('<a href="%s">%s</a>', $href($_('footer-legal-file')), $_('footer-legal-link')); ?>
412                </p>
413        </li>
414        <li class="copy">
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>
418        </li>
419        </ul>
420        -->
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        ?>
429  </footer>
430</div><!-- end of div id="footer-background-container" helper -->
431
432  <? /* JavaScript at the bottom for fast page loading */ ?>
433  <script src="/shared/js-v6/libs/jquery-1.7.2.min.js"></script>
434  <script>window.t29={'conf': <?php print json_encode($this->javascript_config); ?>};</script>
435  <?php
436        $this->print_ressourceloader_links('js', '  <script src="%s"></script>'.PHP_EOL);
437  ?>
438  <? /* Piwik Noscript, Script selbst wird asynchron im JS-Bereich aufgerufen */ ?>
439  <noscript><img src="<? $p("js-piwik-noscript-imgsrc"); ?>" alt="" /></noscript>
440</body>
441</html>
442<?php
443        } // function print_footer()
444       
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        }
451
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
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);
462                                printf($template, $link);
463                        }
464                }
465        }
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        }
471
472
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