Просмотр исходного кода

Ticket #5005 - Timeline: Allow to attach poll to an update. Core changes.

AntonLV 1 год назад
Родитель
Сommit
05c861f68e

+ 8 - 0
modules/base/general/classes/BxBaseModGeneralConfig.php

@@ -143,6 +143,14 @@ class BxBaseModGeneralConfig extends BxDolModuleConfig
     {
         return isset($this->_aJsObjects[$sType]) ? $this->_aJsObjects[$sType] : '';
     }
+    
+    public function getJsObjectPoll($iId = 0)
+    {
+        if(($sJsObject = $this->getJsObject('poll')) !== '')
+            return $sJsObject .= (int)$iId == 0 ? 'Add' : 'Edit' . $iId;
+
+        return '';
+    }
 
     public function getGridObject($sType)
     {

+ 33 - 10
modules/base/general/classes/BxBaseModGeneralFormEntry.php

@@ -102,17 +102,32 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
         }
     }
 
-    protected function _isChangeUserForAdmins($sDisplay)
+    protected function _isModeAdd($sDisplay = '')
     {
         $CNF = &$this->_oModule->_oConfig->CNF;
+        
+        if(!$sDisplay && !empty($this->aParams['display']))
+            $sDisplay = $this->aParams['display'];
 
+        return isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'];
+    }
+
+    protected function _isModeEdit($sDisplay = '')
+    {
+        $CNF = &$this->_oModule->_oConfig->CNF;
+
+        if(!$sDisplay && !empty($this->aParams['display']))
+            $sDisplay = $this->aParams['display'];
+
+        return isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'];
+    }
+
+    protected function _isChangeUserForAdmins($sDisplay)
+    {
         if(!$this->_bAllowChangeUserForAdmins)
             return false;
 
-        if(isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'])
-            return true;
-
-        if(isset($CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT']) && $sDisplay == $CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT'])
+        if($this->_isModeAdd($sDisplay) || $this->_isModeEdit($sDisplay))
             return true;
 
         return false;
@@ -135,14 +150,21 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
 
         if(isset($CNF['PARAM_MULTICAT_ENABLED']) && $CNF['PARAM_MULTICAT_ENABLED'] === true) {
             $sInclude2 = '';
-            $sInclude2 .= $this->_oModule->_oTemplate->addJs(array('BxDolCategories.js'), $bDynamicMode);
-            $sInclude2 .= $this->_oModule->_oTemplate->addCss(array('categories.css'), $bDynamicMode);
+            $sInclude2 .= $this->_oModule->_oTemplate->addJs(['BxDolCategories.js'], $bDynamicMode);
+            $sInclude2 .= $this->_oModule->_oTemplate->addCss(['categories.css'], $bDynamicMode);
 
             $sResult .= ($bDynamicMode ? $sInclude2 : '') . $this->_oModule->_oTemplate->getJsCode('categories');
         }
 
-        if(isset($CNF['PARAM_POLL_ENABLED']) && $CNF['PARAM_POLL_ENABLED'] === true)
-            $sResult .= $this->addCssJsPolls($bDynamicMode) . $this->_oModule->_oTemplate->getJsCode('poll');
+        if(($sKey = 'PARAM_POLL_ENABLED') && isset($CNF[$sKey]) && $CNF[$sKey] === true) {
+            $iContentId = $this->_isModeEdit() ? $this->_iContentId : 0;
+
+            $sResult .= $this->addCssJsPolls($bDynamicMode) . $this->_oModule->_oTemplate->getJsCode('poll', [
+                'js_object' => $this->_oModule->_oConfig->getJsObjectPoll($iContentId),
+                'iParentContentId' => $iContentId,
+                'sParentFormId' => $this->getId()
+            ]);
+        }
 
         return $sResult;
     }
@@ -1345,7 +1367,8 @@ class BxBaseModGeneralFormEntry extends BxTemplFormView
             return array_merge($aInput, [
                 'type' => 'polls',
                 'form_get' => '/api.php?r=' . $sModule . '/get_poll_form',
-                'form_submit' => '/api.php?r=' . $sModule . '/submit_poll_form'
+                'form_submit' => '/api.php?r=' . $sModule . '/submit_poll_form',
+                'remove' => '/api.php?r=' . $sModule . '/delete_poll',
             ]);
         }
 

+ 16 - 2
modules/base/general/classes/BxBaseModGeneralFormPoll.php

@@ -14,6 +14,8 @@ class BxBaseModGeneralFormPoll extends BxTemplFormView
     protected $_sModule;
     protected $_oModule;
 
