Changeset 259 in t29-www for lib


Ignore:
Timestamp:
May 28, 2012, 6:07:42 PM (12 years ago)
Author:
sven
Message:

Navigationsdateien vereinheitlicht in eine navigation.xml. menu.php kann jetzt mehrsprachige Titel extrahieren und gibt selber kein HTML mehr aus. Template/JS massiv für Mehrsprachlichkeit umgebaut.

Location:
lib
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • lib/menu.php

    r255 r259  
    11<?php
    2 
     2/**
     3 * Needs conf:
     4 *  webroot lang_path lang seiten_id languages
     5 *
     6 **/
    37class t29Menu {
    48        public $conf;
     9        public $xml;
    510
    6         const horizontal_menu = 'hauptnavigation.xml';
    7         const sidebar_menu = 'sidebar.xml';
     11        // jeweils relativ zum lang_path
     12        const navigation_file = 'navigation.xml';
    813        const news_file = 'news.php';
     14
     15        // xpath queries to the navigation elements
     16        const horizontal_menu = '/html/nav[@class="horizontal"]';
     17        const sidebar_menu = '/html/nav[@class="side"]';
    918
    1019        function __construct($conf_array) {
    1120                $this->conf = $conf_array;
     21
     22                // load xml file
     23                $this->xml = simplexml_load_file($this->conf['webroot'].$this->conf['lang_path'] . '/' . self::navigation_file);
    1224        }
    13        
     25
     26        ///////////////////// NEWS EXTRACTION
    1427        function load_news_data() {
    1528                $newsfile = $this->conf['webroot'].$this->conf['lang_path']."/".self::news_file;
     
    4356                return $news_ul_content;
    4457        }
     58       
     59        ///////////////////// RETURN INFOS ABOUT SEITEN_ID LINK
     60        function get_link_infos($seiten_id=false) {
     61                if(!$seiten_id) $seiten_id = $this->conf['seiten_id'];
     62
     63                $matches = $this->xml->xpath("//a[@seiten_id='$seiten_id']");
     64                if($matches && count($matches)) {
     65                        // strip the first one
     66                        return $matches[0];
     67                }
     68        }
     69
     70        ///////////////////// INTER LANGUAGE DETECTION
     71        /**
     72         * @param seiten_id Get interlanguage link for that seiten_id or default.
     73         **/
     74        function get_interlanguage_link($seiten_id=false) {
     75                if(!$seiten_id) $seiten_id = $this->conf['seiten_id'];
     76               
     77                $interlinks = array(); // using a loop instead of mappings since php is stupid
     78                foreach($this->conf['languages'] as $lang => $lconf) {
     79                        $foreign_menu = new t29Menu(array(
     80                                'webroot' => $this->conf['webroot'],
     81                                'seiten_id' => $this->conf['seiten_id'],
     82                                'languages' => $this->conf['languages'],
     83                                'lang' => $lang,
     84                                'lang_path' => $lconf[1],
     85                        ));
     86
     87                        $link = $foreign_menu->get_link_infos($seiten_id);
     88                        $interlinks[$lang] = $link;
     89                }
     90               
     91                return $interlinks;
     92        }
    4593
    4694        // helper method
    4795        public static function dom_add_class($simplexml_element, $value) {
    48                 $dom = dom_import_simplexml($simplexml_element);
     96                $dom = dom_import_simplexml($simplexml_element); // is a fast operation
    4997                $simplexml_element['class'] =
    5098                        ($dom->hasAttribute("class") ? ($simplexml_element['class'].' '):'').$value;
    5199        }
     100       
     101        public static function dom_new_link($href, $label) {
     102                return new SimpleXMLElement(sprintf('<a href="%s">%s</a>', $href, $label));
     103        }
    52104
    53         function print_menu($file) {
     105        ///////////////////// MENU ACTIVE LINK DETECTION
     106        /**
     107         * @arg $xpath_menu_selection  one of the horizontal_menu / sidebar_menu consts.
     108         **/
     109        function print_menu($xpath_menu_selection) {
    54110                $seiten_id = $this->conf['seiten_id'];
    55                 $xml = simplexml_load_file($this->conf['webroot'].$this->conf['lang_path'] . '/' . $file);
    56        
     111
     112                // find wanted menu
     113                $xml = $this->xml->xpath($xpath_menu_selection);
     114                if(!$xml) {
     115                        print "Menu <i>$xpath_menu_selection</i> not found!";
     116                        return false;
     117                }
     118                $xml = $xml[0]; // just take the first result (should only one result be present)
     119
    57120                // aktuelle Seite anmarkern und Hierarchie hochgehen
    58121                // (<ul><li>bla<ul><li>bla<ul><li>hierbin ich <- hochgehen.)
     
    74137                }
    75138       
    76                 if($file == self::horizontal_menu) {
     139                if($xpath_menu_selection == self::horizontal_menu) {
    77140                        # inject news
    78141                        $news_ul_content = $this->convert_news_data();
     
    85148        }
    86149
    87         function print_relations() {
    88                 $seiten_id = $this->conf['seiten_id'];
    89        
    90                 $sidebar = simplexml_load_file($this->conf['webroot'] . $this->conf['lang_path'] . '/' . self::sidebar_menu);
     150        ///////////////////// PAGE RELATIONS
     151        /**
     152         * Usage:
     153         * foreach(get_page_relations() as $a) {
     154         *    echo "Link $a going to $a[href]";
     155         * }
     156         * @param $seiten_id A seiten_id string or nothing for taking the current active string
     157         * @returns an array(prev=>..., next=>...) or empty array, elements are SimpleXML a links
     158         **/
     159        function get_page_relations($seiten_id=false) {
     160                if(!$seiten_id) $seiten_id = $this->conf['seiten_id'];
     161               
     162                $xml = $this->xml->xpath(self::sidebar_menu);
     163                if(!$xml) { print "<i>Sidebar not found</i>"; return; }
     164                $sidebar = $xml[0];
     165               
     166                $return = array();
    91167                $current_a = $sidebar->xpath("//a[@seiten_id='$seiten_id']");
    92168                if(count($current_a)) {
    93                         $prev = $current_a[0]->xpath("preceding::a[@seiten_id][1]");
    94                         if(count($prev)) {
    95                                 $a = $prev[0];
    96                                 print "<li class='prev'><a href='$a[href]'>vorherige Seite <strong>$a</strong></a></li>";
    97                         }
    98                         $next = $current_a[0]->xpath("following::a[@seiten_id][1]");
    99                         if(count($next)) {
    100                                 $a = $next[0];
    101                                 print "<li class='next'><a href='$a[href]'>nächste Seite <strong>$a</strong></a></li>";
     169                        foreach(array(
     170                          "prev" => "preceding::a[@seiten_id][1]",
     171                          "next" => "following::a[@seiten_id][1]") as $rel => $xpath) {
     172                                $node = $current_a[0]->xpath($xpath);
     173                                if(count($node))
     174                                        $return[$rel] = $node[0]; # $node[0] = <a href=../> tag
    102175                        }
    103176                } else {
    104                         print '<li class="start"><a href="#">Starte Führung <strong>Blabla</strong></a>';
     177                        // TODO PENDING: Der Fall tritt derzeit niemals ein, da das XML
     178                        // sich dann doch irgendwie auf alles bezieht ($sidebar = alles) und
     179                        // ueberall gesucht wird. Ist aber okay. oder?
     180                        $return['start'] = t29Menu::dom_new_link('#', 'bla');
    105181                }
     182                return $return;
    106183        }
    107184
  • lib/messages.php

    r255 r259  
    6262                'sidebar-h2-mainnav'     => array('Hauptnavigation', 'Main Navigation'),
    6363                'sidebar-h2-lang'        => array('Sprachauswahl', 'Language'),
    64                 'sidebar-search-label'   => array('Suchen', 'Search'),
     64
     65                'topnav-interlang-title' => array('Read this page (%s) in English', 'Diese Seite (%s) auf Deutsch lesen'),
     66                'topnav-search-label'    => array('Suchen', 'Search'),
    6567
    6668                'js-menu-collapse-out'   => array('Menü ausklappen', 'Expand menu'),
     
    7173                'footer-copyright-tag'   => '&copy; 2003-2012 technikum29.',
    7274                'footer-legal-link'      => array('Impressum und Kontakt', 'Legal notices'),
     75                'footer-legal-file'      => array('/impressum.php', '/contact.php'),
     76               
     77                'nav-rel-prev'           => array('vorherige Seite', 'previous page'),
     78                'nav-rel-next'           => array('nächste Seite', 'next page'),
     79                'nav-rel-start'          => array('Starte Führrung', 'Start guided tour'),
     80               
     81                'head-rel-first'         => array('Deutscher Start', 'English start'),
     82                'head-rel-prev'          => array('Zur vorherigen Seite (%s)', 'Previous Page (%s)'),
     83                'head-rel-next'          => array('Zur folgenden Seite (%s)', 'Next Page (%s)'),
     84                'head-rel-interlang'     => array('Englische Version dieser Seite (%s)', 'Deutsche Version dieser Seite (%s)'),
    7385        );
    7486}
  • lib/technikum29.php

    r255 r259  
    2121);
    2222
    23 $lang = "de"; # must be index in $languages
     23// try to determine the language from the file path
     24$lang = substr($file, 1, 2);
     25if(!in_array($lang, array_keys($languages))) $lang = "de"; # check if language exists
    2426$lang_path = $languages[$lang][1]; # shorthand, relative to webroot. use "$webroot$lang_path" for local.
    2527
     
    3537        "$lib/menu.php",
    3638        "$lib/messages.php",
    37         "$lib/../de-v6/hauptnavigation.xml",
    38         "$lib/../de-v6/sidebar.xml",
    39         "$lib/../de-v6/news.php",
     39        "$lib$lang_path/navigation.xml",
     40        "$lib$lang_path/news.php",
    4041);
    4142
  • lib/template.php

    r255 r259  
    1212 
    1313class t29Template {
    14         private static $legal_pagenames = array( // should be const
    15         # lang => file relative to $lang_path, with starting "/".
    16                 "de" => "/impressum.php",
    17                 "en" => "/contact.php",
    18         );
    19 
    2014        public $conf, $menu, $msg;
    2115        public $body_classes = array();
    2216        public $javascript_config = array();
     17        public $page_relations, $interlang_links;
    2318
    2419        function __construct($conf_array) {
    2520                $this->conf = $conf_array;
    26 
    27                 // fill up configuration
    28                 $this->conf['legal_pagename'] = $this->conf['lang_path'] . self::$legal_pagenames[$this->conf['lang']];
    2921
    3022                // create a menu:
     
    3527                require_once $this->conf['lib'].'/messages.php';
    3628                $this->msg = new t29Messages($this->conf['lang']);
     29
     30                // fill up configuration
     31                $this->conf['legal_pagename'] = $this->conf['lang_path'] . $this->msg->_('footer-legal-file');
    3732
    3833                // setup body classes:
     
    4338                $this->javascript_config['lang'] = $this->conf['lang'];
    4439                $this->javascript_config['seiten_id'] = $this->conf['seiten_id'];
     40               
     41                // get all kind of relations
     42                $this->page_relations = $this->menu->get_page_relations();
     43                $this->interlang_links = $this->menu->get_interlanguage_link();
    4544        }
    4645       
     
    7978
    8079        function print_header() {
    81                 $_ = $this->msg->get_shorthand_printer();
     80                $p = $this->msg->get_shorthand_printer();
     81                $_ = $this->msg->get_shorthand_returner();
    8282?>
    8383<!doctype html>
     
    8585<head>
    8686  <meta charset="utf-8">
    87   <title><?php isset($this->conf['titel']) ? $this->conf['titel'].' - ' : ''; $_('html-title'); ?></title>
     87  <title><?php isset($this->conf['titel']) ? $this->conf['titel'].' - ' : ''; $p('html-title'); ?></title>
    8888  <meta name="description" content="Produziert am 08.01.2012">
    8989  <meta name="author" content="Sven">
    9090  <meta name="generator" content="t29v6 $Id$">
    9191  <meta name="t29.cachedate" content="<?php print date('r'); ?>">
     92 
     93  <?php
     94        foreach(array_merge(array("first" => t29Menu::dom_new_link($this->conf['lang_path'], $_('head-rel-first'))),
     95          $this->page_relations) as $rel => $a) {
     96                if($rel == 'start') continue; // not in standard
     97                printf("\n  <link rel='%s' href='%s' title='%s' />",
     98                        $rel, $a['href'], sprintf($_('head-rel-'.$rel), $a)
     99                );
     100        }
     101  ?>
     102 
     103  <link rel="copyright" href="<?php echo $this->conf['legal_pagename']; ?>" title="<?php $p('footer-legal-link'); ?>">
     104  <?php
     105        // print interlanguage links for all languages except the active one
     106        foreach($this->interlang_links as $lang => $a) {
     107                if($lang != $this->conf['lang']) {
     108                        printf('<link rel="alternate" href="%s" hreflang="%s" title="%s">',
     109                                $a['href'], $lang, sprintf($_('head-rel-interlang'), $a)
     110                        );
     111                }
     112        }
     113  ?>
     114 
    92115  <meta name="viewport" content="width=device-width,initial-scale=1">
    93116  <link rel="stylesheet" href="/shared/css-v6/boiler.css">
     
    101124<div id="footer-background-container"><!-- helper -->
    102125  <div id="container">
    103         <h1 role="banner"><a href="/" title="<?php $_('head-h1-title'); ?>"><?php $_('head-h1'); ?></a></h1>
     126        <h1 role="banner"><a href="/" title="<?php $p('head-h1-title'); ?>"><?php $p('head-h1'); ?></a></h1>
    104127        <div id="background-color-container"><!-- helper -->
    105128        <section class="main content" role="main" id="content">
     
    112135} // function print_header().
    113136
    114 function print_footer() {
    115         $p = $this->msg->get_shorthand_printer();
    116         $_ = $this->msg->get_shorthand_returner();
    117 ?>
     137        function print_footer() {
     138                $p = $this->msg->get_shorthand_printer();
     139                $_ = $this->msg->get_shorthand_returner();
     140        ?>
    118141        <!-- end content -->
    119142        </section>
     
    137160                        <ul>
    138161                                <?php
    139                                         foreach($this->conf['languages'] as $l => $sets) {
     162                                        foreach($this->interlang_links as $lang => $a) {
    140163                                                printf("\t\t\t\t<li%s><a href='%s' title='%s'>%s</a></li>\n",
    141                                                         ($l == $this->conf['lang'] ? ' class="active"' : ''),
    142                                                         $sets[1].'#',
    143                                                         "View in $sets[0]",
    144                                                         $sets[0]
     164                                                        ($lang == $this->conf['lang'] ? ' class="active"' : ''),
     165                                                        $a['href'], sprintf($_('topnav-interlang-title'), $a),
     166                                                        $this->conf['languages'][$lang][0] // verbose language name
    145167                                                );
    146                                        
    147168                                        }
    148169                                ?>
     
    152173                                <input type="text" value="" data-defaultvalue="%s" name="q" class="text">
    153174                                <input type="submit" value="%s" class="button">
    154                                 ', $_('sidebar-search-label'), $_('sidebar-search-label'), $_('sidebar-search-label')); ?>
     175                                ', $_('topnav-search-label'), $_('topnav-search-label'), $_('topnav-search-label')); ?>
    155176                        </form>
    156177                </nav>
     
    163184                <nav class="rel clearfix">
    164185                <ul>
    165                         <?php $this->menu->print_relations(); ?>
    166                         <!--
    167                         <li class="prev"><a href="#">vorherige Seite <strong>Univac 9200</strong></a>
    168                         <li class="next"><a href="#">nächste Seite <strong>Analog und Hybridrechner</strong></a>
    169                         -->
     186                        <?php
     187                                foreach($this->page_relations as $rel => $a) {
     188                                        printf("\t<li class='%s'><a href='%s' title='%s'>%s <strong>%s</strong></a>\n",
     189                                                $rel, $a['href'], 'TITLE', $_('nav-rel-'.$rel), $a
     190                                        );
     191                                }
     192                        ?>
    170193                </ul>
    171194                </nav>
Note: See TracChangeset for help on using the changeset viewer.
© 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