BxDolMConv.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 BxDolMConv extends BxDolMData
  14. {
  15. public function __construct(&$oMigrationModule, &$oDb)
  16. {
  17. parent::__construct($oMigrationModule, $oDb);
  18. $this -> _sModuleName = 'conversations';
  19. $this -> _sTableWithTransKey = 'bx_convos_cmts';
  20. }
  21. public function getTotalRecords()
  22. {
  23. return (int)$this -> _mDb -> getOne("SELECT COUNT(*) FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` WHERE `Type` = 'letter' AND `Sender` <> `Recipient` ORDER BY `ID`");
  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_conversations'));
  33. $aResult = $this -> _mDb -> getAll("SELECT * FROM `" . $this -> _oConfig -> _aMigrationModules[$this -> _sModuleName]['table_name'] . "` ORDER BY `ID`");
  34. $iCmts = 0;
  35. foreach($aResult as $iKey => $aValue)
  36. {
  37. $iSenderId = $this -> getProfileId((int)$aValue['Sender']);
  38. $iRecipientId = $this -> getProfileId((int)$aValue['Recipient']);
  39. $iDate = isset($aValue['Date']) ? strtotime($aValue['Date']) : time();
  40. if (!$iSenderId || !$iRecipientId || ($iSenderId == $iRecipientId))
  41. continue;
  42. $iConvId = $this -> isConvExisted($aValue['Subject'], $iSenderId, $iRecipientId);
  43. if (!$iConvId)
  44. {
  45. $sQuery = $this -> _oDb -> prepare(
  46. "
  47. INSERT INTO
  48. `bx_convos_conversations`
  49. SET
  50. `author` = ?,
  51. `added` = ?,
  52. `changed` = ?,
  53. `views` = ?,
  54. `last_reply_timestamp` = ?,
  55. `text` = ?
  56. ",
  57. $iSenderId,
  58. $iDate,
  59. $iDate,
  60. $aValue['New'] ? 0 : 1,
  61. $iDate,
  62. $aValue['Subject']
  63. );
  64. $this -> _oDb -> query($sQuery);
  65. $iConvId = $this -> _oDb -> lastId();
  66. if (!$iConvId){
  67. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_conv_error', (int)$aValue['ID']));
  68. return BX_MIG_FAILED;
  69. }
  70. $this -> updateConvFolder($iSenderId, stripos($aValue['Trash'], 'sender') !== FALSE ? 4 : 1, $iConvId);
  71. $this -> updateConvFolder($iRecipientId, stripos($aValue['Trash'], 'recipient') !== FALSE ? 4 : 1, $iConvId);
  72. }
  73. $this -> createMIdField();
  74. if (!($iMessId = $this -> isItemExisted($aValue['ID'], 'cmt_id')))
  75. {
  76. $this -> _oDb -> query("INSERT INTO `bx_convos_cmts` (`cmt_id`, `cmt_parent_id`, `cmt_vparent_id`, `cmt_object_id`, `cmt_author_id`, `cmt_level`, `cmt_text`, `cmt_time`, `cmt_replies`, `cmt_rate`, `cmt_rate_count`)
  77. VALUES (NULL, 0, 0, :object, :user, 0, :message, :time, 0, 0, 0)",
  78. array(
  79. 'object' => $iConvId,
  80. 'user' => $iSenderId,
  81. 'message' => $aValue['Text'],
  82. 'time' => $iDate
  83. ));
  84. $iMessId = $this -> _oDb -> lastId();
  85. $this -> setMID($iMessId, $aValue['ID'], 'cmt_id');
  86. }
  87. $this -> _iTransferred ++;
  88. $this -> _oDb -> query("UPDATE `bx_convos_conversations`
  89. SET
  90. `comments` = `comments` + 1,
  91. `last_reply_profile_id` = :lpi,
  92. `last_reply_comment_id` = :lci,
  93. `last_reply_timestamp` = :time
  94. WHERE `id` = :id",
  95. array(
  96. 'lpi' => $iSenderId,
  97. 'lci' => $iMessId,
  98. 'id' => $iMessId,
  99. 'time' => $iDate
  100. ));
  101. $this -> updateOwnTables($iConvId, $iSenderId, $aValue['New']);
  102. }
  103. //set as finished;
  104. $this -> setResultStatus(_t('_bx_dolphin_migration_started_migration_conv_finished', $this -> _iTransferred));
  105. return BX_MIG_SUCCESSFUL;
  106. }
  107. private function updateConvFolder($iCollaborator, $iFolder, $iConvId){
  108. $iId = $this -> _oDb -> getOne("SELECT `id` FROM `bx_convos_conv2folder` WHERE `collaborator` = :coll AND `folder_id` = :folder AND `conv_id` = :id",
  109. array('coll' => $iCollaborator, 'folder' => $iFolder, 'id' => $iConvId));
  110. if($iId)
  111. return $iId;
  112. $sQuery = $this -> _oDb -> prepare(
  113. "
  114. INSERT INTO
  115. `bx_convos_conv2folder`
  116. SET
  117. `conv_id` = ?,
  118. `folder_id` = ?,
  119. `collaborator` = ?
  120. ",
  121. $iConvId,
  122. $iFolder,
  123. $iCollaborator
  124. );
  125. return $this -> _oDb -> query($sQuery);
  126. }
  127. protected function isConvExisted($sSubject, $iSenderId, $iRecipientId)
  128. {
  129. $sSubject = preg_replace('/^Re.*:\s+(.*)$/i', '\\1', $sSubject);
  130. return (int)$this -> _oDb -> getOne("SELECT `c`.`id`
  131. FROM `bx_convos_conversations` as `c`
  132. LEFT JOIN `bx_convos_conv2folder` as `f` ON `c`.`id` = `conv_id`
  133. WHERE `text` = :text AND ((`f`.`collaborator`=:sender AND `c`.`author` = :recipient) OR (`f`.`collaborator`=:recipient AND `c`.`author` = :sender)) LIMIT 1", array('text' => $sSubject, 'sender' => $iSenderId, 'recipient' => $iRecipientId));
  134. }
  135. /**
  136. * Builds folders for conversations
  137. *
  138. * @param int $iObject conversation id
  139. * @param int $iProfileId profile id
  140. * @param int $iComments message id
  141. * @return res query result
  142. */
  143. protected function updateOwnTables($iObject, $iProfileId, $iComments)
  144. {
  145. $sQuery = $this -> _oDb -> prepare(
  146. "
  147. UPDATE
  148. `bx_convos_conv2folder`
  149. SET
  150. `read_comments` = `read_comments` + ?
  151. WHERE `conv_id` = ? AND `collaborator` = ?
  152. ",
  153. $iComments ? 1 : 0,
  154. $iObject,
  155. $iProfileId
  156. );
  157. return $this -> _oDb -> query($sQuery);
  158. }
  159. public function removeContent()
  160. {
  161. if (!$this -> _oDb -> isTableExists($this -> _sTableWithTransKey) || !$this -> _oDb -> isFieldExists($this -> _sTableWithTransKey, $this -> _sTransferFieldIdent))
  162. return false;
  163. $aRecords = $this -> _oDb -> getAll("SELECT *
  164. FROM `{$this -> _sTableWithTransKey}`
  165. WHERE `{$this -> _sTransferFieldIdent}`!=0");
  166. $iNumber = 0;
  167. if (!empty($aRecords))
  168. {
  169. foreach($aRecords as $iKey => $aValue)
  170. {
  171. $sQuery = $this-> _oDb -> prepare("DELETE FROM `tl`, `tle` USING `bx_convos_conversations` AS `tl` LEFT JOIN `bx_convos_cmts` AS `tle` ON `tl`.`id`=`tle`.`cmt_object_id` WHERE `tle`.`cmt_object_id` = ?", $aValue['cmt_object_id']);
  172. $this -> _oDb -> query($sQuery);
  173. $this -> _oDb -> query("DELETE FROM `bx_convos_conv2folder` WHERE `conv_id`=:id", array('id' => $aValue['cmt_object_id']));
  174. $iNumber++;
  175. }
  176. }
  177. parent::removeContent();
  178. return $iNumber;
  179. }
  180. }
  181. /** @} */