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: * JavaScript helper
29: *
30: * @author Magento Core Team <core@magentocommerce.com>
31: */
32: class Mage_Core_Helper_Js extends Mage_Core_Helper_Abstract
33: {
34: /**
35: * Key for cache
36: */
37: const JAVASCRIPT_TRANSLATE_CONFIG_KEY = 'javascript_translate_config';
38:
39: /**
40: * Translate file name
41: */
42: const JAVASCRIPT_TRANSLATE_CONFIG_FILENAME = 'jstranslator.xml';
43:
44: /**
45: * Array of senteces of JS translations
46: *
47: * @var array
48: */
49: protected $_translateData = null;
50:
51: /**
52: * Translate config
53: *
54: * @var Varien_Simplexml_Config
55: */
56: protected $_config = null;
57:
58: /**
59: * Retrieve JSON of JS sentences translation
60: *
61: * @return string
62: */
63: public function getTranslateJson()
64: {
65: return Mage::helper('core')->jsonEncode($this->_getTranslateData());
66: }
67:
68: /**
69: * Retrieve JS translator initialization javascript
70: *
71: * @return string
72: */
73: public function getTranslatorScript()
74: {
75: $script = 'var Translator = new Translate('.$this->getTranslateJson().');';
76: return $this->getScript($script);
77: }
78:
79: /**
80: * Retrieve framed javascript
81: *
82: * @param string $script
83: * @return script
84: */
85: public function getScript($script)
86: {
87: return '<script type="text/javascript">//<![CDATA[
88: '.$script.'
89: //]]></script>';
90: }
91:
92: /**
93: * Retrieve javascript include code
94: *
95: * @param string $file
96: * @return string
97: */
98: public function includeScript($file)
99: {
100: return '<script type="text/javascript" src="'.$this->getJsUrl($file).'"></script>'."\n";
101: }
102:
103: /**
104: * Retrieve
105: *
106: * @param string $file
107: * @return string
108: */
109: public function includeSkinScript($file)
110: {
111: return '<script type="text/javascript" src="'.$this->getJsSkinUrl($file).'"></script>';
112: }
113:
114: /**
115: * Retrieve JS file url
116: *
117: * @param string $file
118: * @return string
119: */
120: public function getJsUrl($file)
121: {
122: return Mage::getBaseUrl('js').$file;
123: }
124:
125: /**
126: * Retrieve skin JS file url
127: *
128: * @param string $file
129: * @return string
130: */
131: public function getJsSkinUrl($file)
132: {
133: return Mage::getDesign()->getSkinUrl($file, array());
134: }
135:
136: /**
137: * Retrieve JS translation array
138: *
139: * @return array
140: */
141: protected function _getTranslateData()
142: {
143: if ($this->_translateData === null) {
144: $this->_translateData = array();
145: $messages = $this->_getXmlConfig()->getXpath('*/message');
146: if (!empty($messages)) {
147: foreach ($messages as $message) {
148: $messageText = (string)$message;
149: $module = $message->getParent()->getAttribute("module");
150: $this->_translateData[$messageText] = Mage::helper(empty($module) ? 'core' : $module
151: )->__($messageText);
152: }
153: }
154:
155: foreach ($this->_translateData as $key => $value) {
156: if ($key == $value) {
157: unset($this->_translateData[$key]);
158: }
159: }
160: }
161: return $this->_translateData;
162: }
163:
164: /**
165: * Load config from files and try to cache it
166: *
167: * @return Varien_Simplexml_Config
168: */
169: protected function _getXmlConfig()
170: {
171: if (is_null($this->_config)) {
172: $canUsaCache = Mage::app()->useCache('config');
173: $cachedXml = Mage::app()->loadCache(self::JAVASCRIPT_TRANSLATE_CONFIG_KEY);
174: if ($canUsaCache && $cachedXml) {
175: $xmlConfig = new Varien_Simplexml_Config($cachedXml);
176: } else {
177: $xmlConfig = new Varien_Simplexml_Config();
178: $xmlConfig->loadString('<?xml version="1.0"?><jstranslator></jstranslator>');
179: Mage::getConfig()->loadModulesConfiguration(self::JAVASCRIPT_TRANSLATE_CONFIG_FILENAME, $xmlConfig);
180:
181: if ($canUsaCache) {
182: Mage::app()->saveCache($xmlConfig->getXmlString(), self::JAVASCRIPT_TRANSLATE_CONFIG_KEY,
183: array(Mage_Core_Model_Config::CACHE_TAG));
184: }
185: }
186: $this->_config = $xmlConfig;
187: }
188: return $this->_config;
189: }
190: }
191: