Completed
Push — dev ( 214409...8e757d )
by
unknown
03:22
created

Widget::setScriptData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace eXpansion\Framework\Core\Model\Gui;
4
5
use eXpansion\Framework\Core\Exceptions\Gui\MissingCloseActionException;
6
use eXpansion\Framework\Core\Helpers\Translations;
7
use eXpansion\Framework\Core\Model\UserGroups\Group;
8
use FML\Controls\Control;
9
use FML\Controls\Frame;
10
use FML\Controls\Label;
11
use FML\Controls\Quad;
12
use FML\Controls\Quads\Quad_Bgs1;
13
use FML\Controls\Quads\Quad_Bgs1InRace;
14
use FML\Elements\Dico;
15
use FML\Elements\Format;
16
use FML\Script\Features\ToggleInterface;
17
use FML\Types\Container;
18
use FML\Types\Renderable;
19
20
class Widget extends Manialink implements Container
21
{
22
    /** @var Translations */
23
    protected $translationHelper;
24
25
    /** @var \FML\ManiaLink */
26
    protected $manialink;
27
28
    /** @var Dico */
29
    protected $dictionary;
30
31
    /** @var Label */
32
    protected $closeButton;
33
34
    /** @var Frame */
35
    protected $contentFrame;
36
37
    /** @var Frame */
38
    protected $windowFrame;
39
40 3
    public function __construct(
41
        Group $group,
42
        Translations $translationHelper,
43
        $name,
44
        $sizeX,
45
        $sizeY,
46
        $posX = null,
47
        $posY = null
48
    ) {
49 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
50
51 3
        $this->translationHelper = $translationHelper;
52
53
        // Manialink containing everything
54 3
        $this->manialink = new \FML\ManiaLink();
55 3
        $this->manialink->setId($this->getId())
56 3
            ->setName($name)
57 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
58
59 3
        $this->dictionary = new Dico();
60 3
        $this->manialink->setDico($this->dictionary);
61
62 3
        $windowFrame = new Frame('Window');
63 3
        $windowFrame->setPosition($posX, $posY);
64 3
        $this->manialink->addChild($windowFrame);
65
66
        // Frame to handle the content of the window.
67 3
        $this->contentFrame = new Frame();
68 3
        $this->contentFrame->setPosition(0, 0);
69 3
        $this->contentFrame->setSize($sizeX, $sizeY);
70 3
        $windowFrame->addChild($this->contentFrame);
71
72 3
        $this->windowFrame = $windowFrame;
73 3
    }
74
75
    /**
76
     * @return \FML\ManiaLink
77
     */
78
    public function getFmlManialink()
79
    {
80
        return $this->manialink;
81
    }
82
83
    public function setScriptData(ManiaScript $script) {
84
        $this->scriptData = $script->__toString();
0 ignored issues
show
Bug introduced by
The property scriptData does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

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