BxCnvFormEntry.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. {
  76. $sVals = '';
  77. if (!empty($aInput['value']) && is_array($aInput['value'])) {
  78. foreach ($aInput['value'] as $sVal) {
  79. if (!$sVal || !($oProfile = BxDolProfile::getInstance($sVal)))
  80. continue;
  81. $sVals .= '<b class="val bx-def-color-bg-hl bx-def-round-corners">' . $oProfile->getDisplayName() . '<input type="hidden" name="' . $aInput['name'] . '[]" value="' . $sVal . '" /></b>';
  82. }
  83. $sVals = trim($sVals, ',');
  84. }
  85. $sId = $aInput['name'] . time();
  86. $sPlaceholderText = bx_html_attribute(_t('_bx_cnv_form_entry_input_recipients_placeholder'), BX_ESCAPE_STR_QUOTE);
  87. $sUrlGetRecipients = BX_DOL_URL_ROOT . "modules/?r=convos/ajax_get_recipients";
  88. return <<<EOS
  89. <script>
  90. $(function() {
  91. $('#{$sId} input[type=text]').autocomplete({
  92. source: "{$sUrlGetRecipients}",
  93. select: function(e, ui) {
  94. $(this).val(ui.item.label);
  95. $(this).trigger('superselect', ui.item);
  96. e.preventDefault();
  97. }
  98. });
  99. $('#{$sId} input[type=text]').on('superselect', function(e, item) {
  100. $(this).hide();
  101. if ('undefined' != typeof(item))
  102. $(this).before('<b class="bx-def-color-bg-hl bx-def-round-corners">'+ item.label +'<input type="hidden" name="{$aInput['name']}[]" value="'+ item.value +'" /></b>');
  103. fAutoShrinkInput();
  104. $(this).show();
  105. this.value = '';
  106. }).on('keydown', function(e) {
  107. // if: comma,enter (delimit more keyCodes with | pipe)
  108. if (/(13)/.test(e.which))
  109. e.preventDefault();
  110. });
  111. $('#{$sId}').on('click', 'b', function() {
  112. $(this).remove();
  113. fAutoShrinkInput();
  114. });
  115. fAutoShrinkInput = function () {
  116. var iWidthCont = $('#{$sId}.bx-form-input-autotoken').innerWidth();
  117. var iWidthExisting = 0;
  118. $('#{$sId}.bx-form-input-autotoken b').each(function () {
  119. iWidthExisting += $(this).outerWidth(true);
  120. });
  121. $('#{$sId}.bx-form-input-autotoken input').width(parseInt(iWidthCont - iWidthExisting > 180 ? iWidthCont - iWidthExisting : 180) - 5);
  122. };
  123. fAutoShrinkInput();
  124. });
  125. </script>
  126. <style>
  127. .bx-form-input-autotoken {
  128. float:left;
  129. padding:0px;
  130. height:auto;
  131. }
  132. .bx-form-input-autotoken b {
  133. cursor:pointer;
  134. display:block;
  135. float:left;
  136. padding:0.5em;
  137. padding-right:1.5em;
  138. margin:0.1em;
  139. font-weight:normal;
  140. }
  141. .bx-form-input-autotoken b:hover {
  142. opacity:0.7;
  143. }
  144. .bx-form-input-autotoken b:after {
  145. position:absolute;
  146. content:"x";
  147. padding:0 0.5em;
  148. margin:0.2em 0 0.7em 0.5em;
  149. font-size:0.7em;
  150. font-weight:bold;
  151. }
  152. .bx-form-input-autotoken input {
  153. border:0;
  154. margin:0;
  155. padding:0 0 0 5px;
  156. width:auto;
  157. }
  158. </style>
  159. <div id="{$sId}" class="bx-form-input-autotoken bx-def-font-inputs bx-form-input-text">
  160. {$sVals}
  161. <input type="text" value="" placeholder="{$sPlaceholderText}" class="bx-def-font-inputs bx-form-input-text" />
  162. </div>
  163. EOS;
  164. }
  165. protected function genCustomInputSubmitText ($aInput)
  166. {
  167. $aVars = array();
  168. return $this->_oModule->_oTemplate->parseHtmlByName('form_submit_text.html', $aVars);
  169. }
  170. function initChecker ($aValues = array (), $aSpecificValues = array())
  171. {
  172. if ($iContentId = bx_get('draft_id')) { // if adding from draft, fill in existing fields info
  173. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  174. if ($aContentInfo)
  175. $aValues = array_merge($aContentInfo, $aValues);
  176. }
  177. if ($sProfilesIds = bx_get('profiles')) { // if writing directly to some recipients, pre-fill them
  178. $a = explode(',', $sProfilesIds);
  179. $a = array_unique(BxDolFormCheckerHelper::passInt($a));
  180. if ($a)
  181. $aValues['recipients'] = array_merge(empty($aValues['recipients']) ? array() : $aValues['recipients'], $a);
  182. }
  183. return parent::initChecker ($aValues, $aSpecificValues);
  184. }
  185. function isValid ()
  186. {
  187. if (bx_get('draft_save')) // form is always valid when saving to drafts
  188. return true;
  189. return parent::isValid ();
  190. }
  191. }
  192. /** @} */