Classes

File phpboost/langs/LangsManager.class.php

File phpboost/langs/LangsManager.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 
<?php
/**
 * @package     PHPBoost
 * @subpackage  Langs
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Kevin MASSY <reidlos@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 01 21
 * @since       PHPBoost 3.0 - 2012 01 19
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class LangsManager
{
    private static $error = null;

    public static function get_installed_langs_map()
    {
        return LangsConfig::load()->get_langs();
    }

    /**
     * @return Lang[string] the Lang map (name => lang) of the installed langs (activated or not)
     * sorted by name
     */
    public static function get_installed_langs_map_sorted_by_localized_name()
    {
        $langs = self::get_installed_langs_map();
        try {
            usort($langs, array(__CLASS__, 'callback_sort_langs_by_name'));
        } catch (IOException $ex) {
        }
        return $langs;
    }

    public static function get_activated_langs_map()
    {
        $activated_langs = array();
        foreach (LangsConfig::load()->get_langs() as $lang) {
            if ($lang->is_activated()) {
                $activated_langs[$lang->get_id()] = $lang;
            }
        }
        return $activated_langs;
    }

    /**
     * @return Lang[string] the Langs map (name => lang) of the installed langs (and activated)
     * sorted by name
     */
    public static function get_activated_langs_map_sorted_by_localized_name()
    {
        $langs = self::get_activated_langs_map();
        try {
            usort($langs, array(__CLASS__, 'callback_sort_langs_by_name'));
        } catch (IOException $ex) {
        }
        return $langs;
    }

    public static function get_activated_and_authorized_langs_map()
    {
        $langs = array();
        foreach (LangsConfig::load()->get_langs() as $lang) {
            if ($lang->is_activated() && $lang->check_auth()) {
                $langs[$lang->get_id()] = $lang;
            }
        }
        return $langs;
    }

    /**
     * @return Lang[string] the Langs map (name => lang) of the installed langs (and activated and authorized)
     * sorted by name
     */
    public static function get_activated_and_authorized_langs_map_sorted_by_localized_name()
    {
        $langs = self::get_activated_and_authorized_langs_map();
        try {
            usort($langs, array(__CLASS__, 'callback_sort_langs_by_name'));
        } catch (IOException $ex) {
        }
        return $langs;
    }

    public static function callback_sort_langs_by_name(Lang $lang1, Lang $lang2)
    {
        if (TextHelper::strtolower($lang1->get_configuration()->get_name()) > TextHelper::strtolower($lang2->get_configuration()->get_name()))
        {
            return 1;
        }
        return -1;
    }

    public static function get_default_lang()
    {
        return UserAccountsConfig::load()->get_default_lang();
    }

    public static function get_lang($id)
    {
        return LangsConfig::load()->get_lang($id);
    }

    public static function get_lang_existed($id)
    {
        if (LangsConfig::load()->get_lang($id) !== null)
        {
            return true;
        }
        return false;
    }

    public static function install($id, $authorizations = array(), $enable = true)
    {
        if (!empty($id) && !self::get_lang_existed($id))
        {
            $lang = new Lang($id, $authorizations, $enable);
            $configuration = $lang->get_configuration();

            $phpboost_version = GeneralConfig::load()->get_phpboost_major_version();
            if (version_compare($phpboost_version, $configuration->get_compatibility(), '>'))
            {
                self::$error = LangLoader::get_message('misfit.phpboost', 'status-messages-common');
            }
            else
            {
                LangsConfig::load()->add_lang($lang);
                LangsConfig::save();
            }
        }
        else if (self::get_lang_existed($id))
        {
            self::$error = LangLoader::get_message('element.already_exists', 'status-messages-common');
        }
        else
        {
            self::$error = LangLoader::get_message('process.error', 'status-messages-common');
        }
    }

    public static function uninstall($id, $drop_files = false)
    {
        if (!empty($id) && self::get_lang_existed($id))
        {
            $default_lang = self::get_default_lang();
            if (self::get_lang($id)->get_id() !== $default_lang)
            {
                PersistenceContext::get_querier()->update(DB_TABLE_MEMBER, array('locale' => $default_lang),
                    'WHERE locale=:old_locale', array('old_locale' => $id
                ));

                LangsConfig::load()->remove_lang_by_id($id);
                LangsConfig::save();

                if ($drop_files)
                {
                    $folder = new Folder(PATH_TO_ROOT . '/lang/' . $id);
                    $folder->delete();
                }
            }
        }
    }

    public static function change_visibility($id, $visibility)
    {
        if (!empty($id) && self::get_lang_existed($id))
        {
            $lang = self::get_lang($id);
            $lang->set_activated($visibility);
            LangsConfig::load()->update($lang);
            LangsConfig::save();
        }
    }

    public static function change_authorizations($id, Array $authorizations)
    {
        if (!empty($id) && self::get_lang_existed($id))
        {
            $lang = self::get_lang($id);
            $lang->set_authorizations($authorizations);
            LangsConfig::load()->update($lang);
            LangsConfig::save();
        }
    }

    public static function change_informations($id, $visibility, Array $authorizations = array())
    {
        if (!empty($id) && self::get_lang_existed($id))
        {
            $lang = self::get_lang($id);
            $lang->set_activated($visibility);

            if (!empty($authorizations))
            {
                $lang->set_authorizations($authorizations);
            }

            LangsConfig::load()->update($lang);
            LangsConfig::save();
        }
    }

    public static function get_error()
    {
        return self::$error;
    }
}
?>