Completed
Pull Request — master (#95)
by
unknown
04:44
created

Widget::getDictionaryInformation()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 15
nc 5
nop 2
crap 6
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 FML\Controls\Frame;
8
use FML\Controls\Label;
9
use FML\Elements\Dico;
10
use FML\Elements\Format;
11
use FML\Elements\SimpleScript;
12
use FML\Script\Features\ToggleInterface;
13
use FML\Script\Script;
14
use FML\Script\ScriptInclude;
15
use FML\Types\Container;
16
use FML\Types\Renderable;
17
18
class Widget extends Manialink implements Container
19
{
20
21
    /** @var  ManiaScript */
22
    protected $scriptData;
23
24
    /** @var Translations */
25
    protected $translationHelper;
26
27
    /** @var \FML\ManiaLink */
28
    protected $manialink;
29
30
    /** @var Dico */
31
    protected $dictionary;
32
33
    /** @var Label */
34
    protected $closeButton;
35
36
    /** @var Frame */
37
    protected $contentFrame;
38
39
    /** @var Frame */
40
    protected $windowFrame;
41
42
    /** @var array[] */
43
    protected $cachedMessages = [];
44
45 3
    public function __construct(
46
        Group $group,
47
        Translations $translationHelper,
48
        $name,
49
        $sizeX,
50
        $sizeY,
51
        $posX = null,
52
        $posY = null
53
    ) {
54 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
55
56 3
        $this->translationHelper = $translationHelper;
57
58
        // Manialink containing everything
59 3
        $this->manialink = new \FML\ManiaLink();
60 3
        $this->manialink->setId($this->getId())
61 3
            ->setName($name)
62 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
63
64 3
        $this->dictionary = new Dico();
65 3
        $this->manialink->setDico($this->dictionary);
66
67 3
        $windowFrame = new Frame('Window');
68 3
        $windowFrame->setPosition($posX, $posY);
69 3
        $this->manialink->addChild($windowFrame);
70
71
        // Frame to handle the content of the window.
72 3
        $this->contentFrame = new Frame();
73 3
        $this->contentFrame->setPosition(0, 0);
74 3
        $this->contentFrame->setSize($sizeX, $sizeY);
75 3
        $windowFrame->addChild($this->contentFrame);
76
77 3
        $toggleInterfaceF9 = new ToggleInterface("F9");
78 3
        $this->manialink->getScript()
79 3
            ->addFeature($toggleInterfaceF9);
80
81 3
        $this->windowFrame = $windowFrame;
82 3
    }
83
84
    /**
85
     * @return \FML\ManiaLink
86
     */
87
    public function getFmlManialink()
88
    {
89
        return $this->manialink;
90
    }
91
92
    /**
93
     * sets scripts data
94
     *
95
     * @param ManiaScript $script
96
     */
97
    public function setScriptData(ManiaScript $script)
98
    {
99
        $this->scriptData = $script->__toString();
0 ignored issues
show
Documentation Bug introduced by
It seems like $script->__toString() of type string is incompatible with the declared type object<eXpansion\Framewo...\Model\Gui\ManiaScript> of property $scriptData.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
    }
101
102
    /**
103
     * @inheritdoc
104
     */
105 1
    public function getXml()
106
    {
107 1
        $this->addDictionaryInformation();
108
109 1
        return $this->manialink->__toString();
110
    }
111
112
    /**
113
     * Add translations to dictionary.
114
     */
115 1
    protected function addDictionaryInformation()
116
    {
117 1
        $translations = [];
118 1
        $this->dictionary->removeAllEntries();
119 1
        $this->getDictionaryInformation($this->manialink, $translations);
120
121 1
        foreach ($translations as $msgId => $messages) {
122 1
            foreach ($messages as $message) {
123 1
                $this->dictionary->setEntry($message['Lang'], $msgId, htmlspecialchars($message['Text']));
124
            }
125
        }
126 1
    }
127
128
    /**
129
     * Recursive search all dome tree in order to find all translatable labels.
130
     *
131
     * @param Container|\FML\ManiaLink $control
132
     * @param $translations
133
     */
134 1
    protected function getDictionaryInformation($control, &$translations)
135
    {
136 1
        foreach ($control->getChildren() as $child) {
137 1
            if ($child instanceof Label && $child->getTranslate()) {
138 1
                $id = $child->getTextId();
139
140 1
                if (!isset($this->cachedMessages[$id])) {
141 1
                    $textId = 'exp_'.md5($id);
142
143 1
                    $messages = $this->translationHelper->getTranslations($child->getTextId(), []);
144 1
                    $translations[$textId] = $messages;
145 1
                    $this->cachedMessages[$textId] = $messages;
146
147
                    // Replaces with text id that can be used in the xml.
148 1
                    $child->setTextId($textId);
149
                } else {
150 1
                    $translations[$id] = $this->cachedMessages[$id];
151
                }
152
            } else {
153 1
                if ($child instanceof Frame) {
154 1
                    $this->getDictionaryInformation($child, $translations);
155
                }
156
            }
157
        }
158 1
    }
159
160
    /**
161
     * Get the children
162
     *
163
     * @api
164
     * @return Renderable[]
165
     */
166 1
    public function getChildren()
167
    {
168 1
        return $this->contentFrame->getChildren();
169
    }
170
171
    /**
172
     * Add a new child
173
     *
174
     * @api
175
     *
176
     * @param Renderable $child Child Control to add
177
     *
178
     * @return static
179
     */
180 1
    public function addChild(Renderable $child)
181
    {
182 1
        $this->contentFrame->addChild($child);
183
184 1
        return $this;
185
    }
186
187
    /**
188
     * Add a new child
189
     *
190
     * @api
191
     *
192
     * @param Renderable $child Child Control to add
193
     *
194
     * @return static
195
     * @deprecated Use addChild()
196
     * @see        Container::addChild()
197
     */
198 1
    public function add(Renderable $child)
199
    {
200 1
        $this->contentFrame->addChild($child);
201
202 1
        return $this;
203
    }
204
205
    /**
206
     * Add new children
207
     *
208
     * @api
209
     *
210
     * @param Renderable[] $children Child Controls to add
211
     *
212
     * @return static
213
     */
214 1
    public function addChildren(array $children)
215
    {
216 1
        $this->contentFrame->addChildren($children);
217
218 1
        return $this;
219
    }
220
221
    /**
222
     * Remove all children
223
     *
224
     * @api
225
     * @return static
226
     */
227 1
    public function removeAllChildren()
228
    {
229 1
        $this->contentFrame->removeAllChildren();
230
231 1
        return $this;
232
    }
233
234
    /**
235
     * Remove all children
236
     *
237
     * @api
238
     * @return static
239
     * @deprecated Use removeAllChildren()
240
     * @see        Container::removeAllChildren()
241
     */
242 1
    public function removeChildren()
243
    {
244 1
        $this->contentFrame->removeAllChildren();
245
246 1
        return $this;
247
    }
248
249
    /**
250
     * Get the Format
251
     *
252
     * @api
253
     *
254
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
255
     *
256
     * @return Format
257
     * @deprecated Use Style
258
     * @see        Style
259
     */
260 1
    public function getFormat($createIfEmpty = true)
261
    {
262 1
        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...
263
    }
264
265
    /**
266
     * Set the Format
267
     *
268
     * @api
269
     *
270
     * @param Format $format New Format
271
     *
272
     * @return static
273
     * @deprecated Use Style
274
     * @see        Style
275
     *
276
     */
277 1
    public function setFormat(Format $format = null)
278
    {
279 1
        $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...
280
281 1
        return $this;
282
    }
283
284
    /**
285
     * @return Frame
286
     */
287
    public function getContentFrame()
288
    {
289
        return $this->contentFrame;
290
    }
291
292
293
}
294