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

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