-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variable.php
160 lines (150 loc) · 4.45 KB
/
Variable.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
<?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@magento.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.magento.com for more information.
*
* @category Mage
* @package Mage_Core
<<<<<<< HEAD
* @copyright Copyright (c) 2006-2015 X.commerce, Inc. (http://www.magento.com)
=======
* @copyright Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
>>>>>>> origin/master
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Custom variable model
*
* @method Mage_Core_Model_Resource_Variable _getResource()
* @method Mage_Core_Model_Resource_Variable getResource()
* @method string getCode()
* @method Mage_Core_Model_Variable setCode(string $value)
* @method string getName()
* @method Mage_Core_Model_Variable setName(string $value)
*
* @category Mage
* @package Mage_Core
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Core_Model_Variable extends Mage_Core_Model_Abstract
{
const TYPE_TEXT = 'text';
const TYPE_HTML = 'html';
protected $_storeId = 0;
/**
* Internal Constructor
*/
protected function _construct()
{
parent::_construct();
$this->_init('core/variable');
}
/**
* Setter
*
* @param integer $storeId
* @return Mage_Core_Model_Variable
*/
public function setStoreId($storeId)
{
$this->_storeId = $storeId;
return $this;
}
/**
* Getter
*
* @return integer
*/
public function getStoreId()
{
return $this->_storeId;
}
/**
* Load variable by code
*
* @param string $code
* @return Mage_Core_Model_Variable
*/
public function loadByCode($code)
{
$this->getResource()->loadByCode($this, $code);
return $this;
}
/**
* Return variable value depend on given type
*
* @param string $type
* @return string
*/
public function getValue($type = null)
{
if ($type === null) {
$type = self::TYPE_HTML;
}
if ($type == self::TYPE_TEXT || !(strlen((string)$this->getData('html_value')))) {
$value = $this->getData('plain_value');
//escape html if type is html, but html value is not defined
if ($type == self::TYPE_HTML) {
$value = nl2br(Mage::helper('core')->escapeHtml($value));
}
return $value;
}
return $this->getData('html_value');
}
/**
* Validation of object data. Checking for unique variable code
*
* @return boolean | string
*/
public function validate()
{
if ($this->getCode() && $this->getName()) {
$variable = $this->getResource()->getVariableByCode($this->getCode());
if (!empty($variable) && $variable['variable_id'] != $this->getId()) {
return Mage::helper('core')->__('Variable Code must be unique.');
}
return true;
}
return Mage::helper('core')->__('Validation has failed.');
}
/**
* Retrieve variables option array
*
* @param boolean $withValues
* @return array
*/
public function getVariablesOptionArray($withGroup = false)
{
/* @var $collection Mage_Core_Model_Mysql4_Variable_Collection */
$collection = $this->getCollection();
$variables = array();
foreach ($collection->toOptionArray() as $variable) {
$variables[] = array(
'value' => '{{customVar code=' . $variable['value'] . '}}',
'label' => Mage::helper('core')->__('%s', $variable['label'])
);
}
if ($withGroup && $variables) {
$variables = array(
'label' => Mage::helper('core')->__('Custom Variables'),
'value' => $variables
);
}
return $variables;
}
}