BxCnvFormEntry.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup Convos Convos
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. /**
  12. * Create/Edit entry form
  13. */
  14. class BxCnvFormEntry extends BxBaseModTextFormEntry
  15. {
  16. public function __construct($aInfo, $oTemplate = false)
  17. {
  18. $this->MODULE = 'bx_convos';
  19. parent::__construct($aInfo, $oTemplate);
  20. $oTemplate = BxDolTemplate::getInstance();
  21. $oTemplate->addJsTranslation(array(
  22. '_bx_cnv_draft_saving_error',
  23. '_bx_cnv_draft_saved_success',
  24. ));
  25. }
  26. public function update ($iContentId, $aValsToAdd = array(), &$aTrackTextFieldsChanges = null)
  27. {
  28. if ($bRet = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges)) {
  29. $iFolder = $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id());
  30. $this->_updateParticipants ($iContentId, $iFolder, $iFolder == BX_CNV_FOLDER_DRAFTS ? true : false);
  31. }
  32. return $bRet;
  33. }
  34. public function insert ($aValsToAdd = array(), $isIgnore = false)
  35. {
  36. $aValsToAdd['last_reply_timestamp'] = time();
  37. $aValsToAdd['last_reply_profile_id'] = bx_get_logged_profile_id();
  38. $bSaveToDrafts = bx_get('draft_save');
  39. $iContentId = bx_get('draft_id');
  40. $bDraft = $iContentId ? BX_CNV_FOLDER_DRAFTS == $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id()) : false;
  41. if ($iContentId) {
  42. if (!$bDraft)
  43. return 0;
  44. if (!parent::update ($iContentId, $aValsToAdd, $isIgnore))
  45. return 0;
  46. } else {
  47. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  48. if (!$iContentId)
  49. return 0;
  50. }
  51. if ($bSaveToDrafts) {
  52. if (!$bDraft)
  53. $this->_oModule->_oDb->conversationToFolder($iContentId, BX_CNV_FOLDER_DRAFTS, bx_get_logged_profile_id(), 0);
  54. // draft is saved via ajax call only, upon successfull draft saving content id is returned
  55. echo $iContentId;
  56. exit;
  57. } else {
  58. // check for spam
  59. $bSpam = false;
  60. bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => $this->getCleanValue('text'), 'where' => $this->MODULE));
  61. $iFolder = $bSpam ? BX_CNV_FOLDER_SPAM : BX_CNV_FOLDER_INBOX;
  62. $this->_updateParticipants ($iContentId, $iFolder, $bDraft);
  63. }
  64. return $iContentId;
  65. }
  66. protected function _updateParticipants ($iContentId, $iFolder, $bDraft)
  67. {
  68. $aRecipientsOld = $this->_oModule->_oDb->getCollaborators($iContentId);
  69. // place conversation to "inbox" (or "spam" - in case of spam) folder
  70. $aRecipients = array_unique(array_merge($this->getCleanValue('recipients'), array(bx_get_logged_profile_id())), SORT_NUMERIC);
  71. foreach ($aRecipients as $iProfile) {
  72. $oProfile = BxDolProfile::getInstance($iProfile);
  73. if (!$oProfile)
  74. continue;
  75. if ($bDraft && $oProfile->id() == bx_get_logged_profile_id())
  76. $this->_oModule->_oDb->moveConvo($iContentId, $oProfile->id(), $iFolder);
  77. else
  78. $this->_oModule->_oDb->conversationToFolder($iContentId, $iFolder, $oProfile->id(), $oProfile->id() == bx_get_logged_profile_id() ? 0 : -1);
  79. }
  80. // remove participants
  81. foreach ($aRecipientsOld as $iProfileId => $iCount) {
  82. if (!in_array($iProfileId, $aRecipients))
  83. $this->_oModule->_oDb->removeCollaborator($iContentId, $iProfileId);
  84. }
  85. }
  86. protected function genCustomInputRecipients ($aInput)
  87. {
  88. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=convos/ajax_get_recipients";
  89. return $this->genCustomInputUsernamesSuggestions($aInput);
  90. }
  91. protected function genCustomInputSubmitText ($aInput)
  92. {
  93. $aVars = array();
  94. return $this->_oModule->_oTemplate->parseHtmlByName('form_submit_text.html', $aVars);
  95. }
  96. function initChecker ($aValues = array (), $aSpecificValues = array())
  97. {
  98. if ($iContentId = bx_get('draft_id')) { // if adding from draft, fill in existing fields info
  99. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  100. if ($aContentInfo)
  101. $aValues = array_merge($aContentInfo, $aValues);
  102. }
  103. if ($sProfilesIds = bx_get('profiles')) { // if writing directly to some recipients, pre-fill them
  104. $a = explode(',', $sProfilesIds);
  105. $a = array_unique(BxDolFormCheckerHelper::passInt($a));
  106. if ($a)
  107. $aValues['recipients'] = array_merge(empty($aValues['recipients']) ? array() : $aValues['recipients'], $a);
  108. }
  109. return parent::initChecker ($aValues, $aSpecificValues);
  110. }
  111. function isValid ()
  112. {
  113. if (bx_get('draft_save')) // form is always valid when saving to drafts
  114. return true;
  115. return parent::isValid ();
  116. }
  117. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  118. {
  119. if (!$isAssociateWithContent && bx_get('draft_id')) // when draft is already saved then db update is called but we still need to do association since it's draft
  120. $isAssociateWithContent = true; // TODO: if edit mode will be added, then this functionality maybe reconsidered
  121. return parent::processFiles ($sFieldFile, $iContentId, $isAssociateWithContent);
  122. }
  123. }
  124. /** @} */