00001 <?php 00002 /** 00003 * Magento 00004 * 00005 * NOTICE OF LICENSE 00006 * 00007 * This source file is subject to the Open Software License (OSL 3.0) 00008 * that is bundled with this package in the file LICENSE.txt. 00009 * It is also available through the world-wide-web at this URL: 00010 * http://opensource.org/licenses/osl-3.0.php 00011 * If you did not receive a copy of the license and are unable to 00012 * obtain it through the world-wide-web, please send an email 00013 * to license@magentocommerce.com so we can send you a copy immediately. 00014 * 00015 * DISCLAIMER 00016 * 00017 * Do not edit or add to this file if you wish to upgrade Magento to newer 00018 * versions in the future. If you wish to customize Magento for your 00019 * needs please refer to http://www.magentocommerce.com for more information. 00020 * 00021 * @category Mage 00022 * @package Mage_Eav 00023 * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) 00024 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) 00025 */ 00026 00027 class Mage_Eav_Model_Entity_Attribute_Backend_Datetime extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract 00028 { 00029 public function beforeSave($object) 00030 { 00031 $_formated = $object->getData($this->getAttribute()->getName() . '_is_formated'); 00032 if (!$_formated) { 00033 try { 00034 $value = $this->formatDate($object->getData($this->getAttribute()->getName())); 00035 } catch (Exception $e) { 00036 throw new Exception("Invalid date."); 00037 } 00038 $object->setData($this->getAttribute()->getName(), $value); 00039 $object->setData($this->getAttribute()->getName() . '_is_formated', true); 00040 } 00041 } 00042 00043 /** 00044 * Prepare date for save in DB 00045 * 00046 * string format used from input fields (all date input fields need apply locale settings) 00047 * int value can be declared in code (this meen whot we use valid date) 00048 * 00049 * @param string | int $date 00050 * @return string 00051 */ 00052 public function formatDate($date) 00053 { 00054 if (empty($date)) { 00055 return null; 00056 } 00057 // unix timestamp given - simply instantiate date object 00058 if (preg_match('/^[0-9]+$/', $date)) { 00059 $date = new Zend_Date((int)$date); 00060 } 00061 // parse this date in current locale, do not apply GMT offset 00062 else { 00063 $date = Mage::app()->getLocale()->date($date, 00064 Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT), 00065 null, false 00066 ); 00067 } 00068 return $date->toString(Varien_Date::DATETIME_INTERNAL_FORMAT); 00069 } 00070 00071 }