<commit_before><?php

namespace atomar\core;

use atomar\Atomar;
use atomar\hook\Menu;
use atomar\hook\Page;
use atomar\hook\Twig;

/**
 * Class View handles all of the view management
 * @package atomar
 */
class Templator {
    /**
     * An array of scripts that will be included on each page.
     * This allows for dynamic use of scripts so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $js = array();

    /**
     * An array of scripts that will be executed after the page is ready.
     * This allows for dynamic use of scripts so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $js_onload = array();

    /**
     * An array of css files to be inserted in the head of each page.
     * This allows for dynamic use of css so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $css = array();

    /**
     * An array of css properties to be inserted in the head of each page.
     * This allows for dynamic use of css so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $css_inline = array();

    /**
     * Puts jQuery into no conflict mode so it can work with prototype.
     * http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/
     * @var boolean
     */
    public static $jquery_no_conflict = false;

    /**
     * An array of variables stored as key=>values to be include in the template.
     * This allows variables to be dynamically added to a template from anywhere within the system.
     * @var array
     */
    public static $global_template_vars = array();

    /**
     * Indicates that the Templator has been initialized by init()
     * @var bool
     */
    private static $is_initialized = false;

    /**
     * Initializes the view manager
     */
    public static function init() {
        // set up scripts. Order does matter.
        self::$js = array(
            '/atomar/assets/js/jquery.min.js',
            '/atomar/assets/js/bootstrap.min.js',
            '/atomar/assets/js/bootstrap.file-input.js',
            '/atomar/assets/js/bootstrap-datetimepicker.js',
            '/atomar/assets/js/chosen.jquery.min.js',
            '/atomar/assets/js/Validate.js',
            '/atomar/assets/js/Process.js',
            '/atomar/assets/js/Confirmation.js',
            '/atomar/assets/js/InlineEdit.js',
            '/atomar/assets/js/Lightbox.js',
            '/atomar/assets/js/sonic.js',
            '/atomar/assets/js/js_loader_animation.js',
            '/atomar/assets/js/functions.js',
            '/atomar/assets/js/main.js',
        );

        // set up css. Order does matter
        self::$css = array(
            '/atomar/assets/css/bootstrap.min.css',
            '/atomar/assets/css/chosen.min.css',
            '/atomar/assets/css/chosen-bootstrap.css',
            '/atomar/assets/css/bootstrap-datetimepicker.min.css',
            '/atomar/assets/css/main.css',
        );

        self::$is_initialized = true;
    }

    /**
     * Renders an error page
     * @param string $title the title of the error page
     * @param string $message the message to display on the error page
     * @return string
     */
    public static function render_error($title, $message) {
        $loader = new \Twig_Loader_Filesystem(Atomar::atomar_dir() . '/views');
        $twig = new AtomarTwigEnvironment($loader, array(
            'debug' => true
        ));
        $twig->addExtension(new \Twig_Extension_Debug());
        $atomar['css'][] = '/atomar/assets/css/bootstrap.min.css';
        $atomar['css'][] = '/atomar/assets/css/main.css';
        $atomar['version'] = Atomar::version();

        $atomar['css_inline'] = <<<CSS
body {
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #eee;
}
.error-page {
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.error-page .error-page-heading, .error-page .checkbox {
  margin-bottom: 10px;
}
.error-page .form-control {
  position: relative;
  font-size: 16px;
  height: auto;
  padding: 10px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.error-page #field-username {
  margin-bottom: -1px;
  border-radius: 0;
}
.error-page #field-email {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  margin-bottom: -1px;
}
.error-page #field-password {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}
.error-page .checkbox {
  font-weight: normal;
}
CSS;
        return $twig->render('error.html', array(
            'body_class' => 'install',
            'title' => $title,
            'message' => $message,
            'atomar' => $atomar,
            'sys' => $atomar // TRICKY: backwards compatibility
        ));
    }

    /**
     * Formats a relative path to an extension asset into an absolute path
     * @param $asset
     * @return string
     */
    public static function resolve_ext_asset($asset) {
        return '/' . ltrim(rtrim(Atomar::extension_dir(), '\\/'), '\\/') . '/' . $asset;
    }

    /**
     * Formats a relative path to an application asset into an absolute path
     * @param $asset
     * @return string
     */
    public static function resolve_app_asset($asset) {
        return '/' . ltrim(rtrim(Atomar::application_dir(), '\\/'), '\\/') . '/' . $asset;
    }

    /**
     * Utility to render the view
     *
     * @param string $template the path to the template that will be rendered
     * @param array $args the arguments that will be passed to the template
     * @param array $options optional rules regarding how the template is rendered.
     * @throws \Exception
     * @return string
     */
    public static function render_view($template, $args = array(), $options = array()) {
        $template = ltrim($template, '/');

        // TRICKY: app templates are prefixed with the app namespace, but templates are loaded relative to
        // the app dir. We trim them here so they match.
        if(substr($template, 0, strlen(Atomar::application_namespace())) == Atomar::application_namespace()) {
            $template = substr($template, strlen(Atomar::application_namespace()));
        }

        $default_options = array(
            'render_messages' => true,
            'render_menus' => true,
            'trigger_preprocess_page' => true,
            'trigger_twig_function' => true,
            'trigger_menu' => true
        );
        $options = array_merge($default_options, $options);
        if ($options['trigger_preprocess_page']) Atomar::hook(new Page());
        try {
            // initialize twig template engine
            $loader = new \Twig_Loader_Filesystem(array(
                Atomar::application_dir(),
                Atomar::extension_dir(),
                Atomar::atomar_dir() . DIRECTORY_SEPARATOR . 'views'
            ));
            if (Atomar::$config['debug']) {
                $twig = new AtomarTwigEnvironment($loader, array(
                    'debug' => Atomar::$config['debug'],
                ));
//                require_once(Atomar::atomar_dir() . '/vendor/Twig/Extension/Debug.php');
                $twig->addExtension(new \Twig_Extension_Debug());
                // delete the cache if it exists
                if (is_dir(Atomar::$config['cache'] . 'twig')) {
                    deleteDir(Atomar::$config['cache'] . 'twig');
                }
            } else {
                if (!is_dir(Atomar::$config['cache'] . 'twig')) {
                    $old = umask(0002);
                    mkdir(Atomar::$config['cache'] . 'twig', 0777, true);
                    umask($old);
                }
                $twig = new AtomarTwigEnvironment($loader, array(
                    'cache' => Atomar::$config['cache'] . 'twig'
                ));
            }

            if ($options['trigger_twig_function']) {
                Atomar::hook(new Twig($twig));
            }

            // prepare user
            if (Auth::$user) {
                $user = Auth::$user->export();
                $user['authenticated'] = 1;
            } else {
                $user = array();
                $user['authenticated'] = 0;
            }
            if (isset($user['last_login'])) {
                $user['last_login'] = fancy_date(strtotime($user['last_login']));
            } else {
                $user['last_login'] = '';
            }
            $user['is_admin'] = Auth::has_authentication('administer_site');
            $user['is_super'] = Auth::is_super();
            $args['atomar']['user'] = $user;
            unset($user);

            // prepare return
            if (isset($_SESSION['return']) && !isset($args['return'])) {
                $args['return'] = $_SESSION['return'];
            }

            // prepare site info
            $args['atomar']['favicon'] = Atomar::$config['favicon'];
            $args['atomar']['site_name'] = Atomar::$config['site_name'];
            $args['atomar']['site_url'] = Atomar::$config['site_url'];
            $args['atomar']['email']['contact_email'] = Atomar::$config['email']['contact_email'];
            $args['atomar']['cron_token'] = Atomar::$config['cron_token'];
            $args['atomar']['maintenance'] = Atomar::get_system('maintenance_mode', '0');
            $args['atomar']['version'] = Atomar::version();
            $args['atomar']['year'] = date('Y');

            if ($options['render_menus']) {
                // admin users
                if (Auth::has_authentication('administer_site')) {
                    Atomar::$menu['primary_menu']['/atomar'] = array(
                        'link' => l('administer', '/atomar'),
                        'class' => array(),
                        'weight' => 9999,
                        'access' => 'administer_site',
                        'menu' => array()
                    );
                }
                if (Auth::$user) {
                    Atomar::$menu['primary_menu']['/atomar/logout'] = array(
                        'link' => l('logout', '/atomar/logout'),
                        'class' => array(),
                        'weight' => 0,<commit_msg>added page url to templates<commit_after><?php

namespace atomar\core;

use atomar\Atomar;
use atomar\hook\Menu;
use atomar\hook\Page;
use atomar\hook\Twig;

/**
 * Class View handles all of the view management
 * @package atomar
 */
class Templator {
    /**
     * An array of scripts that will be included on each page.
     * This allows for dynamic use of scripts so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $js = array();

    /**
     * An array of scripts that will be executed after the page is ready.
     * This allows for dynamic use of scripts so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $js_onload = array();

    /**
     * An array of css files to be inserted in the head of each page.
     * This allows for dynamic use of css so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $css = array();

    /**
     * An array of css properties to be inserted in the head of each page.
     * This allows for dynamic use of css so we don't have to load
     * everything on each page.
     * @var array
     */
    public static $css_inline = array();

    /**
     * Puts jQuery into no conflict mode so it can work with prototype.
     * http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/
     * @var boolean
     */
    public static $jquery_no_conflict = false;

    /**
     * An array of variables stored as key=>values to be include in the template.
     * This allows variables to be dynamically added to a template from anywhere within the system.
     * @var array
     */
    public static $global_template_vars = array();

    /**
     * Indicates that the Templator has been initialized by init()
     * @var bool
     */
    private static $is_initialized = false;

    /**
     * Initializes the view manager
     */
    public static function init() {
        // set up scripts. Order does matter.
        self::$js = array(
            '/atomar/assets/js/jquery.min.js',
            '/atomar/assets/js/bootstrap.min.js',
            '/atomar/assets/js/bootstrap.file-input.js',
            '/atomar/assets/js/bootstrap-datetimepicker.js',
            '/atomar/assets/js/chosen.jquery.min.js',
            '/atomar/assets/js/Validate.js',
            '/atomar/assets/js/Process.js',
            '/atomar/assets/js/Confirmation.js',
            '/atomar/assets/js/InlineEdit.js',
            '/atomar/assets/js/Lightbox.js',
            '/atomar/assets/js/sonic.js',
            '/atomar/assets/js/js_loader_animation.js',
            '/atomar/assets/js/functions.js',
            '/atomar/assets/js/main.js',
        );

        // set up css. Order does matter
        self::$css = array(
            '/atomar/assets/css/bootstrap.min.css',
            '/atomar/assets/css/chosen.min.css',
            '/atomar/assets/css/chosen-bootstrap.css',
            '/atomar/assets/css/bootstrap-datetimepicker.min.css',
            '/atomar/assets/css/main.css',
        );

        self::$is_initialized = true;
    }

    /**
     * Renders an error page
     * @param string $title the title of the error page
     * @param string $message the message to display on the error page
     * @return string
     */
    public static function render_error($title, $message) {
        $loader = new \Twig_Loader_Filesystem(Atomar::atomar_dir() . '/views');
        $twig = new AtomarTwigEnvironment($loader, array(
            'debug' => true
        ));
        $twig->addExtension(new \Twig_Extension_Debug());
        $atomar['css'][] = '/atomar/assets/css/bootstrap.min.css';
        $atomar['css'][] = '/atomar/assets/css/main.css';
        $atomar['version'] = Atomar::version();

        $atomar['css_inline'] = <<<CSS
body {
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #eee;
}
.error-page {
  max-width: 330px;
  padding: 15px;
  margin: 0 auto;
}
.error-page .error-page-heading, .error-page .checkbox {
  margin-bottom: 10px;
}
.error-page .form-control {
  position: relative;
  font-size: 16px;
  height: auto;
  padding: 10px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.error-page #field-username {
  margin-bottom: -1px;
  border-radius: 0;
}
.error-page #field-email {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  margin-bottom: -1px;
}
.error-page #field-password {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}
.error-page .checkbox {
  font-weight: normal;
}
CSS;
        return $twig->render('error.html', array(
            'body_class' => 'install',
            'title' => $title,
            'message' => $message,
            'atomar' => $atomar,
            'sys' => $atomar // TRICKY: backwards compatibility
        ));
    }

    /**
     * Formats a relative path to an extension asset into an absolute path
     * @param $asset
     * @return string
     */
    public static function resolve_ext_asset($asset) {
        return '/' . ltrim(rtrim(Atomar::extension_dir(), '\\/'), '\\/') . '/' . $asset;
    }

    /**
     * Formats a relative path to an application asset into an absolute path
     * @param $asset
     * @return string
     */
    public static function resolve_app_asset($asset) {
        return '/' . ltrim(rtrim(Atomar::application_dir(), '\\/'), '\\/') . '/' . $asset;
    }

    /**
     * Utility to render the view
     *
     * @param string $template the path to the template that will be rendered
     * @param array $args the arguments that will be passed to the template
     * @param array $options optional rules regarding how the template is rendered.
     * @throws \Exception
     * @return string
     */
    public static function render_view($template, $args = array(), $options = array()) {
        $template = ltrim($template, '/');

        // TRICKY: app templates are prefixed with the app namespace, but templates are loaded relative to
        // the app dir. We trim them here so they match.
        if(substr($template, 0, strlen(Atomar::application_namespace())) == Atomar::application_namespace()) {
            $template = substr($template, strlen(Atomar::application_namespace()));
        }

        $default_options = array(
            'render_messages' => true,
            'render_menus' => true,
            'trigger_preprocess_page' => true,
            'trigger_twig_function' => true,
            'trigger_menu' => true
        );
        $options = array_merge($default_options, $options);
        if ($options['trigger_preprocess_page']) Atomar::hook(new Page());
        try {
            // initialize twig template engine
            $loader = new \Twig_Loader_Filesystem(array(
                Atomar::application_dir(),
                Atomar::extension_dir(),
                Atomar::atomar_dir() . DIRECTORY_SEPARATOR . 'views'
            ));
            if (Atomar::$config['debug']) {
                $twig = new AtomarTwigEnvironment($loader, array(
                    'debug' => Atomar::$config['debug'],
                ));
//                require_once(Atomar::atomar_dir() . '/vendor/Twig/Extension/Debug.php');
                $twig->addExtension(new \Twig_Extension_Debug());
                // delete the cache if it exists
                if (is_dir(Atomar::$config['cache'] . 'twig')) {
                    deleteDir(Atomar::$config['cache'] . 'twig');
                }
            } else {
                if (!is_dir(Atomar::$config['cache'] . 'twig')) {
                    $old = umask(0002);
                    mkdir(Atomar::$config['cache'] . 'twig', 0777, true);
                    umask($old);
                }
                $twig = new AtomarTwigEnvironment($loader, array(
                    'cache' => Atomar::$config['cache'] . 'twig'
                ));
            }

            if ($options['trigger_twig_function']) {
                Atomar::hook(new Twig($twig));
            }

            // prepare user
            if (Auth::$user) {
                $user = Auth::$user->export();
                $user['authenticated'] = 1;
            } else {
                $user = array();
                $user['authenticated'] = 0;
            }
            if (isset($user['last_login'])) {
                $user['last_login'] = fancy_date(strtotime($user['last_login']));
            } else {
                $user['last_login'] = '';
            }
            $user['is_admin'] = Auth::has_authentication('administer_site');
            $user['is_super'] = Auth::is_super();
            $args['atomar']['user'] = $user;
            unset($user);

            // prepare return
            if (isset($_SESSION['return']) && !isset($args['return'])) {
                $args['return'] = $_SESSION['return'];
            }

            // prepare site info
            $args['atomar']['favicon'] = Atomar::$config['favicon'];
            $args['atomar']['site_name'] = Atomar::$config['site_name'];
            $args['atomar']['site_url'] = Atomar::$config['site_url'];
            $args['atomar']['page_url'] = rtrim(Atomar::$config['site_url'], '/') . '/' . ltrim(Router::request_path(), '/') . Router::request_query();
            $args['atomar']['email']['contact_email'] = Atomar::$config['email']['contact_email'];
            $args['atomar']['cron_token'] = Atomar::$config['cron_token'];
            $args['atomar']['maintenance'] = Atomar::get_system('maintenance_mode', '0');
            $args['atomar']['version'] = Atomar::version();
            $args['atomar']['year'] = date('Y');

            if ($options['render_menus']) {
                // admin users
                if (Auth::has_authentication('administer_site')) {
                    Atomar::$menu['primary_menu']['/atomar'] = array(
                        'link' => l('administer', '/atomar'),
                        'class' => array(),
                        'weight' => 9999,
                        'access' => 'administer_site',
                        'menu' => array()
                    );
                }
                if (Auth::$user) {
                    Atomar::$menu['primary_menu']['/atomar/logout'] = array(
                        'link' => l('logout', '/atomar/logout'),
                        'class' => array(),
                        'weight' => 0,
{'fraction_non_alphanumeric': 0.14704459707845602, 'fraction_numerical': 0.0040630743929573375, 'mean_word_length': 1.9476760764185914, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4395412', 'n_tokens_mistral': 6134, 'n_tokens_neox': 5974, 'n_words': 1413}