Classes

File content/formatting/ContentFormattingService.class.php

File content/formatting/ContentFormattingService.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: 
<?php
/**
 * This class contains the default content formatting factory that must be used if you want
 * a formatting factory having the default settings.
 * @package     Content
 * @subpackage  Formatting
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 11 30
 * @since       PHPBoost 3.0 - 2009 12 20
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

class ContentFormattingService
{
    /**
     * @var AbstractContentFormattingFactory
     */
    private $default_factory;

    /**
     * Returns the content formatting factory corresponding to the default configuration
     * @return ContentFormattingFactory
     */
    public function get_default_factory()
    {
        if ($this->default_factory === null)
        {
            $this->default_factory = $this->create_factory($this->get_user_editor());
        }
        return $this->default_factory;
    }

    /**
     * Creates a factory for the given language
     * @param string $language
     * @return ContentFormattingFactory
     */
    public function create_factory($language = '')
    {
        $editor = $this->get_existing_editor($language);
        return ContentFormattingProvidersService::create_factory($editor);
    }

    /**
     * Returns the name of the editor of the current user (chosen in its profile).
     * @return string The editor used by the current user.
     */
    public function get_user_editor()
    {
        return AppContext::get_current_user()->get_editor();
    }

    /**
     * Returns the parser to use in the default configuration
     * @return FormattingParser
     */
    public function get_default_parser()
    {
        return $this->get_default_factory()->get_parser();
    }

    /**
     * Returns the unparser to use in the default configuration
     * @return FormattingParser
     */
    public function get_default_unparser()
    {
        return $this->get_default_factory()->get_unparser();
    }

    /**
     * Returns the second parser to use in the default configuration
     * @return FormattingParser
     */
    public function get_default_second_parser()
    {
        return $this->get_default_factory()->get_second_parser();
    }

    /**
     * Returns the editor displayer that you have to display beside the associated HTML textarea
     * if you use the default configuration.
     * @return ContentEditor
     */
    public function get_default_editor()
    {
        return $this->get_default_factory()->get_editor();
    }

    /**
     * @param string $editor
     * @return string
     */
    private function get_existing_editor($editor)
    {
        if (in_array($editor, self::get_editors_identifier()))
        {
            return $editor;
        }
        else
        {
            return ContentFormattingConfig::load()->get_default_editor();
        }
    }

    public function get_editors_identifier()
    {
        return array_keys(ContentFormattingProvidersService::get_editors());
    }

    public function get_available_editors()
    {
        $available_editors = array();
        foreach (ContentFormattingProvidersService::get_editors() as $id => $provider)
        {
            $available_editors[$id] = $provider->get_name();
        }
        return $available_editors;
    }

    /**
     * @param string $id_module
     */
    public function uninstall_editor($id_module)
    {
        $editors = $this->get_available_editors();

        if (in_array($id_module, $editors))
        {
            if (count($editors) > 1)
            {
                $default_editor = ContentFormattingConfig::load()->get_default_editor();

                if ($default_editor !== $id_module)
                {
                    PersistenceContext::get_querier()->update(DB_TABLE_MEMBER, array('editor' => $default_editor),
                        'WHERE editor=:old_editor', array('old_editor' => $id_module
                    ));
                }
                else
                    return LangLoader::get_message('is_default_editor', 'editor-common');
            }
            else
                return LangLoader::get_message('last_editor_installed', 'editor-common');
        }
    }

    /**
     * Returns the map of all the formatting types supported by the PHPBoost formatting editors and parsers.
     * The keys of the map are the tags identifiers and the values the tags names.
     * @return string[] The map
     */
    public function get_available_tags()
    {
        $editor_lang = LangLoader::get('editor-common');
        return array(
            'b' => $editor_lang['format_bold'],
            'i' => $editor_lang['format_italic'],
            'u' => $editor_lang['format_underline'],
            's' => $editor_lang['format_strike'],
            'title' => $editor_lang['format_title'],
            'style' => $editor_lang['format_style'],
            'url' => $editor_lang['format_url'],
            'img' => $editor_lang['format_img'],
            'quote' => $editor_lang['format_quote'],
            'hide' => $editor_lang['format_hide'],
            'list' => $editor_lang['format_list'],
            'color' => $editor_lang['format_color'],
            'bgcolor' => $editor_lang['format_bgcolor'],
            'font' => $editor_lang['format_font'],
            'size' => $editor_lang['format_size'],
            'align' => $editor_lang['format_align'],
            'float' => $editor_lang['format_float'],
            'sup' => $editor_lang['format_sup'],
            'sub' => $editor_lang['format_sub'],
            'indent' => $editor_lang['format_indent'],
            'pre' => $editor_lang['format_pre'],
            'table' => $editor_lang['format_table'],
            'swf' => $editor_lang['format_flash'],
            'movie' => $editor_lang['format_movie'],
            'sound' => $editor_lang['format_sound'],
            'code' => $editor_lang['format_code'],
            'math' => $editor_lang['format_math'],
            'anchor' => $editor_lang['format_anchor'],
            'acronym' => $editor_lang['format_acronym'],
            'block' => $editor_lang['format_block'],
            'fieldset' => $editor_lang['format_fieldset'],
            'mail' => $editor_lang['format_mail'],
            'line' => $editor_lang['format_line'],
            'wikipedia' => $editor_lang['format_wikipedia'],
            'html' => $editor_lang['format_html'],
            'feed' => $editor_lang['format_feed'],
            'youtube' => $editor_lang['format_youtube'],
            'lightbox' => $editor_lang['format_lightbox'],
            'charmap' => $editor_lang['format_charmap'],
            'insertdatetime' => $editor_lang['format_insertdatetime'],
            'fa' => $editor_lang['format_fa'],
            'p' => $editor_lang['format_paragraph']
        );
    }
}
?>