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_Model_Theme
35: {
36: 37: 38: 39: 40:
41: protected $_file = null;
42:
43: 44: 45: 46: 47:
48: protected $_xml = null;
49:
50: 51: 52: 53: 54:
55: protected $_conf = null;
56:
57: 58: 59: 60: 61: 62:
63: public function __construct($file)
64: {
65: $this->_file = $file;
66: if (!file_exists($file)) {
67: Mage::throwException(Mage::helper('xmlconnect')->__('File doesn\'t exist "%s".', $file));
68: }
69: if (!is_readable($file)) {
70: Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t read file "%s".', $file));
71: }
72: try {
73: $text = file_get_contents($file);
74: $this->_xml = simplexml_load_string($text);
75: } catch (Exception $e) {
76: Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t load XML.'));
77: }
78: if (empty($this->_xml)) {
79: Mage::throwException(Mage::helper('xmlconnect')->__('Invalid XML.'));
80: }
81: $this->_conf = $this->_xmlToArray($this->_xml->configuration);
82: $this->_conf = $this->_conf['configuration'];
83: if (!is_array($this->_conf)) {
84: Mage::throwException(Mage::helper('xmlconnect')->__('Wrong theme format.'));
85: }
86: }
87:
88: 89: 90: 91: 92: 93:
94: protected function _xmlToArray($xml)
95: {
96: $result = array();
97: foreach ($xml as $key => $value) {
98: if (count($value)) {
99: $result[$key] = $this->_xmlToArray($value);
100: } else {
101: $result[$key] = (string) $value;
102: }
103: }
104: return $result;
105: }
106:
107: 108: 109: 110: 111:
112: protected function _getThemeFile()
113: {
114: return $this->_file;
115: }
116:
117: 118: 119: 120: 121:
122: public function getName()
123: {
124: return (string) $this->_xml->manifest->name;
125: }
126:
127: 128: 129: 130: 131: 132:
133: public function setName($name)
134: {
135: $name = trim((string) $name);
136: $this->_xml->manifest->name = htmlentities($name, ENT_QUOTES, 'UTF-8');
137: return $this;
138: }
139:
140: 141: 142: 143: 144:
145: public function getLabel()
146: {
147: return (string) $this->_xml->manifest->label;
148: }
149:
150: 151: 152: 153: 154: 155:
156: public function setLabel($label)
157: {
158: $label = trim((string) $label);
159: $this->_xml->manifest->label = htmlentities($label, ENT_QUOTES, 'UTF-8');
160: return $this;
161: }
162:
163: 164: 165: 166: 167:
168: protected function _createThemeName()
169: {
170:
171: $themesHelper = Mage::helper('xmlconnect/theme');
172:
173: $coreHelper = Mage::helper('core');
174:
175: $themeFileName = $themesHelper->getMediaThemePath() . DS .$themesHelper->getCustomThemeName() . '_' . time()
176: . '_' . $coreHelper->getRandomString(10, 'abcdefghijklmnopqrstuvwxyz0123456789') . '.xml';
177: return $themeFileName;
178: }
179:
180: 181: 182: 183: 184: 185:
186: protected function _createCopy($filePath)
187: {
188: $currentThemeFileName = $this->_getThemeFile();
189:
190: $ioFile = new Varien_Io_File();
191: if (!$ioFile->cp($currentThemeFileName, $filePath)) {
192: Mage::throwException(
193: Mage::helper('xmlconnect')->__('Can\'t copy file "%s" to "%s".', $currentThemeFileName, $filePath)
194: );
195: } else {
196: $ioFile->chmod($filePath, 0755);
197: }
198:
199: return $filePath;
200: }
201:
202: 203: 204: 205: 206: 207: 208:
209: public function createNewTheme($themeName, $data)
210: {
211: $filePath = $this->_createThemeName();
212: $themeFileName = $this->_createCopy($filePath);
213:
214:
215: $themeFileName = Mage::getModel('xmlconnect/theme', $filePath);
216: $themeFileName->setLabel($themeName);
217: $fileName = basename($filePath);
218: $themeFileName->setName(substr($fileName, 0, -4));
219: $themeFileName->importAndSaveData($data);
220: return $themeFileName;
221: }
222:
223: 224: 225: 226: 227:
228: public function getFormData()
229: {
230: return $this->_flatArray($this->_conf, 'conf');
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: protected function _flatArray($subtree, $prefix = null)
241: {
242: $result = array();
243: foreach ($subtree as $key => $value) {
244: if (is_null($prefix)) {
245: $name = $key;
246: } else {
247: $name = $prefix . '[' . $key . ']';
248: }
249:
250: if (is_array($value)) {
251: $result = array_merge($result, $this->_flatArray($value, $name));
252: } else {
253: $result[$name] = $value;
254: }
255: }
256: return $result;
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: protected function _validateFormInput($data, $xml = null)
267: {
268: $root = false;
269: $result = array();
270: if (is_null($xml)) {
271: $root = true;
272: $data = array('configuration' => $data);
273: $xml = $this->_xml->configuration;
274: }
275: foreach ($xml as $key => $value) {
276: if (isset($data[$key])) {
277: if (is_array($data[$key])) {
278: $result[$key] = $this->_validateFormInput($data[$key], $value);
279: } else {
280: $result[$key] = $data[$key];
281: }
282: }
283: }
284: if ($root) {
285: $result = $result['configuration'];
286: }
287: return $result;
288: }
289:
290: 291: 292: 293: 294: 295: 296:
297: protected function _buildRecursive($parent, $data)
298: {
299: foreach ($data as $key=>$value) {
300: if (is_array($value)) {
301: $this->_buildRecursive($parent->addChild($key), $value);
302: } else {
303: $parent->addChild($key, $value);
304: }
305: }
306: }
307:
308: 309: 310: 311: 312: 313:
314: public function importAndSaveData($data)
315: {
316: $xml = new SimpleXMLElement('<theme>'.$this->_xml->manifest->asXML().'</theme>');
317: $this->_buildRecursive($xml->addChild('configuration'), $this->_validateFormInput($data));
318: clearstatcache();
319: if (is_writeable($this->_file)) {
320: file_put_contents($this->_file, $xml->asXML());
321: } else {
322: Mage::throwException(Mage::helper('xmlconnect')->__('Can\'t write to file "%s".', $this->_file));
323: }
324: }
325: }
326: