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: /**
28: * "Serialized" attribute backend
29: */
30: class Mage_Eav_Model_Entity_Attribute_Backend_Serialized extends Mage_Eav_Model_Entity_Attribute_Backend_Abstract
31: {
32: /**
33: * Serialize before saving
34: *
35: * @param Varien_Object $object
36: * @return Mage_Eav_Model_Entity_Attribute_Backend_Serialized
37: */
38: public function beforeSave($object)
39: {
40: // parent::beforeSave() is not called intentionally
41: $attrCode = $this->getAttribute()->getAttributeCode();
42: if ($object->hasData($attrCode)) {
43: $object->setData($attrCode, serialize($object->getData($attrCode)));
44: }
45:
46: return $this;
47: }
48:
49: /**
50: * Unserialize after saving
51: *
52: * @param Varien_Object $object
53: * @return Mage_Eav_Model_Entity_Attribute_Backend_Serialized
54: */
55: public function afterSave($object)
56: {
57: parent::afterSave($object);
58: $this->_unserialize($object);
59: return $this;
60: }
61:
62: /**
63: * Unserialize after loading
64: *
65: * @param Varien_Object $object
66: * @return Mage_Eav_Model_Entity_Attribute_Backend_Serialized
67: */
68: public function afterLoad($object)
69: {
70: parent::afterLoad($object);
71: $this->_unserialize($object);
72: return $this;
73: }
74:
75: /**
76: * Try to unserialize the attribute value
77: *
78: * @param Varien_Object $object
79: * @return Mage_Eav_Model_Entity_Attribute_Backend_Serialized
80: */
81: protected function _unserialize(Varien_Object $object)
82: {
83: $attrCode = $this->getAttribute()->getAttributeCode();
84: if ($object->getData($attrCode)) {
85: try {
86: $unserialized = unserialize($object->getData($attrCode));
87: $object->setData($attrCode, $unserialized);
88: } catch (Exception $e) {
89: $object->unsetData($attrCode);
90: }
91: }
92:
93: return $this;
94: }
95: }
96: