Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
created

AdminBundle/Helper/FormWidgets/Tabs/TabPane.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\AdminBundle\Helper\FormWidgets\Tabs;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
7
use Symfony\Component\Form\Extension\Core\Type\FormType;
8
use Symfony\Component\Form\Form;
9
use Symfony\Component\Form\FormFactoryInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\Form\FormView;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * A tab pane is a container which holds tabs
16
 */
17
class TabPane
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $identifier;
23
24
    /**
25
     * @var TabInterface[]
26
     */
27
    protected $tabs = array();
28
29
    /**
30
     * @var string
31
     */
32
    protected $activeTab;
33
34
    /**
35
     * @var FormFactoryInterface
36
     */
37
    protected $formFactory;
38
39
    /**
40
     * @var Form
41
     */
42
    protected $form;
43
44
    /**
45
     * @var FormView
46
     */
47
    protected $formView;
48
49
    /**
50
     * @param string               $identifier  The identifier
51
     * @param Request              $request     The request
52
     * @param FormFactoryInterface $formFactory The form factory
53
     */
54 2
    public function __construct($identifier, Request $request, FormFactoryInterface $formFactory)
55
    {
56 2
        $this->identifier = $identifier;
57 2
        $this->formFactory = $formFactory;
58
59 2
        $this->slugifier = new Slugifier();
0 ignored issues
show
The property slugifier 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...
60 2
        if ($request->request->get('currenttab')) {
61 1
            $this->activeTab = $request->request->get('currenttab');
62 2
        } elseif ($request->get('currenttab')) {
63 1
            $this->activeTab = $request->get('currenttab');
64
        }
65 2
    }
66
67
    /**
68
     * @return FormInterface
69
     */
70 1
    public function buildForm()
71
    {
72 1
        $builder = $this->formFactory->createBuilder(FormType::class, null);
73
74 1
        foreach ($this->tabs as $tab) {
75 1
            $tab->buildForm($builder);
76
        }
77
78 1
        $this->form = $builder->getForm();
79
80 1
        return $this->form;
81
    }
82
83
    /**
84
     * @param Request $request
85
     */
86 1
    public function bindRequest(Request $request)
87
    {
88 1
        $this->form->handleRequest($request);
89
90 1
        foreach ($this->tabs as $tab) {
91 1
            $tab->bindRequest($request);
92
        }
93 1
    }
94
95
    /**
96
     * @param EntityManager $em The entity manager
97
     */
98 1
    public function persist(EntityManager $em)
99
    {
100 1
        foreach ($this->tabs as $tab) {
101 1
            $tab->persist($em);
102
        }
103 1
    }
104
105
    /**
106
     * @param TabInterface $tab
107
     *
108
     * @return string
109
     */
110 2
    private function generateIdentifier(TabInterface $tab)
111
    {
112 2
        return $this->slugifier->slugify($tab->getTitle());
113
    }
114
115
    /**
116
     * @param TabInterface $tab      The tab
117
     * @param null|int     $position The position
118
     *
119
     * @return TabPane
120
     */
121 2
    public function addTab(TabInterface $tab, $position = null)
122
    {
123 2
        $identifier = $tab->getIdentifier();
124 2
        if (!$identifier || empty($identifier)) {
125 2
            $tab->setIdentifier($this->generateIdentifier($tab));
126
        }
127
128 2
        if (!\is_null($position) && is_numeric($position) && $position < \count($this->tabs)) {
129 1
            array_splice($this->tabs, $position, 0, array($tab));
130
        } else {
131 2
            $this->tabs[] = $tab;
132
        }
133
134 2
        return $this;
135
    }
136
137
    /**
138
     * @param TabInterface $tab
139
     *
140
     * @return TabPane
141
     */
142 1
    public function removeTab(TabInterface $tab)
143
    {
144 1
        if (\in_array($tab, $this->tabs)) {
145 1
            unset($this->tabs[array_search($tab, $this->tabs)]);
146 1
            $this->reindexTabs();
147
        }
148
149 1
        return $this;
150
    }
151
152
    /**
153
     * @param string $title
154
     *
155
     * @return TabPane
156
     */
157 1
    public function removeTabByTitle($title)
158
    {
159 1
        foreach ($this->tabs as $key => $tab) {
160 1
            if ($tab->getTitle() === $title) {
161 1
                unset($this->tabs[$key]);
162 1
                $this->reindexTabs();
163
164 1
                return $this;
165
            }
166
        }
167
168 1
        return $this;
169
    }
170
171
    /**
172
     * @param int $position
173
     *
174
     * @return TabPane
175
     */
176 1 View Code Duplication
    public function removeTabByPosition($position)
177
    {
178 1
        if (is_numeric($position) && $position < \count($this->tabs)) {
179 1
            array_splice($this->tabs, $position, 1);
180
        }
181
182 1
        return $this;
183
    }
184
185
    /**
186
     * @return TabInterface[]
187
     */
188 2
    public function getTabs()
189
    {
190 2
        return $this->tabs;
191
    }
192
193
    /**
194
     * @param string $title
195
     *
196
     * @return TabInterface|null
197
     */
198 1
    public function getTabByTitle($title)
199
    {
200 1
        foreach ($this->tabs as $key => $tab) {
201 1
            if ($tab->getTitle() === $title) {
202 1
                return $this->tabs[$key];
203
            }
204
        }
205
206 1
        return null;
207
    }
208
209
    /**
210
     * @param int $position
211
     *
212
     * @return TabInterface|null
213
     */
214 1 View Code Duplication
    public function getTabByPosition($position)
215
    {
216 1
        if (is_numeric($position) && $position < \count($this->tabs)) {
217 1
            return $this->tabs[$position];
218
        }
219
220 1
        return null;
221
    }
222
223
    /**
224
     * @return string
225
     */
226 1
    public function getActiveTab()
227
    {
228 1
        return !empty($this->activeTab) ? $this->activeTab : $this->tabs[0]->getIdentifier();
229
    }
230
231
    /**
232
     * @return Form
233
     */
234 1
    public function getForm()
235
    {
236 1
        return $this->form;
237
    }
238
239
    /**
240
     * @return FormView
241
     */
242 1
    public function getFormView()
243
    {
244 1
        if (\is_null($this->formView)) {
245 1
            $this->formView = $this->form->createView();
246
        }
247
248 1
        return $this->formView;
249
    }
250
251
    /**
252
     * @return bool
253
     */
254 1
    public function isValid()
255
    {
256 1
        return $this->form->isValid();
257
    }
258
259
    /**
260
     * Reset the indexes of the tabs
261
     */
262 1
    private function reindexTabs()
263
    {
264 1
        $this->tabs = array_values($this->tabs);
265 1
    }
266
267
    /**
268
     * @param Request $request
269
     *
270
     * @return array
271
     */
272 1
    public function getExtraParams(Request $request)
273
    {
274 1
        $extraParams = array();
275 1
        foreach ($this->getTabs() as $tab) {
276 1
            $extraParams = array_merge($extraParams, $tab->getExtraParams($request));
277
        }
278
279 1
        return $extraParams;
280
    }
281
}
282