Completed
Push — dev ( 845ef5...8eacd8 )
by
unknown
02:50
created

Widget::setFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Frame;
9
use FML\Controls\Label;
10
use FML\Controls\Quad;
11
use FML\Controls\Quads\Quad_Bgs1;
12
use FML\Controls\Quads\Quad_Bgs1InRace;
13
use FML\Elements\Dico;
14
use FML\Elements\Format;
15
use FML\Types\Container;
16
use FML\Types\Renderable;
17
18
class Widget extends Manialink implements Container
19
{
20
    /** @var Translations */
21
    protected $translationHelper;
22
23
    /** @var \FML\ManiaLink */
24
    protected $manialink;
25
26
    /** @var Dico */
27
    protected $dictionary;
28
29
    /** @var Label */
30
    protected $closeButton;
31
32
    /** @var Frame */
33
    protected $contentFrame;
34
35
    /** @var Frame */
36
    protected $windowFrame;
37
38 3
    public function __construct(
39
        Group $group,
40
        Translations $translationHelper,
41
        $name,
42
        $sizeX,
43
        $sizeY,
44
        $posX = null,
45
        $posY = null
46
    )
47
    {
48 3
        parent::__construct($group, $name, $sizeX, $sizeY, $posX, $posY);
49
50 3
        $this->translationHelper = $translationHelper;
51
52
        // Manialink containing everything
53 3
        $this->manialink = new \FML\ManiaLink();
54 3
        $this->manialink->setId($this->getId())
55 3
            ->setName($name)
56 3
            ->setVersion(\FML\ManiaLink::VERSION_3);
57
58 3
        $this->dictionary = new Dico();
59 3
        $this->manialink->setDico($this->dictionary);
60
61 3
        $windowFrame = new Frame('Window');
62 3
        $windowFrame->setPosition($posX, $posY);
63 3
        $this->manialink->addChild($windowFrame);
64
65
        // Frame to handle the content of the window.
66 3
        $this->contentFrame = new Frame();
67 3
        $this->contentFrame->setPosition(0, 0);
68 3
        $this->contentFrame->setSize($sizeX, $sizeY);
69 3
        $windowFrame->addChild($this->contentFrame);
70
71 3
        $this->windowFrame = $windowFrame;
72 3
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77 1
    public function getXml()
78
    {
79 1
        $this->addDictionaryInformation();
80
81 1
        return $this->manialink->__toString();
82
    }
83
84
    /**
85
     * Add translations to dictionary.
86
     */
87 1
    protected function addDictionaryInformation()
88
    {
89 1
        $translations = array();
90 1
        $this->getDictionaryInformation($this->manialink, $translations);
91 1
        $this->dictionary->removeAllEntries();
92
93 1
        foreach ($translations as $msgId => $messages) {
94 1
            foreach ($messages as $message) {
95 1
                $this->dictionary->setEntry($message['Lang'], $msgId, htmlspecialchars($message['Text']));
96
            }
97
        }
98 1
    }
99
100
    /**
101
     * Recursive search all dome tree in order to find all translatable labels.
102
     *
103
     * @param $frame
104
     * @param $translations
105
     */
106 1
    protected function getDictionaryInformation($frame, &$translations)
107
    {
108 1
        foreach ($frame->getChildren() as $child) {
109 1
            if ($child instanceof Label && $child->getTranslate()) {
110 1
                $textId = 'exp_' . md5($child->getTextId());
111 1
                $translations[$textId] = $this->translationHelper->getTranslations($child->getTextId(), []);
112
113
                // Replaces with text id that can be used in the xml.
114 1
                $child->setTextId($textId);
115 1
            } else if ($child instanceof Frame) {
116 1
                $this->getDictionaryInformation($child, $translations);
117
            }
118
        }
119 1
    }
120
121
    /**
122
     * Get the children
123
     *
124
     * @api
125
     * @return Renderable[]
126
     */
127 1
    public function getChildren()
128
    {
129 1
        return $this->contentFrame->getChildren();
130
    }
131
132
    /**
133
     * Add a new child
134
     *
135
     * @api
136
     *
137
     * @param Renderable $child Child Control to add
138
     *
139
     * @return static
140
     */
141 1
    public function addChild(Renderable $child)
142
    {
143 1
        $this->contentFrame->addChild($child);
144
145 1
        return $this;
146
    }
147
148
    /**
149
     * Add a new child
150
     *
151
     * @api
152
     *
153
     * @param Renderable $child Child Control to add
154
     *
155
     * @return static
156
     * @deprecated Use addChild()
157
     * @see        Container::addChild()
158
     */
159 1
    public function add(Renderable $child)
160
    {
161 1
        $this->contentFrame->addChild($child);
162
163 1
        return $this;
164
    }
165
166
    /**
167
     * Add new children
168
     *
169
     * @api
170
     *
171
     * @param Renderable[] $children Child Controls to add
172
     *
173
     * @return static
174
     */
175 1
    public function addChildren(array $children)
176
    {
177 1
        $this->contentFrame->addChildren($children);
178
179 1
        return $this;
180
    }
181
182
    /**
183
     * Remove all children
184
     *
185
     * @api
186
     * @return static
187
     */
188 1
    public function removeAllChildren()
189
    {
190 1
        $this->contentFrame->removeAllChildren();
191
192 1
        return $this;
193
    }
194
195
    /**
196
     * Remove all children
197
     *
198
     * @api
199
     * @return static
200
     * @deprecated Use removeAllChildren()
201
     * @see        Container::removeAllChildren()
202
     */
203 1
    public function removeChildren()
204
    {
205 1
        $this->contentFrame->removeAllChildren();
206
207 1
        return $this;
208
    }
209
210
    /**
211
     * Get the Format
212
     *
213
     * @api
214
     *
215
     * @param bool $createIfEmpty If the format should be created if it doesn't exist yet
216
     *
217
     * @return Format
218
     * @deprecated Use Style
219
     * @see        Style
220
     */
221 1
    public function getFormat($createIfEmpty = true)
222
    {
223 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...
224
    }
225
226
    /**
227
     * Set the Format
228
     *
229
     * @api
230
     *
231
     * @param Format $format New Format
232
     *
233
     * @return static
234
     * @deprecated Use Style
235
     * @see        Style
236
     */
237 1
    public function setFormat(Format $format = null)
238
    {
239 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...
240
    }
241
242
    /**
243
     * @return Frame
244
     */
245
    public function getContentFrame()
246
    {
247
        return $this->contentFrame;
248
    }
249
}
250