PageRenderTime 40ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Magiccart/Magicinstall/Model/Import/Cms.php

https://gitlab.com/blingbang2016/shop
PHP | 390 lines | 335 code | 18 blank | 37 comment | 38 complexity | 1778b9bcb92a4bb4d681b2b18ba0d16f MD5 | raw file
  1. <?php
  2. /**
  3. * Magiccart
  4. * @category Magiccart
  5. * @copyright Copyright (c) 2014 Magiccart (http://www.magiccart.net/)
  6. * @license http://www.magiccart.net/license-agreement.html
  7. * @Author: Magiccart<team.magiccart@gmail.com>
  8. * @@Create Date: 2014-07-25 17:02:47
  9. * @@Modify Date: 2015-11-06 17:12:09
  10. * @@Function:
  11. */
  12. ?>
  13. <?php
  14. class Magiccart_Magicinstall_Model_Import_Cms extends Mage_Core_Model_Abstract
  15. {
  16. private $_importPath;
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. //$this->_importPath = Mage::getModuleDir('etc', 'Magiccart_Magicinstall'). '/import/';
  21. $this->_importPath = Mage::getBaseDir().'/app/code/local/Magiccart/Magicinstall/etc/import/';
  22. }
  23. /**
  24. * Import CMS items
  25. * @param string model string
  26. * @param string name of the main XML node (and name of the XML file)
  27. * @param bool overwrite existing items
  28. */
  29. public function importCmsItems($typeModel, $typeImport, $theme, $overwrite = false, $storeIds=array(0))
  30. {
  31. try
  32. {
  33. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  34. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  35. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  36. $conflictingOldItems = array();
  37. $i = 0;
  38. if($xmlObj->getNode($typeImport)){
  39. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  40. //Check if block already exists
  41. $oldBlocks = Mage::getModel($typeModel)->getCollection()
  42. ->addFieldToFilter('identifier', $item->identifier)
  43. ->addStoreFilter($storeIds);
  44. //If items can be overwritten
  45. if ($overwrite){
  46. if (count($oldBlocks) > 0){
  47. $conflictingOldItems[] = $item->identifier;
  48. foreach ($oldBlocks as $old) $old->delete();
  49. }
  50. }else {
  51. if (count($oldBlocks) > 0){
  52. $conflictingOldItems[] = $item->identifier;
  53. continue;
  54. }
  55. }
  56. // var_dump(get_class_methods($item));die;
  57. Mage::getModel($typeModel)->setInstanceId($item->instance_id)
  58. ->setData($item->asArray())
  59. ->setStores($storeIds)
  60. ->save();
  61. $i++;
  62. }
  63. }
  64. //Final info
  65. if ($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of imported items: %s $typeImport", $i));
  66. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were imported"));
  67. if ($overwrite){
  68. if ($conflictingOldItems)
  69. Mage::getSingleton('adminhtml/session')->addSuccess(
  70. Mage::helper('adminhtml')->__('Items (%s) with the following identifiers were overwritten:<br />%s', count($conflictingOldItems), implode(', ', $conflictingOldItems))
  71. );
  72. }else{
  73. if ($conflictingOldItems)
  74. Mage::getSingleton('adminhtml/session')->addNotice(
  75. Mage::helper('adminhtml')->__('Unable to import items (%s) with the following identifiers (they already exist in the database):<br />%s', count($conflictingOldItems), implode(', ', $conflictingOldItems))
  76. );
  77. }
  78. }
  79. catch (Exception $e)
  80. {
  81. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  82. // Mage::logException($e);
  83. }
  84. }
  85. public function deleteCmsItems($typeModel, $typeImport, $theme, $storeIds=array(0))
  86. {
  87. try
  88. {
  89. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  90. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  91. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  92. $conflictingOldItems = array();
  93. $i = 0;
  94. if($xmlObj->getNode($typeImport)){
  95. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  96. $model = Mage::getModel($typeModel)->load($item->identifier);
  97. $storesOld = $model->getStoreId();
  98. $storeNew = array();
  99. if(is_array($storesOld)){
  100. foreach ($storesOld as $storeId) {
  101. if (!in_array($storeId, $storeIds)) $storeNew[] = $storeId;
  102. }
  103. } else {
  104. if (!in_array($storesOld, $storeIds)) $storeNew[] = $storesOld;
  105. }
  106. if (!$storeNew) $model->delete();
  107. else $model->setStores($storeNew)->save();
  108. $i++;
  109. }
  110. }
  111. //Final info
  112. if ($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of uninstall items: %s $typeImport", $i));
  113. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were uninstall"));
  114. }
  115. catch (Exception $e)
  116. {
  117. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  118. // Mage::logException($e);
  119. }
  120. }
  121. /**
  122. * Import Widget items
  123. * @param string model string
  124. * @param string name of the main XML node (and name of the XML file)
  125. * @param bool overwrite existing items
  126. */
  127. public function importWidgetItems($typeModel, $typeImport, $theme, $overwrite = false, $storeIds=array(0))
  128. {
  129. try
  130. {
  131. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  132. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  133. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  134. $i = 0;
  135. if($xmlObj->getNode($typeImport)){
  136. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  137. $model = Mage::getModel($typeModel)
  138. ->setData($item->asArray())
  139. ->setsStoreIds(implode(',', $storeIds))
  140. ->save();
  141. foreach($item->page as $object){
  142. Mage::getSingleton('magicinstall/resource_widget')->importInstancePage($model->getInstanceId(), $object, $item->sort_order, $item->xml);
  143. }
  144. $i++;
  145. }
  146. }
  147. //Final info
  148. if($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of imported items: %s $typeImport", $i));
  149. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were imported"));
  150. } catch (Exception $e){
  151. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  152. // Mage::logException($e);
  153. }
  154. }
  155. public function importMenuItems($typeModel, $typeImport, $theme, $overwrite = false, $storeIds=array(0))
  156. {
  157. try
  158. {
  159. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  160. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  161. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  162. $i = 0;
  163. if($xmlObj->getNode($typeImport)){
  164. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  165. //Check if Extra Menu already exists
  166. $oldMenus = Mage::getModel($typeModel)->getCollection()
  167. ->addFieldToFilter('link', $item->link)
  168. ->load();
  169. //If items can be overwritten
  170. $overwrite = false; // get in cfg
  171. if ($overwrite){
  172. if (count($oldMenus) > 0){
  173. foreach ($oldMenus as $old) $old->delete();
  174. }
  175. }else {
  176. if (count($oldMenus) > 0){
  177. continue;
  178. }
  179. }
  180. $model = Mage::getModel($typeModel)
  181. ->setData($item->asArray())
  182. ->setsStoreIds(implode(',', $storeIds))
  183. ->save();
  184. $i++;
  185. }
  186. }
  187. //Final info
  188. if($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of imported items: %s $typeImport", $i));
  189. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were imported"));
  190. } catch (Exception $e){
  191. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  192. // Mage::logException($e);
  193. }
  194. }
  195. public function importSlideItems($typeModel, $typeImport, $theme, $overwrite = false, $storeIds=array(0))
  196. {
  197. try
  198. {
  199. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  200. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  201. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  202. $i = 0;
  203. if($xmlObj->getNode($typeImport)){
  204. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  205. //Check if Extra Menu already exists
  206. $oldSlide = Mage::getModel($typeModel)->getCollection()
  207. ->addFieldToFilter('identifier', $item->identifier)
  208. ->load();
  209. //If items can be overwritten
  210. $overwrite = false; // get in cfg
  211. if ($overwrite){
  212. if (count($oldSlide) > 0){
  213. foreach ($oldSlide as $old) $old->delete();
  214. }
  215. }else {
  216. if (count($oldSlide) > 0){
  217. continue;
  218. }
  219. }
  220. $model = Mage::getModel($typeModel)->setData($item->asArray())->save();
  221. $i++;
  222. }
  223. }
  224. //Final info
  225. if($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of imported items: %s $typeImport", $i));
  226. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were imported"));
  227. } catch (Exception $e){
  228. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  229. // Mage::logException($e);
  230. }
  231. }
  232. public function importSystemConfig($typeModel, $typeImport, $theme, $scope = 'default', $storeIds=array(0))
  233. {
  234. try
  235. {
  236. $exception = array(
  237. 'magicproduct/identifier/',
  238. 'magiccategory/identifier/',
  239. );
  240. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  241. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  242. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  243. $i = 0;
  244. if($xmlObj->getNode($typeImport)){
  245. $model = Mage::getModel('core/config');
  246. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  247. $node = $item->asArray();
  248. // $item->value->asArray(); // get value path
  249. // $item->value->asArray(); // get value value
  250. //Check if Config already exists
  251. $continue = false;
  252. foreach ($exception as $exp) { // config for magicproduct and magiccategory
  253. $tmpstr = substr($node['path'], 0, strlen($exp));
  254. if($tmpstr == $exp){
  255. $model->saveConfig($node['path'], $node['value'], 'default', 0);
  256. $i++;
  257. $continue = true;
  258. }
  259. }
  260. if($continue) continue;
  261. if(is_array($storeIds)){
  262. foreach ($storeIds as $storeId) {
  263. if($scope == 'websites'){
  264. $oldValue = Mage::app()->getStore($storeId)->getWebsite()->getConfig($node['path']);
  265. }else{
  266. $oldValue = Mage::getStoreConfig($node['path'], $storeId);
  267. }
  268. if($oldValue != $node['value']){
  269. $model->saveConfig($item->path, $node['value'], $scope, $storeId);
  270. $i++;
  271. }
  272. }
  273. } else {
  274. $oldValue = Mage::getStoreConfig($node['path'], $storeIds);
  275. if($oldValue != $node['value']){
  276. $model->saveConfig($node['path'], $node['value'], $scope, $storeIds);
  277. $i++;
  278. }
  279. }
  280. }
  281. }
  282. //Final info
  283. if($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of imported items: %s $typeImport", $i));
  284. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were imported"));
  285. } catch (Exception $e){
  286. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  287. // Mage::logException($e);
  288. }
  289. }
  290. public function deleteSystemConfig($typeModel, $typeImport, $theme, $storeIds=array(0))
  291. {
  292. try
  293. {
  294. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  295. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  296. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  297. $i = 0;
  298. if($xmlObj->getNode($typeImport)){
  299. $model = Mage::getModel($typeModel);
  300. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  301. $node = $item->asArray();
  302. // $item->value->asArray(); // get value path
  303. // $item->value->asArray(); // get value value
  304. //Check if Config already exists
  305. if(is_array($storeIds)){
  306. foreach ($storeIds as $storeId) {
  307. $oldValue = Mage::getStoreConfig($node['path'], $storeId);
  308. if($oldValue != $node['value']){
  309. $model->deleteConfig($node['path'], 'stores', $storeId);
  310. $i++;
  311. }
  312. }
  313. } else {
  314. $oldValue = Mage::getStoreConfig($node['path'], $storeIds);
  315. if($oldValue != $node['value']){
  316. $model->deleteConfig($node['path'], 'stores', $storeIds);
  317. $i++;
  318. }
  319. }
  320. }
  321. }
  322. //Final info
  323. if ($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of uninstall items: %s $typeImport", $i));
  324. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were uninstall"));
  325. }
  326. catch (Exception $e)
  327. {
  328. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  329. // Mage::logException($e);
  330. }
  331. }
  332. public function importPermissionsItems($typeModel, $typeImport, $theme, $overwrite = false, $storeIds=array(0))
  333. {
  334. try
  335. {
  336. $xmlPath = $this->_importPath . $theme .DIRECTORY_SEPARATOR. $typeImport . '.xml';
  337. if (!is_readable($xmlPath)) throw new Exception(Mage::helper('adminhtml')->__("Can't read data file: %s", $xmlPath));
  338. $xmlObj = new Varien_Simplexml_Config($xmlPath);
  339. $i = 0;
  340. if($xmlObj->getNode($typeImport)){
  341. foreach ($xmlObj->getNode($typeImport)->children() as $item){
  342. //Check if Permissions Block already exists
  343. $oldSlide = Mage::getModel($typeModel)->getCollection()
  344. ->addFieldToFilter('block_name', $item->block_name)
  345. ->load();
  346. if (count($oldSlide) > 0) continue;
  347. else Mage::getModel($typeModel)->setData($item->asArray())->save();
  348. $i++;
  349. }
  350. }
  351. //Final info
  352. if($i) Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__("Number of imported items: %s $typeImport", $i));
  353. else Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('adminhtml')->__("No $typeImport items were imported"));
  354. } catch (Exception $e){
  355. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  356. // Mage::logException($e);
  357. }
  358. }
  359. }