1: <?php
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: class Mage_XmlConnect_Helper_Theme extends Mage_Adminhtml_Helper_Data
35: {
36: 37: 38: 39: 40:
41: protected $_themeArray = null;
42:
43: 44: 45: 46: 47:
48: public function getThemeAjaxParameters()
49: {
50: $themesArray = array (
51: 'conf_native_navigationBar_tintColor'
52: => 'conf[native][navigationBar][tintColor]',
53: 'conf_native_body_primaryColor'
54: => 'conf[native][body][primaryColor]',
55: 'conf_native_body_secondaryColor'
56: => 'conf[native][body][secondaryColor]',
57: 'conf_native_categoryItem_backgroundColor'
58: => 'conf[native][categoryItem][backgroundColor]',
59: 'conf_native_categoryItem_tintColor'
60: => 'conf[native][categoryItem][tintColor]',
61:
62: 'conf_extra_fontColors_header'
63: => 'conf[extra][fontColors][header]',
64: 'conf_extra_fontColors_primary'
65: => 'conf[extra][fontColors][primary]',
66: 'conf_extra_fontColors_secondary'
67: => 'conf[extra][fontColors][secondary]',
68: 'conf_extra_fontColors_price'
69: => 'conf[extra][fontColors][price]',
70:
71: 'conf_native_body_backgroundColor'
72: => 'conf[native][body][backgroundColor]',
73: 'conf_native_body_scrollBackgroundColor'
74: => 'conf[native][body][scrollBackgroundColor]',
75: 'conf_native_itemActions_relatedProductBackgroundColor'
76: => 'conf[native][itemActions][relatedProductBackgroundColor]'
77: );
78: return $themesArray;
79: }
80:
81: 82: 83: 84: 85: 86:
87: public function getAllThemesArray($flushCache = false)
88: {
89: $result = array();
90: $themes = $this->getAllThemes($flushCache);
91: foreach ($themes as $theme) {
92: $result[$theme->getName()] = $theme->getFormData();
93: }
94: return $result;
95: }
96:
97: 98: 99: 100: 101: 102:
103: public function getThemeImageUrl($themeId)
104: {
105: $themeImage = array_key_exists($themeId, $this->getDefaultThemes()) ? $themeId : 'user_custom';
106: return Mage::helper('xmlconnect/image')->getSkinImagesUrl('swatch_' . $themeImage . '.gif');
107: }
108:
109: 110: 111: 112: 113: 114:
115: public function getThemesSelector($themeId = '')
116: {
117: if (Mage::registry('current_app') !== null) {
118: $themeId = Mage::registry('current_app')->getData('conf/extra/theme');
119: }
120:
121: if (!$themeId) {
122: $themeId = $this->getDefaultThemeName();
123: }
124:
125: $currentTheme = $this->getThemeByName($themeId);
126: if ($currentTheme === null) {
127: $themeId = $this->getDefaultThemeName();
128: $currentTheme = $this->getThemeByName($themeId);
129: }
130:
131: if (!($currentTheme instanceof Mage_XmlConnect_Model_Theme)) {
132: Mage::throwException(
133: Mage::helper('xmlconnect')->__('Can\'t load selected theme. Please check your media folder permissions.')
134: );
135: }
136:
137: $themeList = '';
138: foreach ($this->getAllThemes(true) as $theme) {
139: $themeList .= '<li id="' . $theme->getName() . '">';
140: $themeList .= '<a rel="' . $theme->getName() . '" style="cursor:pointer;">' . $theme->getLabel();
141: $themeList .= '<span>';
142: $themeList .= '<img src="' . $this->getThemeImageUrl($theme->getName()) . '"/>';
143: $themeList .= '</span></a></li>';
144: }
145:
146: $themesDdl = <<<EOT
147: <ul class="dropdown theme_selector" id="theme_selector_id">
148: <li class="ddtitle theme_selector">
149: <a style="cursor:pointer;">{$currentTheme->getLabel()}
150: <span>
151: <img src="{$this->getThemeImageUrl($themeId)}"/>
152: </span>
153: </a>
154: </li>
155: <li style="display:none;" class="ddlist">
156: <ul>
157: {$themeList}
158: </ul>
159: </li>
160: </ul>
161: EOT;
162: return $themesDdl;
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function getAllThemes($flushCache = false)
172: {
173: if (!$this->_themeArray || $flushCache) {
174: $saveLibxmlErrors = libxml_use_internal_errors(true);
175: $this->_themeArray = array();
176: $themeDir = $this->getMediaThemePath();
177: $ioFile = new Varien_Io_File();
178: $ioFile->checkAndCreateFolder($themeDir);
179: $ioFile->open(array('path' => $themeDir));
180: try {
181: $fileList = $ioFile->ls(Varien_Io_File::GREP_FILES);
182: if (!count($fileList)) {
183: $this->resetTheme();
184: $this->getAllThemes(true);
185: }
186: foreach ($fileList as $file) {
187: $src = $themeDir . DS . $file['text'];
188: if (is_readable($src)) {
189: $theme = Mage::getModel('xmlconnect/theme', $src);
190: $this->_themeArray[$theme->getName()] = $theme;
191: }
192: }
193: asort($this->_themeArray);
194: libxml_use_internal_errors($saveLibxmlErrors);
195: } catch (Exception $e) {
196: Mage::logException($e);
197: }
198: }
199: return $this->_themeArray;
200: }
201:
202: 203: 204: 205: 206: 207:
208: public function getDefaultThemes()
209: {
210: $saveLibxmlErrors = libxml_use_internal_errors(true);
211: $defaultThemeArray = array();
212: $themeDir = $this->_getDefaultThemePath();
213: $ioFile = new Varien_Io_File();
214: $ioFile->open(array('path' => $themeDir));
215: try {
216: $fileList = $ioFile->ls(Varien_Io_File::GREP_FILES);
217: foreach ($fileList as $file) {
218: $src = $themeDir . DS . $file['text'];
219: if (is_readable($src)) {
220: $theme = Mage::getModel('xmlconnect/theme', $src);
221: $defaultThemeArray[$theme->getName()] = $theme;
222: }
223: }
224: libxml_use_internal_errors($saveLibxmlErrors);
225: } catch (Exception $e) {
226: Mage::logException($e);
227: }
228: if (!count($defaultThemeArray)) {
229: Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load default themes.'));
230: }
231: return $defaultThemeArray;
232: }
233:
234: 235: 236: 237: 238: 239: 240:
241: public function createNewTheme($themeName, $data)
242: {
243:
244: $defaultTheme = $this->getThemeByName($this->getDefaultThemeName());
245: return $defaultTheme->createNewTheme($themeName, $data);
246: }
247:
248: 249: 250: 251: 252:
253: protected function _getDefaultThemePath()
254: {
255: return Mage::getModuleDir('etc', 'Mage_XmlConnect') . DS . 'themes';
256: }
257:
258: 259: 260: 261: 262:
263: public function getMediaThemePath()
264: {
265: return Mage::getBaseDir('media') . DS . 'xmlconnect' . DS . 'themes';
266: }
267:
268: 269: 270: 271: 272: 273: 274: 275:
276: public function resetTheme($theme = null)
277: {
278: $themeDir = $this->getMediaThemePath();
279: $defaultThemeDir = $this->_getDefaultThemePath();
280:
281: $ioFile = new Varien_Io_File();
282: $ioFile->open(array('path' => $defaultThemeDir));
283: $fileList = $ioFile->ls(Varien_Io_File::GREP_FILES);
284: foreach ($fileList as $file) {
285: $f = $file['text'];
286: $src = $defaultThemeDir . DS . $f;
287: $dst = $themeDir . DS .$f;
288:
289: if ($theme && ($theme . '.xml') != $f) {
290: continue;
291: }
292:
293: if (!$ioFile->cp($src, $dst)) {
294: Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t copy file "%s" to "%s".', $src, $dst));
295: } else {
296: $ioFile->chmod($dst, 0755);
297: }
298: }
299: }
300:
301: 302: 303: 304: 305: 306:
307: public function getThemeByName($name)
308: {
309: $themes = $this->getAllThemes();
310: $theme = isset($themes[$name]) ? $themes[$name] : null;
311: return $theme;
312: }
313:
314: 315: 316: 317: 318:
319: public function getCustomThemeName()
320: {
321: return 'custom';
322: }
323:
324: 325: 326: 327: 328:
329: public function getDefaultThemeName()
330: {
331: return 'default';
332: }
333:
334: 335: 336: 337: 338:
339: public function getThemeId()
340: {
341: $themeId = Mage::helper('xmlconnect')->getApplication()->getData('conf/extra/theme');
342:
343: if ($this->getThemeByName($themeId) === null) {
344: $themeId = null;
345: }
346:
347: if (empty($themeId)) {
348: $themeId = $this->getDefaultThemeName();
349: }
350: return $themeId;
351: }
352:
353: 354: 355: 356: 357: 358: 359:
360: public function getThemeLabel(array $themes, $themeId = false)
361: {
362: $themeLabel = '';
363: $themeId = $themeId ? $themeId : $this->getThemeId();
364:
365: foreach ($themes as $theme) {
366: if ($theme->getName() == $themeId) {
367: $themeLabel = $theme->getLabel();
368: break;
369: }
370: }
371: return $themeLabel;
372: }
373:
374: 375: 376: 377: 378: 379:
380: public function deleteTheme($themeId)
381: {
382: $result = false;
383: $ioFile = new Varien_Io_File();
384: $ioFile->cd($this->getMediaThemePath());
385: $themeFile = basename($themeId . '.xml');
386: if ($ioFile->fileExists($themeFile)) {
387: $result = $ioFile->rm($themeFile);
388: }
389: return $result;
390: }
391: }
392: