1
0

BxDolMSimpleMessenger.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 BxDolMSimpleMessenger extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'simple_messenger';
  19. $this -> _sTableWithTransKey = 'bx_messenger_jots';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return $this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "`");
  24. }
  25. private function getLotId($iSenderId, $iRecipientId)
  26. {
  27. $iLot = $this -> _oDb -> getOne("SELECT `id` FROM `bx_messenger_lots` WHERE (`participants` = :l1 OR `participants` = :l2) AND `type`=2 AND `author` != 0 LIMIT 1", array('l1' => "{$iSenderId},{$iRecipientId}", 'l2' => "{$iRecipientId},{$iSenderId}"));
  28. if ((int)$iLot)
  29. return $iLot;
  30. $this -> _oDb -> query("INSERT INTO `bx_messenger_lots` SET `created` = UNIX_TIMESTAMP(), `author`= :author, `participants` = :parts, `type` = 2", array('parts' => "{$iSenderId},{$iRecipientId}", 'author' => $iSenderId));
  31. return $this -> _oDb -> lastId();
  32. }
  33. public function runMigration()
  34. {
  35. if (!$this -> getTotalRecords())
  36. {
  37. $this -> setResultStatus(_t('_bx_dolphin_migration_no_data_to_transfer'));
  38. return BX_MIG_SUCCESSFUL;
  39. }
  40. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_simple_messanger'));
  41. $this -> createMIdField();
  42. $aMessages = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` ORDER BY `ID`");
  43. foreach($aMessages as $iMes => $aMessage)
  44. {
  45. $iMessageId = $this -> isItemExisted($aMessage['ID']);
  46. $iSenderId = $this -> getProfileId((int)$aMessage['SenderID']);
  47. $iRecipientId = $this -> getProfileId((int)$aMessage['RecipientID']);
  48. if (!$iMessageId && $iSenderId && $iRecipientId)
  49. {
  50. $sQuery = $this -> _oDb -> prepare(
  51. "
  52. INSERT INTO
  53. `{$this -> _sTableWithTransKey}`
  54. SET
  55. `lot_id` = ?,
  56. `message` = ?,
  57. `user_id` = ?,
  58. `created` = ?
  59. ",
  60. $this -> getLotId($iSenderId, $iRecipientId),
  61. $aMessage['Message'],
  62. $iSenderId,
  63. strtotime($aMessage['Date'])
  64. );
  65. $this -> _oDb -> query($sQuery);
  66. $iMessageId = $this -> _oDb -> lastId();
  67. if (!$iMessageId)
  68. {
  69. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_simple_messanger_error', (int)$aMessage['ID']));
  70. return BX_MIG_FAILED;
  71. }
  72. $this -> setMID($iMessageId, $aMessage['ID']);
  73. }
  74. $this -> _iTransferred++;
  75. }
  76. // set as finished;
  77. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_simple_messanger_finished', $this -> _iTransferred));
  78. return BX_MIG_SUCCESSFUL;
  79. }
  80. public function removeContent()
  81. {
  82. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  83. return false;
  84. $aRecords = $this -> _oDb -> getAll("SELECT * FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0");
  85. $aLots = $this -> _oDb -> getPairs("SELECT `lot_id` FROM `{$this -> _sTableWithTransKey}` WHERE `{$this -> _sTransferFieldIdent}` !=0 GROUP BY `lot_id`", 'lot_id', 'lot_id');
  86. parent::removeContent();
  87. $iNumber = 0;
  88. if (!empty($aRecords))
  89. {
  90. foreach($aRecords as $iKey => $aValue)
  91. {
  92. $this -> _oDb -> query("DELETE FROM `{$this -> _sTableWithTransKey}` WHERE `id`=:id", array('id' => $aValue['id']));
  93. $iNumber++;
  94. }
  95. }
  96. if (!empty($aLots))
  97. {
  98. $sIn = "WHERE `id` IN (" . implode(',', $aLots) . ")";
  99. $this -> _oDb -> query("DELETE FROM `bx_messenger_lots` {$sIn}");
  100. }
  101. return $iNumber;
  102. }
  103. }
  104. /** @} */