Completed
Push — master ( 02f2d8...e7d60a )
by Michael
02:40
created

XoopsFormTinymce4Bootstrap::renderValidationJS()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 16
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
/*
4
 You may not change or alter any portion of this comment or credits
5
 of supporting developers from this source code or any supporting source code
6
 which is considered copyrighted (c) material of the original comment or credit authors.
7
8
 This program is distributed in the hope that it will be useful,
9
 but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
*/
12
13
/**
14
 *  TinyMCE adapter for XOOPS
15
 *
16
 * @copyright       XOOPS Project (https://xoops.org)
17
 * @license         http://www.fsf.org/copyleft/gpl.html GNU public license
18
 * @package         class
19
 * @subpackage      editor
20
 * @since           2.3.0
21
 * @author          Taiwen Jiang <[email protected]>
22
 * @version         $Id: formtinymce.php 8066 2011-11-06 05:09:33Z beckmi $
23
 */
24
25
xoops_load('XoopsEditor');
26
27 View Code Duplication
class XoopsFormTinymce4Bootstrap extends XoopsEditor
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
{
29
    public $language;
30
    public $width  = '100%';
31
    public $height = '500px';
32
33
    public $editor;
34
35
    /**
36
     * Constructor
37
     *
38
     * @param array $configs Editor Options
39
     */
40
41
    public function __construct($configs)
42
    {
43
        $current_path = __FILE__;
44
        if (DIRECTORY_SEPARATOR !== '/') {
45
            $current_path = str_replace(strpos($current_path, "\\\\", 2) ? "\\\\" : DIRECTORY_SEPARATOR, '/', $current_path);
0 ignored issues
show
Unused Code introduced by
$current_path is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
46
        }
47
48
        $this->rootPath = '/class/xoopseditor/tinymce4bootstrap';
49
        parent::__construct($configs);
50
        $this->configs['elements']    = $this->getName();
51
        $this->configs['language']    = $this->getLanguage();
52
        $this->configs['rootpath']    = $this->rootPath;
53
        $this->configs['area_width']  = isset($this->configs['width']) ? $this->configs['width'] : $this->width;
54
        $this->configs['area_height'] = isset($this->configs['height']) ? $this->configs['height'] : $this->height;
55
        $this->configs['fonts']       = $this->getFonts();
56
57
        require_once __DIR__ . '/tinymce.php';
58
        $this->editor = new TinyMCE($this->configs);
59
    }
60
61
    /**
62
     * Renders the Javascript function needed for client-side for validation
63
     *
64
     * I'VE USED THIS EXAMPLE TO WRITE VALIDATION CODE
65
     * http://tinymce.moxiecode.com/punbb/viewtopic.php?id=12616
66
     *
67
     * @return string
68
     */
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
93
    public function getLanguage()
94
    {
95
        if ($this->language) {
96
            return $this->language;
97
        }
98
        if (defined('_XOOPS_EDITOR_TINYMCE4_LANGUAGE')) {
99
            $this->language = strtolower(constant('_XOOPS_EDITOR_TINYMCE4_LANGUAGE'));
100
        } else {
101
            $this->language = str_replace('_', '-', strtolower(_LANGCODE));
102
            if ('utf-8' === strtolower(_CHARSET)) {
103
                $this->language .= '_utf8';
104
            }
105
        }
106
107
        return $this->language;
108
    }
109
110
    public function getFonts()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
111
    {
112
        if (empty($this->config['fonts']) && defined('_XOOPS_EDITOR_TINYMCE4_FONTS')) {
113
            $this->config['fonts'] = constant('_XOOPS_EDITOR_TINYMCE4_FONTS');
114
        }
115
116
        return @$this->config['fonts'];
117
    }
118
119
    /**
120
     * prepare HTML for output
121
     *
122
     * @return sting HTML
123
     */
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
124
125
    public function render()
126
    {
127
        $ret = $this->editor->render();
128
        $ret .= parent::render();
129
130
        return $ret;
131
    }
132
133
    /**
134
     * Check if compatible
135
     *
136
     * @return bool
137
     */
138
139
    public function isActive()
140
    {
141
        return is_readable(XOOPS_ROOT_PATH . $this->rootPath . '/tinymce.php');
142
    }
143
}
144
145