<commit_before><?php

namespace Backend\Modules\Locale\Engine;

/*
 * This file is part of Fork CMS.
 *
 * For the full copyright and license information, please view the license
 * file that was distributed with this source code.
 */

use Common\Uri as CommonUri;
use Backend\Core\Engine\Authentication as BackendAuthentication;
use Backend\Core\Language\Language as BL;
use Backend\Core\Engine\Model as BackendModel;

/**
 * In this file we store all generic functions that we will be using in the locale module
 */
class Model
{
    /**
     * Build the language files
     *
     * @param string $language    The language to build the locale-file for.
     * @param string $application The application to build the locale-file for.
     */
    public static function buildCache($language, $application)
    {
        $cacheBuilder = new CacheBuilder(BackendModel::get('database'));
        $cacheBuilder->buildCache($language, $application);
    }

    /**
     * Build a query for the URL based on the filter
     *
     * @param array $filter The filter.
     *
     * @return string
     */
    public static function buildURLQueryByFilter($filter)
    {
        $query = http_build_query($filter, null, '&', PHP_QUERY_RFC3986);
        if ($query != '') {
            $query = '&' . $query;
        }

        return $query;
    }

    /**
     * Create an XML-string that can be used for export.
     *
     * @param array $items The items.
     *
     * @return string
     */
    public static function createXMLForExport(array $items)
    {
        $charset = BackendModel::getContainer()->getParameter('kernel.charset');
        $xml = new \DOMDocument('1.0', $charset);

        // set some properties
        $xml->preserveWhiteSpace = false;
        $xml->formatOutput = true;

        // locale root element
        $root = $xml->createElement('locale');
        $xml->appendChild($root);

        // loop applications
        foreach ($items as $application => $modules) {
            // create application element
            $applicationElement = $xml->createElement($application);
            $root->appendChild($applicationElement);

            // loop modules
            foreach ($modules as $module => $types) {
                // create application element
                $moduleElement = $xml->createElement($module);
                $applicationElement->appendChild($moduleElement);

                // loop types
                foreach ($types as $type => $items) {
                    // loop items
                    foreach ($items as $name => $translations) {
                        // create application element
                        $itemElement = $xml->createElement('item');
                        $moduleElement->appendChild($itemElement);

                        // attributes
                        $itemElement->setAttribute('type', self::getTypeName($type));
                        $itemElement->setAttribute('name', $name);

                        // loop translations
                        foreach ($translations as $translation) {
                            // create translation
                            $translationElement = $xml->createElement('translation');
                            $itemElement->appendChild($translationElement);

                            // attributes
                            $translationElement->setAttribute('language', $translation['language']);

                            // set content
                            $translationElement->appendChild(new \DOMCdataSection($translation['value']));
                        }
                    }
                }
            }
        }

        return $xml->saveXML();
    }

    /**
     * Delete (multiple) items from locale
     *
     * @param array $ids The id(s) to delete.
     */
    public static function delete(array $ids)
    {
        // loop and cast to integers
        foreach ($ids as &$id) {
            $id = (int) $id;
        }

        // create an array with an equal amount of questionmarks as ids provided
        $idPlaceHolders = array_fill(0, count($ids), '?');

        // delete records
        BackendModel::getContainer()->get('database')->delete(
            'locale',
            'id IN (' . implode(', ', $idPlaceHolders) . ')',
            $ids
        );

        // rebuild cache
        self::buildCache(BL::getWorkingLanguage(), 'Backend');
        self::buildCache(BL::getWorkingLanguage(), 'Frontend');
    }

    /**
     * Does an id exist.
     *
     * @param int $id The id to check for existence.
     *
     * @return bool
     */
    public static function exists($id)
    {
        return (bool) BackendModel::getContainer()->get('database')->getVar(
            'SELECT 1
             FROM locale
             WHERE id = ?
             LIMIT 1',
            array((int) $id)
        );
    }

    /**
     * Does a locale exists by its name.
     *
     * @param string $name        The name of the locale.
     * @param string $type        The type of the locale.
     * @param string $module      The module wherein will be searched.
     * @param string $language    The language to use.
     * @param string $application The application wherein will be searched.
     * @param int    $id          The id to exclude in the check.
     *
     * @return bool
     */
    public static function existsByName($name, $type, $module, $language, $application, $id = null)
    {
        $name = (string) $name;
        $type = (string) $type;
        $module = (string) $module;
        $language = (string) $language;
        $application = (string) $application;
        $id = ($id !== null) ? (int) $id : null;

        // get db
        $db = BackendModel::getContainer()->get('database');

        // return
        if ($id !== null) {
            return (bool) $db->getVar(
                'SELECT 1
                 FROM locale
                 WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ? AND id != ?
                 LIMIT 1',
                array($name, $type, $module, $language, $application, $id)
            );
        }

        return (bool) BackendModel::getContainer()->get('database')->getVar(
            'SELECT 1
             FROM locale
             WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ?
             LIMIT 1',
            array($name, $type, $module, $language, $application)
        );
    }

