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

Last change on this file since 560 was 560, checked in by sven, 10 years ago

Unterstützung von externen Einbindungen, also Einbindungen
des Homepage-Layouts von Seiten außerhalb der Homepage.

  • Cache: Skip-Cache-Unterstüztung verbessert, Bugfix
  • Template: Short Open Tags vermieden, mehr Hooks (zwecks externer Einbindung)
  • WebStart: Unterstützung für external-Keyword, bei Anwesenheit wird Caching komplett unterdrückt (noch extremere Form wie bei dynamischen Seiten).
  • Property svn:keywords set to Id
File size: 19.3 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, if not given:
55                require_once $this->conf['lib'].'/menu.php';
56                $this->menu = isset($this->conf['menu']) ? $this->conf['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                elseif(is_string($this->conf['header_prepend']))
74                        $this->conf['header_prepend'] = array($this->conf['header_prepend']); // string to list
75
76                // ask t29Host for configuration fillup
77                $this->conf['host']->fillup_template_conf($this->conf);
78               
79                // Path names in messages
80                foreach(array('footer-legal-file', 'topnav-search-page') as $msg_id)
81                        $this->msg->set($msg_id, $this->conf['lang_path'].$this->msg->_($msg_id));
82
83                // store informations about the current page
84                $this->conf['seiten_link'] = $this->menu->get_link();
85                $this->conf['seite_in_nav'] = $this->menu->get_link_navigation_class($this->conf['seiten_link']);
86                $this->conf['seite_in_ul'] = $this->menu->get_link_ul_classes($this->conf['seiten_link']);
87
88                // setup body classes:
89                $body_classprefixes = array(
90                        // css prefix => configuration array value
91                        'lang-' => 'lang',
92                        'page-' => 'seiten_id',
93                        'in-nav-' => 'seite_in_nav',
94                        'in-' => 'seite_in_ul',
95                );
96                foreach($body_classprefixes as $prefix => $key) {
97                        if(is_array($this->conf[$key]))
98                                // append each element of array conf values
99                                foreach($this->conf[$key] as $x)
100                                        $this->body_classes[] = $prefix . $x;
101                        elseif($this->conf[$key]) // skip null/false/empty conf values
102                                $this->body_classes[] = $prefix . $this->conf[$key];
103                }
104               
105                // setup javascript configuration
106                $javascript_transfer = array('lang', 'seiten_id', 'seite_in_nav', 'seite_in_ul');
107                foreach($javascript_transfer as $key)
108                        $this->javascript_config[$key] = $this->conf[$key];
109                // also collect data from other classes, e.g. t29Host:
110                $this->javascript_config['web_prefix'] = $this->conf['host']->web_prefix;
111               
112                // get all kind of relations. Pages can afterwards be overwritten with t29Template
113                // methods (see below).
114                $this->page_relations = $this->menu->get_page_relations();
115                $this->interlang_links = $this->menu->get_interlanguage_link();
116                $this->current_link_classes = $this->menu->get_link_classes();
117               
118                // check and load additional css.
119                // #51: This is now checked by the site caching in technikum29.php.
120                $this->conf['pagecss'] = $this->conf['host']->ressources_get_pagestyle($this->conf['seiten_id']);
121                $this->conf['has_pagecss'] = file_exists($this->conf['webroot'].$this->conf['pagecss']);
122               
123                // setup html title
124                $this->conf['html_title'] = '';
125                if(isset($this->conf['titel']) && !empty($this->conf['titel']))
126                        $this->conf['html_title'] = $this->conf['titel'] . ' - ';
127                // Startseite macht ihren Titel jetzt selbst (SEO):
128                //elseif($this->conf['seiten_id'] == $this->msg->_('homepage-pagename'))
129                //      {} // nop: Startseitentitel soll nur sein "technikum29"
130                elseif($this->conf['seiten_link'])
131                        // Titel vom Menu nehmen
132                        $this->conf['html_title'] = $this->conf['seiten_link'] . ' - ';
133                $this->conf['html_title'] .= $this->msg->_('html-title');
134               
135                // Unfortunately mostly a t29Template instance won't be visible to a page
136                // handled by technikum29.php. Therefore there is this small "future" trick:
137                if(isset($this->conf['template_callback']))
138                        $this->conf['template_callback']($this);
139                // Now you can use code like
140                // $template_callback = function($template) {
141                //    $template->set_page_relation("next", "/de/example", "foo");
142                //    $template->menu->... read and modify anything ... etc
143                // }
144                // so the callback function is called at the end of the template constructor.
145                // This can be considered whenever giving a static configuration variables
146                // is not enough.
147        }
148       
149        /**
150         * Overwrite the page relations given by the t29Menu.
151         * By setting $relation to "prev" or "next", you can overwrite the
152         * relations which has been set up by construction by $this->menu->get_page_relations().
153         * Thus any page can state any relations. For sure they are only one-directional,
154         * the other pages don't know anything about such relations because no vice-versa
155         * introspection can be done.
156         **/
157        function set_page_relation($relation, $href, $label) {
158                // good values for $relation are: "prev", "next".
159                // Link is composed as <a href="$href">$label</a>.
160                $this->page_relations[$relation] = t29Menu::dom_new_link($href, $label);
161                //print_r($this->page_relations);
162        }
163
164        /**
165         * Overwrite the interlanguage link list given by the t29Menu.
166         * This does the same as set_page_relation() only for interlanguage links.
167         **/
168        function set_interlang_link($lang, $href, $label) {
169                // good values for $lang are: "de", "en".
170                // Link is composed as <a href="$href">$label</a>.
171                $this->interlang_links[$lang] = t29Menu::dom_new_link($href, $label);
172        }
173       
174        /**
175         * Main caching and output system.
176         * Parameters (global configuration):
177         *    skip_cache  -  if true, skips writing output to cache file
178         *    purge_cache -  if true, forces creation of new cache file
179         *                   (does not change behaviour of this file's code)
180         **/
181        function create_cache($cache_object) {
182                $cache_object->start_cache(array(
183                        'shutdown_func' => array($this, 'print_footer'), // print the footer with it
184                        'filter_func'   => $this->conf['host']->has_web_prefix ? 
185                                array($this, 'rewrite_page_prefix_links') : null, // Entrypoint for URL and content rewriting!
186                ));
187                // directly start header printing
188                $this->print_header();
189        }
190       
191        /**
192         * Write header and footer in separate cache files.
193         **/
194        function create_separate_caches($header_cache, $footer_cache) {
195                $header_cache->start_cache(array(
196                         // start with no shutdown, filter, nor writing
197                        'filter_func' => $this->conf['host']->has_web_prefix ? array($this, 'rewrite_page_prefix_links') : null,
198                        'write_cache' => false,
199                ));
200                $this->print_header();
201                $header_cache->write_cache(); // will also print out header immediately.
202               
203                $footer_cache->start_cache(array(
204                         // start with no shutdown, filter, nor writing
205                        'filter_func' => $this->conf['host']->has_web_prefix ? array($this, 'rewrite_page_prefix_links') : null,
206                        'write_cache' => false,
207                ));
208                $this->print_footer();
209                $footer_content = $footer_cache->write_cache(null, true); // don't print footer immediately.
210               
211                // print footer on exit.
212                register_shutdown_function(function() use ($footer_content) {
213                        print $footer_content;
214                });
215        }
216
217        function print_header() {
218                $p = $this->msg->get_shorthand_printer(); // t29Messages gettext printer
219                $_ = $this->msg->get_shorthand_returner(); // t29Messages gettext
220                $href = $this->conf['host']->get_shorthand_link_returner(); // t29Host link rewriter
221?>
222<!doctype html>
223<html class="no-js" lang="<?php echo $this->conf['lang']; ?>">
224<head>
225  <meta charset="utf-8">
226  <title><?php echo $this->conf['html_title']; ?></title>
227  <meta name="author" content="technikum29-Team">
228  <meta name="generator" content="<?php print $this->conf['host']; ?>">
229  <meta name="t29.cachedate" content="<?php print date('r'); ?>">
230  <?php
231        foreach($this->conf['header_prepend'] as $h) print $h."\n  ";
232       
233        $indicators = array('ajax', 'external');
234        foreach($indicators as $key)
235                if($this->conf[$key]) print "\n  <meta name='t29.$key' content='true'>";
236 
237        if(isset($this->conf['version'])) printf('<meta name="t29.version" content="%s">', $this->conf['version']);
238        if(isset($_GET['debug']))
239                foreach(explode(' ','debug rl_debug skip_cache purge_cache verbose_cache') as $x)
240                        printf("\n  <meta name='t29.template.data-%s' content='%s'>", $x, isset($_GET[$x])?'true':'false');
241  ?>
242 
243  <?php
244        foreach(array_merge(
245                array("first" => t29Menu::dom_new_link($this->conf['lang_path'], $_('head-rel-first'))),
246                $this->page_relations
247        ) as $rel => $a) {
248                if($rel == 'start') continue; // not in standard
249                printf("\n  <link rel='%s' href='%s' title='%s' />",
250                        $rel, $href($a['href']), sprintf($_('head-rel-'.$rel), $this->relational_link_to_string($a))
251                );
252        }
253  ?>
254 
255  <link rel="copyright" href="<?php print $href($_('footer-legal-file')); ?>" title="<?php $p('footer-legal-link'); ?>">
256  <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'); ?>">
257  <link rel="alternate" type="application/rss+xml" href="/de/news.php?format=rss" title="Neuigkeiten vom technikum29-Museum" />
258  <?php
259        // print interlanguage links for all languages except the active one
260        foreach($this->interlang_links as $lang => $a) {
261                if($lang != $this->conf['lang'] && !is_null($a)) {
262                        printf('<link rel="alternate" href="%s" hreflang="%s" title="%s">',
263                                $href($a['href']), $lang, $this->relational_link_to_string($a)
264                        );
265                }
266        }
267  ?>
268 
269  <meta name="viewport" content="width=device-width,initial-scale=1">
270  <?php
271        $this->print_ressourceloader_links('css', PHP_EOL . '  <link rel="stylesheet" href="%s">');
272  ?>
273
274  <script src="/shared/js-v6/libs/modernizr-2.0.6.min.js"></script>
275</head>
276
277<body class="<?php echo implode(' ', $this->body_classes) ?>">
278  <div id="container">
279        <h1 role="banner"><a href="/" title="<?php $p('head-h1-title'); ?>"><?php $p('head-h1'); ?></a></h1>
280        <div id="background-color-container"><!-- helper -->
281        <section class="main content" role="main" id="content">
282                <ul class="messages panel <?php if($this->log->is_empty()) echo 'empty'; ?> nolist">
283                <?php
284                        // This prints out error messages collected by the log only until this point
285                        $this->log->print_all();
286                        // All log entries generated until final processing will be flushed out at the
287                        // end; a userspace javascript helper will then move them here.
288                ?>
289                </ul>   
290                <!--<header class="teaser">
291                        <h2 id="pdp8L">Wissenschaftliche Rechner und Minicomputer</h2>
292                        <img width=880 src="http://www.technikum29.de/shared/photos/rechnertechnik/univac/panorama-rechts.jpg">
293                </header>-->
294        <!-- start content -->
295<?php 
296} // function print_header().
297
298        function print_footer() {
299                $p = $this->msg->get_shorthand_printer(); // t29Messages gettext printer
300                $_ = $this->msg->get_shorthand_returner(); // t29Messages gettext
301                $href = $this->conf['host']->get_shorthand_link_returner(); // t29Host link rewriter
302               
303        ?>
304        <!-- end content -->
305        </section>
306        <hr>
307        <section class="sidebar top">
308                        <?php /* Platz fuer eigentlich per "Extension" eingepflegtem Inhalt */ ?>
309                        <!-- Anfang Test -->
310                        <a class="button alertbox termine" href="/de/#termine">
311                                <strong>Aktuelle Führungen</strong>
312                                <span>Tage der Industriekultur im technikum29</span>
313                        </a>
314                        <!-- Ende Test -->
315                       
316                        <h2 class="visuallyhidden"><?php $p("sidebar-h2-tour"); ?></h2>
317                        <?php
318                                $sidebar_contains_menu  = !isset($this->conf['sidebar_content']);
319                        ?>
320                        <nav class="side <?php print $sidebar_contains_menu ? 'contains-menu' : 'contains-custom'; ?>">
321                                <?php
322                                        if(!$sidebar_contains_menu)
323                                                // used in external page calls
324                                                print $this->conf['sidebar_content'];
325                                        else
326                                                $this->menu->print_menu(t29Menu::sidebar_menu, $this->conf['host']);
327                                ?>
328                        </nav>
329                        <!-- menu changing buttons are made with javascript, but  -->
330        </section>
331        <section class="sidebar bottom">
332                <!-- inhalte die unten ueber dem header sind -->
333        </section>
334        </div><!-- div id="background-color-container" helper end -->
335        <hr>
336        <header class="banner">
337                <h2 class="visuallyhidden"><?php $p("sidebar-h2-mainnav"); ?></h2>
338                <nav class="horizontal">
339                        <?php
340                                if(isset($this->conf['mainnav_content']))
341                                        // used in external page calls
342                                        print $this->conf['mainnav_content'];
343                                else
344                                        $this->menu->print_menu(t29Menu::horizontal_menu, $this->conf['host']);
345                        ?>
346                </nav>
347                <nav class="top">
348                        <h3 class="visuallyhidden"><?php $p("sidebar-h2-lang"); ?></h3>
349                        <ul>
350                                <?php
351                                        foreach($this->interlang_links as $lang => $a) {
352                                                $is_current_lang = $lang == $this->conf['lang'];
353                                                if(is_null($a)) {
354                                                        // when interlanguage link not present (null) = no translation exists
355                                                        $backtitle = isset($this->conf['titel']) ? $this->conf['titel'] : null;
356                                                       
357                                                        $a = t29Menu::dom_new_link(
358                                                                $_('topnav-interlang-nonexistent-page') . '?'
359                                                                   . htmlentities(http_build_query(array(
360                                                                        'backurl' => $_SERVER['REQUEST_URI'],
361                                                                        'backtitle' => $backtitle ? $backtitle : null,
362                                                                     ))),
363                                                                'not present'
364                                                        );
365                                                        $title = sprintf($_('topnav-interlang-nonexistent', $lang));
366                                                        $class = 'nonexistent';
367                                                } elseif($is_current_lang) {
368                                                        $title = sprintf($_('topnav-interlang-active'), $a);
369                                                        $class = 'active';
370                                                } else {
371                                                        // ordinary interlang link
372                                                        $title = sprintf($_('topnav-interlang-title', $lang), $a);
373                                                        $class = '';
374                                                }
375                                                printf("\t\t\t\t<li%s><a href='%s' title='%s'>%s</a></li>\n",
376                                                        (empty($class) ? '' : " class='$class'"),
377                                                        $href($a['href']), htmlspecialchars($title),
378                                                        $this->conf['languages'][$lang][0] // verbose language name
379                                                );
380                                        }
381                                ?>
382                        </ul>
383                        <form method="get" action="<?php $href($p('topnav-search-page')); ?>">
384                                <span class="no-js"><?php $p('topnav-search-label'); ?>:</span>
385                                <input type="text" value="" data-defaultvalue="<?php $p('topnav-search-label'); ?>" name="q" class="text">
386                                <input type="submit" value="<?php $p('topnav-search-label'); ?>" class="button">
387                        </form>
388                </nav>
389    </header>
390        <hr>
391        <?php
392                // only print menu when in sidebar where it applies.
393                // it can also be forced with a global setting $force_footer_menu = 1
394                $print_footer_menu = ($this->conf['seite_in_nav'] == 'side') || isset($this->conf['force_footer_menu']);
395               
396                // print next or prev entry when the current page has a
397                // "show-rel-next" or "show-rel-prev" class entry
398                $show_rel_next = in_array('show-rel-next', $this->current_link_classes);
399                $show_rel_prev = in_array('show-rel-prev', $this->current_link_classes);
400               
401        ?>
402    <footer class="in-sheet <?php if(!$print_footer_menu) print "empty-footer"; ?>">
403                <nav class="guide">
404                        <!-- hier wird nav.side die Liste per JS reinkopiert -->
405                </nav>
406                <nav class="rel clearfix">
407                <ul>
408                        <?php
409                          //if($print_footer_menu)
410                                foreach($this->page_relations as $rel => $a) {
411                                        // only show the links wanted to be shown. Only relevant if
412                                        // the "show-rel-*"-magic is working.
413                                        if( $print_footer_menu ||
414                                            (!$print_footer_menu && $rel == "prev" && $show_rel_prev) ||
415                                            (!$print_footer_menu && $rel == "next" && $show_rel_next)) {
416                                       
417                                                printf("\t<li class='%s'><a href='%s' title='%s'>%s <strong>%s</strong></a>\n",
418                                                        $rel, $href($a['href']), sprintf($_('head-rel-'.$rel), $this->relational_link_to_string($a)),
419                                                        $_('nav-rel-'.$rel), $this->relational_link_to_string($a)
420                                                );
421                                        } // endif
422                                } // endfor
423                        ?>
424                </ul>
425                </nav>
426                <div class="right">
427                        <!-- text der rechts unten steht -->
428                </div>
429    </footer>
430  </div> <!--! end of #container -->
431  <footer class="attached">
432    <div class="legacy"><?php $p('footer-legacy-text'); ?></div>
433        <!--
434        <ul class="clearfix">
435        <li class="logo">
436                <a href="<?php $href($p('footer-legal-file')); ?>" class="img" title="technikum29 Logo">Logo</a>
437                <p><?php $p('footer-copyright-tag'); ?>
438                   <br><?php printf('<a href="%s">%s</a>', $href($_('footer-legal-file')), $_('footer-legal-link')); ?>
439                </p>
440        </li>
441        <li class="copy">
442                <a href="<?php $href($p('footer-legal-file')); ?>#image-copyright" class="img">CC</a>
443                <p>Viele Bilder können unter einer <a href="<?php $href($p('footer-legal-file')); ?>#image-copyright">CreativeCommons-Lizenz</a>
444                   verwendet werden. <a href="<?php $href($p('footer-legal-file')); ?>#image-copyright">Erkundigen Sie sich</a>.</p>
445        </li>
446        </ul>
447        -->
448        <?php
449                // pending log messages
450                if(!$this->log->is_empty()) {
451                        echo '<ul class="messages footer nolist">';
452                        $this->log->print_all();
453                        echo '</ul>';
454                }
455        ?>
456  </footer>
457</div><!-- end of div id="footer-background-container" helper -->
458
459  <?php /* JavaScript at the bottom for fast page loading */ ?>
460  <script src="/shared/js-v6/libs/jquery-1.7.2.min.js"></script>
461  <script>window.t29={'conf': <?php print json_encode($this->javascript_config); ?>};</script>
462  <?php
463        $this->print_ressourceloader_links('js', '  <script src="%s"></script>'.PHP_EOL);
464  ?>
465  <?php /* Piwik Noscript, Script selbst wird asynchron im JS-Bereich aufgerufen */ ?>
466  <noscript><img src="<?php $p("js-piwik-noscript-imgsrc"); ?>" alt="" /></noscript>
467  <?php
468        if(isset($this->conf['body_append']))
469                print $this->conf['body_append'];
470  ?>
471</body>
472</html>
473<?php
474        } // function print_footer()
475       
476        // Hilfsfunktionen
477        private function relational_link_to_string($a) {
478                // wenn es bei einem relationalen Link einen Titel gibt, diesen ausgeben, ansonsten die
479                // Linkbeschreibung. Die Links sind XML-Elemente in der Navigation.
480                return isset($a['title']) ? $a['title'] : $a;
481        }
482
483        function print_ressourceloader_links($type, $template='<!-- RL: %s -->') {
484                $rl = $this->rl[$type];
485                $rl_links = $rl->get_urls( isset($_GET['rl_debug']) );
486                $rl_pagespecific_links = $rl->get_page_specific_urls($this->conf['seiten_id']);
487
488                foreach(array($rl_links, $rl_pagespecific_links) as $rls) {
489                        foreach($rls as $link) {
490                                // do the host link renaming conversion. This is more important if
491                                // there is a web_prefix than for the suffix rewriting.
492                                //$link = $this->conf['host']->rewrite_link($link, true);
493                                printf($template, $link);
494                        }
495                }
496        }
497       
498        function rewrite_page_prefix_links($content) {
499                // called by cache: rewrite the page contents
500                return preg_replace('#(href|src|action)=("|\')/#i', '\\1=\\2'.$this->conf['host']->web_prefix.'/', $content);
501        }
502
503
504} // 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