BxDolMBlogs.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 BxDolMBlogs extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'blogs';
  19. $this -> _sTableWithTransKey = 'bx_posts_posts';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "`");
  24. }
  25. public function runMigration()
  26. {
  27. if (!$this -> getTotalRecords())
  28. {
  29. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  30. return BX_MIG_SUCCESSFUL;
  31. }
  32. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_blogs'));
  33. $this -> createMIdField();
  34. $iBlogId = $this -> getLastMIDField();
  35. $sStart = '';
  36. if ($iBlogId)
  37. $sStart = " WHERE `PostID` >= {$iBlogId}";
  38. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` {$sStart} ORDER BY `PostID`");
  39. $iCmts = 0;
  40. foreach($aResult as $iKey => $aValue)
  41. {
  42. $iProfileId = $this -> getProfileId((int)$aValue['OwnerID']);
  43. $iBlogId = $this -> isItemExisted($aValue['PostID']);
  44. if (!$iProfileId)
  45. continue;
  46. if (!$iBlogId)
  47. {
  48. $sQuery = $this -> _oDb -> prepare(
  49. "
  50. INSERT INTO
  51. `{$this -> _sTableWithTransKey}`
  52. SET
  53. `author` = ?,
  54. `added` = ?,
  55. `changed` = ?,
  56. `thumb` = 0,
  57. `title` = ?,
  58. `text` = ?,
  59. `cat` = ?,
  60. `views` = ?,
  61. `featured` = ?
  62. ",
  63. $iProfileId,
  64. isset($aValue['PostDate']) ? $aValue['PostDate'] : time(),
  65. isset($aValue['PostDate']) ? $aValue['PostDate'] : time(),
  66. isset($aValue['PostCaption']) ? $aValue['PostCaption'] : time() + 1,
  67. isset($aValue['PostText']) ? $aValue['PostText'] : '',
  68. $this -> transferCategory($aValue['Categories']),
  69. isset($aValue['Views']) ? (int)$aValue['Views'] : 0,
  70. isset($aValue['Featured']) ? (int)$aValue['Featured'] : 0
  71. );
  72. $this -> _oDb -> query($sQuery);
  73. $iBlogId = $this -> _oDb -> lastId();
  74. if (!$iBlogId){
  75. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_blogs_error', (int)$aValue['PostID']));
  76. return BX_MIG_FAILED;
  77. }
  78. $this -> setMID($iBlogId, $aValue['PostID']);
  79. }
  80. if (isset($aValue['PostPhoto']))
  81. $this -> exportThumb($iBlogId, $aValue['PostPhoto'], $iProfileId);
  82. $iCmts = $this -> transferComments($iBlogId, (int)$aValue['PostID'], 'blogs');
  83. $this -> _iTransferred++;
  84. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}` SET `allow_view_to` = :privacy, `comments` = :cmts WHERE `id` = :id", array('privacy' => $aValue['allowView'], 'id' => $iBlogId, 'cmts' => $iCmts));
  85. $this -> transferTags((int)$aValue['PostID'], $iBlogId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  86. }
  87. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_blogs_finished', $this -> _iTransferred));
  88. return BX_MIG_SUCCESSFUL;
  89. }
  90. protected function transferCategory($sCategory, $sPrefix = 'bx_posts', $sPreValueCateg = 'bx_posts_cats')
  91. {
  92. return parent::transferCategory($sCategory, $sPrefix, $sPreValueCateg);
  93. }
  94. /**
  95. * Export blog's thumbnail
  96. *
  97. * @param integer $iEntryId una blog Id
  98. * @param string $sPhotoName photo file name
  99. * @param integer $iProfileId profile id
  100. * @return void
  101. */
  102. private function exportThumb($iEntryId, $sPhotoName, $iProfileId)
  103. {
  104. if (!$sPhotoName || $this -> _oDb -> getOne("SELECT `thumb` FROM `{$this -> _sTableWithTransKey}` WHERE `id` = :id", array('id' => $iEntryId)))
  105. return;
  106. $sBlogImgePath = $this -> _oDb -> getExtraParam('root') . 'media' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . "blog" . DIRECTORY_SEPARATOR ."orig_{$sPhotoName}";
  107. if($sBlogImgePath)
  108. {
  109. $oFileStorage = BxDolStorage::getObjectInstance('bx_posts_files');
  110. $iFile = $oFileStorage -> storeFileFromPath($sBlogImgePath, false, $iProfileId, (int)$iEntryId);
  111. if ($iFile)
  112. {
  113. $oFileStorage -> afterUploadCleanup($iFile, $iProfileId);
  114. $sQuery = $this -> _oDb -> prepare("UPDATE `{$this -> _sTableWithTransKey}` SET `thumb` = ? WHERE `id` = ?", $iFile, $iEntryId);
  115. $this -> _oDb -> query($sQuery);
  116. }
  117. }
  118. }
  119. public function removeContent()
  120. {
  121. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  122. return false;
  123. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  124. $iNumber = 0;
  125. if (!empty($aRecords))
  126. {
  127. foreach($aRecords as $iKey => $aValue)
  128. {
  129. BxDolService::call('bx_posts', 'delete_entity', array($aValue['id']));
  130. $iNumber++;
  131. }
  132. }
  133. parent::removeContent();
  134. return $iNumber;
  135. }
  136. }
  137. /** @} */