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_Core
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: * Custom variable model
29: *
30: * @method Mage_Core_Model_Resource_Variable _getResource()
31: * @method Mage_Core_Model_Resource_Variable getResource()
32: * @method string getCode()
33: * @method Mage_Core_Model_Variable setCode(string $value)
34: * @method string getName()
35: * @method Mage_Core_Model_Variable setName(string $value)
36: *
37: * @category Mage
38: * @package Mage_Core
39: * @author Magento Core Team <core@magentocommerce.com>
40: */
41: class Mage_Core_Model_Variable extends Mage_Core_Model_Abstract
42: {
43: const TYPE_TEXT = 'text';
44: const TYPE_HTML = 'html';
45:
46: protected $_storeId = 0;
47:
48: /**
49: * Internal Constructor
50: */
51: protected function _construct()
52: {
53: parent::_construct();
54: $this->_init('core/variable');
55: }
56:
57: /**
58: * Setter
59: *
60: * @param integer $storeId
61: * @return Mage_Core_Model_Variable
62: */
63: public function setStoreId($storeId)
64: {
65: $this->_storeId = $storeId;
66: return $this;
67: }
68:
69: /**
70: * Getter
71: *
72: * @return integer
73: */
74: public function getStoreId()
75: {
76: return $this->_storeId;
77: }
78:
79: /**
80: * Load variable by code
81: *
82: * @param string $code
83: * @return Mage_Core_Model_Variable
84: */
85: public function loadByCode($code)
86: {
87: $this->getResource()->loadByCode($this, $code);
88: return $this;
89: }
90:
91: /**
92: * Return variable value depend on given type
93: *
94: * @param string $type
95: * @return string
96: */
97: public function getValue($type = null)
98: {
99: if ($type === null) {
100: $type = self::TYPE_HTML;
101: }
102: if ($type == self::TYPE_TEXT || !(strlen((string)$this->getData('html_value')))) {
103: $value = $this->getData('plain_value');
104: //escape html if type is html, but html value is not defined
105: if ($type == self::TYPE_HTML) {
106: $value = nl2br(Mage::helper('core')->htmlEscape($value));
107: }
108: return $value;
109: }
110: return $this->getData('html_value');
111: }
112:
113: /**
114: * Validation of object data. Checking for unique variable code
115: *
116: * @return boolean | string
117: */
118: public function validate()
119: {
120: if ($this->getCode() && $this->getName()) {
121: $variable = $this->getResource()->getVariableByCode($this->getCode());
122: if (!empty($variable) && $variable['variable_id'] != $this->getId()) {
123: return Mage::helper('core')->__('Variable Code must be unique.');
124: }
125: return true;
126: }
127: return Mage::helper('core')->__('Validation has failed.');
128: }
129:
130: /**
131: * Retrieve variables option array
132: *
133: * @param boolean $withValues
134: * @return array
135: */
136: public function getVariablesOptionArray($withGroup = false)
137: {
138: /* @var $collection Mage_Core_Model_Mysql4_Variable_Collection */
139: $collection = $this->getCollection();
140: $variables = array();
141: foreach ($collection->toOptionArray() as $variable) {
142: $variables[] = array(
143: 'value' => '{{customVar code=' . $variable['value'] . '}}',
144: 'label' => Mage::helper('core')->__('%s', $variable['label'])
145: );
146: }
147: if ($withGroup && $variables) {
148: $variables = array(
149: 'label' => Mage::helper('core')->__('Custom Variables'),
150: 'value' => $variables
151: );
152: }
153: return $variables;
154: }
155:
156: }
157: