BxCnvFormEntry.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * 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. $CNF = $this->_oModule->_oConfig->CNF;
  29. if ($bRet = parent::update ($iContentId, $aValsToAdd, $aTrackTextFieldsChanges)) {
  30. $iFolder = $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id());
  31. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  32. if ($aContentInfo && bx_get_logged_profile_id() == $aContentInfo[$CNF['FIELD_AUTHOR']]) // allow to edit participants for author only
  33. $this->_updateParticipants ($iContentId, $iFolder, $iFolder == BX_CNV_FOLDER_DRAFTS ? true : false);
  34. }
  35. return $bRet;
  36. }
  37. public function insert ($aValsToAdd = array(), $isIgnore = false)
  38. {
  39. $aValsToAdd['last_reply_timestamp'] = time();
  40. $aValsToAdd['last_reply_profile_id'] = bx_get_logged_profile_id();
  41. $bSaveToDrafts = bx_get('draft_save');
  42. $iContentId = bx_get('draft_id');
  43. $bDraft = $iContentId ? BX_CNV_FOLDER_DRAFTS == $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id()) : false;
  44. if ($iContentId) {
  45. if (!$bDraft)
  46. return 0;
  47. if (!parent::update ($iContentId, $aValsToAdd, $isIgnore))
  48. return 0;
  49. } else {
  50. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  51. if (!$iContentId)
  52. return 0;
  53. }
  54. if ($bSaveToDrafts) {
  55. if (!$bDraft)
  56. $this->_oModule->_oDb->conversationToFolder($iContentId, BX_CNV_FOLDER_DRAFTS, bx_get_logged_profile_id(), 0);
  57. // draft is saved via ajax call only, upon successfull draft saving content id is returned
  58. echo $iContentId;
  59. exit;
  60. } else {
  61. // check for spam
  62. $bSpam = false;
  63. bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => $this->getCleanValue('text'), 'where' => $this->MODULE));
  64. $iFolder = $bSpam ? BX_CNV_FOLDER_SPAM : BX_CNV_FOLDER_INBOX;
  65. $this->_updateParticipants ($iContentId, $iFolder, $bDraft);
  66. }
  67. return $iContentId;
  68. }
  69. protected function _updateParticipants ($iContentId, $iFolder, $bDraft)
  70. {
  71. $aRecipientsOld = $this->_oModule->_oDb->getCollaborators($iContentId);
  72. // place conversation to "inbox" (or "spam" - in case of spam) folder
  73. $aRecipients = array_unique(array_merge($this->getCleanValue('recipients'), array(bx_get_logged_profile_id())), SORT_NUMERIC);
  74. foreach ($aRecipients as $iProfile) {
  75. $oProfile = BxDolProfile::getInstance($iProfile);
  76. if (!$oProfile)
  77. continue;
  78. if ($bDraft && $oProfile->id() == bx_get_logged_profile_id())
  79. $this->_oModule->_oDb->moveConvo($iContentId, $oProfile->id(), $iFolder);
  80. else
  81. $this->_oModule->_oDb->conversationToFolder($iContentId, $iFolder, $oProfile->id(), $oProfile->id() == bx_get_logged_profile_id() ? 0 : -1);
  82. }
  83. // remove participants
  84. foreach ($aRecipientsOld as $iProfileId => $iCount) {
  85. if (!in_array($iProfileId, $aRecipients))
  86. $this->_oModule->_oDb->removeCollaborator($iContentId, $iProfileId);
  87. }
  88. }
  89. protected function genCustomInputRecipients ($aInput)
  90. {
  91. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=convos/ajax_get_recipients";
  92. return $this->genCustomInputUsernamesSuggestions($aInput);
  93. }
  94. protected function genCustomInputSubmitText ($aInput)
  95. {
  96. $aVars = array();
  97. return $this->_oModule->_oTemplate->parseHtmlByName('form_submit_text.html', $aVars);
  98. }
  99. function initChecker ($aValues = array (), $aSpecificValues = array())
  100. {
  101. $CNF = $this->_oModule->_oConfig->CNF;
  102. if ($aValues && bx_get_logged_profile_id() != $aValues[$CNF['FIELD_AUTHOR']]) { // unset some fields for non author
  103. unset($this->aInputs[$CNF['FIELD_ALLOW_EDIT']]);
  104. unset($this->aInputs['recipients']);
  105. }
  106. if ($iContentId = bx_get('draft_id')) { // if adding from draft, fill in existing fields info
  107. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  108. if ($aContentInfo)
  109. $aValues = array_merge($aContentInfo, $aValues);
  110. }
  111. if ($sProfilesIds = bx_get('profiles')) { // if writing directly to some recipients, pre-fill them
  112. $a = explode(',', $sProfilesIds);
  113. $a = array_unique(BxDolFormCheckerHelper::passInt($a));
  114. if ($a)
  115. $aValues['recipients'] = array_merge(empty($aValues['recipients']) ? array() : $aValues['recipients'], $a);
  116. }
  117. $sEt = bx_get('et');
  118. if($sEt !== false) { // if 'attachable email template' was specified, process it
  119. $aEt = unserialize(base64_decode(urldecode($sEt)));
  120. if(!empty($aEt) && is_array($aEt) && !empty($aEt['name'])) {
  121. $sName = bx_process_input($aEt['name']);
  122. $aParams = isset($aEt['params']) ? bx_process_input($aEt['params']) : array();
  123. $sAet = getParam('sys_email_attachable_email_templates');
  124. if(strpos($sAet, $sName) !== false) {
  125. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sName, $aParams);
  126. if(!empty($aTemplate) && is_array($aTemplate)) {
  127. if(isset($this->aInputs[$CNF['FIELD_TITLE']]) && empty($this->aInputs[$CNF['FIELD_TITLE']]['value']))
  128. $this->aInputs[$CNF['FIELD_TITLE']]['value'] = $aTemplate['Subject'];
  129. if(isset($this->aInputs[$CNF['FIELD_TEXT']]) && empty($this->aInputs[$CNF['FIELD_TEXT']]['value']))
  130. $this->aInputs[$CNF['FIELD_TEXT']]['value'] = $aTemplate['Body'];
  131. }
  132. }
  133. }
  134. }
  135. return parent::initChecker ($aValues, $aSpecificValues);
  136. }
  137. function isValid ()
  138. {
  139. if (bx_get('draft_save')) // form is always valid when saving to drafts
  140. return true;
  141. return parent::isValid ();
  142. }
  143. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  144. {
  145. 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
  146. $isAssociateWithContent = true; // TODO: if edit mode will be added, then this functionality maybe reconsidered
  147. return parent::processFiles ($sFieldFile, $iContentId, $isAssociateWithContent);
  148. }
  149. }
  150. /** @} */