    /**
     * Get a single item from locale.
     *
     * @param int $id The id of the item to get.
     *
     * @return array
     */
    public static function get($id)
    {
        // fetch record from db
        $record = (array) BackendModel::getContainer()->get('database')->getRecord(
            'SELECT * FROM locale WHERE id = ?',
            array((int) $id)
        );

        // actions are urlencoded
        if ($record['type'] == 'act') {
            $record['value'] = urldecode($record['value']);
        }

        return $record;
    }

    /**
     * Get a locale by its name
     *
     * @param string $name        The name of the locale.
     * @param string $type        The type of the locale.
     * @param string $module      The module wherein will be searched.
     * @param string $language    The language to use.
     * @param string $application The application wherein will be searched.
     *
     * @return bool
     */
    public static function getByName($name, $type, $module, $language, $application)
    {
        $name = (string) $name;
        $type = (string) $type;
        $module = (string) $module;
        $language = (string) $language;
        $application = (string) $application;

        return BackendModel::getContainer()->get('database')->getVar(
            'SELECT l.id
             FROM locale AS l
             WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ?',
            array($name, $type, $module, $language, $application)
        );
    }

    /**
     * Grab labels found in the backend navigation.
     *
     * @return array
     */
    private static function getLabelsFromBackendNavigation()
    {
        return (array) BackendModel::getContainer()->get('database')->getColumn('SELECT label FROM backend_navigation');
    }

