PageRenderTime 947ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Core/Block/Html/Date/Jquery/Calendar.php

https://bitbucket.org/jokusafet/magento2
PHP | 137 lines | 70 code | 12 blank | 55 comment | 7 complexity | 2513b63481265e7f61ab875a400c5865 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Core
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * HTML calendar element block implemented using the jQuery datepicker widget.
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Block_Html_Date_Jquery_Calendar extends Mage_Core_Block_Html_Date
  34. {
  35. /**
  36. * File path for the regional localized Javascript file.
  37. */
  38. const LOCALIZED_URL_PATH = 'jquery/ui/i18n/jquery.ui.datepicker-%s.js';
  39. /**
  40. * Return the path to the localized Javascript file given the locale or null if it doesn't exist.
  41. *
  42. * @param string $locale - The locale (e.g. en-US or just en)
  43. * @return string - Url path to the localized Javascript file
  44. *
  45. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  46. */
  47. private function _getUrlPathByLocale($locale)
  48. {
  49. $urlPath = sprintf(self::LOCALIZED_URL_PATH, $locale);
  50. try {
  51. $urlPath = $this->getViewFileUrl($urlPath);
  52. } catch (Magento_Exception $e) {
  53. $urlPath = null;
  54. }
  55. return $urlPath;
  56. }
  57. /**
  58. * Generate HTML containing a Javascript <script> tag for creating a calendar instance implemented
  59. * using the jQuery datepicker.
  60. *
  61. * @return string
  62. */
  63. protected function _toHtml()
  64. {
  65. $displayFormat = Magento_Date_Jquery_Calendar::convertToDateTimeFormat(
  66. $this->getFormat(), true, (bool)$this->getTime()
  67. );
  68. $html = '<input type="text" name="' . $this->getName() . '" id="' . $this->getId() . '" ';
  69. $html .= 'value="' . $this->escapeHtml($this->getValue()) . '" class="' . $this->getClass() . '" '
  70. . $this->getExtraParams() . '/> ';
  71. $yearRange = "c-10:c+10"; /* Default the range to the current year + or - 10 years. */
  72. $calendarYearsRange = $this->getYearsRange();
  73. if ($calendarYearsRange) {
  74. /* Convert to the year range format that the jQuery datepicker understands. */
  75. sscanf($calendarYearsRange, "[%[0-9], %[0-9]]", $yearStart, $yearEnd);
  76. $yearRange = "$yearStart:$yearEnd";
  77. }
  78. /* First include jquery-ui. */
  79. $jsFiles = '"' . $this->getViewFileUrl("jquery/ui/jquery-ui.js") . '", ';
  80. /* There are a small handful of localized files that use the 5 character locale. */
  81. $locale = str_replace('_', '-', Mage::app()->getLocale()->getLocaleCode());
  82. $localizedJsFilePath = $this->_getUrlPathByLocale($locale);
  83. if ($localizedJsFilePath == null) {
  84. /* Most localized files use the 2 character locale. */
  85. $locale = substr($locale, 0, 2);
  86. $localizedJsFilePath = $this->_getUrlPathByLocale($locale);
  87. if ($localizedJsFilePath == null) {
  88. /* Localized Javascript file doesn't exist. Default locale to empty string (English). */
  89. $locale = '';
  90. } else {
  91. /* Include the regional localized Javascript file. */
  92. $jsFiles .= '"' . $localizedJsFilePath . '", ';
  93. }
  94. } else {
  95. /* Include the regional localized Javascript file. */
  96. $jsFiles .= '"' . $localizedJsFilePath . '", ';
  97. }
  98. $jsFiles .= '"' . $this->getViewFileUrl("mage/calendar/calendar.js") . '"'; /* Lastly, the datepicker. */
  99. $cssFile = '"' . $this->getViewFileUrl("mage/calendar/css/calendar.css") . '"';
  100. $html
  101. .= '
  102. <script type="text/javascript">
  103. //<![CDATA[
  104. (function($) {
  105. $.mage.event.observe("mage.calendar.initialize", function (event, initData) {
  106. var datepicker = {
  107. inputSelector: "#' . $this->getId() . '",
  108. locale: "' . $locale . '",
  109. options: {
  110. buttonImage: "' . $this->getImage() . '",
  111. buttonText: "' . $this->helper("Mage_Core_Helper_Data")->__("Select Date") . '",
  112. dateFormat: "' . $displayFormat . '",
  113. yearRange: "' . $yearRange . '"
  114. }
  115. };
  116. initData.datepicker.push(datepicker);
  117. });
  118. $.mage.load.css( ' . $cssFile . ' );
  119. $.mage.load.jsSync(' . $jsFiles . ');
  120. })(jQuery);
  121. //]]>
  122. </script>';
  123. return $html;
  124. }
  125. }