+    protected $_iParentCid;
+
     public function __construct($aInfo, $oTemplate = false)
     {
         parent::__construct($aInfo, $oTemplate);
@@ -21,6 +23,17 @@ class BxBaseModGeneralFormPoll extends BxTemplFormView
         $this->_aFieldsCheckForSpam = ['answers'];
 
         $this->_oModule = BxDolModule::getInstance($this->_sModule);
+
+        $this->_iParentCid = 0;
+    }
+
+    public function setParentContentId($iParentCid)
+    {
+        $this->_iParentCid = $iParentCid;
+
+        $this->aFormAttrs['action'] = BX_DOL_URL_ROOT . bx_append_url_params($this->_oModule->_oConfig->getBaseUri() . 'submit_poll_form/', [
+            'parent_cid' => $this->_iParentCid
+        ]);
     }
 
     function initChecker ($aValues = array (), $aSpecificValues = array())
@@ -145,7 +158,7 @@ class BxBaseModGeneralFormPoll extends BxTemplFormView
                 'subtype' => 'text'
             ]);
 
-        $sJsObject = $this->_oModule->_oConfig->getJsObject('poll');
+        $sJsObject = $this->_oModule->_oConfig->getJsObjectPoll($this->_iParentCid);
 
         $aTmplVarsAnswers = array(
             array('class' => 'bx-fi-answer-blank', 'js_object' => $sJsObject, 'input_text' => $this->genCustomInputAnswersText($aInput, '', true)),
@@ -203,7 +216,7 @@ class BxBaseModGeneralFormPoll extends BxTemplFormView
         $aInput['name'] .= '_add';
         $aInput['value'] = _t($CNF['T']['txt_poll_form_answers_add']);
         $aInput['attrs']['class'] = 'bx-def-margin-sec-top';
-        $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObject('poll') . ".addPollAnswer(this, '" . $sName . "');";
+        $aInput['attrs']['onclick'] = $this->_oModule->_oConfig->getJsObjectPoll($this->_iParentCid) . ".addPollAnswer(this, '" . $sName . "');";
 
         return $this->genInputButton($aInput);
     }
@@ -213,6 +226,7 @@ class BxBaseModGeneralFormPollCheckerHelper extends BxDolFormCheckerHelper
 {
     static public function checkAvailAnswers ($s)
     {
+        //TODO: try to explode for API
         return bx_is_api() ? self::checkAvail($s) : !self::_isEmptyArray($s) && count($s) >= 2;
     }
 }

+ 24 - 14
modules/base/general/classes/BxBaseModGeneralModule.php

@@ -366,25 +366,25 @@ class BxBaseModGeneralModule extends BxDolModule
 
         $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
         if(empty($iId))
-            return echoJson(array());
+            return echoJson([]);
 
-        $aResult = array();
-        if($this->_oDb->deletePolls(array($CNF['FIELD_POLL_ID'] => $iId)))
-            $aResult = array('code' => 0);
+        $aResult = [];
+        if($this->serviceDeletePoll($iId))
+            $aResult = ['code' => 0];
         else
-            $aResult = array('code' => 1, 'message' => _t($CNF['txt_err_cannot_perform_action']));
+            $aResult = ['code' => 1, 'message' => _t($CNF['txt_err_cannot_perform_action'])];
 
         echoJson($aResult);
     }
 
     public function actionGetPollForm()
     {
-        echo $this->_oTemplate->getPollForm();
+        echo $this->_oTemplate->getPollForm((int)bx_get('parent_cid'));
     }
 
     public function actionSubmitPollForm()
     {
-        echoJson($this->getPollForm());
+        echoJson($this->getPollForm((int)bx_get('parent_cid')));
     }
 
     public function serviceGetPollForm()
@@ -424,30 +424,39 @@ class BxBaseModGeneralModule extends BxDolModule
         return $this->serviceGetPollForm();
     }
 
