BxCnvDb.php 5.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 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. public function getCollaborators($iConversationId)
  41. {
  42. $sQuery = $this->prepare("SELECT `collaborator`, `read_comments` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ?", $iConversationId);
  43. return $this->getPairs($sQuery, 'collaborator', 'read_comments');
  44. }
  45. public function updateReadComments($iProfileId, $iConversationId, $iReadComments)
  46. {
  47. $sQuery = $this->prepare("UPDATE `" . $this->getPrefix() . "conv2folder` SET `read_comments` = ? WHERE `conv_id` = ? AND `collaborator` = ?", $iReadComments, $iConversationId, $iProfileId);
  48. return $this->query($sQuery);
  49. }
  50. public function updateLastCommentTimeProfile($iConversationId, $iProfileId, $iCommentId, $iTimestamp)
  51. {
  52. $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);
  53. return $this->query($sQuery);
  54. }
  55. public function moveConvo($iConversationId, $iProfileId, $iFolderId)
  56. {
  57. $sQuery = $this->prepare("SELECT `folder_id` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ? AND `collaborator` = ?", $iConversationId, $iProfileId);
  58. $iFolderIdOld = $this->getOne($sQuery);
  59. if (BX_CNV_FOLDER_TRASH == $iFolderIdOld) // if convo is already in trash folder - delete it
  60. return $this->deleteConvo($iConversationId, $iProfileId);
  61. $sQuery = $this->prepare("UPDATE `" . $this->getPrefix() . "conv2folder` SET `folder_id` = ? WHERE `conv_id` = ? AND `collaborator` = ?", $iFolderId, $iConversationId, $iProfileId);
  62. return $this->query($sQuery);
  63. }
  64. public function deleteConvo($iConversationId, $iProfileId = 0)
  65. {
  66. $aContentInfo = $this->getContentInfoById ($iConversationId);
  67. if (!$aContentInfo)
  68. return true;
  69. // delete convo
  70. $aBindings = array(
  71. 'conv_id' => $iConversationId
  72. );
  73. $sWhere = '';
  74. if ($iProfileId) {
  75. $aBindings['collaborator'] = $iProfileId;
  76. $sWhere = " AND `collaborator` = :collaborator";
  77. }
  78. $sQuery = "DELETE FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = :conv_id" . $sWhere;
  79. if (!$this->query($sQuery, $aBindings))
  80. return false;
  81. // delete whole conversation if there is no refencences to the conversation in conv2folder table
  82. $sQuery = $this->prepare("SELECT `id` FROM `" . $this->getPrefix() . "conv2folder` WHERE `conv_id` = ?", $iConversationId);
  83. if (!$this->getOne($sQuery)) {
  84. $CNF = &$this->_oConfig->CNF;
  85. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']);
  86. return $oForm->delete((int)$iConversationId, $aContentInfo);
  87. }
  88. return true;
  89. }
  90. public function getUnreadMessagesNum ($iProfileId, $iFolderId = BX_CNV_FOLDER_INBOX)
  91. {
  92. $sQuery = $this->prepare("SELECT SUM(`c`.`comments` - `f`.`read_comments`)
  93. FROM `" . $this->getPrefix() . "conv2folder` as `f`
  94. INNER JOIN `" . $this->getPrefix() . "conversations` AS `c` ON (`c`.`id` = `f`.`conv_id`)
  95. WHERE `f`.`collaborator` = ? AND `f`.`folder_id` = ?", $iProfileId, $iFolderId);
  96. return $this->getOne($sQuery);
  97. }
  98. public function getMessagesPreviews ($iProfileId, $iStart = 0, $iLimit = 4, $iFolderId = BX_CNV_FOLDER_INBOX)
  99. {
  100. $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`
  101. FROM `" . $this->getPrefix() . "conv2folder` as `f`
  102. INNER JOIN `" . $this->getPrefix() . "conversations` AS `c` ON (`c`.`id` = `f`.`conv_id`)
  103. LEFT JOIN `" . $this->getPrefix() . "cmts` AS `cmts` ON (`cmts`.`cmt_id` = `c`.`last_reply_comment_id`)
  104. WHERE `f`.`collaborator` = ? AND `f`.`folder_id` = ?
  105. ORDER BY `c`.`last_reply_timestamp` DESC
  106. LIMIT ?, ?", $iProfileId, $iFolderId, $iStart, $iLimit);
  107. return $this->getAll($sQuery);
  108. }
  109. }
  110. /** @} */