Przeglądaj źródła

#42 - messages module: drafts functionality

Alex Trofimov 12 lat temu
rodzic
commit
1907df1f6c

+ 1 - 1
inc/js/functions.js

@@ -340,7 +340,7 @@ function bx_time(sLang, isAutoupdate, sRootSel) {
     if ($('time.bx-time-autoupdate').size()) {
         setTimeout(function () {
             bx_time(sLang, true);
-        }, 30000);
+        }, 20000);
     }
 }
 

+ 7 - 4
modules/base/text/classes/BxBaseModTextFormEntry.php

@@ -74,7 +74,7 @@ class BxBaseModTextFormEntry extends BxTemplFormView
                 'content_id' => $this->aInputs[$CNF['FIELD_PHOTO']]['content_id'],
                 'editor_id' => $CNF['FIELD_TEXT_ID'],
                 'summary_id' => $CNF['FIELD_SUMMARY_ID'],
-                'thumb_id' => $aContentInfo[$CNF['FIELD_THUMB']],
+                'thumb_id' => isset($aContentInfo[$CNF['FIELD_THUMB']]) ? $aContentInfo[$CNF['FIELD_THUMB']] : 0,
                 'bx_if:set_thumb' => array (
                     'condition' => CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb(),
                     'content' => array (
@@ -93,8 +93,10 @@ class BxBaseModTextFormEntry extends BxTemplFormView
         $CNF = &$this->_oModule->_oConfig->CNF;
 
         $aValsToAdd[$CNF['FIELD_AUTHOR']] = bx_get_logged_profile_id ();
-        $aValsToAdd[$CNF['FIELD_ADDED']] = time();
-        $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
+        if (isset($CNF['FIELD_ADDED']))
+            $aValsToAdd[$CNF['FIELD_ADDED']] = time();
+        if (isset($CNF['FIELD_CHANGED']))
+            $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
 
         if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb()) {
             $aThumb = isset($_POST[$CNF['FIELD_THUMB']]) ? bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT) : false;
@@ -112,7 +114,8 @@ class BxBaseModTextFormEntry extends BxTemplFormView
     {
         $CNF = &$this->_oModule->_oConfig->CNF;
 
-        $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
+        if (isset($CNF['FIELD_CHANGED']))
+            $aValsToAdd[$CNF['FIELD_CHANGED']] = time();
 
         if (CHECK_ACTION_RESULT_ALLOWED === $this->_oModule->checkAllowedSetThumb()) {
             $aThumb = bx_process_input ($_POST[$CNF['FIELD_THUMB']], BX_DATA_INT);

+ 83 - 22
modules/boonex/convos/classes/BxCnvFormEntry.php

@@ -21,33 +21,70 @@ class BxCnvFormEntry extends BxBaseModTextFormEntry
         $this->MODULE = 'bx_convos';
         parent::__construct($aInfo, $oTemplate);
 
-        $aJs = array (
-            'jquery.ui.all.min.js', // TODO: remake by adding individual files, instead of all
-        );
-        BxDolTemplate::getInstance()->addJs($aJs);
+        $oTemplate = BxDolTemplate::getInstance();
+        $oTemplate->addJs(array (
+            'jquery.ui.all.min.js',
+            'jquery.form.js',
+        ));
+        $oTemplate->addJsTranslation(array(
+            '_bx_cnv_draft_saving_error',
+            '_bx_cnv_draft_saved_success',
+        ));    
     }
 
     public function insert ($aValsToAdd = array(), $isIgnore = false) 
     {
         $aValsToAdd['last_reply_timestamp'] = time();
         $aValsToAdd['last_reply_profile_id'] = bx_get_logged_profile_id();
-        $iContentId = parent::insert ($aValsToAdd, $isIgnore);
-        if (!$iContentId)
-            return 0;
-
-        bx_import('BxDolProfile');
-
-        // check for spam
-        $bSpam = false;
-        bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => $this->getCleanValue('text'), 'where' => $this->MODULE));
-        $iFolder = $bSpam ? BX_CNV_FOLDER_SPAM : BX_CNV_FOLDER_INBOX;
-
-        // place conversation to "inbox" (or "spam" - in case of spam) folder 
-        $aRecipients = array_unique(array_merge($this->getCleanValue('recipients'), array(bx_get_logged_profile_id())), SORT_NUMERIC);
-        foreach ($aRecipients as $iProfile) {
-            $oProfile = BxDolProfile::getInstance($iProfile);
-            if ($oProfile)
-                $this->_oModule->_oDb->conversationToFolder($iContentId, $iFolder, $oProfile->id());
+
+        $bSaveToDrafts = bx_get('draft_save');
+        $iContentId = bx_get('draft_id');
+        $bDraft = $iContentId ? BX_CNV_FOLDER_DRAFTS == $this->_oModule->_oDb->getConversationFolder($iContentId, bx_get_logged_profile_id()) : false; 
+
+        if ($iContentId) {
+
+            if (!$bDraft)
+                return 0;
+
+            if (!parent::update ($iContentId, $aValsToAdd, $isIgnore))
+                return 0;
+
+        } else {
+            $iContentId = parent::insert ($aValsToAdd, $isIgnore);
+            if (!$iContentId)
+                return 0;
+        }
+
+        if ($bSaveToDrafts) {
+
+            if (!$bDraft)
+                $this->_oModule->_oDb->conversationToFolder($iContentId, BX_CNV_FOLDER_DRAFTS, bx_get_logged_profile_id(), 0);
+
+            // draft is saved via ajax call only, upon successfull draft saving content id is returned
+            echo $iContentId;
+            exit;
+
+        } else {
+
+            bx_import('BxDolProfile');
+
+            // check for spam
+            $bSpam = false;
+            bx_alert('system', 'check_spam', 0, getLoggedId(), array('is_spam' => &$bSpam, 'content' => $this->getCleanValue('text'), 'where' => $this->MODULE));
+            $iFolder = $bSpam ? BX_CNV_FOLDER_SPAM : BX_CNV_FOLDER_INBOX;
+
+            // place conversation to "inbox" (or "spam" - in case of spam) folder 
+            $aRecipients = array_unique(array_merge($this->getCleanValue('recipients'), array(bx_get_logged_profile_id())), SORT_NUMERIC);
+            foreach ($aRecipients as $iProfile) {
+                $oProfile = BxDolProfile::getInstance($iProfile);
+                if (!$oProfile) 
+                    continue;
+                
+                if ($bDraft && $oProfile->id() == bx_get_logged_profile_id())
+                    $this->_oModule->_oDb->moveConvo($iContentId, $oProfile->id(), $iFolder);
+                else
+                    $this->_oModule->_oDb->conversationToFolder($iContentId, $iFolder, $oProfile->id(), $oProfile->id() == bx_get_logged_profile_id() ? 0 : -1);
+            }
         }
 
         return $iContentId;
@@ -62,7 +99,7 @@ class BxCnvFormEntry extends BxBaseModTextFormEntry
             $sVals = trim($sVals, ',');
         }
         $sId = $aInput['name'] . time();
-        $sPlaceholderText = bx_html_attribute("Type name here...", BX_ESCAPE_STR_QUOTE); // TODO: lang key
+        $sPlaceholderText = bx_html_attribute(_t('_bx_cnv_form_entry_input_recipients_placeholder'), BX_ESCAPE_STR_QUOTE);
         $sUrlGetRecipients = BX_DOL_URL_ROOT . "modules/?r=convos/ajax_get_recipients";
         return <<<EOS
 <script>
@@ -134,6 +171,30 @@ class BxCnvFormEntry extends BxBaseModTextFormEntry
 </div>
 EOS;
     }
