forked from magento/magento2
/
Design.php
165 lines (151 loc) · 4.89 KB
/
Design.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
namespace Magento\Core\Model;
use Magento\Framework\App\DesignInterface;
use Magento\Framework\Model\Resource\AbstractResource;
use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Object\IdentityInterface;
/**
* Design settings change model
*
* @method int getStoreId()
* @method \Magento\Core\Model\Design setStoreId(int $value)
* @method string getDesign()
* @method \Magento\Core\Model\Design setDesign(string $value)
* @method string getDateFrom()
* @method \Magento\Core\Model\Design setDateFrom(string $value)
* @method string getDateTo()
* @method \Magento\Core\Model\Design setDateTo(string $value)
*/
class Design extends AbstractModel implements IdentityInterface, DesignInterface
{
/**
* Cache tag
*/
const CACHE_TAG = 'CORE_DESIGN';
/**
* Prefix of model events names
*
* @var string
*/
protected $_eventPrefix = 'core_design';
/**
* Model cache tag for clear cache in after save and after delete
*
* When you use true - all cache will be clean
*
* @var string|bool
*/
protected $_cacheTag = self::CACHE_TAG;
/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
protected $_localeDate;
/**
* @var \Magento\Framework\Stdlib\DateTime
*/
protected $_dateTime;
/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param AbstractResource $resource
* @param \Magento\Framework\Data\Collection\Db $resourceCollection
* @param array $data
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
\Magento\Framework\Stdlib\DateTime $dateTime,
AbstractResource $resource = null,
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
array $data = array()
) {
$this->_localeDate = $localeDate;
$this->_dateTime = $dateTime;
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
}
/**
* Initialize resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Magento\Core\Model\Resource\Design');
}
/**
* Load custom design settings for specified store and date
*
* @param string $storeId
* @param string|null $date
* @return $this
*/
public function loadChange($storeId, $date = null)
{
if (is_null($date)) {
$date = $this->_dateTime->formatDate($this->_localeDate->scopeTimeStamp($storeId), false);
}
$changeCacheId = 'design_change_' . md5($storeId . $date);
$result = $this->_cacheManager->load($changeCacheId);
if ($result === false) {
$result = $this->getResource()->loadChange($storeId, $date);
if (!$result) {
$result = array();
}
$this->_cacheManager->save(serialize($result), $changeCacheId, array(self::CACHE_TAG), 86400);
} else {
$result = unserialize($result);
}
if ($result) {
$this->setData($result);
}
return $this;
}
/**
* Apply design change from self data into specified design package instance
*
* @param \Magento\Framework\View\DesignInterface $packageInto
* @return $this
*/
public function changeDesign(\Magento\Framework\View\DesignInterface $packageInto)
{
$design = $this->getDesign();
if ($design) {
$packageInto->setDesignTheme($design);
}
return $this;
}
/**
* Get identities
*
* @return array
*/
public function getIdentities()
{
return array(self::CACHE_TAG . '_' . $this->getId());
}
}