BxDolMChat.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 BxDolMChat extends BxDolMData
  14. {
  15. private $_sMigField = 'c_mig_id';
  16. public function __construct(&$oMigrationModule, &$oDb)
  17. {
  18. parent::__construct($oMigrationModule, $oDb);
  19. $this -> _sModuleName = 'chat';
  20. $this -> _sTableWithTransKey = 'bx_messenger_jots';
  21. }
  22. public function getTotalRecords()
  23. {
  24. return $this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "`");
  25. }
  26. /**
  27. * Transfer Flash chat rooms
  28. *
  29. * @return array - created Lots ids associated with Dolphin chat rooms
  30. *
  31. */
  32. private function transferRooms()
  33. {
  34. $aRooms = $this -> _mDb -> getAll("SELECT * FROM `RayChatRooms`");
  35. if (empty($aRooms))
  36. return false;
  37. $aResult = array();
  38. foreach($aRooms as $iKey => $aRoom)
  39. {
  40. $aUsers = $this -> _mDb -> getPairs("SELECT `Sender` FROM `RayChatHistory` WHERE `Room`=:Room GROUP BY `Sender`", 'Sender', 'Sender', array('Room' => $aRoom['ID']));
  41. if (empty($aUsers))
  42. continue;
  43. foreach($aUsers as $iKey => $iId)
  44. $aRealProfiles[] = $this -> getProfileId($iId);
  45. $aParams = array('title' => $aRoom['Name'], 'parts' => implode(',', $aRealProfiles));
  46. if (!($ilotId = $this -> _oDb -> getOne("SELECT `id` FROM `bx_messenger_lots` WHERE `title` = :title AND `participants` = :parts AND `type` = 2", $aParams)))
  47. {
  48. $this -> _oDb -> query("INSERT INTO `bx_messenger_lots` SET `created` = UNIX_TIMESTAMP(), `title` = :title, `participants` = :parts, `type` = 2", $aParams);
  49. $ilotId = $this -> _oDb -> lastId();
  50. }
  51. $aResult[$aRoom['ID']] = $ilotId;
  52. }
  53. return $aResult;
  54. }
  55. public function runMigration()
  56. {
  57. $aLots = $this -> transferRooms();
  58. if (!$this -> getTotalRecords() || empty($aLots))
  59. {
  60. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  61. return BX_MIG_SUCCESSFUL;
  62. }
  63. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_simple_messanger'));
  64. $this -> createMIdField($this -> _sMigField);
  65. $aMessages = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` ORDER BY `ID`");
  66. foreach($aMessages as $iMes => $aMessage)
  67. {
  68. $iMessageId = $this -> isItemExisted($aMessage['ID'], 'id', $this -> _sMigField);
  69. $iSenderId = $this -> getProfileId((int)$aMessage['Sender']);
  70. if (!$iMessageId && $iSenderId)
  71. {
  72. $sQuery = $this -> _oDb -> prepare(
  73. "
  74. INSERT INTO
  75. `{$this -> _sTableWithTransKey}`
  76. SET
  77. `lot_id` = ?,
  78. `message` = ?,
  79. `user_id` = ?,
  80. `created` = ?
  81. ",
  82. $aLots[$aMessage['Room']],
  83. $aMessage['Message'],
  84. $iSenderId,
  85. $aMessage['When']
  86. );
  87. $this -> _oDb -> query($sQuery);
  88. $iMessageId = $this -> _oDb -> lastId();
  89. if (!$iMessageId)
  90. {
  91. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_simple_messanger_error', (int)$aMessage['ID']));
  92. return BX_MIG_FAILED;
  93. }
  94. $this -> setMID($iMessageId, $aMessage['ID'], 'id', $this -> _sMigField);
  95. $this -> _iTransferred++;
  96. }
  97. }
  98. // set as finished;
  99. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_simple_messanger_finished', $this -> _iTransferred));
  100. return BX_MIG_SUCCESSFUL;
  101. }
  102. public function dropMID($sIdentFieldName = '', $sTableName = '')
  103. {
  104. return parent::dropMID($this -> _sMigField);
  105. }
  106. public function removeContent()
  107. {
  108. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sMigField))
  109. return false;
  110. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sMigField}` !=0");
  111. $aLots = $this -> _oDb -> getPairs("SELECT `lot_id` FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sMigField}` !=0 GROUP BY `lot_id`", 'lot_id', 'lot_id');
  112. $iNumber = 0;
  113. if (!empty($aRecords))
  114. {
  115. foreach($aRecords as $iKey => $aValue)
  116. {
  117. $this -> _oDb -> query("DELETE FROM `{$this -> _sTableWithTransKey}` WHERE `id`=:id", array('id' => $aValue['id']));
  118. $iNumber++;
  119. }
  120. }
  121. if (!empty($aLots))
  122. {
  123. $sIn = "WHERE `id` IN (" . implode(',', $aLots) . ")";
  124. $this -> _oDb -> query("DELETE FROM `bx_messenger_lots` {$sIn}");
  125. }
  126. parent::removeContent();
  127. return $iNumber;
  128. }
  129. }
  130. /** @} */