+
+    protected function genCustomInputSubmitText ($aInput) 
+    {
+        $aVars = array();
+        return $this->_oModule->_oTemplate->parseHtmlByName('form_submit_text.html', $aVars);
+    }
+
+    function initChecker ($aValues = array (), $aSpecificValues = array())  
+    {
+        if ($iContentId = bx_get('draft_id')) { // if adding from draft, fill in existing fields info
+            $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
+            if ($aContentInfo)
+                $aValues = array_merge($aContentInfo, $aValues);
+        }
+
+        return parent::initChecker ($aValues, $aSpecificValues);
+    }
+
+    function isValid () 
+    {
+        if (bx_get('draft_save')) // form is always valid when saving to drafts
+            return true;
+        return parent::isValid ();
+    }
 }
 
 /** @} */

+ 11 - 0
modules/boonex/convos/classes/BxCnvPageEntry.php

@@ -26,9 +26,20 @@ class BxCnvPageEntry extends BxBaseModTextPageEntry
         $this->_oModule->_oDb->updateReadComments(bx_get_logged_profile_id(), $this->_aContentInfo[$CNF['FIELD_ID']], $this->_aContentInfo[$CNF['FIELD_COMMENTS']]);
 
         $iFolder = $this->_oModule->_oDb->getConversationFolder($this->_aContentInfo[$CNF['FIELD_ID']], bx_get_logged_profile_id());
+        if (BX_CNV_FOLDER_DRAFTS == $iFolder) { // if draft is opened - redirect to compose page
+            bx_import('BxDolPermalinks');
+            $sUrl = BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_ADD_ENTRY']);
+            $sUrl = bx_append_url_params($sUrl, array(
+                'draft_id' => $this->_aContentInfo[$CNF['FIELD_ID']],
+            ));
+            header('Location: ' . BX_DOL_URL_ROOT . $sUrl);
+            exit;
+        }
+
         $this->_oModule->setModuleSubmenu ($iFolder);
 
         $this->_oModule->_oTemplate->addJs('main.js');
+        $this->_oModule->_oTemplate->addCss(array('main-media-tablet.css', 'main-media-desktop.css'));
     }
 }
 

+ 6 - 0
modules/boonex/convos/install/langs/en.xml

@@ -23,6 +23,9 @@
     <string name="_bx_cnv_form_entry_input_recipients"><![CDATA[Recipients]]></string>
     <string name="_bx_cnv_form_entry_input_sys_recipients"><![CDATA[Recipients]]></string>
     <string name="_bx_cnv_form_entry_input_recipients_err"><![CDATA[Recipients is mandatory]]></string>
+    <string name="_bx_cnv_form_entry_input_recipients_placeholder"><![CDATA[Type name here...]]></string>
+    <string name="_bx_cnv_form_entry_input_sys_submit_text"><![CDATA[Draft status]]></string>
+    <string name="_bx_cnv_form_entry_input_sys_submit_block"><![CDATA[Submit block]]></string>
 
 	<string name="_bx_cnv_acl_action_create_entry"><![CDATA[Start conversation]]></string>
 	<string name="_bx_cnv_acl_action_delete_entry"><![CDATA[Delete convo]]></string>
@@ -98,4 +101,7 @@
     <string name="_bx_cnv_see_all"><![CDATA[See all convos...]]></string>
     <string name="_bx_cnv_you_are_last_replier"><![CDATA[You are last replier]]></string>
 
+    <string name="_bx_cnv_draft_saving_error"><![CDATA[Draft saving error!]]></string>
+    <string name="_bx_cnv_draft_saved_success"><![CDATA[Draft saved.]]></string>
+
 </resources>

+ 7 - 1
modules/boonex/convos/install/sql/install.sql

