protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element) { $html = "<input class=' input-text' type='hidden' id='" . $element->getHtmlId() . "' name='" . $element->getName() . "' value='" . $element->getEscapedValue() . "' '" . $element->serialize($element->getHtmlAttributes()) . "/>"; $html .= "<table cellpadding='2' style='width:600px !important;width:auto;' class='wl-cron'>\n <thead> \n <tr><th>Days of the week</th><th width='20'></th><th colspan='4'>Hours of the day</th></tr>\n </thead>\n <tr>\n <td width='300' align='left'>\n <div>" . $this->checkbox("Monday", "d-Monday", "Monday", 'd') . "</div>\n <div>" . $this->checkbox("Tuesday", "d-Tuesday", "Tuesday", 'd') . "</div>\n <div>" . $this->checkbox("Wednesday", "d-Wednesday", "Wednesday", 'd') . "</div>\n <div>" . $this->checkbox("Thursday", "d-Thursday", "Thurday", 'd') . "</div>\n <div>" . $this->checkbox("Friday", "d-Friday", "Friday", 'd') . "</div>\n <div>" . $this->checkbox("Saturday", "d-Saturday", "Saturday", 'd') . "</div>\n <div>" . $this->checkbox("Sunday", "d-Sunday", "Sunday", 'd') . "</div>\n </td>\n <td width='175' class='morning-half'>\n <div>" . $this->checkbox("00:00", "h-0000", "00:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("01:00", "h-0100", "01:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("02:00", "h-0200", "02:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("03:00", "h-0300", "03:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("04:00", "h-0400", "04:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("05:00", "h-0500", "05:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("06:00", "h-0600", "06:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("07:00", "h-0700", "07:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("08:00", "h-0800", "08:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("09:00", "h-0900", "09:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("10:00", "h-1000", "10:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("11:00", "h-1100", "11:00 AM", 'h') . "</div>\n </td>\n <td width='175' class='morning'>\n <div>" . $this->checkbox("00:30", "h-0030", "00:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("01:30", "h-0130", "01:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("02:30", "h-0230", "02:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("03:30", "h-0330", "03:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("04:30", "h-0430", "04:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("05:30", "h-0530", "05:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("06:30", "h-0630", "06:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("07:30", "h-0730", "07:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("08:30", "h-0830", "08:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("09:30", "h-0930", "09:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("10:30", "h-1030", "10:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("11:30", "h-1130", "11:30 AM", 'h') . "</div>\n </td>\n <td width='175' class='afternoon-half'>\n <div>" . $this->checkbox("12:00", "h-1200", "12:00 AM", 'h') . "</div>\n <div>" . $this->checkbox("13:00", "h-1300", "01:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("14:00", "h-1400", "02:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("15:00", "h-1500", "03:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("16:00", "h-1600", "04:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("17:00", "h-1700", "05:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("18:00", "h-1800", "06:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("19:00", "h-1900", "07:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("20:00", "h-2000", "08:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("21:00", "h-2100", "09:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("22:00", "h-2200", "10:00 PM", 'h') . "</div>\n <div>" . $this->checkbox("23:00", "h-2300", "11:00 PM", 'h') . "</div>\n </td>\n <td width='175' class='afternoon'>\n <div>" . $this->checkbox("12:30", "h-1230", "12:30 AM", 'h') . "</div>\n <div>" . $this->checkbox("13:30", "h-1330", "01:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("14:30", "h-1430", "02:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("15:30", "h-1530", "03:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("16:30", "h-1630", "04:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("17:30", "h-1730", "05:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("18:30", "h-1830", "06:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("19:30", "h-1930", "07:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("20:30", "h-2030", "08:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("21:30", "h-2130", "09:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("22:30", "h-2230", "10:30 PM", 'h') . "</div>\n <div>" . $this->checkbox("23:30", "h-2330", "11:30 PM", 'h') . "</div>\n </td>\n </tr>\n </table>"; $html .= "<script>\nrequire([\n 'jquery',\n 'mage/mage'\n], function (\$) {\n \$(function () {\n jQuery(document).ready(function () {\n\n if (jQuery('#" . $element->getHtmlId() . "').length > 0) {\n\n if (jQuery('#" . $element->getHtmlId() . "').val() === '') {\n jQuery('#" . $element->getHtmlId() . "').val('{\"days\":[],\"hours\":[]}');\n }\n var cron = jQuery.parseJSON(jQuery('#" . $element->getHtmlId() . "').val());\n\n for (var i = 0; i < cron.days.length; i++) {\n if (jQuery('#d-' + cron.days[i])) {\n jQuery('#d-' + cron.days[i]).prop('checked', true);\n }\n }\n\n for (var i = 0; i < cron.hours.length; i++) {\n if (jQuery('#h-' + cron.hours[i].replace(':', ''))) {\n jQuery('#h-' + cron.hours[i].replace(':', '')).prop('checked', true);\n }\n }\n\n jQuery('.cron-box').on('click', function () {\n var d = new Array();\n jQuery('.cron-d-box').each(function () {\n if (jQuery(this).prop('checked')) {\n d.push(jQuery(this).val());\n }\n });\n var h = new Array();\n jQuery('.cron-h-box').each(function () {\n if (jQuery(this).prop('checked')) {\n h.push(jQuery(this).val());\n }\n });\n jQuery('#" . $element->getHtmlId() . "').val(Object.toJSON({days: d, hours: h}));\n });\n }\n });\n });\n});\n</script>"; $html .= $element->getAfterElementHtml(); return $html; }
/** * @param array $initialData * @param string $expectedValue * @dataProvider serializeDataProvider * @covers \Magento\Framework\Data\Form\Element\AbstractElement::serialize() */ public function testSerialize(array $initialData, $expectedValue) { $attributes = []; if (isset($initialData['attributes'])) { $attributes = $initialData['attributes']; unset($initialData['attributes']); } $this->_model->setData($initialData); $this->assertEquals($expectedValue, $this->_model->serialize($attributes)); }
/** * @param AbstractElement $element * @return string * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function render(AbstractElement $element) { $countryId = false; $isRegionRequired = false; if ($country = $element->getForm()->getElement('country_id')) { $countryId = $country->getValue(); $isRegionRequired = $this->_directoryHelper->isRegionRequired($countryId); } $html = '<div class="field field-region ' . ($isRegionRequired ? 'required' : '') . '">' . "\n"; $regionCollection = false; if ($countryId) { if (!isset(self::$_regionCollections[$countryId])) { self::$_regionCollections[$countryId] = $this->_countryFactory->create()->setId($countryId)->getLoadedRegionCollection()->toOptionArray(); } $regionCollection = self::$_regionCollections[$countryId]; } $regionId = intval($element->getForm()->getElement('region_id')->getValue()); $htmlAttributes = $element->getHtmlAttributes(); foreach ($htmlAttributes as $key => $attribute) { if ('type' === $attribute) { unset($htmlAttributes[$key]); break; } } // Output two elements - for 'region' and for 'region_id'. // Two elements are needed later upon form post - to properly set data to address model, // otherwise old value can be left in region_id attribute and saved to DB. // Depending on country selected either 'region' (input text) or 'region_id' (selectbox) is visible to user $regionHtmlName = $element->getName(); $regionIdHtmlName = str_replace('region', 'region_id', $regionHtmlName); $regionHtmlId = $element->getHtmlId(); $regionIdHtmlId = str_replace('region', 'region_id', $regionHtmlId); if ($isRegionRequired) { $element->addClass('required-entry'); } if ($regionCollection && count($regionCollection) > 0) { $elementClass = $element->getClass(); $html .= '<label class="label" for="' . $regionIdHtmlId . '"><span>' . $element->getLabel() . '</span>' . '</label>'; $html .= '<div class="control">'; $html .= '<select id="' . $regionIdHtmlId . '" name="' . $regionIdHtmlName . '" ' . $element->serialize($htmlAttributes) . '>' . "\n"; foreach ($regionCollection as $region) { $selected = $regionId == $region['value'] ? ' selected="selected"' : ''; $regionVal = 0 == $region['value'] ? '' : (int) $region['value']; $html .= '<option value="' . $regionVal . '"' . $selected . '>' . $this->_escaper->escapeHtml(__($region['label'])) . '</option>'; } $html .= '</select>' . "\n"; $html .= '<input type="hidden" name="' . $regionHtmlName . '" id="' . $regionHtmlId . '" value=""/>'; $html .= '</div>'; $element->setClass($elementClass); } else { $html .= '<label class="label" for="' . $regionHtmlId . '"><span>' . $element->getLabel() . '</span></label>'; $html .= '<div class="control">'; $html .= '<input id="' . $regionHtmlId . '" name="' . $regionHtmlName . '" value="' . $element->getEscapedValue() . '" ' . $element->serialize($htmlAttributes) . "/>" . "\n"; $html .= '<input type="hidden" name="' . $regionIdHtmlName . '" id="' . $regionIdHtmlId . '" value=""/>'; $html .= '</div>' . "\n"; } $html .= '</div>' . "\n"; return $html; }