-    public function getPollForm()
+    public function serviceDeletePoll($iId)
+    {
+        return $this->_oDb->deletePolls([$this->_oConfig->CNF['FIELD_POLL_ID'] => $iId]);
+    }
+
+    public function getPollForm($iParentCid = 0)
     {
         $CNF = &$this->_oConfig->CNF;
 
         if(empty($CNF['OBJECT_FORM_POLL']))
-            return array('code' => 1, 'message' => '_sys_txt_error_occured');
+            return ['code' => 1, 'message' => '_sys_txt_error_occured'];
 
         $iProfileId = bx_get_logged_profile_id();
 
         $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_POLL'], $CNF['OBJECT_FORM_POLL_DISPLAY_ADD'], $this->_oTemplate);
-        $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'submit_poll_form/';
+        $oForm->setParentContentId($iParentCid);
 
         $oForm->initChecker();
         if($oForm->isSubmittedAndValid()) {
             $iId = $oForm->insert();
             if($iId)
-                return array('code' => 0, 'id' => $iId, 'item' => $this->_oTemplate->getPollItem($iId, $iProfileId, array(
+                return ['code' => 0, 'id' => $iId, 'item' => $this->_oTemplate->getPollItem($iId, $iProfileId, [
+                    'parent_cid' => $iParentCid,
                     'manage' => true
-                )));
+                ])];
             else
-                return array('code' => 2, 'message' => '_sys_txt_error_entry_creation');
+                return ['code' => 2, 'message' => '_sys_txt_error_entry_creation'];
         }
 
-        return array('form' => $oForm->{'getCode' . ($this->_bIsApi ? 'API' : '')}(), 'form_id' => $oForm->id);
+        return [
+            'form' => $oForm->{'getCode' . ($this->_bIsApi ? 'API' : '')}(), 
+            'form_id' => $oForm->id
+        ];
     }
 
     public function actionGetCreatePostForm()
@@ -713,6 +722,7 @@ class BxBaseModGeneralModule extends BxDolModule
             // polls
             'GetPollForm' => '',
             'SubmitPollForm' => '',
+            'DeletePoll' => '',
             // page blocks
             'EntityTextBlock' => '',
             'EntityInfo' => '',

+ 28 - 25
modules/base/general/classes/BxBaseModGeneralTemplate.php

@@ -791,15 +791,15 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         );  
     }
 
-    public function getPollForm()
+    public function getPollForm($iParentCid = 0)
     {
-        $aForm = $this->getModule()->getPollForm();
+        $aForm = $this->getModule()->getPollForm($iParentCid);
 
-        return $this->parseHtmlByName('poll_form.html', array(
-            'js_object' => $this->_oConfig->getJsObject('poll'),
+        return $this->parseHtmlByName('poll_form.html', [
+            'js_object' => $this->_oConfig->getJsObjectPoll($iParentCid),
             'form_id' => $aForm['form_id'],
             'form' => $aForm['form'],
-        ));
+        ]);
     }
     
     public function getPollField($iContentId = 0, $iProfileId = 0)
@@ -823,6 +823,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         $sPolls = '';
         foreach($aPolls as $aPoll)
             $sPolls .= $this->getPollItem($aPoll, $iProfileId, array(
+                'parent_cid' => $iContentId,
                 'manage' => true
             ));
 
@@ -832,7 +833,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         ));
     }
 
-    public function getPollItem($mixedPoll, $iProfileId = 0, $aParams = array())
+    public function getPollItem($mixedPoll, $iProfileId = 0, $aParams = [])
     {
         $oModule = $this->getModule();
         $CNF = &$oModule->_oConfig->CNF;
@@ -858,23 +859,26 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
             ];
         }
 
-        $sJsObject = $this->_oConfig->getJsObject('poll');
-
         $bDynamic = isset($aParams['dynamic']) && $aParams['dynamic'] === true;
         $bManage = isset($aParams['manage']) && $aParams['manage'] === true;
         $bSwitchMenu = isset($aParams['switch_menu']) ? (bool)$aParams['switch_menu'] : true;
         $bForceDisplayAnswers = isset($aParams['force_display_answers']) && (bool)$aParams['force_display_answers'] === true;
+        $iParentСid = isset($aParams['parent_cid']) ? (int)$aParams['parent_cid'] : 0;
 
         $sPollView = !$bForceDisplayAnswers && $oModule->isPollPerformed($iPollId, $iProfileId) ? 'results' : 'answers';
-        
+
         $sMethod = '_getPoll' . ucfirst($sPollView);
         if(!method_exists($this, $sMethod))
             return '';
 
         $mixedMenu = '';
         if($bSwitchMenu)
-            $mixedMenu = $this->_getPollBlockMenu($aPoll, $sPollView, array('template' => 'menu_interactive.html'));
+            $mixedMenu = $this->_getPollBlockMenu($aPoll, $sPollView, [
+                'parent_cid' => $iParentСid,
+                'template' => 'menu_interactive.html'
+            ]);
 
