Completed
Pull Request — master (#130)
by
unknown
02:52
created

Widget::getXml()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui;
4
5
use eXpansion\Framework\Core\Helpers\Translations;
6
use eXpansion\Framework\Core\Model\UserGroups\Group;
7
use eXpansion\Framework\Gui\Components\uiButton;
8
use eXpansion\Framework\Gui\Components\uiCheckbox;
9
use eXpansion\Framework\Gui\Components\uiLabel;
10
use FML\Controls\Frame;
11
use FML\Controls\Label;
12
use FML\Elements\Dico;
13
use FML\Elements\Format;
14
use FML\Elements\SimpleScript;
15
use FML\Script\Features\ToggleInterface;
16
use FML\Script\Script;
17
use FML\Script\ScriptInclude;
18
use FML\Types\Container;
19
use FML\Types\Renderable;
20
21
class Widget extends Manialink implements Container
22
{
23
24
    /** @var  string */
25
    protected $scriptData;
26
27
    /** @var Translations */
28
    protected $translationHelper;
29
30
    /** @var \FML\ManiaLink */
31
    protected $manialink;
32
33
    /** @var Dico */
34
    protected $dictionary;
35
36
    /** @var Label */
37
    protected $closeButton;
38
39
    /** @var Frame */
40
    protected $contentFrame;
41
42
    /** @var Frame */
43
    protected $windowFrame;
44
45
    /** @var array[] */
46
    protected $cachedMessages = [];
47
48
    public function __construct(
49
        Group $group,
50
        Translations $translationHelper,
51
        $name,
52
        $sizeX,
53
        $sizeY,
54
        $posX = null,
55
        $posY = null
56
    ) {
57
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
58
59
        $this->translationHelper = $translationHelper;
60
61
        // Manialink containing everything
62
        $this->manialink = new \FML\ManiaLink();
63
        $this->manialink->setId($this->getId())
64
            ->setName($name)
65
            ->setVersion(\FML\ManiaLink::VERSION_3);
66
67
        $this->dictionary = new Dico();
68
        $this->manialink->setDico($this->dictionary);
69
70
        $windowFrame = new Frame('Window');
71
        $windowFrame->setPosition($posX, $posY);
72
        $this->manialink->addChild($windowFrame);
73
74
        // Frame to handle the content of the window.
75
        $this->contentFrame = new Frame();
76
        $this->contentFrame->setPosition(0, 0);
77
        $this->contentFrame->setSize($sizeX, $sizeY);
78
        $windowFrame->addChild($this->contentFrame);
79
80
        $toggleInterfaceF9 = new ToggleInterface("F9");
0 ignored issues
show
Documentation introduced by
'F9' is of type string, but the function expects a object<FML\Controls\Control>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $this->manialink->getScript()
82
            ->addFeature($toggleInterfaceF9);
83
84
        $this->windowFrame = $windowFrame;
85
    }
86
87
    /**
88
     * @return \FML\ManiaLink
89
     */
90
    public function getFmlManialink()
91
    {
92
        return $this->manialink;
93
    }
94
95
    /**
96
     * sets scripts data
97
     *
98
     * @param ManiaScript $script
99
     */
100
    public function setScriptData(ManiaScript $script)
101
    {
102
        $this->scriptData = $script->__toString();
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function getXml()
109
    {
110
        $this->addDictionaryInformation();
111
112
        return $this->manialink->__toString();
113
    }
114
115
    /**
116
     * Add translations to dictionary.
117
     */
118
    protected function addDictionaryInformation()
119
    {
120
        $translations = [];
121
        $this->dictionary->removeAllEntries();
122
        $this->getDictionaryInformation($this->manialink, $translations);
123
124
        foreach ($translations as $msgId => $messages) {
125
            foreach ($messages as $message) {
126
                $this->dictionary->setEntry($message['Lang'], $msgId, htmlspecialchars($message['Text']));
127
            }
128
        }
129
    }
130
131
    /**
132
     * Recursive search all dome tree in order to find all translatable labels.
133
     *
134
     * @param Container|\FML\ManiaLink $control
135
     * @param $translations
136
     */
137
    protected function getDictionaryInformation($control, &$translations)
138
    {
139
        foreach ($control->getChildren() as $child) {
140
            if (($child instanceof Label || $child instanceof uiLabel) && $child->getTranslate()) {
141
                $id = $child->getTextId();
142
143
                if (!isset($this->cachedMessages[$id])) {
144
                    $textId = 'exp_'.md5($id);
145
146
                    $messages = $this->translationHelper->getTranslations($child->getTextId(), []);
147
                    $translations[$textId] = $messages;
148
                    $this->cachedMessages[$textId] = $messages;
149
150
                    // Replaces with text id that can be used in the xml.
151
                    $child->setTextId($textId);
152
                } else {
153
                    $translations[$id] = $this->cachedMessages[$id];
154
                }
155
            } else {
156
                if ($child instanceof Container) {
157
                    $this->getDictionaryInformation($child, $translations);
158
                }
159
            }
160
        }
161
    }
162
163
    /**
164
     * Get the children
165
     *
166
     * @api
167
     * @return Renderable[]
168
     */
169
    public function getChildren()
170
    {
171
        return $this->contentFrame->getChildren();
172
    }
173
174
    /**
175
     * Add a new child
176
     *
177
     * @api
178
     *
179
     * @param Renderable $child Child Control to add
180
     *
181
     * @return static
182
     */
183
    public function addChild(Renderable $child)
184
    {
185
        $this->contentFrame->addChild($child);
186
187
        return $this;
188
    }
189
190
    /**
191
     * Add a new child
192
     *
193
     * @api
194
     *
195
     * @param Renderable $child Child Control to add
196
     *
197
     * @return static
198
     * @deprecated Use addChild()
199
     * @see        Container::addChild()
200
     */
201
    public function add(Renderable $child)
202
    {
203
        $this->contentFrame->addChild($child);
204
205
        return $this;
206
    }
207
208
    /**
209
     * Add new children
210
     *
211
     * @api
212
     *
213
     * @param Renderable[] $children Child Controls to add
214
     *
215
     * @return static
216
     */
217
    public function addChildren(array $children)
218
    {
219
        $this->contentFrame->addChildren($children);
220
221
        return $this;
222
    }
223
224
    /**
225
     * Remove all children
226
     *
227
     * @api
228
     * @return static
229
     */
230
    public function removeAllChildren()
231
    {
232
        $this->contentFrame->removeAllChildren();
233
234
        return $this;
235
    }
236
237
    /**
238
     * Remove all children
239
     *
240
     * @api
241
     * @return static
242
     * @deprecated Use removeAllChildren()
243
     * @see        Container::removeAllChildren()
244
     */
245
    public function removeChildren()
246
    {
247
        $this->contentFrame->removeAllChildren();
248
249
        return $this;
250
    }
251
252
    /**
253
     * Get the Format
254
     *
255
     * @api
256
     *
257
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
258
     *
259
     * @return Format
260
     * @deprecated Use Style
261
     * @see        Style
262
     */
263
    public function getFormat($createIfEmpty = true)
264
    {
265
        return $this->contentFrame->getFormat($createIfEmpty);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::getFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
266
    }
267
268
    /**
269
     * Set the Format
270
     *
271
     * @api
272
     *
273
     * @param Format $format New Format
274
     *
275
     * @return static
276
     * @deprecated Use Style
277
     * @see        Style
278
     *
279
     */
280
    public function setFormat(Format $format = null)
281
    {
282
        $this->contentFrame->setFormat($format);
0 ignored issues
show
Deprecated Code introduced by
The method FML\Controls\Frame::setFormat() has been deprecated with message: Use Style

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
283
284
        return $this;
285
    }
286
287
    /**
288
     * @return Frame
289
     */
290
    public function getContentFrame()
291
    {
292
        return $this->contentFrame;
293
    }
294
295
296
}
297