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_Persistent
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: /**
29: * Persistent Session Model
30: *
31: * @category Mage
32: * @package Mage_Persistent
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Persistent_Model_Session extends Mage_Core_Model_Abstract
36: {
37: const KEY_LENGTH = 50;
38: const COOKIE_NAME = 'persistent_shopping_cart';
39:
40: /**
41: * Fields which model does not save into `info` db field
42: *
43: * @var array
44: */
45: protected $_unserializableFields = array('persistent_id', 'key', 'customer_id', 'website_id', 'info', 'updated_at');
46:
47: /**
48: * If model loads expired sessions
49: *
50: * @var bool
51: */
52: protected $_loadExpired = false;
53:
54: /**
55: * Define resource model
56: */
57: protected function _construct()
58: {
59: $this->_init('persistent/session');
60: }
61:
62: /**
63: * Set if load expired persistent session
64: *
65: * @param bool $loadExpired
66: * @return Mage_Persistent_Model_Session
67: */
68: public function setLoadExpired($loadExpired = true)
69: {
70: $this->_loadExpired = $loadExpired;
71: return $this;
72: }
73:
74: /**
75: * Get if model loads expired sessions
76: *
77: * @return bool
78: */
79: public function getLoadExpired()
80: {
81: return $this->_loadExpired;
82: }
83:
84: /**
85: * Get date-time before which persistent session is expired
86: *
87: * @param int|string|Mage_Core_Model_Store $store
88: * @return string
89: */
90: public function getExpiredBefore($store = null)
91: {
92: return gmdate('Y-m-d H:i:s', time() - Mage::helper('persistent')->getLifeTime($store));
93: }
94:
95: /**
96: * Serialize info for Resource Model to save
97: * For new model check and set available cookie key
98: *
99: * @return Mage_Persistent_Model_Session
100: */
101: protected function _beforeSave()
102: {
103: parent::_beforeSave();
104:
105: // Setting info
106: $info = array();
107: foreach ($this->getData() as $index => $value) {
108: if (!in_array($index, $this->_unserializableFields)) {
109: $info[$index] = $value;
110: }
111: }
112: $this->setInfo(Mage::helper('core')->jsonEncode($info));
113:
114: if ($this->isObjectNew()) {
115: $this->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
116: // Setting cookie key
117: do {
118: $this->setKey(Mage::helper('core')->getRandomString(self::KEY_LENGTH));
119: } while (!$this->getResource()->isKeyAllowed($this->getKey()));
120: }
121:
122: return $this;
123: }
124:
125: /**
126: * Set model data from info field
127: *
128: * @return Mage_Persistent_Model_Session
129: */
130: protected function _afterLoad()
131: {
132: parent::_afterLoad();
133: $info = Mage::helper('core')->jsonDecode($this->getInfo());
134: if (is_array($info)) {
135: foreach ($info as $key => $value) {
136: $this->setData($key, $value);
137: }
138: }
139: return $this;
140: }
141:
142: /**
143: * Get persistent session by cookie key
144: *
145: * @param string $key
146: * @return Mage_Persistent_Model_Session
147: */
148: public function loadByCookieKey($key = null)
149: {
150: if (is_null($key)) {
151: $key = Mage::getSingleton('core/cookie')->get(Mage_Persistent_Model_Session::COOKIE_NAME);
152: }
153: if ($key) {
154: $this->load($key, 'key');
155: }
156:
157: return $this;
158: }
159:
160: /**
161: * Load session model by specified customer id
162: *
163: * @param int $id
164: * @return Mage_Core_Model_Abstract
165: */
166: public function loadByCustomerId($id)
167: {
168: return $this->load($id, 'customer_id');
169: }
170:
171: /**
172: * Delete customer persistent session by customer id
173: *
174: * @param int $customerId
175: * @param bool $clearCookie
176: * @return Mage_Persistent_Model_Session
177: */
178: public function deleteByCustomerId($customerId, $clearCookie = true)
179: {
180: if ($clearCookie) {
181: $this->removePersistentCookie();
182: }
183: $this->getResource()->deleteByCustomerId($customerId);
184: return $this;
185: }
186:
187: /**
188: * Remove persistent cookie
189: *
190: * @return Mage_Persistent_Model_Session
191: */
192: public function removePersistentCookie()
193: {
194: Mage::getSingleton('core/cookie')->delete(Mage_Persistent_Model_Session::COOKIE_NAME);
195: return $this;
196: }
197:
198: /**
199: * Delete expired persistent sessions for the website
200: *
201: * @param null|int $websiteId
202: * @return Mage_Persistent_Model_Session
203: */
204: public function deleteExpired($websiteId = null)
205: {
206: if (is_null($websiteId)) {
207: $websiteId = Mage::app()->getStore()->getWebsiteId();
208: }
209:
210: $lifetime = Mage::getConfig()->getNode(
211: Mage_Persistent_Helper_Data::XML_PATH_LIFE_TIME,
212: 'website',
213: intval($websiteId)
214: );
215:
216: if ($lifetime) {
217: $this->getResource()->deleteExpired(
218: $websiteId,
219: gmdate('Y-m-d H:i:s', time() - $lifetime)
220: );
221: }
222:
223: return $this;
224: }
225:
226: /**
227: * Delete 'persistent' cookie
228: *
229: * @return Mage_Core_Model_Abstract
230: */
231: protected function _afterDeleteCommit() {
232: Mage::getSingleton('core/cookie')->delete(Mage_Persistent_Model_Session::COOKIE_NAME);
233: return parent::_afterDeleteCommit();
234: }
235:
236: /**
237: * Set `updated_at` to be always changed
238: *
239: * @return Mage_Persistent_Model_Session
240: */
241: public function save()
242: {
243: $this->setUpdatedAt(gmdate('Y-m-d H:i:s'));
244: return parent::save();
245: }
246: }
247: