BxCnvFormEntry.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. $CNF = $this->_oModule->_oConfig->CNF;
  40. $aValsToAdd['last_reply_timestamp'] = time();
  41. $aValsToAdd['last_reply_profile_id'] = bx_get_logged_profile_id();
  42. $bSaveToDrafts = bx_get('draft_save');
  43. $iContentId = (int)bx_get('draft_id');
  44. $bDraft = $iContentId ? BX_CNV_FOLDER_DRAFTS == $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id()) : false;
  45. // check for spam
  46. $bSpam = false;
  47. $sValue = $this->getCleanValue($CNF['FIELD_TEXT']);
  48. bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => &$sValue, 'where' => $this->MODULE));
  49. self::setSubmittedValue($CNF['FIELD_TEXT'], $sValue, $this->aFormAttrs['method']);
  50. if($iContentId) {
  51. if(!$bDraft)
  52. return 0;
  53. if(!parent::update($iContentId, $aValsToAdd, $isIgnore))
  54. return 0;
  55. }
  56. else {
  57. $iContentId = parent::insert($aValsToAdd, $isIgnore);
  58. if(!$iContentId)
  59. return 0;
  60. }
  61. if($bSaveToDrafts) {
  62. if(!$bDraft)
  63. $this->_oModule->_oDb->conversationToFolder($iContentId, BX_CNV_FOLDER_DRAFTS, bx_get_logged_profile_id(), 0);
  64. // process uploaded files
  65. if(isset($CNF['FIELD_PHOTO']))
  66. $this->processFiles ($CNF['FIELD_PHOTO'], $iContentId, true);
  67. // draft is saved via ajax call only, upon successfull draft saving content id is returned
  68. echo $iContentId . ',' . $this->getCsrfToken();
  69. exit;
  70. }
  71. else {
  72. $iFolder = $bSpam ? BX_CNV_FOLDER_SPAM : BX_CNV_FOLDER_INBOX;
  73. $this->updateParticipants ($iContentId, $iFolder, $bDraft);
  74. }
  75. return $iContentId;
  76. }
  77. public function updateParticipants ($iContentId, $iFolder, $bDraft, $aRecipientsAdd = array())
  78. {
  79. $iSender = bx_get_logged_profile_id();
  80. $aRecipientsOld = $this->_oModule->_oDb->getCollaborators($iContentId);
  81. // place conversation to "inbox" (or "spam" - in case of spam) folder
  82. $aRecipientsFormForm = $this->getCleanValue('recipients') ? $this->getCleanValue('recipients') : array();
  83. $aRecipients = array_unique(array_merge($aRecipientsFormForm, array($iSender), $aRecipientsAdd), SORT_NUMERIC);
  84. foreach ($aRecipients as $iProfile) {
  85. $oProfile = BxDolProfile::getInstance($iProfile);
  86. if(!$oProfile)
  87. continue;
  88. if($this->_oModule->checkAllowedContact($iProfile) !== CHECK_ACTION_RESULT_ALLOWED)
  89. continue;
  90. $iRecipient = $oProfile->id();
  91. if($bDraft && $iRecipient == $iSender)
  92. $this->_oModule->_oDb->moveConvo($iContentId, $iRecipient, $iFolder);
  93. else {
  94. if($iRecipient != $iSender) {
  95. $bCanContact = true;
  96. bx_alert('profile', 'check_contact', 0, false, array('can_contact' => &$bCanContact, 'sender' => $iSender, 'recipient' => $iRecipient, 'where' => $this->MODULE));
  97. if(!$bCanContact)
  98. $iFolder = BX_CNV_FOLDER_SPAM;
  99. }
  100. if ($aRecipientsAdd && in_array($iRecipient, $aRecipientsAdd))
  101. $this->_oModule->_oDb->removeCollaborator($iContentId, $iRecipient);
  102. $this->_oModule->_oDb->conversationToFolder($iContentId, $iFolder, $iRecipient, $iRecipient == $iSender ? 0 : -1);
  103. }
  104. }
  105. // remove participants
  106. foreach ($aRecipientsOld as $iProfileId => $iCount) {
  107. if (!in_array($iProfileId, $aRecipients))
  108. $this->_oModule->_oDb->removeCollaborator($iContentId, $iProfileId);
  109. }
  110. }
  111. protected function genCustomInputRecipients ($aInput)
  112. {
  113. $aInput['ajax_get_suggestions'] = BX_DOL_URL_ROOT . "modules/?r=convos/ajax_get_recipients";
  114. return $this->genCustomInputUsernamesSuggestions($aInput);
  115. }
  116. protected function genCustomInputSubmitText ($aInput)
  117. {
  118. $aVars = array();
  119. return $this->_oModule->_oTemplate->parseHtmlByName('form_submit_text.html', $aVars);
  120. }
  121. function initChecker ($aValues = array (), $aSpecificValues = array())
  122. {
  123. $CNF = $this->_oModule->_oConfig->CNF;
  124. if ($aValues && bx_get_logged_profile_id() != $aValues[$CNF['FIELD_AUTHOR']]) { // unset some fields for non author
  125. unset($this->aInputs[$CNF['FIELD_ALLOW_EDIT']]);
  126. unset($this->aInputs['recipients']);
  127. }
  128. if ($iContentId = bx_get('draft_id')) { // if adding from draft, fill in existing fields info
  129. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  130. if ($aContentInfo)
  131. $aValues = array_merge($aContentInfo, $aValues);
  132. }
  133. if ($sProfilesIds = bx_get('profiles')) { // if writing directly to some recipients, pre-fill them
  134. $a = explode(',', $sProfilesIds);
  135. $a = array_unique(BxDolFormCheckerHelper::passInt($a));
  136. if ($a)
  137. $aValues['recipients'] = array_merge(empty($aValues['recipients']) ? array() : $aValues['recipients'], $a);
  138. }
  139. $sEt = bx_get('et');
  140. if($sEt !== false) { // if 'attachable email template' was specified, process it
  141. $aEt = unserialize(base64_decode(urldecode($sEt)));
  142. if(!empty($aEt) && is_array($aEt) && !empty($aEt['name'])) {
  143. $sName = bx_process_input($aEt['name']);
  144. $aParams = isset($aEt['params']) ? bx_process_input($aEt['params']) : array();
  145. $sAet = getParam('sys_email_attachable_email_templates');
  146. if(strpos($sAet, $sName) !== false) {
  147. $aTemplate = BxDolEmailTemplates::getInstance()->parseTemplate($sName, $aParams);
  148. if(!empty($aTemplate) && is_array($aTemplate)) {
  149. if(isset($this->aInputs[$CNF['FIELD_TITLE']]) && empty($this->aInputs[$CNF['FIELD_TITLE']]['value']))
  150. $this->aInputs[$CNF['FIELD_TITLE']]['value'] = $aTemplate['Subject'];
  151. if(isset($this->aInputs[$CNF['FIELD_TEXT']]) && empty($this->aInputs[$CNF['FIELD_TEXT']]['value']))
  152. $this->aInputs[$CNF['FIELD_TEXT']]['value'] = $aTemplate['Body'];
  153. }
  154. }
  155. }
  156. }
  157. return parent::initChecker ($aValues, $aSpecificValues);
  158. }
  159. function isValid ()
  160. {
  161. if (bx_get('draft_save')) // form is always valid when saving to drafts
  162. return true;
  163. return parent::isValid ();
  164. }
  165. public function processFiles ($sFieldFile, $iContentId = 0, $isAssociateWithContent = false)
  166. {
  167. 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
  168. $isAssociateWithContent = true; // TODO: if edit mode will be added, then this functionality maybe reconsidered
  169. return parent::processFiles ($sFieldFile, $iContentId, $isAssociateWithContent);
  170. }
  171. }
  172. class BxCnvFormEntryCheckerHelper extends BxDolFormCheckerHelper
  173. {
  174. static public function checkRecipients($aIds)
  175. {
  176. $bResult = self::checkAvail($aIds);
  177. if(!$bResult)
  178. return $bResult;
  179. $oModule = BxDolModule::getInstance('bx_convos');
  180. $aIdsAllowed = array();
  181. foreach($aIds as $iId)
  182. if($oModule->checkAllowedContact($iId) === CHECK_ACTION_RESULT_ALLOWED)
  183. $aIdsAllowed[] = $iId;
  184. if(empty($aIdsAllowed))
  185. return false;
  186. return true;
  187. }
  188. }
  189. /** @} */