@@ -117,6 +117,9 @@ INSERT INTO `sys_form_displays`(`object`, `display_name`, `module`, `view_mode`,
 INSERT INTO `sys_form_inputs`(`object`, `module`, `name`, `value`, `values`, `checked`, `type`, `caption_system`, `caption`, `info`, `required`, `collapsed`, `html`, `attrs`, `attrs_tr`, `attrs_wrapper`, `checker_func`, `checker_params`, `checker_error`, `db_pass`, `db_params`, `editable`, `deletable`) VALUES 
 ('bx_convos', 'bx_convos', 'delete_confirm', 1, '', 0, 'checkbox', '_bx_cnv_form_entry_input_sys_delete_confirm', '_bx_cnv_form_entry_input_delete_confirm', '_bx_cnv_form_entry_input_delete_confirm_info', 1, 0, 0, '', '', '', 'Avail', '', '_bx_cnv_form_entry_input_delete_confirm_error', '', '', 1, 0),
 ('bx_convos', 'bx_convos', 'do_submit', '_bx_cnv_form_entry_input_do_submit', '', 0, 'submit', '_bx_cnv_form_entry_input_sys_do_submit', '', '', 0, 0, 0, '', '', '', '', '', '', '', '', 1, 0),
+('bx_convos', 'bx_convos', 'submit_text', '', '', 0, 'custom', '_bx_cnv_form_entry_input_sys_submit_text', '', '', 0, 0, 0, '', '', '', '', '', '', '', '', 0, 0),
+('bx_convos', 'bx_convos', 'submit_block', '', 'do_submit,submit_text', 0, 'input_set', '_bx_cnv_form_entry_input_sys_submit_block', '', '', 0, 0, 0, '', '', '', '', '', '', '', '', 0, 0),
+('bx_convos', 'bx_convos', 'draft_id', '0', '', 0, 'hidden', '_bx_cnv_form_entry_input_sys_draft_id', '', '', 0, 0, 0, '', '', '', '', '', '', '', '', 0, 0),
 ('bx_convos', 'bx_convos', 'text', '', '', 0, 'textarea', '_bx_cnv_form_entry_input_sys_text', '_bx_cnv_form_entry_input_text', '', 1, 0, 2, '', '', '', 'Avail', '', '_bx_cnv_form_entry_input_text_err', 'XssHtml', '', 1, 0),
 ('bx_convos', 'bx_convos', 'recipients', '', '', 0, 'custom', '_bx_cnv_form_entry_input_sys_recipients', '_bx_cnv_form_entry_input_recipients', '', 1, 0, 0, '', '', '', 'Avail', '', '_bx_cnv_form_entry_input_recipients_err', '', '', 1, 0),
 ('bx_convos', 'bx_convos', 'attachments', '', '', 0, 'files', '_bx_cnv_form_entry_input_sys_attachments', '_bx_cnv_form_entry_input_attachments', '', 0, 0, 0, '', '', '', '', '', '', '', '', 1, 0);
@@ -126,7 +129,10 @@ INSERT INTO `sys_form_display_inputs` (`display_name`, `input_name`, `visible_fo
 ('bx_convos_entry_add', 'recipients', 2147483647, 1, 1),
 ('bx_convos_entry_add', 'text', 2147483647, 1, 2),
 ('bx_convos_entry_add', 'attachments', 2147483647, 1, 3),
-('bx_convos_entry_add', 'do_submit', 2147483647, 1, 4),
+('bx_convos_entry_add', 'submit_block', 2147483647, 1, 4),
+('bx_convos_entry_add', 'do_submit', 2147483647, 1, 5),
+('bx_convos_entry_add', 'submit_text', 2147483647, 1, 6),
+('bx_convos_entry_add', 'draft_id', 2147483647, 1, 7),
 
 ('bx_convos_entry_view', 'delete_confirm', 2147483647, 0, 0),
 ('bx_convos_entry_view', 'do_submit', 2147483647, 0, 0),

+ 5 - 0
modules/boonex/convos/template/css/forms.css

@@ -1,3 +1,8 @@
 
 @import url(../../../../base/text/template/css/forms.css);
 
+.bx-cnv-form-submit-text {
+    float:left;
+    line-height:36px; 
+    line-height:2.2rem;
+}

+ 2 - 6
modules/boonex/convos/template/css/main.css

@@ -1,10 +1,6 @@
 
 @import url(../../../../base/text/template/css/main.css);
 
-.bx-grid-header-collaborators span {
-    display:none;
-}
-
 .bx-cnv-collaborators {
     white-space:nowrap;
 }
@@ -13,8 +9,8 @@
     margin-right:0px;
 }
 
-.bx-cnv-collaborator {
-    white-space:nowrap;
+.bx-cnv-collaborator,
+.bx-grid-header-collaborators span {
     display:none;
 }
 

+ 45 - 0
modules/boonex/convos/template/form_submit_text.html

@@ -0,0 +1,45 @@
+
+<div id="bx-cnv-form-submit-text" class="bx-cnv-form-submit-text bx-def-margin-left bx-def-font-grayed"></div>
+<div class="clear_both"></div>
+
+<script>
+
+    $(document).ready(function() {
+
+        setInterval(function () {
+            var $e = $('#bx-cnv-form-submit-text');
+            var $eForm = $e.parents('form');
+            var $eSubmit = $eForm.find('[type=submit]');
+            var aData = { draft_save: '1' }
+            var fCallback = function (data) {
+                if ('undefined' != typeof(data) && parseInt(data) > 0 ) {
+                    $('input[name=draft_id]').val(parseInt(data));
+                    $e.html('<span>' + _t('_bx_cnv_draft_saved_success') + '</span>');
+                    setTimeout(function () {
+                        $e.find('span').fadeOut();
+                    }, 3000);
+                } else {
+                    $e.html(_t('_bx_cnv_draft_saving_error'));
+                }
+            };
+            
+            aData[$eSubmit.attr('name')] = 1;
+            
+            $eForm.ajaxSubmit({
+                url: document.location.href,
+                data: aData,
+                beforeSubmit: function (formData, jqForm, options) {
+                    for (var i=0; i < formData.length; i++) { 
+                        if ('text' == formData[i].name && !formData[i].value.length) // don't save empty drafts
+                            return false;
+                    }
+                    return true;
+                },         
+                error: fCallback, 
+                success: fCallback
+            }); 
+        }, 10000);
+
+    });
+
+</script>