+        $sJsObject = $this->_oConfig->getJsObjectPoll($iParentСid);
         return $this->parseHtmlByName('poll_item.html', array(
             'html_id' => $this->_oConfig->getHtmlIds('poll') . $iPollId,
             'bx_if:show_input_hidden' => array(
@@ -954,16 +958,17 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
             $iPolls += 1;
         }
 
+        $sJsObjectType = 'poll';
         if(!empty($sContent) && isset($aParams['showcase']) && (bool)$aParams['showcase'] === true) {
             $this->addJs(array('flickity/flickity.pkgd.min.js'));
             $this->addCss(BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css');
 
-            $sContent = $this->parseHtmlByName('poll_items_showcase.html', array(
-                'js_object' => $this->_oConfig->getJsObject('poll'),
+            $sContent = $this->parseHtmlByName('poll_items_showcase.html', [
+                'js_object' => $this->_oConfig->getJsObject($sJsObjectType),
                 'html_id' => $this->_oConfig->getHtmlIds('polls_showcase') . $iContentId,
                 'type' => $iPolls == 1 ? 'single' : 'multiple',
                 'polls' => $sContent
-            ));
+            ]);
         }
 
         $sHeader = '';
@@ -971,7 +976,7 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
             $sHeader = $iPolls == 1 ? array_shift($aPolls)[$CNF['FIELD_POLL_TEXT']] : _t('_polls_from', $aContentInfo[$CNF['FIELD_TITLE']]);
             $sHeader = strmaxtextlen($sHeader, 32, '...');
 
-            $sContent = $this->getJsCode('poll') . $sContent;
+            $sContent = $this->getJsCode($sJsObjectType) . $sContent;
         }
 
         $this->_addCssJsPolls();
@@ -1055,19 +1060,17 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         ));
     }
 
-    protected function _getPollBlockMenu($aPoll, $sSelected = '', $aParams = array())
+    protected function _getPollBlockMenu($aPoll, $sSelected = '', $aParams = [])
     {
-        $oModule = $this->getModule();
-        $CNF = &$oModule->_oConfig->CNF;
+        $CNF = &$this->_oConfig->CNF;
 
-        $sPostfix = '-' . time() . rand(0, PHP_INT_MAX);
-        $sJsObject = $this->_oConfig->getJsObject('poll');
+        $sJsObject = isset($aParams['parent_cid']) ? $this->_oConfig->getJsObjectPoll((int)$aParams['parent_cid']) : $this->_oConfig->getJsObject('poll');
         $iPollId = $aPoll[$CNF['FIELD_POLL_ID']];
 
-        $aViews = array(
+        $aViews = [
             'answers' => true, 
-            'results' => $CNF['PARAM_POLL_HIDDEN_RESULTS'] === false || $oModule->isPollPerformed($iPollId)
-        );
+            'results' => $CNF['PARAM_POLL_HIDDEN_RESULTS'] === false || $this->getModule()->isPollPerformed($iPollId)
+        ];
 
         $aMenu = array();
         foreach($aViews as $sView => $bActive) {
@@ -1093,11 +1096,11 @@ class BxBaseModGeneralTemplate extends BxDolModuleTemplate
         if(count($aMenu) <= 1)
             return '';
 
-        $oMenu = new BxTemplMenuInteractive(array(
+        $oMenu = new BxTemplMenuInteractive([
             'template' => !empty($aParams['template']) ? $aParams['template'] : 'menu_interactive_vertical.html', 
-            'menu_id' => $this->_oConfig->getHtmlIds('poll_view_menu') . $sPostfix, 
+            'menu_id' => $this->_oConfig->getHtmlIds('poll_view_menu') . '-' . time() . rand(0, PHP_INT_MAX), 
             'menu_items' => $aMenu
-        ));
+        ]);
 
         if(!empty($sSelected))
             $oMenu->setSelected('', $sSelected);

+ 1 - 1
modules/base/general/classes/BxBaseModGeneralVotePollAnswers.php

@@ -57,7 +57,7 @@ class BxBaseModGeneralVotePollAnswers extends BxTemplVoteLikes
         $CNF = &$this->_oModule->_oConfig->CNF;
 
         $sJsObjectVote = $this->getJsObjectName();
-        $sJsObjectPoll = $this->_oModule->_oConfig->getJsObject('poll');
+        $sJsObjectPoll = $this->_oModule->_oConfig->getJsObjectPoll((int)$this->_aPollInfo[$CNF['FIELD_POLL_CONTENT_ID']]);
 
         return $sJsObjectVote . '.vote(this, ' . $this->getValue() . ', function(oLink, oData) {' . $sJsObjectPoll . '.onPollAnswerVote(oLink, oData, ' . $this->_aPollInfo[$CNF['FIELD_POLL_ID']] . ');})';
     }

+ 6 - 3
modules/base/general/js/polls.js

@@ -13,6 +13,8 @@ function BxBaseModGeneralPolls(oOptions) {
     this._sActionsUrl = oOptions.sActionUrl;
     this._sObjName = oOptions.sObjName == undefined ? 'oBxBaseModGeneralPolls' : oOptions.sObjName;
     this._iOwnerId = oOptions.iOwnerId == undefined ? 0 : oOptions.iOwnerId;
+    this._iParentContentId = oOptions.iParentContentId == undefined ? 0 : oOptions.iParentContentId;
+    this._sParentFormId = oOptions.sParentFormId == undefined ? '' : oOptions.sParentFormId;
     this._sEditorId = oOptions.sEditorId == undefined ? '' : oOptions.sEditorId;
     this._sAnimationEffect = oOptions.sAnimationEffect == undefined ? 'slide' : oOptions.sAnimationEffect;
     this._iAnimationSpeed = oOptions.iAnimationSpeed == undefined ? 'slow' : oOptions.iAnimationSpeed;
@@ -78,7 +80,7 @@ BxBaseModGeneralPolls.prototype.afterPollFormSubmit = function (oForm, oData)
                 return;
 
             var oItem = $(oData.item).hide();
-            $('#' + $this._aHtmlIds['add_poll_form_field']).append(oItem).find('#' + $this._aHtmlIds['poll'] + oData.id).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed);
+            $('#' + $this._sParentFormId).find('#' + $this._aHtmlIds['add_poll_form_field']).append(oItem).find('#' + $this._aHtmlIds['poll'] + oData.id).bx_anim('show', $this._sAnimationEffect, $this._sAnimationSpeed);
 
             return;
         }
@@ -114,8 +116,8 @@ BxBaseModGeneralPolls.prototype.deletePoll = function(oLink, iId, aEditors)
     bx_confirm('', function() {
         var oData = $this._getDefaultData();
         oData['id'] = iId;
-
-        var oPoll = $('#' + $this._aHtmlIds['poll'] + iId);
+console.log('#' + $this._sParentFormId, $('#' + $this._sParentFormId).find('#' + $this._aHtmlIds['poll'] + iId));
+        var oPoll = $('#' + $this._sParentFormId).find('#' + $this._aHtmlIds['poll'] + iId);
 
         bx_loading(oPoll, true);
 
@@ -155,6 +157,7 @@ BxBaseModGeneralPolls.prototype.deletePoll = function(oLink, iId, aEditors)
 BxBaseModGeneralPolls.prototype.showPollForm = function(oLink)
 {
     var oData = this._getDefaultData();    
+    oData['parent_cid'] = this._iParentContentId;
 
     $(window).dolPopupAjax({
         id: {value: this._aHtmlIds['add_poll_popup'], force: true},

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

@@ -24,16 +24,19 @@ class BxBaseModTextMenuAttachments extends BxTemplMenu
  
         $this->_oModule = BxDolModule::getInstance($this->_sModule);
 
-        $this->addMarkers(array(
-            'js_object' => $this->_oModule->_oConfig->getJsObject('poll'),
+        $this->addMarkers([
             'js_object_link' => $this->_oModule->_oConfig->getJsObject('links'),
-        ));
+        ]);
     }
     
     public function setContentId($iContentId)
     {
         $this->_iContentId = (int)$iContentId;
-        $this->addMarkers(array('content_id' => (int)$this->_iContentId));
+
+        $this->addMarkers([
+            'js_object' => $this->_oModule->_oConfig->getJsObjectPoll($this->_iContentId),
+            'content_id' => $this->_iContentId
+        ]);
     }
 
     protected function _isVisible ($a)

+ 5 - 5
modules/boonex/timeline/classes/BxTimelineMenuPostAttachments.php

@@ -29,17 +29,17 @@ class BxTimelineMenuPostAttachments extends BxTemplMenu
 
         $this->addMarkers([
             'js_object' => $this->_oModule->_oConfig->getJsObject('post'),
-            'js_object_poll' => $this->_oModule->_oConfig->getJsObject('poll'),
         ]);
     }
 
-    public function setEventById($iEventId, $aBrowseParams = array())
+    public function setEventById($iEventId, $aBrowseParams = [])
     {
-        $this->_iEvent = $iEventId;
+        $this->_iEvent = (int)$iEventId;
 
-        $this->addMarkers(array(
+        $this->addMarkers([
+            'js_object_poll' => $this->_oModule->_oConfig->getJsObjectPoll($this->_iEvent),
             'content_id' => $this->_iEvent,
-        ));
+        ]);
     }
 
     public function setUploadersInfo($aUploadersInfo)