1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\CatalogBundle\Form\Admin; |
14
|
|
|
|
15
|
|
|
use WellCommerce\Bundle\CatalogBundle\Entity\Category; |
16
|
|
|
use WellCommerce\Bundle\CoreBundle\Form\AbstractFormBuilder; |
17
|
|
|
use WellCommerce\Component\Form\Elements\FormInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class CategoryForm |
21
|
|
|
* |
22
|
|
|
* @author Adam Piotrowski <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class CategoryFormBuilder extends AbstractFormBuilder |
25
|
|
|
{ |
26
|
|
|
public function getAlias(): string |
27
|
|
|
{ |
28
|
|
|
return 'admin.category'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function buildForm(FormInterface $form) |
32
|
|
|
{ |
33
|
|
|
$currentCategory = $this->getCurrentCategory(); |
34
|
|
|
|
35
|
|
|
$requiredData = $form->addChild($this->getElement('nested_fieldset', [ |
36
|
|
|
'name' => 'required_data', |
37
|
|
|
'label' => 'common.fieldset.general', |
38
|
|
|
])); |
39
|
|
|
|
40
|
|
|
$languageData = $requiredData->addChild($this->getElement('language_fieldset', [ |
41
|
|
|
'name' => 'translations', |
42
|
|
|
'label' => 'common.fieldset.translations', |
43
|
|
|
'transformer' => $this->getRepositoryTransformer('translation', $this->get('category.repository')), |
44
|
|
|
])); |
45
|
|
|
|
46
|
|
|
$name = $languageData->addChild($this->getElement('text_field', [ |
47
|
|
|
'name' => 'name', |
48
|
|
|
'label' => 'common.label.name', |
49
|
|
|
'rules' => [ |
50
|
|
|
$this->getRule('required'), |
51
|
|
|
], |
52
|
|
|
])); |
53
|
|
|
|
54
|
|
|
$languageData->addChild($this->getElement('slug_field', [ |
55
|
|
|
'name' => 'slug', |
56
|
|
|
'label' => 'category.label.slug', |
57
|
|
|
'name_field' => $name, |
58
|
|
|
'generate_route' => 'route.generate', |
59
|
|
|
'translatable_id' => $this->getRequestHelper()->getAttributesBagParam('id'), |
60
|
|
|
'rules' => [ |
61
|
|
|
$this->getRule('required'), |
62
|
|
|
], |
63
|
|
|
])); |
64
|
|
|
|
65
|
|
|
$requiredData->addChild($this->getElement('checkbox', [ |
66
|
|
|
'name' => 'enabled', |
67
|
|
|
'label' => 'category.label.enabled', |
68
|
|
|
'comment' => 'category.comment.enabled', |
69
|
|
|
])); |
70
|
|
|
|
71
|
|
|
$requiredData->addChild($this->getElement('text_field', [ |
72
|
|
|
'name' => 'hierarchy', |
73
|
|
|
'label' => 'common.label.hierarchy', |
74
|
|
|
'rules' => [ |
75
|
|
|
$this->getRule('required'), |
76
|
|
|
], |
77
|
|
|
])); |
78
|
|
|
|
79
|
|
|
$requiredData->addChild($this->getElement('text_field', [ |
80
|
|
|
'name' => 'symbol', |
81
|
|
|
'label' => 'common.label.symbol', |
82
|
|
|
])); |
83
|
|
|
|
84
|
|
|
$requiredData->addChild($this->getElement('tree', [ |
85
|
|
|
'name' => 'parent', |
86
|
|
|
'label' => 'category.label.parent', |
87
|
|
|
'choosable' => true, |
88
|
|
|
'selectable' => false, |
89
|
|
|
'sortable' => false, |
90
|
|
|
'clickable' => false, |
91
|
|
|
'items' => $this->getCategoryParentOptions($currentCategory), |
92
|
|
|
'restrict' => $currentCategory->getId(), |
93
|
|
|
'transformer' => $this->getRepositoryTransformer('entity', $this->get('category.repository')), |
94
|
|
|
])); |
95
|
|
|
|
96
|
|
|
$descriptionData = $form->addChild($this->getElement('nested_fieldset', [ |
97
|
|
|
'name' => 'description_data', |
98
|
|
|
'label' => 'category.form.fieldset.description', |
99
|
|
|
])); |
100
|
|
|
|
101
|
|
|
$languageData = $descriptionData->addChild($this->getElement('language_fieldset', [ |
102
|
|
|
'name' => 'translations', |
103
|
|
|
'label' => 'common.fieldset.translations', |
104
|
|
|
'transformer' => $this->getRepositoryTransformer('translation', $this->get('category.repository')), |
105
|
|
|
])); |
106
|
|
|
|
107
|
|
|
$languageData->addChild($this->getElement('rich_text_editor', [ |
108
|
|
|
'name' => 'shortDescription', |
109
|
|
|
'label' => 'common.label.short_description', |
110
|
|
|
])); |
111
|
|
|
|
112
|
|
|
$languageData->addChild($this->getElement('rich_text_editor', [ |
113
|
|
|
'name' => 'description', |
114
|
|
|
'label' => 'common.label.description', |
115
|
|
|
])); |
116
|
|
|
|
117
|
|
|
$mediaData = $form->addChild($this->getElement('nested_fieldset', [ |
118
|
|
|
'name' => 'media_data', |
119
|
|
|
'label' => 'common.fieldset.photos', |
120
|
|
|
])); |
121
|
|
|
|
122
|
|
|
$mediaData->addChild($this->getElement('image', [ |
123
|
|
|
'name' => 'photo', |
124
|
|
|
'label' => 'form.media_data.image_id', |
125
|
|
|
'repeat_min' => 0, |
126
|
|
|
'repeat_max' => 1, |
127
|
|
|
'transformer' => $this->getRepositoryTransformer('media_entity', $this->get('media.repository')), |
128
|
|
|
'session_id' => $this->getRequestHelper()->getSessionId(), |
129
|
|
|
'session_name' => $this->getRequestHelper()->getSessionName(), |
130
|
|
|
])); |
131
|
|
|
|
132
|
|
|
$this->addMetadataFieldset($form, $this->get('category.repository')); |
133
|
|
|
|
134
|
|
|
$this->addShopsFieldset($form); |
135
|
|
|
|
136
|
|
|
$form->addFilter($this->getFilter('trim')); |
137
|
|
|
$form->addFilter($this->getFilter('secure')); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
protected function getCategoryParentOptions(Category $category): array |
141
|
|
|
{ |
142
|
|
|
$options = $this->get('category.dataset.admin')->getResult('flat_tree', ['limit' => 10000]); |
143
|
|
|
|
144
|
|
|
if ($category->getParent() instanceof Category) { |
145
|
|
|
array_push($options, [ |
146
|
|
|
'id' => 0, |
147
|
|
|
'hierarchy' => 0, |
148
|
|
|
'parent' => null, |
149
|
|
|
'children_count' => 0, |
150
|
|
|
'products_count' => 0, |
151
|
|
|
'name' => $this->trans('category.label.empty_parent'), |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $options; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function getCurrentCategory(): Category |
159
|
|
|
{ |
160
|
|
|
return $this->get('category.storage')->getCurrentCategory(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|