<commit_before><?php
/**
 * Created by PhpStorm.
 * User: ogiba
 * Date: 23.09.2017
 * Time: 00:12
 */

namespace Controller;

use Exception;
use Twig_Environment;
use Twig_Loader_Filesystem;
use Twig_SimpleFunction;

abstract class BaseController {
    /**
     * @param string|null $name
     * @param string|null $action
     * @param array $params
     */
    public abstract function action($name, $action, $params);

    /**
     * @param string $viewName
     * @param array $properties
     * @return string
     */
    protected function render($viewName, $properties = [])
    {
        $loader = new Twig_Loader_Filesystem('views');
        $twig = new Twig_Environment($loader, array());
        $twig->addFunction(new Twig_SimpleFunction("asset", function ($path) {
            return "/web/assets/" . $path;
        }));
        $twig->addFunction(new Twig_SimpleFunction("trans", function ($key) {
            $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
            $translatedString = $key;
            switch ($lang) {
                case "pl":
                    //echo "PAGE EN";
                    $jsonString = file_get_contents("Translations/translation-pl.json", true);
                    if ($jsonString) {
                        try {
                            $translatedString = json_decode($jsonString, true)[$key];
                        } catch (Exception $ex) {

                        }
                    }
                    break;
                default:
                    //echo "PAGE EN - Setting Default";
                    $jsonString = file_get_contents("/src/Translations/translation-" . $lang . ".json", true);
                    if ($jsonString) {
                        try {
                            $translatedString = json_decode($jsonString, true)[$key];
                        } catch (Exception $ex) {

                        }
                    }
                    break;
            }
            return $translatedString;
        }));
        return $twig->render($viewName, $properties);
    }

    protected function checkIfUserLogged()
    {
        $userLogged = false;
        if (isset($_SESSION["token"])) {<commit_msg>[Base] Extract code form twig extension trans to new method translate in BaseController class<commit_after><?php
/**
 * Created by PhpStorm.
 * User: ogiba
 * Date: 23.09.2017
 * Time: 00:12
 */

namespace Controller;

use Exception;
use Twig_Environment;
use Twig_Loader_Filesystem;
use Twig_SimpleFunction;

abstract class BaseController {
    /**
     * @param string|null $name
     * @param string|null $action
     * @param array $params
     */
    public abstract function action($name, $action, $params);

    /**
     * @param string $viewName
     * @param array $properties
     * @return string
     */
    protected function render($viewName, $properties = [])
    {
        $loader = new Twig_Loader_Filesystem('views');
        $twig = new Twig_Environment($loader, array());
        $twig->addFunction(new Twig_SimpleFunction("asset", function ($path) {
            return "/web/assets/" . $path;
        }));
        $twig->addFunction(new Twig_SimpleFunction("trans", function ($key) {
            return $this->translate($key);
        }));
        return $twig->render($viewName, $properties);
    }

    protected function translate($key)
    {
        $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
        $translatedString = $key;
        switch ($lang) {
            case "pl":
                //echo "PAGE EN";
                $jsonString = file_get_contents("Translations/translation-pl.json", true);
                if ($jsonString) {
                    try {
                        $translatedString = json_decode($jsonString, true)[$key];
                    } catch (Exception $ex) {

                    }
                }
                break;
            default:
                //echo "PAGE EN - Setting Default";
                $jsonString = file_get_contents("/src/Translations/translation-" . $lang . ".json", true);
                if ($jsonString) {
                    try {
                        $translatedString = json_decode($jsonString, true)[$key];
                    } catch (Exception $ex) {

                    }
                }
                break;
        }
        return $translatedString;
    }

    protected function checkIfUserLogged()
    {
        $userLogged = false;
        if (isset($_SESSION["token"])) {
{'fraction_non_alphanumeric': 0.1219241853247617, 'fraction_numerical': 0.006207049434715141, 'mean_word_length': 1.6714031971580816, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 10, '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': '2994494', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1162, 'n_words': 272}