BxCnvDb.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Convos Convos
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxCnvDb extends BxBaseModTextDb
  15. {
  16. public function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function conversationToFolder($iConversationId, $iFolderId, $iProfileCollaborator, $iReadCommentsNum = -1)
  21. {
  22. $sQuery = $this->prepare("INSERT IGNORE INTO `" . $this->getPrefix() . "conv2folder` SET `conv_id` = ?, `folder_id` = ?, `collaborator` = ?, `read_comments` = ?", $iConversationId, $iFolderId, $iProfileCollaborator, $iReadCommentsNum);
  23. return $this->query($sQuery);
  24. }
  25. public function getFolder($iFolderId)
  26. {
  27. $sQuery = $this->prepare("SELECT `id`, `author`, `name` FROM `" . $this->getPrefix() . "folders` WHERE `id` = ?", $iFolderId);
  28. return $this->getRow($sQuery);
  29. }
  30. public function getConversationFolder($iConversationId, $iProfileCollaborator)
  31. {
  32. $sQuery = $this->prepare("SELECT `folder_id` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ? AND `collaborator` = ?", $iConversationId, $iProfileCollaborator);
  33. return $this->getOne($sQuery);
  34. }
  35. public function removeCollaborator($iConversationId, $iProfileId)
  36. {
  37. $sQuery = $this->prepare("DELETE FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ? AND `collaborator` = ?", $iConversationId, $iProfileId);
  38. return $this->query($sQuery);
  39. }
  40. /**
  41. * Remove collaborator from all conversations (when profile is deleted)
  42. * @param $iProfileId profile id to remove
  43. * @return number of deletions
  44. */
  45. public function removeCollaboratorFromAllConversations($iProfileId)
  46. {
  47. $aConvos = $this->getColumn("SELECT `conv_id` FROM `" . $this->getPrefix() . "conv2folder` WHERE `collaborator` = ?", array($iProfileId));
  48. if (!($iRet = $this->query("DELETE FROM `" . $this->getPrefix() . "conv2folder` WHERE `collaborator` = ?", array($iProfileId))))
  49. return 0;
  50. foreach ($aConvos as $iConversationId) {
  51. if (!$this->getCollaborators($iConversationId))
  52. $this->deleteConvo($iConversationId);
  53. }
  54. return $iRet;
  55. }
  56. public function getCollaborators($iConversationId)
  57. {
  58. $sQuery = $this->prepare("SELECT `collaborator`, `read_comments` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ?", $iConversationId);
  59. return $this->getPairs($sQuery, 'collaborator', 'read_comments');
  60. }
  61. public function updateReadComments($iProfileId, $iConversationId, $iReadComments)
  62. {
  63. $sQuery = $this->prepare("UPDATE `" . $this->getPrefix() . "conv2folder` SET `read_comments` = ? WHERE `conv_id` = ? AND `collaborator` = ?", $iReadComments, $iConversationId, $iProfileId);
  64. return $this->query($sQuery);
  65. }
  66. public function updateLastCommentTimeProfile($iConversationId, $iProfileId, $iCommentId, $iTimestamp)
  67. {
  68. $sQuery = $this->prepare("UPDATE `" . $this->getPrefix() . "conversations` SET `last_reply_profile_id` = ?, `last_reply_timestamp` = ?, `last_reply_comment_id` = ? WHERE `id` = ?", $iProfileId, $iTimestamp, $iCommentId, $iConversationId);
  69. return $this->query($sQuery);
  70. }
  71. public function moveConvo($iConversationId, $iProfileId, $iFolderId)
  72. {
  73. $sQuery = $this->prepare("SELECT `folder_id` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ? AND `collaborator` = ?", $iConversationId, $iProfileId);
  74. $iFolderIdOld = $this->getOne($sQuery);
  75. if (BX_CNV_FOLDER_TRASH == $iFolderIdOld) // if convo is already in trash folder - delete it
  76. return '' === $this->deleteConvo($iConversationId, $iProfileId) ? true : false;
  77. $sQuery = $this->prepare("UPDATE `" . $this->getPrefix() . "conv2folder` SET `folder_id` = ? WHERE `conv_id` = ? AND `collaborator` = ?", $iFolderId, $iConversationId, $iProfileId);
  78. return $this->query($sQuery);
  79. }
  80. public function deleteConvo($iConversationId, $iProfileId = 0)
  81. {
  82. $aContentInfo = $this->getContentInfoById ($iConversationId);
  83. if (!$aContentInfo)
  84. return '';
  85. // delete convo
  86. $aBindings = array(
  87. 'conv_id' => $iConversationId
  88. );
  89. $sWhere = '';
  90. if ($iProfileId) {
  91. $aBindings['collaborator'] = $iProfileId;
  92. $sWhere = " AND `collaborator` = :collaborator";
  93. }
  94. $sQuery = "DELETE FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = :conv_id" . $sWhere;
  95. $this->query($sQuery, $aBindings);
  96. // delete whole conversation if there is no refencences to the conversation in conv2folder table
  97. if ($this->getCollaborators($iConversationId))
  98. return '';
  99. $sQuery = $this->prepare("SELECT `id` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ?", $iConversationId);
  100. if (!$this->getOne($sQuery)) {
  101. $CNF = &$this->_oConfig->CNF;
  102. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  103. return $oForm->delete((int)$iConversationId, $aContentInfo) ? '' : _t('_sys_txt_error_occured');
  104. }
  105. return '';
  106. }
  107. public function getUnreadMessagesNum ($iProfileId, $iFolderId = BX_CNV_FOLDER_INBOX)
  108. {
  109. $sQuery = $this->prepare("SELECT SUM(`c`.`comments` - `f`.`read_comments`)
  110. FROM `" . $this->getPrefix() . "conv2folder` as `f`
  111. INNER JOIN `" . $this->getPrefix() . "conversations` AS `c` ON (`c`.`id` = `f`.`conv_id`)
  112. WHERE `f`.`collaborator` = ? AND `f`.`folder_id` = ?", $iProfileId, $iFolderId);
  113. return $this->getOne($sQuery);
  114. }
  115. public function getMessagesPreviews ($iProfileId, $iStart = 0, $iLimit = 4, $iFolderId = BX_CNV_FOLDER_INBOX)
  116. {
  117. $sQuery = $this->prepare("SELECT `c`.`id`, `c`.`text`, `c`.`author`, `cmts`.`cmt_text`, `c`.`last_reply_profile_id`, `c`.`comments`, (`c`.`comments` - `f`.`read_comments`) AS `unread_messages`, `last_reply_timestamp`
  118. FROM `" . $this->getPrefix() . "conv2folder` as `f`
  119. INNER JOIN `" . $this->getPrefix() . "conversations` AS `c` ON (`c`.`id` = `f`.`conv_id`)
  120. LEFT JOIN `" . $this->getPrefix() . "cmts` AS `cmts` ON (`cmts`.`cmt_id` = `c`.`last_reply_comment_id`)
  121. WHERE `f`.`collaborator` = ? AND `f`.`folder_id` = ?
  122. ORDER BY `c`.`last_reply_timestamp` DESC
  123. LIMIT ?, ?", $iProfileId, $iFolderId, $iStart, $iLimit);
  124. return $this->getAll($sQuery);
  125. }
  126. }
  127. /** @} */