BxCnvFormEntry.php 11 KB

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