Time.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Form time element
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Form\Element;
  12. use Magento\Framework\Escaper;
  13. class Time extends AbstractElement
  14. {
  15. /**
  16. * @param Factory $factoryElement
  17. * @param CollectionFactory $factoryCollection
  18. * @param Escaper $escaper
  19. * @param array $data
  20. */
  21. public function __construct(
  22. Factory $factoryElement,
  23. CollectionFactory $factoryCollection,
  24. Escaper $escaper,
  25. $data = []
  26. ) {
  27. parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
  28. $this->setType('time');
  29. }
  30. /**
  31. * @return mixed
  32. */
  33. public function getName()
  34. {
  35. $name = parent::getName();
  36. if (strpos($name, '[]') === false) {
  37. $name .= '[]';
  38. }
  39. return $name;
  40. }
  41. /**
  42. * @return string
  43. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  44. * @SuppressWarnings(PHPMD.NPathComplexity)
  45. */
  46. public function getElementHtml()
  47. {
  48. $this->addClass('select admin__control-select');
  49. $valueHrs = 0;
  50. $valueMin = 0;
  51. $valueSec = 0;
  52. if ($value = $this->getValue()) {
  53. $values = explode(',', $value);
  54. if (is_array($values) && count($values) == 3) {
  55. $valueHrs = $values[0];
  56. $valueMin = $values[1];
  57. $valueSec = $values[2];
  58. }
  59. }
  60. $html = '<input type="hidden" id="' . $this->getHtmlId() . '" ' . $this->_getUiId() . '/>';
  61. $html .= '<select name="' . $this->getName() . '" style="width:80px" '
  62. . $this->serialize($this->getHtmlAttributes())
  63. . $this->_getUiId('hour') . '>' . "\n";
  64. for ($i = 0; $i < 24; $i++) {
  65. $hour = str_pad($i, 2, '0', STR_PAD_LEFT);
  66. $html .= '<option value="' . $hour . '" ' . ($valueHrs ==
  67. $i ? 'selected="selected"' : '') . '>' . $hour . '</option>';
  68. }
  69. $html .= '</select>' . "\n";
  70. $html .= ':&nbsp;<select name="' . $this->getName() . '" style="width:80px" '
  71. . $this->serialize($this->getHtmlAttributes())
  72. . $this->_getUiId('minute') . '>' . "\n";
  73. for ($i = 0; $i < 60; $i++) {
  74. $hour = str_pad($i, 2, '0', STR_PAD_LEFT);
  75. $html .= '<option value="' . $hour . '" ' . ($valueMin ==
  76. $i ? 'selected="selected"' : '') . '>' . $hour . '</option>';
  77. }
  78. $html .= '</select>' . "\n";
  79. $html .= ':&nbsp;<select name="' . $this->getName() . '" style="width:80px" '
  80. . $this->serialize($this->getHtmlAttributes())
  81. . $this->_getUiId('second') . '>' . "\n";
  82. for ($i = 0; $i < 60; $i++) {
  83. $hour = str_pad($i, 2, '0', STR_PAD_LEFT);
  84. $html .= '<option value="' . $hour . '" ' . ($valueSec ==
  85. $i ? 'selected="selected"' : '') . '>' . $hour . '</option>';
  86. }
  87. $html .= '</select>' . "\n";
  88. $html .= $this->getAfterElementHtml();
  89. return $html;
  90. }
  91. }