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_XmlConnect
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: * Configuration data model
29: *
30: * @category Mage
31: * @package Mage_Xmlconnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Model_ConfigData extends Mage_Core_Model_Abstract
35: {
36: /**
37: * Default category
38: */
39: const DEFAULT_CATEGORY = 'default';
40:
41: /**
42: * Configuration prefix
43: */
44: const CONFIG_PREFIX = 'app_';
45:
46: /**
47: * Delete on update paths array
48: *
49: * @var array
50: */
51: protected $_deleteOnUpdate = array();
52:
53: /**
54: * Configuration data
55: *
56: * @var array
57: */
58: protected $_configuration = array();
59:
60: /**
61: * Initialize configuration data
62: *
63: * @return null
64: */
65: protected function _construct()
66: {
67: $this->_init('xmlconnect/configData');
68: }
69:
70: /**
71: * Create an array that will be stored in configuration data
72: *
73: * Create an array: application id with a prefix as key and
74: * configuration data as value
75: *
76: * @param $applicationId
77: * @param $configData
78: * @return array
79: */
80: protected function _assignConfig($applicationId, $configData)
81: {
82: return array(self::CONFIG_PREFIX . $applicationId => $configData);
83: }
84:
85: /**
86: * Prepare posted data to store at configuration.
87: *
88: * Posted data have to be in predefined format:
89: * - array('category:config/path/param' => 'value')
90: * where : is a separator of category
91: * - array('config/path/param' => 'value')
92: * if key doesn't have a separator category will be set as default
93: *
94: * @param $configuration posted data array
95: * @return array configuration data array
96: */
97: protected function _prepareData($configuration)
98: {
99: $configData = array();
100: foreach ($configuration as $key => $val) {
101: list($category, $path) = explode(':', $key, 2) + array('', '');
102: if (empty($path)) {
103: $path = $category;
104: $category = self::DEFAULT_CATEGORY;
105: }
106: $val = is_array($val) ? implode(',', $val) : $val;
107: $configData[strtolower($category)][strtolower($path)] = $val;
108: }
109: return $configData;
110: }
111:
112: /**
113: * Prepare and set configuration data
114: *
115: * @param $applicationId
116: * @param array $configData
117: * @param bool $replace
118: * @return Mage_XmlConnect_Model_ConfigData
119: */
120: public function setConfigData($applicationId, array $configData, $replace = true)
121: {
122: $configData = $this->_prepareData($configData);
123: $arrayToStore = $this->_assignConfig($applicationId, $configData);
124: if ($replace) {
125: $this->_configuration = array_merge($this->_configuration, $arrayToStore);
126: } else {
127: $this->_configuration = $this->_configuration + $arrayToStore;
128: }
129: return $this;
130: }
131:
132: /**
133: * Get configuration data
134: *
135: * @param bool $applicationId
136: * @return array
137: */
138: public function getConfigData($applicationId = false)
139: {
140: if ($applicationId && isset($this->_configuration[self::CONFIG_PREFIX . $applicationId])) {
141: return $this->_configuration[self::CONFIG_PREFIX . $applicationId];
142: }
143: return $this->_configuration;
144: }
145:
146: /**
147: * Save predefined configuration data
148: *
149: * @return Mage_XmlConnect_Model_ConfigData
150: */
151: public function initSaveConfig()
152: {
153: foreach ($this->_configuration as $application => $data) {
154: $applicationId = str_ireplace(self::CONFIG_PREFIX, '', $application);
155: $this->_deleteOnUpdate($applicationId);
156: foreach ($data as $category => $config) {
157: $this->saveConfig($applicationId, $config, $category);
158: }
159: }
160: return $this;
161: }
162:
163: /**
164: * Save configuration data by given params
165: *
166: * @param $applicationId
167: * @param array $configData
168: * @param string $category
169: * @return Mage_XmlConnect_Model_ConfigData
170: */
171: public function saveConfig($applicationId, array $configData, $category = self::DEFAULT_CATEGORY)
172: {
173: foreach ($configData as $path => $value) {
174: if (!is_scalar($value)) {
175: Mage::throwException(Mage::helper('xmlconnect')->__('Unsupported value type received'));
176: }
177: $this->getResource()->saveConfig($applicationId, $category, $path, $value);
178: }
179: return $this;
180: }
181:
182: /**
183: * Get delete on update array paths
184: *
185: * @return array
186: */
187: public function getDeleteOnUpdate()
188: {
189: return $this->_deleteOnUpdate;
190: }
191:
192: /**
193: * Set delete on update array paths
194: *
195: * @param array $pathsToDelete
196: * @return Mage_XmlConnect_Model_ConfigData
197: */
198: public function setDeleteOnUpdate(array $pathsToDelete)
199: {
200: $this->_deleteOnUpdate = array_merge($this->_deleteOnUpdate, $pathsToDelete);
201: return $this;
202: }
203:
204: /**
205: * @param $applicationId
206: * @return Mage_XmlConnect_Model_ConfigData
207: */
208: protected function _deleteOnUpdate($applicationId)
209: {
210: foreach ($this->_deleteOnUpdate as $category => $path) {
211: $this->getResource()->deleteConfig($applicationId, $category, $path);
212: }
213: return $this;
214: }
215: }
216: