DateTime.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ m78E7ORRZt+7TLw1IXI+N/E2P6PpR51faGAsEsjZNNM=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.02]
  11. */
  12. /**
  13. * date custom form element
  14. *
  15. * creates an element of type date using the bootstrap datetimepicker plugin
  16. */
  17. namespace Ppb\Form\Element;
  18. use Cube\Form\Element,
  19. Cube\Controller\Front;
  20. class DateTime extends Element
  21. {
  22. const ELEMENT_CLASS = 'date-time';
  23. /**
  24. *
  25. * type of element - override the variable from the parent class
  26. *
  27. * @var string
  28. */
  29. protected $_element = 'text';
  30. /**
  31. *
  32. * class constructor
  33. *
  34. * @param string $name
  35. */
  36. public function __construct($name)
  37. {
  38. parent::__construct($this->_element, $name);
  39. $baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
  40. $this->setHeaderCode('<link href="' . $baseUrl . '/js/bootstrap-datetimepicker/css/bootstrap-datetimepicker.css" media="screen" rel="stylesheet" type="text/css">')
  41. ->setBodyCode('<script type="text/javascript" src="' . $baseUrl . '/js/moment/moment-with-locales.min.js"></script>')
  42. ->setBodyCode('<script type="text/javascript" src="' . $baseUrl . '/js/bootstrap-datetimepicker/js/bootstrap-datetimepicker.min.js"></script>');
  43. $this->addAttribute('id', $name)
  44. ->addAttribute('class', self::ELEMENT_CLASS)
  45. ->addAttribute('readonly', 'readonly');
  46. }
  47. /**
  48. *
  49. * set the custom data for the element, and add the javascript code
  50. *
  51. * @param array $customData
  52. *
  53. * @return $this
  54. */
  55. public function setCustomData($customData)
  56. {
  57. $this->_customData = $customData;
  58. $formData = array(
  59. 'locale: "' . $this->getTranslate()->getLocale() . '"',
  60. 'ignoreReadonly: true',
  61. );
  62. if (isset($this->_customData['formData'])) {
  63. foreach ((array)$this->_customData['formData'] as $key => $value) {
  64. $formData[] = "{$key}: {$value}";
  65. }
  66. }
  67. $formData = implode(", \n", $formData);
  68. $this->setBodyCode(
  69. "<script type=\"text/javascript\">" . "\n"
  70. . " $(document).ready(function() { " . "\n"
  71. . " $('#" . $this->getName() . "').datetimepicker({ " . "\n"
  72. . " {$formData} " . "\n"
  73. . " }); " . "\n"
  74. . " }); " . "\n"
  75. . "</script>");
  76. return $this;
  77. }
  78. /**
  79. *
  80. * renders the date time form element
  81. *
  82. * @return string the html code of the element
  83. */
  84. public function render()
  85. {
  86. $value = $this->getValue();
  87. if (!is_string($value)) {
  88. $value = '';
  89. }
  90. else {
  91. $value = str_replace('"', '&quot;', $value);
  92. }
  93. $multiple = ($this->getMultiple() === true) ? $this->_brackets : '';
  94. $attributes = array(
  95. 'type="' . $this->_type . '"',
  96. 'name="' . $this->_name . $multiple . '"',
  97. 'value="' . $value . '"',
  98. $this->renderAttributes()
  99. );
  100. return $this->getPrefix() . ' '
  101. . '<div class="form-group-datetime has-feedback">'
  102. . '<input ' . implode(' ', array_filter($attributes))
  103. . $this->_endTag . ' '
  104. . '<span class="glyphicon glyphicon-calendar form-control-feedback"></span>'
  105. . '</div>'
  106. . ' '
  107. . $this->getSuffix();
  108. }
  109. }