1
0

BxCnvFormEntry.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxBaseModTextFormEntry');
  12. /**
  13. * Create/Edit entry form
  14. */
  15. class BxCnvFormEntry extends BxBaseModTextFormEntry
  16. {
  17. public function __construct($aInfo, $oTemplate = false)
  18. {
  19. $this->MODULE = 'bx_convos';
  20. parent::__construct($aInfo, $oTemplate);
  21. $oTemplate = BxDolTemplate::getInstance();
  22. $oTemplate->addJs(array (
  23. 'jquery-ui/jquery-ui.custom.min.js',
  24. 'jquery.form.min.js',
  25. ));
  26. $oTemplate->addJsTranslation(array(
  27. '_bx_cnv_draft_saving_error',
  28. '_bx_cnv_draft_saved_success',
  29. ));
  30. }
  31. public function insert ($aValsToAdd = array(), $isIgnore = false)
  32. {
  33. $aValsToAdd['last_reply_timestamp'] = time();
  34. $aValsToAdd['last_reply_profile_id'] = bx_get_logged_profile_id();
  35. $bSaveToDrafts = bx_get('draft_save');
  36. $iContentId = bx_get('draft_id');
  37. $bDraft = $iContentId ? BX_CNV_FOLDER_DRAFTS == $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id()) : false;
  38. if ($iContentId) {
  39. if (!$bDraft)
  40. return 0;
  41. if (!parent::update ($iContentId, $aValsToAdd, $isIgnore))
  42. return 0;
  43. } else {
  44. $iContentId = parent::insert ($aValsToAdd, $isIgnore);
  45. if (!$iContentId)
  46. return 0;
  47. }
  48. if ($bSaveToDrafts) {
  49. if (!$bDraft)
  50. $this->_oModule->_oDb->conversationToFolder($iContentId, BX_CNV_FOLDER_DRAFTS, bx_get_logged_profile_id(), 0);
  51. // draft is saved via ajax call only, upon successfull draft saving content id is returned
  52. echo $iContentId;
  53. exit;
  54. } else {
  55. bx_import('BxDolProfile');
  56. // check for spam
  57. $bSpam = false;
  58. bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => $this->getCleanValue('text'), 'where' => $this->MODULE));
  59. $iFolder = $bSpam ? BX_CNV_FOLDER_SPAM : BX_CNV_FOLDER_INBOX;
  60. // place conversation to "inbox" (or "spam" - in case of spam) folder
  61. $aRecipients = array_unique(array_merge($this->getCleanValue('recipients'), array(bx_get_logged_profile_id())), SORT_NUMERIC);
  62. foreach ($aRecipients as $iProfile) {
  63. $oProfile = BxDolProfile::getInstance($iProfile);
  64. if (!$oProfile)
  65. continue;
  66. if ($bDraft && $oProfile->id() == bx_get_logged_profile_id())
  67. $this->_oModule->_oDb->moveConvo($iContentId, $oProfile->id(), $iFolder);
  68. else
  69. $this->_oModule->_oDb->conversationToFolder($iContentId, $iFolder, $oProfile->id(), $oProfile->id() == bx_get_logged_profile_id() ? 0 : -1);
  70. }
  71. }
  72. return $iContentId;
  73. }
  74. protected function genCustomInputRecipients ($aInput) {
  75. $sVals = '';
  76. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  77. foreach ($aInput['value'] as $sVal) {
  78. $sVals .= '<span class="bx-def-color-bg-hl bx-def-round-corners">' . BxDolProfile::getInstance($sVal)->getDisplayName() . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . $sVal . '" /></span>';
  79. }
  80. $sVals = trim($sVals, ',');
  81. }
  82. $sId = $aInput['name'] . time();
  83. $sPlaceholderText = bx_html_attribute(_t('_bx_cnv_form_entry_input_recipients_placeholder'), BX_ESCAPE_STR_QUOTE);
  84. $sUrlGetRecipients = BX_DOL_URL_ROOT . "modules/?r=convos/ajax_get_recipients";
  85. return <<<EOS
  86. <script>
  87. $(function() {
  88. $('#{$sId} input[type=text]').autocomplete({
  89. source: "{$sUrlGetRecipients}",
  90. select: function(e, ui) {
  91. $(this).val(ui.item.label);
  92. $(this).trigger('superselect', ui.item);
  93. e.preventDefault();
  94. }
  95. });
  96. $('#{$sId} input[type=text]').on('superselect', function(e, item) {
  97. if ('undefined' != typeof(item))
  98. $(this).before('<span class="bx-def-color-bg-hl bx-def-round-corners">'+ item.label +'<input type="hidden" name="{$aInput['name']}[]" value="'+ item.value +'" /></span>');
  99. this.value = '';
  100. }).on('keydown', function(e) {
  101. // if: comma,enter (delimit more keyCodes with | pipe)
  102. if (/(13)/.test(e.which))
  103. e.preventDefault();
  104. });
  105. $('#{$sId}').on('click', 'span', function() {
  106. $(this).remove();
  107. });
  108. });
  109. </script>
  110. <style>
  111. .bx-form-input-autotoken {
  112. float:left;
  113. padding:0px;
  114. height:auto;
  115. }
  116. .bx-form-input-autotoken span {
  117. cursor:pointer;
  118. display:block;
  119. float:left;
  120. padding:0.5em;
  121. padding-right:1.5em;
  122. margin:0.1em;
  123. }
  124. .bx-form-input-autotoken span:hover{
  125. opacity:0.7;
  126. }
  127. .bx-form-input-autotoken span:after{
  128. position:absolute;
  129. content:"x";
  130. padding:0 0.5em;
  131. margin:0.2em 0 0.7em 0.5em;
  132. font-size:0.7em;
  133. font-weight:bold;
  134. }
  135. .bx-form-input-autotoken input {
  136. border:0;
  137. margin:0;
  138. padding:0 0 0 5px;
  139. width:auto;
  140. }
  141. </style>
  142. <div id="{$sId}" class="bx-form-input-autotoken bx-def-font-inputs bx-form-input-text">
  143. {$sVals}
  144. <input type="text" value="" placeholder="{$sPlaceholderText}" class="bx-def-font-inputs bx-form-input-text" />
  145. </div>
  146. EOS;
  147. }
  148. protected function genCustomInputSubmitText ($aInput)
  149. {
  150. $aVars = array();
  151. return $this->_oModule->_oTemplate->parseHtmlByName('form_submit_text.html', $aVars);
  152. }
  153. function initChecker ($aValues = array (), $aSpecificValues = array())
  154. {
  155. if ($iContentId = bx_get('draft_id')) { // if adding from draft, fill in existing fields info
  156. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  157. if ($aContentInfo)
  158. $aValues = array_merge($aContentInfo, $aValues);
  159. }
  160. return parent::initChecker ($aValues, $aSpecificValues);
  161. }
  162. function isValid ()
  163. {
  164. if (bx_get('draft_save')) // form is always valid when saving to drafts
  165. return true;
  166. return parent::isValid ();
  167. }
  168. }
  169. /** @} */