    /**
     * Get the languages for a multicheckbox.
     *
     * @param bool $includeInterfaceLanguages Should we also get the interfacelanguages?
     *
     * @return array
     */
    public static function getLanguagesForMultiCheckbox($includeInterfaceLanguages = false)
    {
        // get working languages
        $aLanguages = BL::getWorkingLanguages();

        // add the interface languages if needed
        if ($includeInterfaceLanguages) {
            $aLanguages = array_merge($aLanguages, BL::getInterfaceLanguages());
        }

        // create a new array to redefine the languages for the multicheckbox
        $languages = array();

        // loop the languages
        foreach ($aLanguages as $key => $lang) {<commit_msg>Remove unused private method getLabelsFromBackendNavigation<commit_after><?php

namespace Backend\Modules\Locale\Engine;

/*
 * This file is part of Fork CMS.
 *
 * For the full copyright and license information, please view the license
 * file that was distributed with this source code.
 */

use Common\Uri as CommonUri;
use Backend\Core\Engine\Authentication as BackendAuthentication;
use Backend\Core\Language\Language as BL;
use Backend\Core\Engine\Model as BackendModel;

/**
 * In this file we store all generic functions that we will be using in the locale module
 */
class Model
{
    /**
     * Build the language files
     *
     * @param string $language    The language to build the locale-file for.
     * @param string $application The application to build the locale-file for.
     */
    public static function buildCache($language, $application)
    {
        $cacheBuilder = new CacheBuilder(BackendModel::get('database'));
        $cacheBuilder->buildCache($language, $application);
    }

    /**
     * Build a query for the URL based on the filter
     *
     * @param array $filter The filter.
     *
     * @return string
     */
    public static function buildURLQueryByFilter($filter)
    {
        $query = http_build_query($filter, null, '&', PHP_QUERY_RFC3986);
        if ($query != '') {
            $query = '&' . $query;
        }

        return $query;
    }

    /**
     * Create an XML-string that can be used for export.
     *
     * @param array $items The items.
     *
     * @return string
     */
    public static function createXMLForExport(array $items)
    {
        $charset = BackendModel::getContainer()->getParameter('kernel.charset');
        $xml = new \DOMDocument('1.0', $charset);

        // set some properties
        $xml->preserveWhiteSpace = false;
        $xml->formatOutput = true;

        // locale root element
        $root = $xml->createElement('locale');
        $xml->appendChild($root);

        // loop applications
        foreach ($items as $application => $modules) {
            // create application element
            $applicationElement = $xml->createElement($application);
            $root->appendChild($applicationElement);

            // loop modules
            foreach ($modules as $module => $types) {
                // create application element
                $moduleElement = $xml->createElement($module);
                $applicationElement->appendChild($moduleElement);

                // loop types
                foreach ($types as $type => $items) {
                    // loop items
                    foreach ($items as $name => $translations) {
                        // create application element
                        $itemElement = $xml->createElement('item');
                        $moduleElement->appendChild($itemElement);

                        // attributes
                        $itemElement->setAttribute('type', self::getTypeName($type));
                        $itemElement->setAttribute('name', $name);

                        // loop translations
                        foreach ($translations as $translation) {
                            // create translation
                            $translationElement = $xml->createElement('translation');
                            $itemElement->appendChild($translationElement);

                            // attributes
                            $translationElement->setAttribute('language', $translation['language']);

                            // set content
                            $translationElement->appendChild(new \DOMCdataSection($translation['value']));
                        }
                    }
                }
            }
        }

        return $xml->saveXML();
    }

    /**
     * Delete (multiple) items from locale
     *
     * @param array $ids The id(s) to delete.
     */
    public static function delete(array $ids)
    {
        // loop and cast to integers
        foreach ($ids as &$id) {
            $id = (int) $id;
        }

        // create an array with an equal amount of questionmarks as ids provided
        $idPlaceHolders = array_fill(0, count($ids), '?');

        // delete records
        BackendModel::getContainer()->get('database')->delete(
            'locale',
            'id IN (' . implode(', ', $idPlaceHolders) . ')',
            $ids
        );

        // rebuild cache
        self::buildCache(BL::getWorkingLanguage(), 'Backend');
        self::buildCache(BL::getWorkingLanguage(), 'Frontend');
    }

    /**
     * Does an id exist.
     *
     * @param int $id The id to check for existence.
     *
     * @return bool
     */
    public static function exists($id)
    {
        return (bool) BackendModel::getContainer()->get('database')->getVar(
            'SELECT 1
             FROM locale
             WHERE id = ?
             LIMIT 1',
            array((int) $id)
        );
    }

    /**
     * Does a locale exists by its name.
     *
     * @param string $name        The name of the locale.
     * @param string $type        The type of the locale.
     * @param string $module      The module wherein will be searched.
     * @param string $language    The language to use.
     * @param string $application The application wherein will be searched.
     * @param int    $id          The id to exclude in the check.
     *
     * @return bool
     */
    public static function existsByName($name, $type, $module, $language, $application, $id = null)
    {
        $name = (string) $name;
        $type = (string) $type;
        $module = (string) $module;
        $language = (string) $language;
        $application = (string) $application;
        $id = ($id !== null) ? (int) $id : null;

        // get db
        $db = BackendModel::getContainer()->get('database');

        // return
        if ($id !== null) {
            return (bool) $db->getVar(
                'SELECT 1
                 FROM locale
                 WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ? AND id != ?
                 LIMIT 1',
                array($name, $type, $module, $language, $application, $id)
            );
        }

        return (bool) BackendModel::getContainer()->get('database')->getVar(
            'SELECT 1
             FROM locale
             WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ?
             LIMIT 1',
            array($name, $type, $module, $language, $application)
        );
    }

    /**
     * Get a single item from locale.
     *
     * @param int $id The id of the item to get.
     *
     * @return array
     */
    public static function get($id)
    {
        // fetch record from db
        $record = (array) BackendModel::getContainer()->get('database')->getRecord(
            'SELECT * FROM locale WHERE id = ?',
            array((int) $id)
        );

        // actions are urlencoded
        if ($record['type'] == 'act') {
            $record['value'] = urldecode($record['value']);
        }

        return $record;
    }

    /**
     * Get a locale by its name
     *
     * @param string $name        The name of the locale.
     * @param string $type        The type of the locale.
     * @param string $module      The module wherein will be searched.
     * @param string $language    The language to use.
     * @param string $application The application wherein will be searched.
     *
     * @return bool
     */
    public static function getByName($name, $type, $module, $language, $application)
    {
        $name = (string) $name;
        $type = (string) $type;
        $module = (string) $module;
        $language = (string) $language;
        $application = (string) $application;

        return BackendModel::getContainer()->get('database')->getVar(
            'SELECT l.id
             FROM locale AS l
             WHERE name = ? AND type = ? AND module = ? AND language = ? AND application = ?',
            array($name, $type, $module, $language, $application)
        );
    }

    /**
     * Get the languages for a multicheckbox.
     *
     * @param bool $includeInterfaceLanguages Should we also get the interfacelanguages?
     *
     * @return array
     */
    public static function getLanguagesForMultiCheckbox($includeInterfaceLanguages = false)
    {
        // get working languages
        $aLanguages = BL::getWorkingLanguages();

        // add the interface languages if needed
        if ($includeInterfaceLanguages) {
            $aLanguages = array_merge($aLanguages, BL::getInterfaceLanguages());
        }

        // create a new array to redefine the languages for the multicheckbox
        $languages = array();

        // loop the languages
        foreach ($aLanguages as $key => $lang) {
{'fraction_non_alphanumeric': 0.11573893138294902, 'fraction_numerical': 0.0014516219083244933, 'mean_word_length': 1.7792086889061287, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 20}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5889992', 'n_tokens_mistral': 4584, 'n_tokens_neox': 4464, 'n_words': 1385}