1+ <?php
2+ /**
3+ * Artbambou SmileCustomEntityWidget Module
4+ *
5+ * @category Artbambou
6+ * @package Artbambou_ElasticsuiteStock
7+ * @author Ilan Parmentier
8+ */
9+ declare (strict_types=1 );
10+
11+ namespace Artbambou \SmileCustomEntityWidget \Block \Adminhtml \Set ;
12+
13+ use Magento \Backend \Block \Template \Context ;
14+ use Magento \Backend \Helper \Data ;
15+ use Magento \Backend \Block \Widget \Grid \Extended ;
16+ use Magento \Eav \Model \Config ;
17+ use Smile \CustomEntity \Api \Data \CustomEntityAttributeInterface ;
18+
19+ /**
20+ * Attribute Set Chooser
21+ */
22+ class Chooser extends Extended
23+ {
24+ /**
25+ * @var \Magento\Eav\Model\Config
26+ */
27+ private $ eavConfig ;
28+
29+ /**
30+ * @var \Magento\Eav\Api\AttributeSetRepositoryInterface
31+ */
32+ protected $ attributeSetRepository ;
33+
34+ /**
35+ * @param Context $context
36+ * @param Data $backendHelper
37+ * @param Magento\Eav\Model\Config $eavConfig
38+ * @param Magento\Eav\Api\AttributeSetRepositoryInterface $attributeSetRepository
39+ * @param array $data
40+ */
41+ public function __construct (
42+ Context $ context ,
43+ Data $ backendHelper ,
44+ \Magento \Eav \Model \Config $ eavConfig ,
45+ \Magento \Eav \Api \AttributeSetRepositoryInterface $ attributeSetRepository ,
46+ array $ data = []
47+ ) {
48+ $ this ->eavConfig = $ eavConfig ;
49+ $ this ->attributeSetRepository = $ attributeSetRepository ;
50+
51+ parent ::__construct ($ context , $ backendHelper , $ data );
52+ }
53+
54+ /**
55+ * Block construction, prepare grid params
56+ *
57+ * @return void
58+ */
59+ protected function _construct ()
60+ {
61+ parent ::_construct ();
62+ $ this ->setDefaultSort ('attribute_set_id ' );
63+ $ this ->setDefaultDir ('DESC ' );
64+ $ this ->setUseAjax (true );
65+ $ this ->setDefaultFilter (['chooser_is_active ' => '1 ' ]);
66+ }
67+
68+ /**
69+ * Prepare chooser element HTML
70+ *
71+ * @param \Magento\Framework\Data\Form\Element\AbstractElement $element Form Element
72+ * @return \Magento\Framework\Data\Form\Element\AbstractElement
73+ */
74+ public function prepareElementHtml (\Magento \Framework \Data \Form \Element \AbstractElement $ element )
75+ {
76+ $ uniqId = $ this ->mathRandom ->getUniqueHash ($ element ->getId ());
77+ $ sourceUrl = $ this ->getUrl ('custom_entity_widget/set/chooser ' , ['uniq_id ' => $ uniqId ]);
78+
79+ $ chooser = $ this ->getLayout ()->createBlock (
80+ \Magento \Widget \Block \Adminhtml \Widget \Chooser::class
81+ )->setElement (
82+ $ element
83+ )->setConfig (
84+ $ this ->getConfig ()
85+ )->setFieldsetId (
86+ $ this ->getFieldsetId ()
87+ )->setSourceUrl (
88+ $ sourceUrl
89+ )->setUniqId (
90+ $ uniqId
91+ );
92+
93+ if ($ element ->getValue ()) {
94+ $ attributeSet = $ this ->attributeSetRepository ->get ((int ) $ element ->getValue ());
95+ if ($ attributeSet ->getId ()) {
96+ $ chooser ->setLabel ($ this ->escapeHtml ($ attributeSet ->getAttributeSetName ()));
97+ }
98+ }
99+
100+ $ element ->setData ('after_element_html ' , $ chooser ->toHtml ());
101+ return $ element ;
102+ }
103+
104+ /**
105+ * Grid Row JS Callback
106+ *
107+ * @return string
108+ */
109+ public function getRowClickCallback ()
110+ {
111+ $ chooserJsObject = $ this ->getId ();
112+
113+ $ js = '
114+ function (grid, event) {
115+
116+ var trElement = Event.findElement(event, "tr");
117+ var customEntitySetTitle = trElement.down("td").next().innerHTML;
118+ var customEntitySetId = trElement.down("td").innerHTML.replace(/^\s+|\s+$/g,"");
119+
120+ ' . $ chooserJsObject . '.setElementValue(customEntitySetId);
121+ ' . $ chooserJsObject . '.setElementLabel(customEntitySetTitle);
122+ ' . $ chooserJsObject . '.close();
123+ }
124+ ' ;
125+ return $ js ;
126+ }
127+
128+ /**
129+ * Prepare pages collection
130+ *
131+ * @return \Magento\Backend\Block\Widget\Grid\Extended
132+ */
133+ protected function _prepareCollection ()
134+ {
135+ $ entityTypeId = CustomEntityAttributeInterface::ENTITY_TYPE_CODE ;
136+ $ entityType = $ this ->eavConfig ->getEntityType ($ entityTypeId );
137+ $ this ->setCollection ($ entityType ->getAttributeSetCollection ());
138+ return parent ::_prepareCollection ();
139+ }
140+
141+ /**
142+ * Prepare columns for slider grid
143+ *
144+ * @return $this
145+ */
146+ protected function _prepareColumns ()
147+ {
148+ $ this ->addColumn (
149+ 'chooser_id ' ,
150+ [
151+ 'header ' => __ ('ID ' ),
152+ 'index ' => 'attribute_set_id ' ,
153+ 'header_css_class ' => 'col-id ' ,
154+ 'column_css_class ' => 'col-id '
155+ ]
156+ );
157+
158+ $ this ->addColumn (
159+ 'chooser_title ' ,
160+ [
161+ 'header ' => __ ('Title ' ),
162+ 'index ' => 'attribute_set_name ' ,
163+ 'header_css_class ' => 'col-title ' ,
164+ 'column_css_class ' => 'col-title '
165+ ]
166+ );
167+
168+ /**
169+ * Check is single store mode
170+ */
171+ if (!$ this ->_storeManager ->isSingleStoreMode ()) {
172+ $ this ->addColumn (
173+ 'store_id ' ,
174+ [
175+ 'header ' => __ ('Store View ' ),
176+ 'index ' => 'store_id ' ,
177+ 'type ' => 'store ' ,
178+ 'store_all ' => true ,
179+ 'store_view ' => true ,
180+ 'sortable ' => false ,
181+ 'filter_condition_callback ' => [$ this , '_filterStoreCondition ' ]
182+ ]
183+ );
184+ }
185+
186+ return parent ::_prepareColumns ();
187+ }
188+
189+ /**
190+ * Filter store condition
191+ *
192+ * @param \Magento\Framework\Data\Collection $collection
193+ * @param \Magento\Framework\DataObject $column
194+ * @return void
195+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
196+ */
197+ protected function _filterStoreCondition ($ collection , \Magento \Framework \DataObject $ column )
198+ {
199+ if (!($ value = $ column ->getFilter ()->getValue ())) {
200+ return ;
201+ }
202+
203+ $ this ->getCollection ()->addStoreFilter ($ value );
204+ }
205+
206+ /**
207+ * Get grid url
208+ *
209+ * @return string
210+ */
211+ public function getGridUrl ()
212+ {
213+ return $ this ->getUrl (
214+ 'custom_entity_widget/set/chooser ' ,
215+ [
216+ '_current ' => true ,
217+ 'uniq_id ' => $ this ->getId ()
218+ ]
219+ );
220+ }
221+ }
0 commit comments