BxDolMMarket.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup DolphinMigration Dolphin Migration
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once('BxDolMData.php');
  12. bx_import('BxDolStorage');
  13. class BxDolMMarket extends BxDolMData
  14. {
  15. private $_sFilesPath = '';
  16. public function __construct(&$oMigrationModule, &$oDb)
  17. {
  18. parent::__construct($oMigrationModule, $oDb);
  19. $this -> _sModuleName = 'store';
  20. $this -> _sTableWithTransKey = 'bx_market_products';
  21. $this -> _sFilesPath = $this -> _oDb -> getExtraParam('root') . 'modules' . DIRECTORY_SEPARATOR . 'boonex' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR ;
  22. }
  23. public function getTotalRecords()
  24. {
  25. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "`");
  26. }
  27. public function runMigration()
  28. {
  29. if (!$this -> getTotalRecords())
  30. {
  31. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  32. return BX_MIG_SUCCESSFUL;
  33. }
  34. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_market'));
  35. $this -> createMIdField();
  36. $iProductId = $this -> getLastMIDField();
  37. $sStart = '';
  38. if ($iProductId)
  39. $sStart = " AND `id` >= {$iProductId}";
  40. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` {$sStart} ORDER BY `id`");
  41. $iCmts = 0;
  42. foreach($aResult as $iKey => $aValue)
  43. {
  44. $iProfileId = $this -> getProfileId((int)$aValue['author_id']);
  45. $iProductId = $this -> isItemExisted($aValue['id']);
  46. if (!$iProfileId)
  47. continue;
  48. $iCreated = isset($aValue['created']) ? $aValue['created'] : time();
  49. $sTitle = isset($aValue['title']) ? $aValue['title'] : '';
  50. if (!$iProductId)
  51. {
  52. $sQuery = $this -> _oDb -> prepare(
  53. "
  54. INSERT INTO
  55. `{$this -> _sTableWithTransKey}`
  56. SET
  57. `author` = ?,
  58. `added` = ?,
  59. `changed` = ?,
  60. `name` = ?,
  61. `title` = ?,
  62. `text` = ?,
  63. `cat` = ?,
  64. `views` = ?,
  65. `featured` = ?,
  66. `allow_view_to` = ?
  67. ",
  68. $iProfileId,
  69. $iCreated,
  70. $iCreated,
  71. $sTitle,
  72. $sTitle,
  73. isset($aValue['desc']) ? $aValue['desc'] : '',
  74. $this -> transferCategory($aValue['categories']),
  75. isset($aValue['views']) ? (int)$aValue['views'] : 0,
  76. isset($aValue['featured']) ? (int)$aValue['featured'] : 0,
  77. $this -> getPrivacy((int)$aValue['author_id'], isset($aValue['allow_view_product_to']) ? (int)$aValue['allow_view_product_to'] : 0, 'store', 'view_product')
  78. );
  79. $this -> _oDb -> query($sQuery);
  80. $iProductId = $this -> _oDb -> lastId();
  81. if (!$iProductId)
  82. {
  83. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_market_error', (int)$aValue['id']));
  84. return BX_MIG_FAILED;
  85. }
  86. $this -> setMID($iProductId, $aValue['id']);
  87. $aInfo = $this -> exportFiles($aValue['id'], $iProductId);
  88. $iCmts = $this -> transferComments($iProductId, (int)$aValue['id'], 'market');
  89. $this -> _iTransferred++;
  90. }
  91. $iPic = $this -> exportPictures($aValue['id'], $iProductId, (int)$aValue['thumb']);
  92. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}`
  93. SET
  94. `thumb` = :thumb,
  95. `cover` = :thumb,
  96. `comments` = :cmts,
  97. `price_single` = :price,
  98. `package` = :file_id
  99. WHERE `id` = :id", array('thumb' => $iPic, 'id' => $iProductId, 'cmts' => $iCmts, 'price' => $aInfo['price'], 'file_id' => $aInfo['file_id']));
  100. $this -> transferTags($aValue['id'], $iProductId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  101. }
  102. // set as finished;
  103. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_market_finished', $this -> _iTransferred, $iCmts));
  104. return BX_MIG_SUCCESSFUL;
  105. }
  106. private function exportFiles($iStoreId, $iProductId)
  107. {
  108. $aFiles = $this -> _mDb -> getAll("SELECT *
  109. FROM `bx_store_product_files` as `f1`
  110. LEFT JOIN `bx_files_main` as `f2` ON `f1`.`media_id` = `f2`.`ID`
  111. WHERE `entry_id` = :id", array('id' => $iStoreId));
  112. $aResult = array('price' => 0, 'file_id' => 0);
  113. $oZip = new ZipArchive();
  114. $i = 0;
  115. foreach($aFiles as $iKey => $aFile)
  116. {
  117. $sFilesPath = $this -> _sFilesPath . "{$aFile['media_id']}_" . sha1($aFile['Date']);
  118. $sName = $aFile['Title'];
  119. $sExt = $aFile['Ext'];
  120. if (file_exists($sFilesPath) && copy($sFilesPath, BX_DIRECTORY_PATH_TMP . "{$sName}.{$sExt}"))
  121. {
  122. if ($oZip -> open(BX_DIRECTORY_PATH_TMP . "{$sName}.zip", ZipArchive::CREATE) !== TRUE)
  123. continue;
  124. $oZip -> addFile(BX_DIRECTORY_PATH_TMP . "{$sName}.{$sExt}", "{$sName}.{$sExt}");
  125. $oZip -> close();
  126. $iProfileId = $this -> getProfileId((int)$aFile['Owner']);
  127. $oStorage = BxDolStorage::getObjectInstance('bx_market_files');
  128. $iId = $oStorage->storeFileFromPath(BX_DIRECTORY_PATH_TMP . "{$sName}.zip", true, $iProfileId, $iProductId);
  129. $sQuery = $this -> _oDb -> prepare("INSERT IGNORE INTO `bx_market_files2products` SET `content_id`=?, `file_id`=?, `version`='1.0', `order`=? ", $iProductId, $iId, $i++);
  130. $this -> _oDb-> query($sQuery);
  131. if (floatval($aFile['price']))
  132. {
  133. $aResult = array(
  134. 'price' => floatval($aFile['price']),
  135. 'file_id' => $iId
  136. );
  137. }
  138. }
  139. }
  140. return $aResult;
  141. }
  142. protected function transferCategory($sCategory, $sPrefix = 'bx_market', $sPreValueCateg = 'bx_market_cats', $iValue = 0, $sData = '')
  143. {
  144. return parent::transferCategory($sCategory, $sPrefix, $sPreValueCateg);
  145. }
  146. private function exportPictures($iStoreId, $iProductId, $iThumb)
  147. {
  148. $iImage = 0;
  149. $aPhotos = $this -> _mDb -> getAll("SELECT `p`.* FROM `bx_store_product_images` as `g`
  150. LEFT JOIN `bx_photos_main` as `p` ON `p`.`ID` = `g`.`media_id`
  151. WHERE `g`.`entry_id` = :id", array('id' => $iStoreId));
  152. $i = 0;
  153. foreach($aPhotos as $iKey => $aFile)
  154. {
  155. $sImageFile = $this -> _sImagePhotoFiles . "{$aFile['ID']}.{$aFile['Ext']}";
  156. $iProfileId = $this -> getProfileId((int)$aFile['Owner']);
  157. if (file_exists($sImageFile))
  158. {
  159. $oStorage = BxDolStorage::getObjectInstance('bx_market_photos');
  160. $iId = $oStorage->storeFileFromPath($sImageFile, false, $iProfileId);
  161. $oStorage -> afterUploadCleanup($iId, $iProfileId);
  162. $sQuery = $this -> _oDb -> prepare("INSERT IGNORE INTO `bx_market_photos2products` SET `content_id`=?, `file_id`=?, `title` = ?, `order`=?", $iProductId, $iId, $aFile['Title'], $i++);
  163. $this -> _oDb-> query($sQuery);
  164. if ($iThumb == $aFile['ID'])
  165. $iImage = $iId;
  166. }
  167. }
  168. return $iImage;
  169. }
  170. public function removeContent()
  171. {
  172. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  173. return false;
  174. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  175. $iNumber = 0;
  176. if (!empty($aRecords))
  177. {
  178. foreach($aRecords as $iKey => $aValue)
  179. {
  180. BxDolService::call('bx_market', 'delete_entity', array($aValue['id']));
  181. $iNumber++;
  182. }
  183. }
  184. parent::removeContent();
  185. return $iNumber;
  186. }
  187. }
  188. /** @} */