PageRenderTime 60ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Convert/Profile.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 380 lines | 298 code | 39 blank | 43 comment | 52 complexity | b5a1fae5a758770967509428d2869828 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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) 2010 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. * Convert profile
  28. *
  29. * @author Magento Core Team <core@magentocommerce.com>
  30. */
  31. class Mage_Core_Model_Convert_Profile extends Mage_Core_Model_Abstract
  32. {
  33. protected function _construct()
  34. {
  35. $this->_init('core/convert_profile');
  36. }
  37. protected function _afterLoad()
  38. {
  39. if (is_string($this->getGuiData())) {
  40. $guiData = unserialize($this->getGuiData());
  41. } else {
  42. $guiData = '';
  43. }
  44. $this->setGuiData($guiData);
  45. parent::_afterLoad();
  46. }
  47. protected function _beforeSave()
  48. {
  49. parent::_beforeSave();
  50. if (is_array($this->getGuiData())) {
  51. $this->_parseGuiData();
  52. $this->setGuiData(serialize($this->getGuiData()));
  53. }
  54. }
  55. protected function _afterSave()
  56. {
  57. if (is_string($this->getGuiData())) {
  58. $this->setGuiData(unserialize($this->getGuiData()));
  59. }
  60. Mage::getModel('core/convert_history')
  61. ->setProfileId($this->getId())
  62. ->setActionCode($this->getOrigData('profile_id') ? 'update' : 'create')
  63. ->save();
  64. parent::_afterSave();
  65. }
  66. public function run()
  67. {
  68. Mage::getModel('core/convert_history')
  69. ->setProfileId($this->getId())
  70. ->setActionCode('run')
  71. ->save();
  72. $xml = '<convert version="1.0"><profile name="default">'.$this->getActionsXml().'</profile></convert>';
  73. $profile = Mage::getModel('core/convert')->importXml($xml)->getProfile('default');
  74. try {
  75. $profile->run();
  76. } catch (Exception $e) {
  77. }
  78. $this->setExceptions($profile->getExceptions());
  79. return $this;
  80. }
  81. public function _parseGuiData()
  82. {
  83. $nl = "\r\n";
  84. $import = $this->getDirection()==='import';
  85. $p = $this->getGuiData();
  86. if ($this->getDataTransfer()==='interactive') {
  87. // $p['file']['type'] = 'file';
  88. // $p['file']['filename'] = $p['interactive']['filename'];
  89. // $p['file']['path'] = 'var/export';
  90. $interactiveXml = '<action type="dataflow/convert_adapter_http" method="'.($import?'load':'save').'">'.$nl;
  91. #$interactiveXml .= ' <var name="filename"><![CDATA['.$p['interactive']['filename'].']]></var>'.$nl;
  92. $interactiveXml .= '</action>';
  93. $fileXml = '';
  94. } else {
  95. $interactiveXml = '';
  96. $fileXml = '<action type="dataflow/convert_adapter_io" method="'.($import?'load':'save').'">'.$nl;
  97. $fileXml .= ' <var name="type">'.$p['file']['type'].'</var>'.$nl;
  98. $fileXml .= ' <var name="path">'.$p['file']['path'].'</var>'.$nl;
  99. $fileXml .= ' <var name="filename"><![CDATA['.$p['file']['filename'].']]></var>'.$nl;
  100. if ($p['file']['type']==='ftp') {
  101. $hostArr = explode(':', $p['file']['host']);
  102. $fileXml .= ' <var name="host"><![CDATA['.$hostArr[0].']]></var>'.$nl;
  103. if (isset($hostArr[1])) {
  104. $fileXml .= ' <var name="port"><![CDATA['.$hostArr[1].']]></var>'.$nl;
  105. }
  106. if (!empty($p['file']['passive'])) {
  107. $fileXml .= ' <var name="passive">true</var>'.$nl;
  108. }
  109. if (!empty($p['file']['user'])) {
  110. $fileXml .= ' <var name="user"><![CDATA['.$p['file']['user'].']]></var>'.$nl;
  111. }
  112. if (!empty($p['file']['password'])) {
  113. $fileXml .= ' <var name="password"><![CDATA['.$p['file']['password'].']]></var>'.$nl;
  114. }
  115. }
  116. $fileXml .= '</action>'.$nl.$nl;
  117. }
  118. switch ($p['parse']['type']) {
  119. case 'excel_xml':
  120. $parseFileXml = '<action type="dataflow/convert_parser_xml_excel" method="'.($import?'parse':'unparse').'">'.$nl;
  121. $parseFileXml .= ' <var name="single_sheet"><![CDATA['.($p['parse']['single_sheet']!==''?$p['parse']['single_sheet']:'_').']]></var>'.$nl;
  122. break;
  123. case 'csv':
  124. $parseFileXml = '<action type="dataflow/convert_parser_csv" method="'.($import?'parse':'unparse').'">'.$nl;
  125. $parseFileXml .= ' <var name="delimiter"><![CDATA['.$p['parse']['delimiter'].']]></var>'.$nl;
  126. $parseFileXml .= ' <var name="enclose"><![CDATA['.$p['parse']['enclose'].']]></var>'.$nl;
  127. break;
  128. }
  129. $parseFileXml .= ' <var name="fieldnames">'.$p['parse']['fieldnames'].'</var>'.$nl;
  130. $parseFileXml .= '</action>'.$nl.$nl;
  131. $mapXml = '';
  132. if (isset($p['map']) && is_array($p['map'])) {
  133. foreach ($p['map'] as $side=>$fields) {
  134. if (!is_array($fields)) {
  135. continue;
  136. }
  137. foreach ($fields['db'] as $i=>$k) {
  138. if ($k=='' || $k=='0') {
  139. unset($p['map'][$side]['db'][$i]);
  140. unset($p['map'][$side]['file'][$i]);
  141. }
  142. }
  143. }
  144. }
  145. $mapXml .= '<action type="dataflow/convert_mapper_column" method="map">'.$nl;
  146. $map = $p['map'][$this->getEntityType()];
  147. if (sizeof($map['db'])>0) {
  148. $from = $map[$import?'file':'db'];
  149. $to = $map[$import?'db':'file'];
  150. foreach ($from as $i=>$f) {
  151. $mapXml .= ' <var name="'.$f.'"><![CDATA['.$to[$i].']]></var>'.$nl;
  152. }
  153. }
  154. if ($p['map']['only_specified']) {
  155. $mapXml .= ' <var name="_only_specified">'.$p['map']['only_specified'].'</var>'.$nl;
  156. }
  157. $mapXml .= '</action>'.$nl.$nl;
  158. $parsers = array(
  159. 'product'=>'catalog/convert_parser_product',
  160. 'customer'=>'customer/convert_parser_customer',
  161. );
  162. if ($import) {
  163. $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="parse">'.$nl;
  164. $parseDataXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  165. $parseDataXml .= '</action>'.$nl.$nl;
  166. } else {
  167. $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="unparse">'.$nl;
  168. $parseDataXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  169. $parseDataXml .= '</action>'.$nl.$nl;
  170. }
  171. $adapters = array(
  172. 'product'=>'catalog/convert_adapter_product',
  173. 'customer'=>'customer/convert_adapter_customer',
  174. );
  175. if ($import) {
  176. $entityXml = '<action type="'.$adapters[$this->getEntityType()].'" method="save">'.$nl;
  177. $entityXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  178. $entityXml .= '</action>'.$nl.$nl;
  179. } else {
  180. $entityXml = '<action type="'.$adapters[$this->getEntityType()].'" method="load">'.$nl;
  181. $entityXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  182. foreach ($p[$this->getEntityType()]['filter'] as $f=>$v) {
  183. if (empty($v)) {
  184. continue;
  185. }
  186. if (is_scalar($v)) {
  187. $entityXml .= ' <var name="filter/'.$f.'"><![CDATA['.$v.']]></var>'.$nl;
  188. } elseif (is_array($v)) {
  189. foreach ($v as $a=>$b) {
  190. if (empty($b)) {
  191. continue;
  192. }
  193. $entityXml .= ' <var name="filter/'.$f.'/'.$a.'"><![CDATA['.$b.']]></var>'.$nl;
  194. }
  195. }
  196. }
  197. $entityXml .= '</action>'.$nl.$nl;
  198. }
  199. if ($import) {
  200. $xml = $interactiveXml.$fileXml.$parseFileXml.$mapXml.$parseDataXml.$entityXml;
  201. } else {
  202. $xml = $entityXml.$parseDataXml.$mapXml.$parseFileXml.$fileXml.$interactiveXml;
  203. }
  204. $this->setGuiData($p);
  205. $this->setActionsXml($xml);
  206. /*echo "<pre>".print_r($p,1)."</pre>";
  207. echo "<xmp>".$xml."</xmp>";
  208. die;*/
  209. return $this;
  210. }
  211. public function _parseGuiDataOrig()
  212. {
  213. $nl = "\r\n";
  214. $import = $this->getDirection()==='import';
  215. $p = $this->getGuiData();
  216. if ($this->getDataTransfer()==='interactive') {
  217. // $p['file']['type'] = 'file';
  218. // $p['file']['filename'] = $p['interactive']['filename'];
  219. // $p['file']['path'] = 'var/export';
  220. $interactiveXml = '<action type="varien/convert_adapter_http" method="'.($import?'load':'save').'">'.$nl;
  221. #$interactiveXml .= ' <var name="filename"><![CDATA['.$p['interactive']['filename'].']]></var>'.$nl;
  222. $interactiveXml .= '</action>';
  223. $fileXml = '';
  224. } else {
  225. $interactiveXml = '';
  226. $fileXml = '<action type="varien/convert_adapter_io" method="'.($import?'load':'save').'">'.$nl;
  227. $fileXml .= ' <var name="type">'.$p['file']['type'].'</var>'.$nl;
  228. $fileXml .= ' <var name="path">'.$p['file']['path'].'</var>'.$nl;
  229. $fileXml .= ' <var name="filename"><![CDATA['.$p['file']['filename'].']]></var>'.$nl;
  230. if ($p['file']['type']==='ftp') {
  231. $hostArr = explode(':', $p['file']['host']);
  232. $fileXml .= ' <var name="host"><![CDATA['.$hostArr[0].']]></var>'.$nl;
  233. if (isset($hostArr[1])) {
  234. $fileXml .= ' <var name="port"><![CDATA['.$hostArr[1].']]></var>'.$nl;
  235. }
  236. if (!empty($p['file']['passive'])) {
  237. $fileXml .= ' <var name="passive">true</var>'.$nl;
  238. }
  239. if (!empty($p['file']['user'])) {
  240. $fileXml .= ' <var name="user"><![CDATA['.$p['file']['user'].']]></var>'.$nl;
  241. }
  242. if (!empty($p['file']['password'])) {
  243. $fileXml .= ' <var name="password"><![CDATA['.$p['file']['password'].']]></var>'.$nl;
  244. }
  245. }
  246. $fileXml .= '</action>'.$nl.$nl;
  247. }
  248. switch ($p['parse']['type']) {
  249. case 'excel_xml':
  250. $parseFileXml = '<action type="varien/convert_parser_xml_excel" method="'.($import?'parse':'unparse').'">'.$nl;
  251. $parseFileXml .= ' <var name="single_sheet"><![CDATA['.($p['parse']['single_sheet']!==''?$p['parse']['single_sheet']:'_').']]></var>'.$nl;
  252. break;
  253. case 'csv':
  254. $parseFileXml = '<action type="varien/convert_parser_csv" method="'.($import?'parse':'unparse').'">'.$nl;
  255. $parseFileXml .= ' <var name="delimiter"><![CDATA['.$p['parse']['delimiter'].']]></var>'.$nl;
  256. $parseFileXml .= ' <var name="enclose"><![CDATA['.$p['parse']['enclose'].']]></var>'.$nl;
  257. break;
  258. }
  259. $parseFileXml .= ' <var name="fieldnames">'.$p['parse']['fieldnames'].'</var>'.$nl;
  260. $parseFileXml .= '</action>'.$nl.$nl;
  261. $mapXml = '';
  262. if (isset($p['map']) && is_array($p['map'])) {
  263. foreach ($p['map'] as $side=>$fields) {
  264. if (!is_array($fields)) {
  265. continue;
  266. }
  267. foreach ($fields['db'] as $i=>$k) {
  268. if ($k=='' || $k=='0') {
  269. unset($p['map'][$side]['db'][$i]);
  270. unset($p['map'][$side]['file'][$i]);
  271. }
  272. }
  273. }
  274. }
  275. $mapXml .= '<action type="varien/convert_mapper_column" method="map">'.$nl;
  276. $map = $p['map'][$this->getEntityType()];
  277. if (sizeof($map['db'])>0) {
  278. $from = $map[$import?'file':'db'];
  279. $to = $map[$import?'db':'file'];
  280. foreach ($from as $i=>$f) {
  281. $mapXml .= ' <var name="'.$f.'"><![CDATA['.$to[$i].']]></var>'.$nl;
  282. }
  283. }
  284. if ($p['map']['only_specified']) {
  285. $mapXml .= ' <var name="_only_specified">'.$p['map']['only_specified'].'</var>'.$nl;
  286. }
  287. $mapXml .= '</action>'.$nl.$nl;
  288. $parsers = array(
  289. 'product'=>'catalog/convert_parser_product',
  290. 'customer'=>'customer/convert_parser_customer',
  291. );
  292. if ($import) {
  293. $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="parse">'.$nl;
  294. $parseDataXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  295. $parseDataXml .= '</action>'.$nl.$nl;
  296. } else {
  297. $parseDataXml = '<action type="'.$parsers[$this->getEntityType()].'" method="unparse">'.$nl;
  298. $parseDataXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  299. $parseDataXml .= '</action>'.$nl.$nl;
  300. }
  301. $adapters = array(
  302. 'product'=>'catalog/convert_adapter_product',
  303. 'customer'=>'customer/convert_adapter_customer',
  304. );
  305. if ($import) {
  306. $entityXml = '<action type="'.$adapters[$this->getEntityType()].'" method="save">'.$nl;
  307. $entityXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  308. $entityXml .= '</action>'.$nl.$nl;
  309. } else {
  310. $entityXml = '<action type="'.$adapters[$this->getEntityType()].'" method="load">'.$nl;
  311. $entityXml .= ' <var name="store"><![CDATA['.$this->getStoreId().']]></var>'.$nl;
  312. foreach ($p[$this->getEntityType()]['filter'] as $f=>$v) {
  313. if (empty($v)) {
  314. continue;
  315. }
  316. if (is_scalar($v)) {
  317. $entityXml .= ' <var name="filter/'.$f.'"><![CDATA['.$v.']]></var>'.$nl;
  318. } elseif (is_array($v)) {
  319. foreach ($v as $a=>$b) {
  320. if (empty($b)) {
  321. continue;
  322. }
  323. $entityXml .= ' <var name="filter/'.$f.'/'.$a.'"><![CDATA['.$b.']]></var>'.$nl;
  324. }
  325. }
  326. }
  327. $entityXml .= '</action>'.$nl.$nl;
  328. }
  329. if ($import) {
  330. $xml = $interactiveXml.$fileXml.$parseFileXml.$mapXml.$parseDataXml.$entityXml;
  331. } else {
  332. $xml = $entityXml.$parseDataXml.$mapXml.$parseFileXml.$fileXml.$interactiveXml;
  333. }
  334. $this->setGuiData($p);
  335. $this->setActionsXml($xml);
  336. /*echo "<pre>".print_r($p,1)."</pre>";
  337. echo "<xmp>".$xml."</xmp>";
  338. die;*/
  339. return $this;
  340. }
  341. }