<?php

/**
 * @@file
 * Functions to support theming in the Bartik theme.
 */

use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\Component\Utility\SafeMarkup;
use Symfony\Component\HttpFoundation\RedirectResponse;

if ({theme_name}_get_value('theme', false) ) {
    register_shutdown_function(function () {
        if ($e = error_get_last()) {
            switch ($e['type']) {
                case E_ERROR:
                case E_CORE_ERROR:
                case E_COMPILE_ERROR:
                case E_PARSE:
                    printf('[PHP_ERROR]%s[PHP_ERROR]', json_encode($e));
            }
        }
    });
}

{theme_name}_include_theme_files();

function {theme_name}_include_theme_files() {
    $include_files = array(/*'/menu/menu.php', '/menu/vmenu.php', '/search/block_form.php', '/search/block_button.php', '/search/block_input.php', '/search/theme_form.php', '/search/theme_button.php', '/search/theme_input.php', '/messages/error.php', '/messages/warning.php', '/messages/status.php',*/ '/templates/navigation/navigation-methods.php', '/theme-methods.php', '/template.php');
    $dirname = dirname(__FILE__);
    foreach ($include_files as $file) {
        $path = $dirname . $file;
        if (file_exists($path)) {
            require_once($path);
        }
    }
}

function {theme_name}_get_major_version() {
    $tok = strtok(\Drupal::VERSION, '.');
    return (int)$tok[0];
}

function {theme_name}_get_theme_url() {
    return {theme_name}_get_base_url_with_slash() . drupal_get_path('theme', '{theme_name}');
}

function {theme_name}_get_base_url_with_slash() {
    $base_url = $GLOBALS["base_url"];
    return $base_url . (substr($base_url, -1) !== '/' ? '/' : '');
}

function {theme_name}_get_style_url() {
    $version = {theme_name}_build_version_parameter();
    $path = {theme_name}_get_theme_url() . '/css/';
    $is_preview = {theme_name}_get_value('theme', false);
    if ($is_preview && file_exists(dirname(__FILE__) . '/css/style.preview.php'))
        return $path . 'style.preview.php?theme={theme_name}';
    if (!$is_preview && file_exists(dirname(__FILE__) . '/css/style.min.css'))
        return $path . 'style.min.css?' . $version;
    return $path . 'style.css?' . $version;
}

function {theme_name}_get_value($key, $default_value = false) {
    if (!isset($key) || empty($key) || !is_string($key))
        return false;
    $ret = (isset($_POST[$key]) ? $_POST[$key] : (isset($_GET[$key]) ? $_GET[$key] : $default_value));

    if (is_string($ret) === true)
        $ret = urldecode(preg_replace('/((\%5C0+)|(\%00+))/i', '', urlencode($ret)));
    return !is_string($ret)? $ret : stripslashes($ret);
}

function {theme_name}_get_preview_js_url() {
    return {theme_name}_get_value('theme', false) ? {theme_name}_get_theme_url() . '/js/preview.js' : '';
}

function {theme_name}_normalize_path($path) {
    $root = ($path[0] === '/') ? '/' : '';
    $segments = preg_split('/[\\/\\\\]/', trim($path, '/'));
    $ret = array();
    foreach ($segments as $segment) {
        if (($segment == '.') || empty($segment)) {
            continue;
        }
        if ($segment == '..') {
            array_pop($ret);
        } else {
            array_push($ret, $segment);
        }
    }
    return $root . implode('/', $ret);
}

function {theme_name}_generate_images_fso($theme_url, $path, $recursive = true) {
    if (is_file($path)) {
        return $theme_url . '/images/' . pathinfo($path, PATHINFO_BASENAME);
    }

    if (!is_dir($path))
        return array();

    $result = array();

    if ($handle = opendir($path)) {
        while (($name = readdir($handle)) !== false) {
            if (preg_match('#^\.#', $name))
                continue;
            $result[$name] = {theme_name}_generate_images_fso($theme_url, $path . "/" . $name, $recursive);
        }
        closedir($handle);
    }
    return $result;
}

function {theme_name}_build_version_parameter() {
    return 'version=@version';
}