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 | public function __construct($identifier, Request $request, FormFactoryInterface $formFactory) |
||
55 | { |
||
56 | $this->identifier = $identifier; |
||
57 | $this->formFactory = $formFactory; |
||
58 | |||
59 | $this->slugifier = new Slugifier(); |
||
0 ignored issues
–
show
|
|||
60 | if ($request->request->get('currenttab')) { |
||
61 | $this->activeTab = $request->request->get('currenttab'); |
||
62 | } elseif ($request->get('currenttab')) { |
||
63 | $this->activeTab = $request->get('currenttab'); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @return FormInterface |
||
69 | */ |
||
70 | public function buildForm() |
||
71 | { |
||
72 | $builder = $this->formFactory->createBuilder(FormType::class, null); |
||
73 | |||
74 | foreach ($this->tabs as $tab) { |
||
75 | $tab->buildForm($builder); |
||
76 | } |
||
77 | |||
78 | $this->form = $builder->getForm(); |
||
79 | |||
80 | return $this->form; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param Request $request |
||
85 | */ |
||
86 | public function bindRequest(Request $request) |
||
87 | { |
||
88 | $this->form->handleRequest($request); |
||
89 | |||
90 | foreach ($this->tabs as $tab) { |
||
91 | $tab->bindRequest($request); |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param EntityManager $em The entity manager |
||
97 | */ |
||
98 | public function persist(EntityManager $em) |
||
99 | { |
||
100 | foreach ($this->tabs as $tab) { |
||
101 | $tab->persist($em); |
||
102 | } |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param TabInterface $tab |
||
107 | * |
||
108 | * @return string |
||
109 | */ |
||
110 | private function generateIdentifier(TabInterface $tab) |
||
111 | { |
||
112 | 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 | public function addTab(TabInterface $tab, $position = null) |
||
122 | { |
||
123 | $identifier = $tab->getIdentifier(); |
||
124 | if (!$identifier || empty($identifier)) { |
||
125 | $tab->setIdentifier($this->generateIdentifier($tab)); |
||
126 | } |
||
127 | |||
128 | if (!is_null($position) && is_numeric($position) && $position < count($this->tabs)) { |
||
129 | array_splice($this->tabs, $position, 0, array($tab)); |
||
130 | } else { |
||
131 | $this->tabs[] = $tab; |
||
132 | } |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param TabInterface $tab |
||
139 | * |
||
140 | * @return TabPane |
||
141 | */ |
||
142 | public function removeTab(TabInterface $tab) |
||
143 | { |
||
144 | if (in_array($tab, $this->tabs)) { |
||
145 | unset($this->tabs[array_search($tab, $this->tabs)]); |
||
146 | $this->reindexTabs(); |
||
147 | } |
||
148 | |||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param string $title |
||
154 | * |
||
155 | * @return TabPane |
||
156 | */ |
||
157 | public function removeTabByTitle($title) |
||
158 | { |
||
159 | foreach ($this->tabs as $key => $tab) { |
||
160 | if ($tab->getTitle() === $title) { |
||
161 | unset($this->tabs[$key]); |
||
162 | $this->reindexTabs(); |
||
163 | |||
164 | return $this; |
||
165 | } |
||
166 | } |
||
167 | |||
168 | return $this; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param int $position |
||
173 | * |
||
174 | * @return TabPane |
||
175 | */ |
||
176 | View Code Duplication | public function removeTabByPosition($position) |
|
177 | { |
||
178 | if (is_numeric($position) && $position < count($this->tabs)) { |
||
179 | array_splice($this->tabs, $position, 1); |
||
180 | } |
||
181 | |||
182 | return $this; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return TabInterface[] |
||
187 | */ |
||
188 | public function getTabs() |
||
189 | { |
||
190 | return $this->tabs; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @param string $title |
||
195 | * |
||
196 | * @return TabInterface|null |
||
197 | */ |
||
198 | public function getTabByTitle($title) |
||
199 | { |
||
200 | foreach ($this->tabs as $key => $tab) { |
||
201 | if ($tab->getTitle() === $title) { |
||
202 | return $this->tabs[$key]; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | return null; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @param int $position |
||
211 | * |
||
212 | * @return TabInterface|null |
||
213 | */ |
||
214 | View Code Duplication | public function getTabByPosition($position) |
|
215 | { |
||
216 | if (is_numeric($position) && $position < count($this->tabs)) { |
||
217 | return $this->tabs[$position]; |
||
218 | } |
||
219 | |||
220 | return null; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @return string |
||
225 | */ |
||
226 | public function getActiveTab() |
||
227 | { |
||
228 | return !empty($this->activeTab) ? $this->activeTab : $this->tabs[0]->getIdentifier(); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @return Form |
||
233 | */ |
||
234 | public function getForm() |
||
235 | { |
||
236 | return $this->form; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @return FormView |
||
241 | */ |
||
242 | public function getFormView() |
||
243 | { |
||
244 | if (is_null($this->formView)) { |
||
245 | $this->formView = $this->form->createView(); |
||
246 | } |
||
247 | |||
248 | return $this->formView; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return bool |
||
253 | */ |
||
254 | public function isValid() |
||
255 | { |
||
256 | return $this->form->isValid(); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Reset the indexes of the tabs |
||
261 | */ |
||
262 | private function reindexTabs() |
||
263 | { |
||
264 | $this->tabs = array_values($this->tabs); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param Request $request |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | public function getExtraParams(Request $request) |
||
273 | { |
||
274 | $extraParams = array(); |
||
275 | foreach ($this->getTabs() as $tab) { |
||
276 | $extraParams = array_merge($extraParams, $tab->getExtraParams($request)); |
||
277 | } |
||
278 | |||
279 | return $extraParams; |
||
280 | } |
||
281 | } |
||
282 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: