Completed
Branch master (220ce5)
by De Cramer
16:11
created

Widget::getDictionaryInformation()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 7.0071

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 18
cts 19
cp 0.9474
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 5
nop 2
crap 7.0071
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 3
    public function __construct(
49
        Group $group,
50
        Translations $translationHelper,
51
        $name,
52
        $sizeX,
53
        $sizeY,
54
        $posX = null,
55
        $posY = null
56
    ) {
57 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
58
59 3
        $this->translationHelper = $translationHelper;
60
61
        // Manialink containing everything
62 3
        $this->manialink = new \FML\ManiaLink();
63 3
        $this->manialink->setId($this->getId())
64 3
            ->setName($name)
65 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
66
67 3
        $this->dictionary = new Dico();
68 3
        $this->manialink->setDico($this->dictionary);
69
70 3
        $windowFrame = new Frame('Window');
71 3
        $windowFrame->setPosition($posX, $posY);
72 3
        $this->manialink->addChild($windowFrame);
73
74
        // Frame to handle the content of the window.
75 3
        $this->contentFrame = new Frame();
76 3
        $this->contentFrame->setPosition(0, 0);
77 3
        $this->contentFrame->setSize($sizeX, $sizeY);
78 3
        $windowFrame->addChild($this->contentFrame);
79
80 3
        $toggleInterfaceF9 = new ToggleInterface("F9");
81 3
        $this->manialink->getScript()
82 3
            ->addFeature($toggleInterfaceF9);
83
84 3
        $this->windowFrame = $windowFrame;
85 3
    }
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 1
    public function getXml()
109
    {
110 1
        $this->addDictionaryInformation();
111
112 1
        return $this->manialink->__toString();
113
    }
114
115
    /**
116
     * Add translations to dictionary.
117
     */
118 1
    protected function addDictionaryInformation()
119
    {
120 1
        $translations = [];
121 1
        $this->dictionary->removeAllEntries();
122 1
        $this->getDictionaryInformation($this->manialink, $translations);
123
124 1
        foreach ($translations as $msgId => $messages) {
125 1
            foreach ($messages as $message) {
126 1
                $this->dictionary->setEntry($message['Lang'], $msgId, htmlspecialchars($message['Text']));
127 1
            }
128 1
        }
129 1
    }
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 1
    protected function getDictionaryInformation($control, &$translations)
138
    {
139 1
        foreach ($control->getChildren() as $child) {
140
            if (($child instanceof Label
141 1
                    || $child instanceof uiLabel
142 1
                ) && $child->getTranslate()) {
143 1
                $id = $child->getTextId();
144
145 1
                if (!isset($this->cachedMessages[$id])) {
146 1
                    $textId = 'exp_'.md5($id);
147
148 1
                    $messages = $this->translationHelper->getTranslations($child->getTextId(), []);
149 1
                    $translations[$textId] = $messages;
150 1
                    $this->cachedMessages[$textId] = $messages;
151
152
                    // Replaces with text id that can be used in the xml.
153 1
                    $child->setTextId($textId);
154 1
                } else {
155
                    $translations[$id] = $this->cachedMessages[$id];
156
                }
157 1
            } else {
158 1
                if ($child instanceof Frame) {
159 1
                    $this->getDictionaryInformation($child, $translations);
160 1
                }
161
            }
162 1
        }
163 1
    }
164
165
    /**
166
     * Get the children
167
     *
168
     * @api
169
     * @return Renderable[]
170
     */
171 1
    public function getChildren()
172
    {
173 1
        return $this->contentFrame->getChildren();
174
    }
175
176
    /**
177
     * Add a new child
178
     *
179
     * @api
180
     *
181
     * @param Renderable $child Child Control to add
182
     *
183
     * @return static
184
     */
185 1
    public function addChild(Renderable $child)
186
    {
187 1
        $this->contentFrame->addChild($child);
188
189 1
        return $this;
190
    }
191
192
    /**
193
     * Add a new child
194
     *
195
     * @api
196
     *
197
     * @param Renderable $child Child Control to add
198
     *
199
     * @return static
200
     * @deprecated Use addChild()
201
     * @see        Container::addChild()
202
     */
203 1
    public function add(Renderable $child)
204
    {
205 1
        $this->contentFrame->addChild($child);
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * Add new children
212
     *
213
     * @api
214
     *
215
     * @param Renderable[] $children Child Controls to add
216
     *
217
     * @return static
218
     */
219 1
    public function addChildren(array $children)
220
    {
221 1
        $this->contentFrame->addChildren($children);
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * Remove all children
228
     *
229
     * @api
230
     * @return static
231
     */
232 1
    public function removeAllChildren()
233
    {
234 1
        $this->contentFrame->removeAllChildren();
235
236 1
        return $this;
237
    }
238
239
    /**
240
     * Remove all children
241
     *
242
     * @api
243
     * @return static
244
     * @deprecated Use removeAllChildren()
245
     * @see        Container::removeAllChildren()
246
     */
247 1
    public function removeChildren()
248
    {
249 1
        $this->contentFrame->removeAllChildren();
250
251 1
        return $this;
252
    }
253
254
    /**
255
     * Get the Format
256
     *
257
     * @api
258
     *
259
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
260
     *
261
     * @return Format
262
     * @deprecated Use Style
263
     * @see        Style
264
     */
265 1
    public function getFormat($createIfEmpty = true)
266
    {
267 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...
268
    }
269
270
    /**
271
     * Set the Format
272
     *
273
     * @api
274
     *
275
     * @param Format $format New Format
276
     *
277
     * @return static
278
     * @deprecated Use Style
279
     * @see        Style
280
     *
281
     */
282 1
    public function setFormat(Format $format = null)
283
    {
284 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...
285
286 1
        return $this;
287
    }
288
289
    /**
290
     * @return Frame
291
     */
292
    public function getContentFrame()
293
    {
294
        return $this->contentFrame;
295
    }
296
297
298
}
299