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

Last change on this file since 357 was 357, checked in by sven, 11 years ago

t29Host-Webroot-System entwickelt.

Damit können Installationen der technikum29-Website ab nun auch in Unterverzeichnissen
(bislang allerdings nur unterhalb des DocumentRoots) installiert werden, was einem
größere Flexibilität beim lokalen Aufsetzen der Seite liefert

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