BxDolMGroups.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 BxDolMGroups extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'groups';
  19. $this -> _sTableWithTransKey = 'bx_groups_data';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `status` = 'approved'");
  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_groups'));
  33. $this -> createMIdField();
  34. $iGroupId = $this -> getLastMIDField();
  35. $sStart = '';
  36. if ($iGroupId)
  37. $sStart = " AND `id` >= {$iGroupId}";
  38. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `status` = 'approved' {$sStart} ORDER BY `id`");
  39. $iCmts = 0;
  40. foreach($aResult as $iKey => $aValue)
  41. {
  42. $iProfileId = $this -> getProfileId((int)$aValue['author_id']);
  43. $iGroupId = $this -> isItemExisted($aValue['id']);
  44. if (!$iProfileId)
  45. continue;
  46. if (!$iGroupId)
  47. {
  48. $sQuery = $this -> _oDb -> prepare(
  49. "
  50. INSERT INTO
  51. `bx_groups_data`
  52. SET
  53. `author` = ?,
  54. `added` = ?,
  55. `changed` = ?,
  56. `group_name` = ?,
  57. `group_desc` = ?,
  58. `group_cat` = ?,
  59. `views` = ?,
  60. `featured` = ?,
  61. `allow_view_to` = ?,
  62. `join_confirmation` = ?
  63. ",
  64. $iProfileId,
  65. isset($aValue['created']) ? $aValue['created'] : time(),
  66. isset($aValue['created']) ? $aValue['created'] : time(),
  67. isset($aValue['title']) ? $aValue['title'] : '',
  68. isset($aValue['desc']) ? $aValue['desc'] : '',
  69. $this -> transferCategory($aValue['categories']),
  70. isset($aValue['views']) ? (int)$aValue['views'] : 0,
  71. isset($aValue['featured']) ? (int)$aValue['featured'] : 0,
  72. $this -> getPrivacy((int)$aValue['author_id'], isset($aValue['allow_view_group_to']) ? (int)$aValue['allow_view_group_to'] : 0, 'groups', 'view_group'),
  73. isset($aValue['join_confirmation']) ? (int)$aValue['join_confirmation'] : 0
  74. );
  75. $this -> _oDb -> query($sQuery);
  76. $iGroupId = $this -> _oDb -> lastId();
  77. if (!$iGroupId)
  78. {
  79. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_groups_error', (int)$aValue['id']));
  80. return BX_MIG_FAILED;
  81. }
  82. $this -> setMID($iGroupId, $aValue['id']);
  83. $this -> exportFans($aValue['id'], $this-> addGroupToProfiles($iProfileId, $iGroupId));
  84. }
  85. $iCmts = $this -> transferComments($iGroupId, (int)$aValue['id'], 'groups');
  86. $iPic = $this -> exportPictures($aValue['id'], $iProfileId);
  87. $this -> _iTransferred++;
  88. $this -> _oDb -> query("UPDATE `{$this -> _sTableWithTransKey}`
  89. SET
  90. `picture` = :pic,
  91. `comments` = :cmts
  92. WHERE `id` = :id", array('pic' => $iPic, 'id' => $iGroupId, 'cmts' => $iCmts));
  93. $this -> transferTags((int)$aValue['id'], $iGroupId, $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['type'], $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['keywords']);
  94. }
  95. // set as finished;
  96. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_groups_finished', $this -> _iTransferred, $iCmts));
  97. return BX_MIG_SUCCESSFUL;
  98. }
  99. protected function transferCategory($sCategory, $sPrefix = 'bx_groups', $sPreValueCateg = 'bx_groups_cats', $iValue = 0, $sData = '')
  100. {
  101. return parent::transferCategory($sCategory, $sPrefix, $sPreValueCateg);
  102. }
  103. /**
  104. * Transfer groups pictures
  105. *
  106. * @param int $iGroupId Dolphin group ID
  107. * @param int $iProfileId UNA profile ID
  108. * @return Return description
  109. */
  110. private function exportPictures($iGroupId, $iProfileId)
  111. {
  112. $iFirstImage = 0;
  113. $iProfileId = (int)$iProfileId;
  114. $aPhotos = $this -> _mDb -> getAll("SELECT `p`.* FROM `bx_photos_main` as `p`
  115. LEFT JOIN `bx_groups_images` as `g` ON `p`.`ID` = `g`.`media_id`
  116. WHERE `g`.`entry_id` = :id", array('id' => $iGroupId));
  117. if (empty($aPhotos))
  118. return false;
  119. foreach($aPhotos as $iKey => $aFile)
  120. {
  121. $sImageFile = $this -> _sImagePhotoFiles . "{$aFile['ID']}.{$aFile['Ext']}";
  122. if (file_exists($sImageFile))
  123. {
  124. $oStorage = BxDolStorage::getObjectInstance('bx_groups_pics');
  125. $iId = $oStorage->storeFileFromPath($sImageFile, false, $iProfileId);
  126. $oStorage -> afterUploadCleanup($iId, $iProfileId);
  127. $iFirstImage = $iFirstImage ? $iFirstImage : $iId;
  128. }
  129. }
  130. return $iFirstImage;
  131. }
  132. private function addGroupToProfiles($iProfileId, $iGroupId)
  133. {
  134. $sQuery = $this -> _oDb -> prepare("
  135. INSERT INTO
  136. `sys_profiles`
  137. SET
  138. `account_id` = ?,
  139. `type` = 'bx_groups',
  140. `content_id` = ?,
  141. `status` = 'active'
  142. ", (int)BxDolProfile::getInstance($iProfileId)->getAccountId(), $iGroupId);
  143. $this -> _oDb -> query($sQuery);
  144. return $this -> _oDb -> lastId();
  145. }
  146. private function exportFans($iMID, $iGroupProfileId)
  147. {
  148. $aFans = $this -> _mDb -> getAll("SELECT `f` . * , IF( `a`.`id_entry` IS NULL , 0, 1 ) AS `admin`
  149. FROM `bx_groups_fans` AS `f`
  150. LEFT JOIN `bx_groups_admins` AS `a` ON `f`.`id_entry` = `a`.`id_entry` AND `f`.`id_profile` = `a`.`id_profile`
  151. WHERE `f`.`id_entry`=:id ", array('id' => $iMID));
  152. foreach($aFans as $iKey => $aValue)
  153. {
  154. $iProfileId = $this -> getProfileId($aValue['id_profile']);
  155. $sQuery = $this -> _oDb -> prepare("
  156. INSERT IGNORE INTO
  157. `bx_groups_fans`
  158. SET
  159. `initiator` = ?,
  160. `content` = ?,
  161. `mutual` = ?,
  162. `added` = ?
  163. ", $iProfileId, $iGroupProfileId, $aValue['confirmed'], $aValue['when']);
  164. if(!$this -> _oDb -> query($sQuery))
  165. return _t('_bx_dolphin_migration_friends_exports_error');
  166. if ($aValue['confirmed']) {
  167. $sQuery = $this->_oDb->prepare("
  168. INSERT IGNORE INTO
  169. `bx_groups_fans`
  170. SET
  171. `initiator` = ?,
  172. `content` = ?,
  173. `mutual` = ?,
  174. `added` = ?
  175. ", $iGroupProfileId, $iProfileId, 1, $aValue['when']);
  176. if(!$this -> _oDb -> query($sQuery))
  177. return _t('_bx_dolphin_migration_friends_exports_error');
  178. }
  179. if ((int)$aValue['admin']) {
  180. $this->_oDb->query("REPLACE INTO `bx_groups_admins` SET `fan_id` = :fan, `group_profile_id` = :group",
  181. array('fan' => $iProfileId, 'group' => $iGroupProfileId));
  182. }
  183. }
  184. }
  185. public function removeContent()
  186. {
  187. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  188. return false;
  189. $iNumber = 0;
  190. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  191. if (!empty($aRecords))
  192. {
  193. foreach($aRecords as $iKey => $aValue)
  194. {
  195. BxDolService::call('bx_groups', 'delete_entity_service', array($aValue['id'], true));
  196. $iNumber++;
  197. }
  198. }
  199. parent::removeContent();
  200. return $iNumber;
  201. }
  202. }
  203. /** @} */