1: <?php
2: /*
3:
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * or OpenGPL v3 license (GNU Public License V3.0)
9: * that is bundled with this package in the file LICENSE.txt.
10: * It is also available through the world-wide-web at this URL:
11: * http://opensource.org/licenses/osl-3.0.php
12: * or
13: * http://www.gnu.org/licenses/gpl-3.0.txt
14: * If you did not receive a copy of the license and are unable to
15: * obtain it through the world-wide-web, please send an email
16: * to info@e-abi.ee so we can send you a copy immediately.
17: *
18: * DISCLAIMER
19: *
20: * Do not edit or add to this file if you wish to upgrade this module to newer
21: * versions in the future.
22: *
23: * @category Eabi
24: * @package Eabi_Dpd
25: * @copyright Copyright (c) 2014 Aktsiamaailm LLC (http://en.e-abi.ee/)
26: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
27: * @license http://www.gnu.org/licenses/gpl-3.0.txt GNU Public License V3.0
28: * @author Matis Halmann
29: *
30:
31: */
32:
33: /**
34: * <p>Handles saving and loading Magento's core_config_data values.</p>
35: * <p>Can save and load PHP objects to core_config_data table.</p>
36: *
37: * @author Aktsiamaailm OÜ, Matis Halmann
38: */
39: class Eabi_Livehandler_Helper_Data extends Mage_Core_Helper_Abstract {
40:
41:
42: /**
43: * <p>Behaves similar to Mage::getStoreConfig() method, but returns PHP objects instead of strings</p>
44: * @param string $key configuration key to fetch
45: * @param string|bool $default default value to return if the value does not exist or unserialization failed
46: * @param int $storeId store id, to fetch the configuration value for
47: * @return object|array|string
48: */
49: final public function getBigConfigData($key, $default = false, $storeId = null) {
50: $i = 0;
51:
52: $selfValue = $this->getConfigData($key, $default, $storeId);
53: while (strlen($loadedString = $this->getConfigData($key . $i, $default, $storeId)) > 0) {
54: if (!is_string($loadedString)) {
55: break;
56: }
57: $selfValue .= $loadedString;
58: $i++;
59: }
60:
61: $finalValue = @unserialize(@gzuncompress(@base64_decode($selfValue)));
62:
63: if (!is_array($finalValue)) {
64: return $default;
65: }
66: return $finalValue;
67: }
68:
69: /**
70: * <p>Stores PHP object to core_config_data table using object serialization.</p>
71: * <p>Save procedure of stored object:
72: * <ul>
73: <li>serialize php object</li>
74: <li>gzcompress the serialized result</li>
75: <li>base64 encode the compressed result</li>
76: <li>If the compressed result is larger than 64K, then the resulting blocks will be saved under $key . $i ID, where $i starts with 0 and is incremented by 1 for each consecutive 64K block.</li>
77: </ul>
78: * </p>
79: * @param string $key configuration key to store this value
80: * @param object|array|string $value object to store into the configuration
81: * @param string $scope Magento's configuration scope
82: * @param int $scopeId store ID this configuration will be saved to
83: * @param bool $skipFirst if this setting is true, then first segment will not be saved to database using this function. Useful, when overriding Mage_Core_Model_Config_Data saving functions.
84: * @return string first saved segment of this saved configuration data.
85: */
86: final public function setBigConfigData($key, $value, $scope = 'default', $scopeId = 0, $skipFirst = false) {
87: $strValue = base64_encode(gzcompress(serialize($value)));
88: $strValues = str_split($strValue, 64000);
89: $cnt = 0;
90:
91: $firstValue = array_shift($strValues);
92: if (!$skipFirst) {
93: $this->setConfigData($key, $firstValue, $scope, $scopeId, false);
94: }
95:
96: foreach ($strValues as $strVal) {
97: $this->setConfigData($key . $cnt, $strVal, $scope, $scopeId, false);
98: $cnt ++;
99: }
100: $this->setConfigData($key . $cnt, '', $scope, $scopeId, true);
101: return $firstValue;
102: }
103:
104:
105: /**
106: * <p>Behaves similar to Mage::getStoreConfig() but attempts to return the resulting value as float.</p>
107: * <p>Supports "." and "," as decimal separator</p>
108: * @param string $key configuration key to load
109: * @param bool|object $default default value to return if the configuration value does not exist.
110: * @param int $storeId store id to load the configuration value for.
111: * @return float
112: */
113: final public function getConfigDataF($key, $default = false, $storeId = null) {
114: return $this->getConfigData($key, $default, $storeId, true);
115: }
116:
117: /**
118: * <p>Behaves similar to Mage::getStoreConfig() but returns array of elements, where each element is one line of data contained. Empty lines are not returned.</p>
119: * @param string $key configuration key to load
120: * @param bool|object $default default value to return if the configuration value does not exist.
121: * @param int $storeId store id to load the configuration value for.
122: * @return array
123: */
124: final public function getConfigDataA($key, $default = false, $storeId = null) {
125: $value = $this->getConfigData($key, $default, $storeId, false);
126: if (is_string($value)) {
127: $rawModules = explode("\n", $value);
128: return array_filter(array_map('trim', $rawModules));
129: }
130: return $value;
131: }
132:
133: /**
134: * <p>Behaves similar to Mage::getStoreConfig() function and can return the value as float.</p>
135: * @param string $key configuration key to load
136: * @param bool|object $default default value to return if the configuration value does not exist.
137: * @param int $storeId store id to load the configuration value for.
138: * @param bool $asFloat when true, then attemts to return the value as float
139: * @return string
140: */
141: final public function getConfigData($key, $default = false, $storeId = null, $asFloat = false) {
142: if ($storeId === null) {
143: $storeId = Mage::app()->getStore()->getId();
144: }
145: $value = Mage::getStoreConfig($key, $storeId);
146: if (is_null($value) || false === $value) {
147: $value = $default;
148: }
149: if ($asFloat) {
150: $value = str_replace(',', '.', $value);
151: }
152: return $value;
153: }
154:
155: /**
156: * <p>Writes the core_config_data value to database and resets the cache.</p>
157: * @param string $key Identification path to write the configuarion to.
158: * @param string $value Value to store
159: * @param string $scope Magento's scope to apply
160: * @param int $scopeId Store ID to apply
161: * @param bool $resetCache If the cache should be reset after saving the configuration value.
162: * @return Eabi_Livehandler_Helper_Data
163: */
164: final public function setConfigData($key, $value, $scope = 'default', $scopeId = 0, $resetCache = true) {
165: $config = Mage::getConfig();
166: $config->saveConfig($key, $value, $scope, $scopeId);
167: if ($resetCache) {
168: $config->cleanCache();
169: }
170: return $this;
171: }
172:
173: /**
174: * <p>Returns all hosts for current Magento installation</p>
175: * @return array
176: */
177: final public function getAllStoreUrls() {
178: $stores = Mage::app()->getStores();
179: $urls = array();
180: $databaseUrls = $this->_getCoreConfigDataModel()->getCollection()
181: ->addFieldToFilter('path', array('in' => array('web/unsecure/base_url', 'web/secure/base_url')))
182: ;
183:
184: foreach ($databaseUrls as $databaseUrl) {
185: /* @var $store Mage_Core_Model_Store */
186: try {
187: $url = Zend_Uri_Http::fromString($databaseUrl->getValue())->getHost();
188: if (!in_array($url, $urls)) {
189: $urls[] = $url;
190: }
191: } catch (Exception $ex) {
192: //do nothing or log
193: Mage::log(sprintf('Invalid base url %s for store id %s', $databaseUrl->getValue(), $databaseUrl->getScopeId()), Zend_Log::WARN);
194:
195: }
196: }
197: return $urls;
198: }
199:
200: /**
201: *
202: * @return Mage_Core_Model_Config_Data
203: */
204: protected function _getCoreConfigDataModel() {
205: return Mage::getModel('core/config_data');
206: }
207:
208:
209: }
210:
211:
212: