Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a HTML-Sitemap with Bootstrap 3 Markup #6

Open
Gummibeer opened this issue Oct 7, 2014 · 1 comment
Open

add a HTML-Sitemap with Bootstrap 3 Markup #6

Gummibeer opened this issue Oct 7, 2014 · 1 comment

Comments

@Gummibeer
Copy link

I added a $twig_var for a HTML-Sitemap with Bootstrap3 Markup und combined the other things from my last Issue #5

<?php

/**
 * Generate an xml sitemap for Pico
 *
 * @author Dave Kinsella
 * @link https://github.com/Techn0tic/Pico_Sitemap
 * @license http://opensource.org/licenses/MIT
 */
class Pico_Sitemap {
    private $is_sitemap;
    private $is_html_sitemap;
    private $exclude;
    private $pages;

    public function __construct() {
        $this->is_sitemap = false;
        $this->is_html_sitemap = false;
        $this->exclude = array('home', 'slider', 'suche', 'sitemap');
        $this->pages = array();
    }

    public function request_url(&$url) {
        if($url == 'sitemap') $this->is_html_sitemap = true;
        if($url == 'sitemap.xml') $this->is_sitemap = true;
    }

    public function get_pages($pages) {
        if($this->is_sitemap || $this->is_html_sitemap) {
            global $config;
            foreach($pages as $page) {
                preg_match('/\/('.implode('|', $this->exclude).')\//', $page['url'].'/', $matches);
                if(empty($matches)) {
                    $index = '';
                    $date = false;
                    $path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'content'.DIRECTORY_SEPARATOR.str_ireplace($config['base_url'], '', $page['url']);
                    if(is_dir($path)) { $index = 'index'; }
                    $file = realpath($path.$index.'.md');
                    if(file_exists($file)) { $date = date ("Y-m-d", filemtime($file)); }
                    array_push($this->pages, array( 'url' => $page['url'], 'title' => $page['title'], 'lastmod' => $date, 'index' => $index == '' ? false : true ));
                }
            }
            sort($this->pages);
        }
    }

    public function before_render(&$twig_vars, &$twig, &$template) {
        if($this->is_sitemap) {
            header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
            header('Content-Type: application/xml; charset=UTF-8');
            $xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
            foreach($this->pages as $page) {
                $xml .= '<url><loc>'.$page['url'].'</loc><lastmod>'.$page['lastmod'].'</lastmod></url>';
            }
            $xml .= '</urlset>';
            header('Content-Type: text/xml');
            die($xml);
        } elseif($this->is_html_sitemap) {
            $panel_raw = '<div class="col-md-4"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title"><a href="%url%">%title%</a></h3></div><div class="list-group">%childs%</div></div></div>';
            $child_raw = '<a href="%url%" class="list-group-item"><i class="icon-link"></i> %title%</a>';

            $panel = '';
            $childs = array();
            $out = '';
            $out .= '<div class="row">';
            foreach($this->pages as $page) {
                if($page['index']) {
                    $panel = str_replace('%childs%', implode('', $childs), $panel);
                    $out .= $panel;
                    $childs = array();
                    $panel = $panel_raw;
                    $panel = str_replace('%url%', $page['url'], $panel);
                    $panel = str_replace('%title%', $page['title'], $panel);
                } else {
                    $child = $child_raw;
                    $child = str_replace('%url%', $page['url'], $child);
                    $child = str_replace('%title%', $page['title'], $child);
                    array_push($childs, $child);
                }
            }
            $panel = str_replace('%childs%', implode('', $childs), $panel);
            $out .= $panel;
            $out .= '</div>';
            $twig_vars['sitemap'] = $out;
        }
    }
}
@Gummibeer
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant