PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Block/Adminhtml/Mobile/Form/Element/Datetime.php

https://bitbucket.org/dnejedly/eaparts
PHP | 205 lines | 118 code | 11 blank | 76 comment | 13 complexity | 6b46cd17d6618e06d78462fe65a2ae30 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_XmlConnect
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * XmlConnect data selector form element
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Block_Adminhtml_Mobile_Form_Element_Datetime
  34. extends Varien_Data_Form_Element_Abstract
  35. {
  36. /**
  37. * Date
  38. *
  39. * @var Zend_Date
  40. */
  41. protected $_value;
  42. public function __construct($attributes=array())
  43. {
  44. parent::__construct($attributes);
  45. $this->setType('text');
  46. $this->setExtType('textfield');
  47. if (isset($attributes['value'])) {
  48. $this->setValue($attributes['value']);
  49. }
  50. }
  51. /**
  52. * If script executes on x64 system, converts large
  53. * numeric values to timestamp limit
  54. *
  55. * @param int $value
  56. * @return int
  57. */
  58. protected function _toTimestamp($value)
  59. {
  60. $value = (int)$value;
  61. if ($value > 3155760000) {
  62. $value = 0;
  63. }
  64. return $value;
  65. }
  66. /**
  67. * Set date value
  68. * If Zend_Date instance is provided instead of value, other params will be ignored.
  69. * Format and locale must be compatible with Zend_Date
  70. *
  71. * @param mixed $value
  72. * @param string $format
  73. * @param string $locale
  74. * @return Varien_Data_Form_Element_Date
  75. */
  76. public function setValue($value, $format = null, $locale = null)
  77. {
  78. if (empty($value)) {
  79. $this->_value = '';
  80. return $this;
  81. }
  82. if ($value instanceof Zend_Date) {
  83. $this->_value = $value;
  84. return $this;
  85. }
  86. if (preg_match('/^[0-9]+$/', $value)) {
  87. $this->_value = new Zend_Date($this->_toTimestamp($value));
  88. //$this->_value = new Zend_Date((int)value);
  89. return $this;
  90. }
  91. // last check, if input format was set
  92. if (null === $format) {
  93. $format = Varien_Date::DATETIME_INTERNAL_FORMAT;
  94. if ($this->getInputFormat()) {
  95. $format = $this->getInputFormat();
  96. }
  97. }
  98. // last check, if locale was set
  99. if (null === $locale) {
  100. if (!$locale = $this->getLocale()) {
  101. $locale = null;
  102. }
  103. }
  104. try {
  105. $this->_value = new Zend_Date($value, $format, $locale);
  106. } catch (Exception $e) {
  107. $this->_value = '';
  108. }
  109. return $this;
  110. }
  111. /**
  112. * Get date value as string.
  113. * Format can be specified, or it will be taken from $this->getFormat()
  114. *
  115. * @param string $format (compatible with Zend_Date)
  116. * @return string
  117. */
  118. public function getValue($format = null)
  119. {
  120. if (empty($this->_value)) {
  121. return '';
  122. }
  123. if (null === $format) {
  124. $format = $this->getFormat() . " " . $this->getFormatT();
  125. }
  126. return $this->_value->toString($format);
  127. }
  128. /**
  129. * Get value instance, if any
  130. *
  131. * @return Zend_Date
  132. */
  133. public function getValueInstance()
  134. {
  135. if (empty($this->_value)) {
  136. return null;
  137. }
  138. return $this->_value;
  139. }
  140. /**
  141. * Output the input field and assign calendar instance to it.
  142. * In order to output the date:
  143. * - the value must be instantiated (Zend_Date)
  144. * - output format must be set (compatible with Zend_Date)
  145. *
  146. * @return string
  147. */
  148. public function getElementHtml()
  149. {
  150. $this->addClass('input-text');
  151. $html = sprintf(
  152. '<input name="%s" id="%s" value="%s" %s style="width:110px !important;" />'
  153. .' <img src="%s" alt="" class="v-middle" id="%s_trig" title="%s" style="%s" />',
  154. $this->getName(),
  155. $this->getHtmlId(),
  156. $this->_escape($this->getValue()),
  157. $this->serialize($this->getHtmlAttributes()),
  158. $this->getImage(),
  159. $this->getHtmlId(),
  160. 'Select Date',
  161. ($this->getDisabled() ? 'display:none;' : '')
  162. );
  163. $outputFormat = $this->getFormat();
  164. $outputTimeFormat = $this->getFormatT();
  165. if (empty($outputFormat)) {
  166. Mage::throwException(
  167. $this->__('Output format is not specified. Please, specify "format" key in constructor, or set it using setFormat().')
  168. );
  169. }
  170. $displayFormat = Varien_Date::convertZendToStrFtime($outputFormat, true, false);
  171. $displayTimeFormat = Varien_Date::convertZendToStrFtime($outputTimeFormat, false, true);
  172. $html .= sprintf('
  173. <script type="text/javascript">
  174. //<![CDATA[
  175. Calendar.setup({
  176. inputField: "%s",
  177. ifFormat: "%s",
  178. showsTime: %s,
  179. button: "%s_trig",
  180. align: "Bl",
  181. singleClick : false,
  182. timeFormat: 12
  183. });
  184. //]]>
  185. </script>',
  186. $this->getHtmlId(),
  187. $displayFormat . " " . $displayTimeFormat,
  188. $this->getTime() ? 'true' : 'false',
  189. $this->getHtmlId()
  190. );
  191. $html .= $this->getAfterElementHtml();
  192. return $html;
  193. }
  194. }