Completed
Branch #338-Save_posting_before_movin... (60770e)
by Schlaefer
02:30
created

ParserHelper::editor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace App\View\Helper;
14
15
use Cake\View\Helper\FormHelper;
16
use Cake\View\Helper\HtmlHelper;
17
use Geshi\View\Helper\GeshiHelper;
18
use SaitoHelp\View\Helper\SaitoHelpHelper;
19
use Saito\App\Registry;
20
use Saito\Markup\MarkupInterface;
21
use Stopwatch\Lib\Stopwatch;
22
23
/**
24
 * Parser Helper
25
 *
26
 * @property GeshiHelper $Geshi
27
 * @property FormHelper $Form
28
 * @property HtmlHelper $Html
29
 * @property SaitoHelpHelper $SaitoHelp
30
 */
31
class ParserHelper extends AppHelper
32
{
33
34
    /**
35
     * @var array these Helpers are also used in the Parser
36
     */
37
    public $helpers = [
38
        'MailObfuscator.MailObfuscator',
39
        'Geshi.Geshi',
40
        'Form',
41
        'Html',
42
        'Text',
43
        'Url',
44
        //= usefull in Parsers
45
        'Layout',
46
        'SaitoHelp'
47
    ];
48
49
    /**
50
     * @var array parserCache for parsed markup
51
     *
52
     * Esp. useful for repeating signatures in long mix view threads
53
     */
54
    protected $_parserCache = [];
55
56
    /** @var MarkupInterface */
57
    protected $Markup;
58
59
    /**
60
     * {@inheritDoc}
61
     */
62
    public function initialize(array $config)
63
    {
64
        parent::initialize($config);
65
        /** @var MarkupInterface */
66
        $Markup = Registry::get('Markup');
67
        $this->Markup = $Markup;
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function beforeRender($viewFile)
0 ignored issues
show
Unused Code introduced by
The parameter $viewFile is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        if (isset($this->request) && $this->request->getParam('action') === 'preview') {
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<App\View\Helper\ParserHelper>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
76
            $this->Geshi->showPlainTextButton = false;
77
        }
78
    }
79
80
    /**
81
     * cite text
82
     *
83
     * @param string $string string
84
     * @return string
85
     */
86
    public function citeText($string)
87
    {
88
        return $this->Markup->citeText($string);
89
    }
90
91
    /**
92
     * get editor help
93
     *
94
     * @return mixed
95
     */
96
    public function editorHelp()
97
    {
98
        return $this->Markup->getEditorHelp($this);
99
    }
100
101
    /**
102
     * get button set
103
     *
104
     * @return mixed
105
     */
106
    public function getButtonSet()
107
    {
108
        $buttons = $this->Markup->getMarkupSet();
109
        $smilies = $this->_View->get('smiliesData')->get();
110
111
        if (!empty($smilies)) {
112
            $buttons[] = [
113
                'type' => 'separator'
114
            ];
115
            $buttons[] = [
116
                'name' => "<i class='fa fa-s-smile-o'></i>",
117
                'title' => __('Smilies'),
118
                'className' => 'btn-markup-Smilies',
119
                'type' => 'saito-smilies',
120
                'handler' => 'smilies',
121
            ];
122
        }
123
124
        return compact('buttons', 'smilies');
125
    }
126
127
    /**
128
     * parse
129
     *
130
     * @param string $string string
131
     * @param array $options options
132
     * @return string
133
     */
134
    public function parse($string, array $options = [])
135
    {
136
        Stopwatch::start('ParseHelper::parse()');
137
        if (empty($string) || $string === 'n/t') {
138
            Stopwatch::stop('ParseHelper::parse()');
139
140
            return $string;
141
        }
142
143
        $defaults = ['return' => 'html', 'embed' => true, 'multimedia' => true, 'wrap' => true];
144
        $options += $defaults;
145
146
        $cacheId = md5(serialize($options) . $string);
147
        if (isset($this->_parserCache[$cacheId])) {
148
            $html = $this->_parserCache[$cacheId];
149
        } else {
150
            $html = $this->Markup->parse($string, $this, $options);
151
            $this->_parserCache[$cacheId] = $html;
152
        }
153
        if ($options['return'] === 'html' && $options['wrap']) {
154
            $html = '<div class="richtext">' . $html . '</div>';
155
        }
156
        Stopwatch::stop('ParseHelper::parse()');
157
158
        return $html;
159
    }
160
}
161