XoopsFormTinymce::getFonts()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 4
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 *  TinyMCE adapter for XOOPS
14
 *
15
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
16
 * @license             GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package             class
18
 * @subpackage          editor
19
 * @since               2.3.0
20
 * @author              Taiwen Jiang <[email protected]>
21
 */
22
23
xoops_load('XoopsEditor');
24
25
/**
26
 * Class XoopsFormTinymce
27
 */
28
class XoopsFormTinymce extends XoopsEditor
29
{
30
    public $config;
31
    public $language;
32
    public $width  = '100%';
33
    public $height = '500px';
34
35
    public $editor;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param array $configs Editor Options
41
     */
42
    public function __construct($configs)
43
    {
44
        $current_path = __FILE__;
45
        if (DIRECTORY_SEPARATOR !== '/') {
46
            $current_path = str_replace(strpos($current_path, "\\\\", 2) ? "\\\\" : DIRECTORY_SEPARATOR, '/', $current_path);
0 ignored issues
show
Unused Code introduced by
The assignment to $current_path is dead and can be removed.
Loading history...
47
        }
48
49
        $this->rootPath = '/class/xoopseditor/tinymce';
50
        parent::__construct($configs);
51
        $this->configs['elements']    = $this->getName();
52
        $this->configs['language']    = $this->getLanguage();
53
        $this->configs['rootpath']    = $this->rootPath;
54
        $this->configs['area_width']  = isset($this->configs['width']) ? $this->configs['width'] : $this->width;
55
        $this->configs['area_height'] = isset($this->configs['height']) ? $this->configs['height'] : $this->height;
56
        $this->configs['fonts']       = $this->getFonts();
57
58
        require_once __DIR__ . '/tinymce.php';
59
        $this->editor = new TinyMCE($this->configs);
60
    }
61
62
    /**
63
     * Renders the Javascript function needed for client-side for validation
64
     *
65
     * I'VE USED THIS EXAMPLE TO WRITE VALIDATION CODE
66
     * http://tinymce.moxiecode.com/punbb/viewtopic.php?id=12616
67
     *
68
     * @return string
69
     */
70
    public function renderValidationJS()
71
    {
72
        if ($this->isRequired() && $eltname = $this->getName()) {
73
            //$eltname = $this->getName();
74
            $eltcaption = $this->getCaption();
75
            $eltmsg     = empty($eltcaption) ? sprintf(_FORM_ENTER, $eltname) : sprintf(_FORM_ENTER, $eltcaption);
76
            $eltmsg     = str_replace('"', '\"', stripslashes($eltmsg));
77
            $ret        = "\n";
78
            $ret .= "if ( tinyMCE.get('{$eltname}').getContent() == \"\" || tinyMCE.get('{$eltname}').getContent() == null) ";
79
            $ret .= "{ window.alert(\"{$eltmsg}\"); tinyMCE.get('{$eltname}').focus(); return false; }";
80
81
            return $ret;
82
        }
83
84
        return '';
85
    }
86
87
    /**
88
     * get language
89
     *
90
     * @return string
91
     */
92
    public function getLanguage()
93
    {
94
        if ($this->language) {
95
            return $this->language;
96
        }
97
        if (defined('_XOOPS_EDITOR_TINYMCE_LANGUAGE')) {
98
            $this->language = strtolower(constant('_XOOPS_EDITOR_TINYMCE_LANGUAGE'));
99
        } else {
100
            $this->language = str_replace('_', '-', strtolower(_LANGCODE));
101
            if (strtolower(_CHARSET) === 'utf-8') {
0 ignored issues
show
introduced by
The condition strtolower(_CHARSET) === 'utf-8' is always true.
Loading history...
102
                $this->language .= '_utf8';
103
            }
104
        }
105
106
        return $this->language;
107
    }
108
109
    /**
110
     * @return mixed
111
     */
112
    public function getFonts()
113
    {
114
        if (empty($this->config['fonts']) && defined('_XOOPS_EDITOR_TINYMCE_FONTS')) {
115
            $this->config['fonts'] = constant('_XOOPS_EDITOR_TINYMCE_FONTS');
116
        }
117
118
        return isset($this->config['fonts']) ? $this->config['fonts'] : null;
119
    }
120
121
    /**
122
     * prepare HTML for output
123
     *
124
     * @return string HTML
125
     */
126
    public function render()
127
    {
128
        $ret = $this->editor->render();
129
        $ret .= parent::render();
130
131
        return $ret;
132
    }
133
134
    /**
135
     * Check if compatible
136
     *
137
     * @return bool
138
     */
139
    public function isActive()
140
    {
141
        return is_readable(XOOPS_ROOT_PATH . $this->rootPath . '/tinymce.php');
142
    }
143
}
144