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_Eav
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: class Mage_Eav_Model_Entity_Attribute_Backend_Datetime extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
28: {
29: /**
30: * Formating date value before save
31: *
32: * Should set (bool, string) correct type for empty value from html form,
33: * neccessary for farther proccess, else date string
34: *
35: * @param Varien_Object $object
36: * @throws Mage_Eav_Exception
37: * @return Mage_Eav_Model_Entity_Attribute_Backend_Datetime
38: */
39: public function beforeSave($object)
40: {
41: $attributeName = $this->getAttribute()->getName();
42: $_formated = $object->getData($attributeName . '_is_formated');
43: if (!$_formated && $object->hasData($attributeName)) {
44: try {
45: $value = $this->formatDate($object->getData($attributeName));
46: } catch (Exception $e) {
47: throw Mage::exception('Mage_Eav', Mage::helper('eav')->__('Invalid date'));
48: }
49:
50: if (is_null($value)) {
51: $value = $object->getData($attributeName);
52: }
53:
54: $object->setData($attributeName, $value);
55: $object->setData($attributeName . '_is_formated', true);
56: }
57:
58: return $this;
59: }
60:
61: /**
62: * Prepare date for save in DB
63: *
64: * string format used from input fields (all date input fields need apply locale settings)
65: * int value can be declared in code (this meen whot we use valid date)
66: *
67: * @param string | int $date
68: * @return string
69: */
70: public function formatDate($date)
71: {
72: if (empty($date)) {
73: return null;
74: }
75: // unix timestamp given - simply instantiate date object
76: if (preg_match('/^[0-9]+$/', $date)) {
77: $date = new Zend_Date((int)$date);
78: }
79: // international format
80: else if (preg_match('#^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$#', $date)) {
81: $zendDate = new Zend_Date();
82: $date = $zendDate->setIso($date);
83: }
84: // parse this date in current locale, do not apply GMT offset
85: else {
86: $date = Mage::app()->getLocale()->date($date,
87: Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
88: null, false
89: );
90: }
91: return $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
92: }
93: }
94: