5
5
*/
6
6
namespace Magento \Catalog \Block \Product \View \Options \Type ;
7
7
8
+ use DateTimeZone ;
9
+ use Magento \Framework \App \ObjectManager ;
10
+ use Magento \Framework \Data \Form \FilterFactory ;
11
+ use Magento \Framework \Stdlib \DateTime ;
12
+
8
13
/**
9
14
* Product options text type block
10
15
*
@@ -27,22 +32,30 @@ class Date extends \Magento\Catalog\Block\Product\View\Options\AbstractOptions
27
32
*/
28
33
protected $ _catalogProductOptionTypeDate ;
29
34
35
+ /**
36
+ * @var FilterFactory
37
+ */
38
+ private $ filterFactory ;
39
+
30
40
/**
31
41
* @param \Magento\Framework\View\Element\Template\Context $context
32
42
* @param \Magento\Framework\Pricing\Helper\Data $pricingHelper
33
43
* @param \Magento\Catalog\Helper\Data $catalogData
34
44
* @param \Magento\Catalog\Model\Product\Option\Type\Date $catalogProductOptionTypeDate
35
45
* @param array $data
46
+ * @param FilterFactory|null $filterFactory
36
47
*/
37
48
public function __construct (
38
49
\Magento \Framework \View \Element \Template \Context $ context ,
39
50
\Magento \Framework \Pricing \Helper \Data $ pricingHelper ,
40
51
\Magento \Catalog \Helper \Data $ catalogData ,
41
52
\Magento \Catalog \Model \Product \Option \Type \Date $ catalogProductOptionTypeDate ,
42
- array $ data = []
53
+ array $ data = [],
54
+ ?FilterFactory $ filterFactory = null
43
55
) {
44
56
$ this ->_catalogProductOptionTypeDate = $ catalogProductOptionTypeDate ;
45
57
parent ::__construct ($ context , $ pricingHelper , $ catalogData , $ data );
58
+ $ this ->filterFactory = $ filterFactory ?? ObjectManager::getInstance ()->get (FilterFactory::class);
46
59
}
47
60
48
61
/**
@@ -77,14 +90,24 @@ public function getDateHtml()
77
90
public function getCalendarDateHtml ()
78
91
{
79
92
$ option = $ this ->getOption ();
80
- $ value = $ this ->getProduct ()->getPreconfiguredValues ()->getData ('options/ ' . $ option ->getId () . ' /date ' );
93
+ $ values = $ this ->getProduct ()->getPreconfiguredValues ()->getData ('options/ ' . $ option ->getId ());
81
94
82
95
$ yearStart = $ this ->_catalogProductOptionTypeDate ->getYearStart ();
83
96
$ yearEnd = $ this ->_catalogProductOptionTypeDate ->getYearEnd ();
84
97
85
- $ dateFormat = $ this ->_localeDate ->getDateFormat (\IntlDateFormatter:: SHORT );
98
+ $ dateFormat = $ this ->_localeDate ->getDateFormatWithLongYear ( );
86
99
/** Escape RTL characters which are present in some locales and corrupt formatting */
87
100
$ escapedDateFormat = preg_replace ('/[^MmDdYy\/\.\-]/ ' , '' , $ dateFormat );
101
+ $ value = null ;
102
+ if (is_array ($ values )) {
103
+ $ date = $ this ->getInternalDateString ($ values );
104
+ if ($ date !== null ) {
105
+ $ dateFilter = $ this ->filterFactory ->create ('date ' , ['format ' => $ escapedDateFormat ]);
106
+ $ value = $ dateFilter ->outputFilter ($ date );
107
+ } elseif (isset ($ values ['date ' ])) {
108
+ $ value = $ values ['date ' ];
109
+ }
110
+ }
88
111
$ calendar = $ this ->getLayout ()->createBlock (
89
112
\Magento \Framework \View \Element \Html \Date::class
90
113
)->setId (
@@ -158,8 +181,8 @@ public function getTimeHtml()
158
181
* Return drop-down html with range of values
159
182
*
160
183
* @param string $name Id/name of html select element
161
- * @param int $from Start position
162
- * @param int $to End position
184
+ * @param int $from Start position
185
+ * @param int $to End position
163
186
* @param int|null $value Value selected
164
187
* @return string Formatted Html
165
188
*/
@@ -209,9 +232,8 @@ protected function _getHtmlSelect($name, $value = null)
209
232
210
233
$ select ->setExtraParams ($ extraParams );
211
234
if ($ value === null ) {
212
- $ value = $ this ->getProduct ()->getPreconfiguredValues ()->getData (
213
- 'options/ ' . $ option ->getId () . '/ ' . $ name
214
- );
235
+ $ values = $ this ->getProduct ()->getPreconfiguredValues ()->getData ('options/ ' . $ option ->getId ());
236
+ $ value = is_array ($ values ) ? $ this ->parseDate ($ values , $ name ) : null ;
215
237
}
216
238
if ($ value !== null ) {
217
239
$ select ->setValue ($ value );
@@ -233,4 +255,56 @@ protected function _getValueWithLeadingZeros($value)
233
255
}
234
256
return $ value < 10 ? '0 ' . $ value : $ value ;
235
257
}
258
+
259
+ /**
260
+ * Get internal date format of provided value
261
+ *
262
+ * @param array $value
263
+ * @return string|null
264
+ */
265
+ private function getInternalDateString (array $ value ): ?string
266
+ {
267
+ $ result = null ;
268
+ if (!empty ($ value ['date ' ]) && !empty ($ value ['date_internal ' ])) {
269
+ $ dateTimeZone = new DateTimeZone ($ this ->_localeDate ->getConfigTimezone ());
270
+ $ dateTimeObject = date_create_from_format (
271
+ DateTime::DATETIME_PHP_FORMAT ,
272
+ $ value ['date_internal ' ],
273
+ $ dateTimeZone
274
+ );
275
+ if ($ dateTimeObject !== false ) {
276
+ $ result = $ dateTimeObject ->format (DateTime::DATE_PHP_FORMAT );
277
+ }
278
+ } elseif (!empty ($ value ['day ' ]) && !empty ($ value ['month ' ]) && !empty ($ value ['year ' ])) {
279
+ $ dateTimeObject = $ this ->_localeDate ->date ();
280
+ $ dateTimeObject ->setDate ((int ) $ value ['year ' ], (int ) $ value ['month ' ], (int ) $ value ['day ' ]);
281
+ $ result = $ dateTimeObject ->format (DateTime::DATE_PHP_FORMAT );
282
+ }
283
+ return $ result ;
284
+ }
285
+
286
+ /**
287
+ * Parse option value and return the requested part
288
+ *
289
+ * @param array $value
290
+ * @param string $part [year, month, day, hour, minute, day_part]
291
+ * @return string|null
292
+ */
293
+ private function parseDate (array $ value , string $ part ): ?string
294
+ {
295
+ $ result = null ;
296
+ if (!empty ($ value ['date ' ]) && !empty ($ value ['date_internal ' ])) {
297
+ $ formatDate = explode (' ' , $ value ['date_internal ' ]);
298
+ $ date = explode ('- ' , $ formatDate [0 ]);
299
+ $ value ['year ' ] = $ date [0 ];
300
+ $ value ['month ' ] = $ date [1 ];
301
+ $ value ['day ' ] = $ date [2 ];
302
+ }
303
+
304
+ if (isset ($ value [$ part ])) {
305
+ $ result = (string ) $ value [$ part ];
306
+ }
307
+
308
+ return $ result ;
309
+ }
236
310
}
0 commit comments