BxDolMShoutBox.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 BxDolMShoutBox extends BxDolMData
  14. {
  15. /**
  16. * @var shout box Lot id in Jot Messenger
  17. */
  18. private $_iLotID = 2;
  19. private $_sMigField = 'shoutbx_id';
  20. public function __construct(&$oMigrationModule, &$oDb)
  21. {
  22. parent::__construct($oMigrationModule, $oDb);
  23. $this -> _sModuleName = 'shoutbox';
  24. $this -> _sTableWithTransKey = 'bx_messenger_jots';
  25. }
  26. public function getTotalRecords()
  27. {
  28. return $this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `HandlerID` = 0 AND `OwnerID` != 0 ");
  29. }
  30. public function runMigration()
  31. {
  32. if (!$this -> getTotalRecords())
  33. {
  34. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  35. return BX_MIG_SUCCESSFUL;
  36. }
  37. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_shoutbox'));
  38. $this -> createMIdField($this -> _sMigField);
  39. $aMessages = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `HandlerID` = 0 AND `OwnerID` != 0 ORDER BY `ID`");
  40. $aParticipantsList = array();
  41. foreach($aMessages as $iMes => $aMessage)
  42. {
  43. $iMessageId = $this -> isItemExisted($aMessage['ID'], 'id', $this -> _sMigField);
  44. $iProfileId = $this -> getProfileId((int)$aMessage['OwnerID']);
  45. if (!$iProfileId)
  46. continue;
  47. if (!$iMessageId && $iProfileId)
  48. {
  49. $sQuery = $this -> _oDb -> prepare(
  50. "
  51. INSERT INTO
  52. `{$this -> _sTableWithTransKey}`
  53. SET
  54. `lot_id` = {$this -> _iLotID},
  55. `message` = ?,
  56. `user_id` = ?,
  57. `created` = ?
  58. ",
  59. $aMessage['Message'],
  60. $iProfileId,
  61. strtotime($aMessage['Date'])
  62. );
  63. $this -> _oDb -> query($sQuery);
  64. $iMessageId = $this -> _oDb -> lastId();
  65. if (!$iMessageId)
  66. {
  67. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_shoutbox_error', (int)$aMessage['ID']));
  68. return BX_MIG_FAILED;
  69. }
  70. if (!in_array($iProfileId, $aParticipantsList))
  71. array_push($aParticipantsList, $iProfileId);
  72. $this -> setMID($iMessageId, $aMessage['ID'], 'id', $this -> _sMigField);
  73. }
  74. $this -> _iTransferred++;
  75. }
  76. if (!empty($aParticipantsList))
  77. $this -> _oDb -> query("UPDATE `bx_messenger_lots`
  78. SET
  79. `participants` = :parts
  80. WHERE `id` = :id", array('parts' => implode(',', $aParticipantsList), 'id' => $this -> _iLotID));
  81. // set as finished;
  82. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_shoutbox_finished', $this -> _iTransferred));
  83. return BX_MIG_SUCCESSFUL;
  84. }
  85. public function dropMID($sIdentFieldName = '', $sTableName = '')
  86. {
  87. return parent::dropMID($this -> _sMigField);
  88. }
  89. public function removeContent()
  90. {
  91. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sMigField))
  92. return false;
  93. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sMigField}` !=0 ");
  94. $iNumber = 0;
  95. if (!empty($aRecords))
  96. {
  97. foreach($aRecords as $iKey => $aValue)
  98. {
  99. $this -> _oDb -> query("DELETE FROM `{$this -> _sTableWithTransKey}` WHERE `id`=:id", array('id' => $aValue['id']));
  100. $iNumber++;
  101. }
  102. }
  103. parent::removeContent();
  104. return $iNumber;
  105. }
  106. }
  107. /** @} */